~rogpeppe/juju-core/azure

« back to all changes in this revision

Viewing changes to cloudinit/cloudinit_test.go

merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package cloudinit_test
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        . "launchpad.net/gocheck"
 
6
        "launchpad.net/juju/go/cloudinit"
 
7
        "testing"
 
8
)
 
9
// TODO integration tests, but how?
 
10
 
 
11
type S struct{}
 
12
 
 
13
var _ = Suite(S{})
 
14
 
 
15
func Test1(t *testing.T) {
 
16
        TestingT(t)
 
17
}
 
18
 
 
19
var ctests = []struct {
 
20
        name      string
 
21
        expect    string
 
22
        setOption func(cfg *cloudinit.Config)
 
23
}{
 
24
        {
 
25
                "User",
 
26
                "user: me\n",
 
27
                func(cfg *cloudinit.Config) {
 
28
                        cfg.SetUser("me")
 
29
                },
 
30
        },
 
31
        {
 
32
                "AptUpgrade",
 
33
                "apt_upgrade: true\n",
 
34
                func(cfg *cloudinit.Config) {
 
35
                        cfg.SetAptUpgrade(true)
 
36
                },
 
37
        },
 
38
        {
 
39
                "AptUpdate",
 
40
                "apt_update: true\n",
 
41
                func(cfg *cloudinit.Config) {
 
42
                        cfg.SetAptUpdate(true)
 
43
                },
 
44
        },
 
45
        {
 
46
                "AptMirror",
 
47
                "apt_mirror: http://foo.com\n",
 
48
                func(cfg *cloudinit.Config) {
 
49
                        cfg.SetAptMirror("http://foo.com")
 
50
                },
 
51
        },
 
52
        {
 
53
                "AptPreserveSourcesList",
 
54
                "apt_mirror: true\n",
 
55
                func(cfg *cloudinit.Config) {
 
56
                        cfg.SetAptPreserveSourcesList(true)
 
57
                },
 
58
        },
 
59
        {
 
60
                "DebconfSelections",
 
61
                "debconf_selections: '# Force debconf priority to critical.\n\n  debconf debconf/priority select critical\n\n'\n",
 
62
                func(cfg *cloudinit.Config) {
 
63
                        cfg.SetDebconfSelections("# Force debconf priority to critical.\ndebconf debconf/priority select critical\n")
 
64
                },
 
65
        },
 
66
        {
 
67
                "DisableEC2Metadata",
 
68
                "disable_ec2_metadata: true\n",
 
69
                func(cfg *cloudinit.Config) {
 
70
                        cfg.SetDisableEC2Metadata(true)
 
71
                },
 
72
        },
 
73
        {
 
74
                "FinalMessage",
 
75
                "final_message: goodbye\n",
 
76
                func(cfg *cloudinit.Config) {
 
77
                        cfg.SetFinalMessage("goodbye")
 
78
                },
 
79
        },
 
80
        {
 
81
                "Locale",
 
82
                "locale: en_us\n",
 
83
                func(cfg *cloudinit.Config) {
 
84
                        cfg.SetLocale("en_us")
 
85
                },
 
86
        },
 
87
        {
 
88
                "DisableRoot",
 
89
                "disable_root: false\n",
 
90
                func(cfg *cloudinit.Config) {
 
91
                        cfg.SetDisableRoot(false)
 
92
                },
 
93
        },
 
94
        {
 
95
                "SSHAuthorizedKeys",
 
96
                "ssh_authorized_keys:\n- key1\n- key2\n",
 
97
                func(cfg *cloudinit.Config) {
 
98
                        cfg.AddSSHAuthorizedKey("key1")
 
99
                        cfg.AddSSHAuthorizedKey("key2")
 
100
                },
 
101
        },
 
102
        {
 
103
                "SSHKeys RSA",
 
104
                "ssh_keys:\n  rsa_private: key1data\n  rsa_public: key2data\n",
 
105
                func(cfg *cloudinit.Config) {
 
106
                        cfg.AddSSHKey(cloudinit.RSAPrivate, "key1data")
 
107
                        cfg.AddSSHKey(cloudinit.RSAPublic, "key2data")
 
108
                },
 
109
        },
 
110
        {
 
111
                "SSHKeys DSA",
 
112
                "ssh_keys:\n  dsa_public: key1data\n  dsa_private: key2data\n",
 
113
                func(cfg *cloudinit.Config) {
 
114
                        cfg.AddSSHKey(cloudinit.DSAPublic, "key1data")
 
115
                        cfg.AddSSHKey(cloudinit.DSAPrivate, "key2data")
 
116
                },
 
117
        },
 
118
        {
 
119
                "Output",
 
120
                "output:\n  all:\n  - '>foo'\n  - '|bar'\n",
 
121
                func(cfg *cloudinit.Config) {
 
122
                        cfg.SetOutput("all", ">foo", "|bar")
 
123
                },
 
124
        },
 
125
        {
 
126
                "Output",
 
127
                "output:\n  all: '>foo'\n",
 
128
                func(cfg *cloudinit.Config) {
 
129
                        cfg.SetOutput(cloudinit.OutAll, ">foo", "")
 
130
                },
 
131
        },
 
132
        {
 
133
                "AptSources",
 
134
                "apt_sources:\n- source: keyName\n  key: someKey\n",
 
135
                func(cfg *cloudinit.Config) {
 
136
                        cfg.AddAptSource("keyName", "someKey")
 
137
                },
 
138
        },
 
139
        {
 
140
                "AptSources",
 
141
                "apt_sources:\n- source: keyName\n  keyid: someKey\n  keyserver: foo.com\n",
 
142
                func(cfg *cloudinit.Config) {
 
143
                        cfg.AddAptSourceWithKeyId("keyName", "someKey", "foo.com")
 
144
                },
 
145
        },
 
146
        {
 
147
                "Packages",
 
148
                "packages:\n- juju\n- ubuntu\n",
 
149
                func(cfg *cloudinit.Config) {
 
150
                        cfg.AddPackage("juju")
 
151
                        cfg.AddPackage("ubuntu")
 
152
                },
 
153
        },
 
154
        {
 
155
                "BootCmd",
 
156
                "bootcmd:\n- ls > /dev\n- - ls\n  - '>with space'\n",
 
157
                func(cfg *cloudinit.Config) {
 
158
                        cfg.AddBootCmd("ls > /dev")
 
159
                        cfg.AddBootCmdArgs("ls", ">with space")
 
160
                },
 
161
        },
 
162
        {
 
163
                "Mounts",
 
164
                "mounts:\n- - x\n  - \"y\"\n- - z\n  - w\n",
 
165
                func(cfg *cloudinit.Config) {
 
166
                        cfg.AddMount("x", "y")
 
167
                        cfg.AddMount("z", "w")
 
168
                },
 
169
        },
 
170
}
 
171
 
 
172
const header = "#cloud-config\n"
 
173
 
 
174
func (S) TestOutput(c *C) {
 
175
        for _, t := range ctests {
 
176
                cfg := cloudinit.New()
 
177
                t.setOption(cfg)
 
178
                data, err := cfg.Render()
 
179
                c.Assert(err, IsNil)
 
180
                c.Assert(data, NotNil)
 
181
                c.Assert(string(data), Equals, header+t.expect, Bug("test %q output differs", t.name))
 
182
        }
 
183
}
 
184
 
 
185
//#cloud-config
 
186
//packages:
 
187
//- juju
 
188
//- ubuntu
 
189
func ExampleConfig() {
 
190
        cfg := cloudinit.New()
 
191
        cfg.AddPackage("juju")
 
192
        cfg.AddPackage("ubuntu")
 
193
        data, err := cfg.Render()
 
194
        if err != nil {
 
195
                fmt.Printf("render error: %v", err)
 
196
                return
 
197
        }
 
198
        fmt.Printf("%s", data)
 
199
}