LEFT | RIGHT |
1 // Copyright 2011, 2012, 2013 Canonical Ltd. | 1 // Copyright 2011, 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package environs_test | 4 package environs_test |
5 | 5 |
6 import ( | 6 import ( |
7 "os" | 7 "os" |
8 "path/filepath" | 8 "path/filepath" |
9 | 9 |
10 . "launchpad.net/gocheck" | 10 . "launchpad.net/gocheck" |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 c.Assert(e.Name(), Equals, "only") | 211 c.Assert(e.Name(), Equals, "only") |
212 } | 212 } |
213 | 213 |
214 func (suite) TestEnvironmentWithLocalConfig(c *C) { | 214 func (suite) TestEnvironmentWithLocalConfig(c *C) { |
215 defer testing.MakeFakeHomeNoEnvironments(c, "blah").Restore() | 215 defer testing.MakeFakeHomeNoEnvironments(c, "blah").Restore() |
216 | 216 |
217 attrs := map[string]interface{}{ | 217 attrs := map[string]interface{}{ |
218 "admin-secret": "secret", | 218 "admin-secret": "secret", |
219 "local-attr": "foo", | 219 "local-attr": "foo", |
220 } | 220 } |
221 » ls := config.NewLocalConfig(attrs) | 221 » ls := environs.NewLocalConfig(attrs) |
222 » err := config.WriteLocalConfig("blah", ls) | 222 » err := environs.WriteLocalConfig("blah", ls) |
223 c.Assert(err, IsNil) | 223 c.Assert(err, IsNil) |
224 | 224 |
225 env := ` | 225 env := ` |
226 environments: | 226 environments: |
227 blah: | 227 blah: |
228 type: dummy | 228 type: dummy |
229 state-server: false | 229 state-server: false |
230 authorized-keys: i-am-a-key | 230 authorized-keys: i-am-a-key |
231 ` | 231 ` |
232 outfile, err := environs.WriteEnvirons("", env) | 232 outfile, err := environs.WriteEnvirons("", env) |
233 c.Assert(err, IsNil) | 233 c.Assert(err, IsNil) |
234 path := testing.HomePath(".juju", "environments.yaml") | 234 path := testing.HomePath(".juju", "environments.yaml") |
235 c.Assert(path, Equals, outfile) | 235 c.Assert(path, Equals, outfile) |
236 | 236 |
237 e, err := environs.NewFromName("blah") | 237 e, err := environs.NewFromName("blah") |
238 c.Assert(err, IsNil) | 238 c.Assert(err, IsNil) |
239 attrs = e.Config().AllAttrs() | 239 attrs = e.Config().AllAttrs() |
240 c.Assert(attrs["admin-secret"], Equals, "secret") | 240 c.Assert(attrs["admin-secret"], Equals, "secret") |
241 c.Assert(attrs["local-attr"], Equals, "foo") | 241 c.Assert(attrs["local-attr"], Equals, "foo") |
242 } | 242 } |
243 | 243 |
244 func (suite) TestLocalConfigDifferentEnvironment(c *C) { | 244 func (suite) TestLocalConfigDifferentEnvironment(c *C) { |
245 defer testing.MakeFakeHomeNoEnvironments(c, "blah").Restore() | 245 defer testing.MakeFakeHomeNoEnvironments(c, "blah").Restore() |
246 | 246 |
247 attrs := map[string]interface{}{ | 247 attrs := map[string]interface{}{ |
248 "admin-secret": "secret", | 248 "admin-secret": "secret", |
249 "local-attr": "foo", | 249 "local-attr": "foo", |
250 } | 250 } |
251 » ls := config.NewLocalConfig(attrs) | 251 » ls := environs.NewLocalConfig(attrs) |
252 » err := config.WriteLocalConfig("foo", ls) | 252 » err := environs.WriteLocalConfig("foo", ls) |
253 c.Assert(err, IsNil) | 253 c.Assert(err, IsNil) |
254 | 254 |
255 env := ` | 255 env := ` |
256 environments: | 256 environments: |
257 blah: | 257 blah: |
258 type: dummy | 258 type: dummy |
259 state-server: false | 259 state-server: false |
260 authorized-keys: i-am-a-key | 260 authorized-keys: i-am-a-key |
261 ` | 261 ` |
262 outfile, err := environs.WriteEnvirons("", env) | 262 outfile, err := environs.WriteEnvirons("", env) |
263 c.Assert(err, IsNil) | 263 c.Assert(err, IsNil) |
264 path := testing.HomePath(".juju", "environments.yaml") | 264 path := testing.HomePath(".juju", "environments.yaml") |
265 c.Assert(path, Equals, outfile) | 265 c.Assert(path, Equals, outfile) |
266 | 266 |
267 e, err := environs.NewFromName("blah") | 267 e, err := environs.NewFromName("blah") |
268 c.Assert(err, IsNil) | 268 c.Assert(err, IsNil) |
269 attrs = e.Config().AllAttrs() | 269 attrs = e.Config().AllAttrs() |
270 c.Assert(attrs["admin-secret"], Equals, "") | 270 c.Assert(attrs["admin-secret"], Equals, "") |
271 c.Assert(attrs["local-attr"], Equals, "dummy-local-attr") | 271 c.Assert(attrs["local-attr"], Equals, "dummy-local-attr") |
| 272 } |
| 273 |
| 274 type hasLocalConfig struct{} |
| 275 |
| 276 func (lc *hasLocalConfig) LocalAttrs(cfg *config.Config) environs.LocalConfig { |
| 277 return *environs.NewLocalConfig(map[string]interface{}{ |
| 278 "local-attr": cfg.AllAttrs()["local-attr"], |
| 279 "override-attr": cfg.AllAttrs()["override-attr"], |
| 280 }) |
| 281 } |
| 282 |
| 283 func (lc *hasLocalConfig) InitLocalAttrs(cfgAttrs map[string]interface{}) enviro
ns.LocalConfig { |
| 284 attrs := cfgAttrs |
| 285 attrs["local-attr"] = "foobar" |
| 286 attrs["override-attr"] = "override" |
| 287 return *environs.NewLocalConfig(attrs) |
| 288 } |
| 289 |
| 290 // mergeWithLocal works as expected when local config exists already for the spe
cified environment. |
| 291 func (suite) TestNewWithLocalExisting(c *C) { |
| 292 files := []testing.TestFile{ |
| 293 {".ssh/identity.pub", "identity"}, |
| 294 } |
| 295 h := testing.MakeFakeHomeWithFiles(c, files) |
| 296 defer h.Restore() |
| 297 |
| 298 localAttrs := map[string]interface{}{ |
| 299 "local-attr": "some-attr", |
| 300 "override-attr": "foo", |
| 301 } |
| 302 localCfg := environs.NewLocalConfig(localAttrs) |
| 303 err := environs.WriteLocalConfig("blah", localCfg) |
| 304 c.Assert(err, IsNil) |
| 305 allAttr, err := environs.MergeWithLocal( |
| 306 &hasLocalConfig{}, |
| 307 map[string]interface{}{"type": "my-type", "name": "blah", "overr
ide-attr": "some-other-attr"}) |
| 308 c.Assert(err, IsNil) |
| 309 c.Assert(allAttr["name"], Equals, "blah") |
| 310 c.Assert(allAttr["local-attr"], Equals, "some-attr") |
| 311 c.Assert(allAttr["override-attr"], Equals, "some-other-attr") |
| 312 } |
| 313 |
| 314 // mergeWithLocal works as expected when there is no existing local config for t
he specified environment. |
| 315 func (suite) TestNewWithLocalNonExisting(c *C) { |
| 316 files := []testing.TestFile{ |
| 317 {".ssh/identity.pub", "identity"}, |
| 318 } |
| 319 h := testing.MakeFakeHomeWithFiles(c, files) |
| 320 defer h.Restore() |
| 321 |
| 322 // Create a local config for some other environ. |
| 323 localAttrs := map[string]interface{}{ |
| 324 "local-attr": "local-attr", |
| 325 } |
| 326 localCfg := environs.NewLocalConfig(localAttrs) |
| 327 err := environs.WriteLocalConfig("something", localCfg) |
| 328 c.Assert(err, IsNil) |
| 329 allAttr, err := environs.MergeWithLocal(&hasLocalConfig{}, map[string]in
terface{}{"type": "my-type", "name": "blah"}) |
| 330 c.Assert(err, IsNil) |
| 331 c.Check(allAttr["local-attr"], Equals, "foobar") |
| 332 c.Assert(allAttr["override-attr"], Equals, "override") |
272 } | 333 } |
273 | 334 |
274 func (suite) TestConfigRoundTrip(c *C) { | 335 func (suite) TestConfigRoundTrip(c *C) { |
275 cfg, err := config.New(map[string]interface{}{ | 336 cfg, err := config.New(map[string]interface{}{ |
276 "name": "bladaam", | 337 "name": "bladaam", |
277 "type": "dummy", | 338 "type": "dummy", |
278 "state-server": false, | 339 "state-server": false, |
279 "authorized-keys": "i-am-a-key", | 340 "authorized-keys": "i-am-a-key", |
280 "ca-cert": testing.CACert, | 341 "ca-cert": testing.CACert, |
281 "ca-private-key": "", | 342 "ca-private-key": "", |
(...skipping 24 matching lines...) Expand all Loading... |
306 c.Assert(err, IsNil) | 367 c.Assert(err, IsNil) |
307 cfg1, err := environs.BootstrapConfig(cfg) | 368 cfg1, err := environs.BootstrapConfig(cfg) |
308 c.Assert(err, IsNil) | 369 c.Assert(err, IsNil) |
309 | 370 |
310 expect := cfg.AllAttrs() | 371 expect := cfg.AllAttrs() |
311 delete(expect, "secret") | 372 delete(expect, "secret") |
312 expect["admin-secret"] = "" | 373 expect["admin-secret"] = "" |
313 expect["ca-private-key"] = "" | 374 expect["ca-private-key"] = "" |
314 c.Assert(cfg1.AllAttrs(), DeepEquals, expect) | 375 c.Assert(cfg1.AllAttrs(), DeepEquals, expect) |
315 } | 376 } |
LEFT | RIGHT |