~dstroppa/juju-core/joyent-provider-storage

1206.2.1 by Martin Packman
Add copyright statement at the top of all go files bar thirdparty
1
// Copyright 2011, 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
25.2.1 by Roger Peppe
add cloudinit package
4
package cloudinit_test
5
6
import (
7
	"fmt"
1408.1.1 by John Arbash Meinel
Start changing the imports of the middle level files.
8
	"testing"
9
1657.1.6 by Roger Peppe
gocheck imports - a-d
10
	gc "launchpad.net/gocheck"
1408.1.1 by John Arbash Meinel
Start changing the imports of the middle level files.
11
255 by Gustavo Niemeyer
launchpad.net/juju-core/juju => launchpad.net/juju-core
12
	"launchpad.net/juju-core/cloudinit"
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
13
	"launchpad.net/juju-core/testing/testbase"
14
	sshtesting "launchpad.net/juju-core/utils/ssh/testing"
25.2.1 by Roger Peppe
add cloudinit package
15
)
37.3.1 by William Reade
apply go-add-control-package changes onto fresh branch
16
25.2.1 by Roger Peppe
add cloudinit package
17
// TODO integration tests, but how?
18
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
19
type S struct {
20
	testbase.LoggingSuite
21
}
25.2.1 by Roger Peppe
add cloudinit package
22
1657.1.6 by Roger Peppe
gocheck imports - a-d
23
var _ = gc.Suite(S{})
25.2.1 by Roger Peppe
add cloudinit package
24
25
func Test1(t *testing.T) {
1657.1.6 by Roger Peppe
gocheck imports - a-d
26
	gc.TestingT(t)
25.2.1 by Roger Peppe
add cloudinit package
27
}
28
29
var ctests = []struct {
25.2.7 by Roger Peppe
changes in response to review
30
	name      string
31
	expect    string
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
32
	setOption func(cfg *cloudinit.Config)
25.2.1 by Roger Peppe
add cloudinit package
33
}{
25.2.7 by Roger Peppe
changes in response to review
34
	{
35
		"User",
36
		"user: me\n",
37
		func(cfg *cloudinit.Config) {
38
			cfg.SetUser("me")
39
		},
40
	},
41
	{
42
		"AptUpgrade",
43
		"apt_upgrade: true\n",
44
		func(cfg *cloudinit.Config) {
45
			cfg.SetAptUpgrade(true)
46
		},
47
	},
48
	{
49
		"AptUpdate",
50
		"apt_update: true\n",
51
		func(cfg *cloudinit.Config) {
52
			cfg.SetAptUpdate(true)
53
		},
54
	},
55
	{
1567.3.5 by Sidnei da Silva
- Add AddAptProxy to method
56
		"AptProxy",
57
		"apt_proxy: http://foo.com\n",
58
		func(cfg *cloudinit.Config) {
59
			cfg.SetAptProxy("http://foo.com")
60
		},
61
	},
62
	{
25.2.7 by Roger Peppe
changes in response to review
63
		"AptMirror",
64
		"apt_mirror: http://foo.com\n",
65
		func(cfg *cloudinit.Config) {
66
			cfg.SetAptMirror("http://foo.com")
67
		},
68
	},
69
	{
70
		"AptPreserveSourcesList",
71
		"apt_mirror: true\n",
72
		func(cfg *cloudinit.Config) {
73
			cfg.SetAptPreserveSourcesList(true)
74
		},
75
	},
76
	{
77
		"DebconfSelections",
25.2.8 by Roger Peppe
simplified key type.
78
		"debconf_selections: '# Force debconf priority to critical.\n\n  debconf debconf/priority select critical\n\n'\n",
25.2.7 by Roger Peppe
changes in response to review
79
		func(cfg *cloudinit.Config) {
25.2.8 by Roger Peppe
simplified key type.
80
			cfg.SetDebconfSelections("# Force debconf priority to critical.\ndebconf debconf/priority select critical\n")
25.2.7 by Roger Peppe
changes in response to review
81
		},
82
	},
83
	{
84
		"DisableEC2Metadata",
85
		"disable_ec2_metadata: true\n",
86
		func(cfg *cloudinit.Config) {
87
			cfg.SetDisableEC2Metadata(true)
88
		},
89
	},
90
	{
91
		"FinalMessage",
92
		"final_message: goodbye\n",
93
		func(cfg *cloudinit.Config) {
94
			cfg.SetFinalMessage("goodbye")
95
		},
96
	},
97
	{
98
		"Locale",
99
		"locale: en_us\n",
100
		func(cfg *cloudinit.Config) {
101
			cfg.SetLocale("en_us")
102
		},
103
	},
104
	{
105
		"DisableRoot",
106
		"disable_root: false\n",
107
		func(cfg *cloudinit.Config) {
108
			cfg.SetDisableRoot(false)
109
		},
110
	},
111
	{
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
112
		"SSHAuthorizedKeys",
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
113
		fmt.Sprintf(
1953.209.15 by Ian Booth
Fix marshalling test
114
			"ssh_authorized_keys:\n- %s\n  Juju:user@host\n- %s\n  Juju:another@host\n",
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
115
			sshtesting.ValidKeyOne.Key, sshtesting.ValidKeyTwo.Key),
25.2.7 by Roger Peppe
changes in response to review
116
		func(cfg *cloudinit.Config) {
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
117
			cfg.AddSSHAuthorizedKeys(sshtesting.ValidKeyOne.Key + " Juju:user@host")
118
			cfg.AddSSHAuthorizedKeys(sshtesting.ValidKeyTwo.Key + " another@host")
39.4.10 by Roger Peppe
SSHAddAuthorizedKey becomes SSHAddAuthorizedKeys and
119
		},
120
	},
121
	{
122
		"SSHAuthorizedKeys",
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
123
		fmt.Sprintf(
1953.209.15 by Ian Booth
Fix marshalling test
124
			"ssh_authorized_keys:\n- %s\n  Juju:sshkey\n- %s\n  Juju:user@host\n- %s\n  Juju:another@host\n",
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
125
			sshtesting.ValidKeyOne.Key, sshtesting.ValidKeyTwo.Key, sshtesting.ValidKeyThree.Key),
39.4.10 by Roger Peppe
SSHAddAuthorizedKey becomes SSHAddAuthorizedKeys and
126
		func(cfg *cloudinit.Config) {
1953.209.10 by Ian Booth
Ensure juju managed keys have Juju: in their comments and add new required util functions
127
			cfg.AddSSHAuthorizedKeys("#command\n" + sshtesting.ValidKeyOne.Key)
128
			cfg.AddSSHAuthorizedKeys(
129
				sshtesting.ValidKeyTwo.Key + " user@host\n# comment\n\n" +
130
					sshtesting.ValidKeyThree.Key + " another@host")
39.4.10 by Roger Peppe
SSHAddAuthorizedKey becomes SSHAddAuthorizedKeys and
131
			cfg.AddSSHAuthorizedKeys("")
132
		},
133
	},
134
	{
135
		"SSHKeys RSAPrivate",
136
		"ssh_keys:\n  rsa_private: key1data\n",
25.2.7 by Roger Peppe
changes in response to review
137
		func(cfg *cloudinit.Config) {
25.2.8 by Roger Peppe
simplified key type.
138
			cfg.AddSSHKey(cloudinit.RSAPrivate, "key1data")
39.4.10 by Roger Peppe
SSHAddAuthorizedKey becomes SSHAddAuthorizedKeys and
139
		},
140
	},
141
	{
142
		"SSHKeys RSAPublic",
143
		"ssh_keys:\n  rsa_public: key2data\n",
144
		func(cfg *cloudinit.Config) {
25.2.8 by Roger Peppe
simplified key type.
145
			cfg.AddSSHKey(cloudinit.RSAPublic, "key2data")
25.2.7 by Roger Peppe
changes in response to review
146
		},
147
	},
148
	{
39.4.10 by Roger Peppe
SSHAddAuthorizedKey becomes SSHAddAuthorizedKeys and
149
		"SSHKeys DSAPublic",
150
		"ssh_keys:\n  dsa_public: key1data\n",
25.2.7 by Roger Peppe
changes in response to review
151
		func(cfg *cloudinit.Config) {
25.2.8 by Roger Peppe
simplified key type.
152
			cfg.AddSSHKey(cloudinit.DSAPublic, "key1data")
39.4.10 by Roger Peppe
SSHAddAuthorizedKey becomes SSHAddAuthorizedKeys and
153
		},
154
	},
155
	{
156
		"SSHKeys DSAPrivate",
157
		"ssh_keys:\n  dsa_private: key2data\n",
158
		func(cfg *cloudinit.Config) {
25.2.8 by Roger Peppe
simplified key type.
159
			cfg.AddSSHKey(cloudinit.DSAPrivate, "key2data")
25.2.7 by Roger Peppe
changes in response to review
160
		},
161
	},
162
	{
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
163
		"Output",
25.2.7 by Roger Peppe
changes in response to review
164
		"output:\n  all:\n  - '>foo'\n  - '|bar'\n",
165
		func(cfg *cloudinit.Config) {
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
166
			cfg.SetOutput("all", ">foo", "|bar")
25.2.7 by Roger Peppe
changes in response to review
167
		},
168
	},
169
	{
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
170
		"Output",
25.2.7 by Roger Peppe
changes in response to review
171
		"output:\n  all: '>foo'\n",
172
		func(cfg *cloudinit.Config) {
173
			cfg.SetOutput(cloudinit.OutAll, ">foo", "")
174
		},
175
	},
176
	{
177
		"AptSources",
178
		"apt_sources:\n- source: keyName\n  key: someKey\n",
179
		func(cfg *cloudinit.Config) {
180
			cfg.AddAptSource("keyName", "someKey")
181
		},
182
	},
183
	{
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
184
		"Packages",
25.2.7 by Roger Peppe
changes in response to review
185
		"packages:\n- juju\n- ubuntu\n",
186
		func(cfg *cloudinit.Config) {
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
187
			cfg.AddPackage("juju")
188
			cfg.AddPackage("ubuntu")
25.2.7 by Roger Peppe
changes in response to review
189
		},
190
	},
191
	{
192
		"BootCmd",
193
		"bootcmd:\n- ls > /dev\n- - ls\n  - '>with space'\n",
194
		func(cfg *cloudinit.Config) {
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
195
			cfg.AddBootCmd("ls > /dev")
196
			cfg.AddBootCmdArgs("ls", ">with space")
25.2.7 by Roger Peppe
changes in response to review
197
		},
198
	},
199
	{
200
		"Mounts",
201
		"mounts:\n- - x\n  - \"y\"\n- - z\n  - w\n",
202
		func(cfg *cloudinit.Config) {
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
203
			cfg.AddMount("x", "y")
204
			cfg.AddMount("z", "w")
25.2.7 by Roger Peppe
changes in response to review
205
		},
206
	},
39.3.4 by Roger Peppe
add cloudinit.Config.SetAttr.
207
	{
208
		"Attr",
209
		"arbitraryAttr: someValue\n",
210
		func(cfg *cloudinit.Config) {
211
			cfg.SetAttr("arbitraryAttr", "someValue")
212
		},
213
	},
1590.1.1 by Sidnei da Silva
- Move addScripts and addFile from environments/cloudinit.go to cloudinit.Config methods.
214
	{
215
		"RunCmd",
216
		"runcmd:\n- ifconfig\n",
217
		func(cfg *cloudinit.Config) {
218
			cfg.AddRunCmd("ifconfig")
219
		},
220
	},
221
	{
222
		"AddScripts",
223
		"runcmd:\n- echo 'Hello World'\n- ifconfig\n",
224
		func(cfg *cloudinit.Config) {
225
			cfg.AddScripts(
226
				"echo 'Hello World'",
227
				"ifconfig",
228
			)
229
		},
230
	},
231
	{
232
		"AddFile",
1590.1.2 by Sidnei da Silva
- Switch expected to a literla string.
233
		addFileExpected,
1590.1.1 by Sidnei da Silva
- Move addScripts and addFile from environments/cloudinit.go to cloudinit.Config methods.
234
		func(cfg *cloudinit.Config) {
235
			cfg.AddFile(
236
				"/etc/apt/apt.conf.d/99proxy",
237
				`"Acquire::http::Proxy "http://10.0.3.1:3142";`,
238
				0644,
239
			)
240
		},
241
	},
25.2.1 by Roger Peppe
add cloudinit package
242
}
243
1590.1.2 by Sidnei da Silva
- Switch expected to a literla string.
244
const (
1590.1.3 by Sidnei da Silva
- Go fmt
245
	header          = "#cloud-config\n"
1590.1.2 by Sidnei da Silva
- Switch expected to a literla string.
246
	addFileExpected = `runcmd:
1953.227.2 by Andrew Wilkins
Update provider/common.Bootstrap
247
- install -D -m 644 /dev/null '/etc/apt/apt.conf.d/99proxy'
1682.2.2 by Andrew Wilkins
Single quote printf format
248
- printf '%s\n' '"Acquire::http::Proxy "http://10.0.3.1:3142";' > '/etc/apt/apt.conf.d/99proxy'
1590.1.2 by Sidnei da Silva
- Switch expected to a literla string.
249
`
250
)
25.2.1 by Roger Peppe
add cloudinit package
251
1657.1.6 by Roger Peppe
gocheck imports - a-d
252
func (S) TestOutput(c *gc.C) {
25.2.1 by Roger Peppe
add cloudinit package
253
	for _, t := range ctests {
254
		cfg := cloudinit.New()
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
255
		t.setOption(cfg)
256
		data, err := cfg.Render()
1657.1.6 by Roger Peppe
gocheck imports - a-d
257
		c.Assert(err, gc.IsNil)
258
		c.Assert(data, gc.NotNil)
259
		c.Assert(string(data), gc.Equals, header+t.expect, gc.Commentf("test %q output differs", t.name))
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
260
	}
261
}
262
1628.11.13 by Andrew Wilkins
Manual provisioning: use cloudinit, check if provisioned
263
func (S) TestRunCmds(c *gc.C) {
264
	cfg := cloudinit.New()
265
	c.Assert(cfg.RunCmds(), gc.HasLen, 0)
266
	cfg.AddScripts("a", "b")
267
	cfg.AddRunCmdArgs("c", "d")
268
	cfg.AddRunCmd("e")
269
	c.Assert(cfg.RunCmds(), gc.DeepEquals, []interface{}{
1953.209.12 by Ian Booth
go fmt
270
		"a", "b", []string{"c", "d"}, "e",
271
	})
1628.11.13 by Andrew Wilkins
Manual provisioning: use cloudinit, check if provisioned
272
}
273
274
func (S) TestPackages(c *gc.C) {
275
	cfg := cloudinit.New()
276
	c.Assert(cfg.Packages(), gc.HasLen, 0)
277
	cfg.AddPackage("a b c")
278
	cfg.AddPackage("d!")
279
	c.Assert(cfg.Packages(), gc.DeepEquals, []string{"a b c", "d!"})
280
}
281
1953.184.11 by Andrew Wilkins
Make manual/synchronous bootstrap/add-machine more consise
282
func (S) TestSetOutput(c *gc.C) {
283
	type test struct {
284
		kind   cloudinit.OutputKind
285
		stdout string
286
		stderr string
287
	}
288
	tests := []test{{
289
		cloudinit.OutAll, "a", "",
290
	}, {
291
		cloudinit.OutAll, "", "b",
292
	}, {
293
		cloudinit.OutInit, "a", "b",
294
	}, {
295
		cloudinit.OutAll, "a", "b",
296
	}, {
297
		cloudinit.OutAll, "", "",
298
	}}
299
300
	cfg := cloudinit.New()
301
	stdout, stderr := cfg.Output(cloudinit.OutAll)
302
	c.Assert(stdout, gc.Equals, "")
303
	c.Assert(stderr, gc.Equals, "")
304
	for i, t := range tests {
305
		c.Logf("test %d: %+v", i, t)
306
		cfg.SetOutput(t.kind, t.stdout, t.stderr)
307
		stdout, stderr = cfg.Output(t.kind)
308
		c.Assert(stdout, gc.Equals, t.stdout)
309
		c.Assert(stderr, gc.Equals, t.stderr)
310
	}
311
}
312
25.2.1 by Roger Peppe
add cloudinit package
313
//#cloud-config
314
//packages:
315
//- juju
316
//- ubuntu
317
func ExampleConfig() {
318
	cfg := cloudinit.New()
25.2.3 by Roger Peppe
move to more direct scheme for adding options.
319
	cfg.AddPackage("juju")
320
	cfg.AddPackage("ubuntu")
25.2.1 by Roger Peppe
add cloudinit package
321
	data, err := cfg.Render()
322
	if err != nil {
323
		fmt.Printf("render error: %v", err)
324
		return
325
	}
326
	fmt.Printf("%s", data)
327
}