~juju-qa/juju-core/1.16-packaging

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/provider/openstack/config_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-10-10 18:07:45 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20131010180745-wuo0vv7hq7faavdk
Tags: 1.16.0-0ubuntu1
New upstream stable release (LP: #1219879).

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
        "time"
11
11
 
12
12
        gc "launchpad.net/gocheck"
13
 
        "launchpad.net/goyaml"
14
13
        "launchpad.net/loggo"
15
14
 
16
15
        "launchpad.net/juju-core/environs"
17
16
        "launchpad.net/juju-core/environs/config"
18
17
        "launchpad.net/juju-core/testing"
 
18
        jc "launchpad.net/juju-core/testing/checkers"
 
19
        "launchpad.net/juju-core/testing/testbase"
19
20
)
20
21
 
21
22
type ConfigSuite struct {
22
 
        testing.LoggingSuite
 
23
        testbase.LoggingSuite
23
24
        savedVars   map[string]string
24
25
        oldJujuHome *testing.FakeHome
25
26
}
49
50
// baseConfigResult when mutated by the mutate function, or that the
50
51
// parse matches the given error.
51
52
type configTest struct {
52
 
        summary       string
53
 
        config        attrs
54
 
        change        attrs
55
 
        expect        attrs
56
 
        envVars       map[string]string
57
 
        region        string
58
 
        controlBucket string
59
 
        publicBucket  string
60
 
        pbucketURL    string
61
 
        useFloatingIP bool
62
 
        username      string
63
 
        password      string
64
 
        tenantName    string
65
 
        authMode      string
66
 
        authURL       string
67
 
        accessKey     string
68
 
        secretKey     string
69
 
        firewallMode  config.FirewallMode
70
 
        err           string
 
53
        summary                 string
 
54
        config                  map[string]interface{}
 
55
        change                  map[string]interface{}
 
56
        expect                  map[string]interface{}
 
57
        envVars                 map[string]string
 
58
        region                  string
 
59
        controlBucket           string
 
60
        toolsURL                string
 
61
        useFloatingIP           bool
 
62
        username                string
 
63
        password                string
 
64
        tenantName              string
 
65
        authMode                string
 
66
        authURL                 string
 
67
        accessKey               string
 
68
        secretKey               string
 
69
        firewallMode            string
 
70
        err                     string
 
71
        sslHostnameVerification bool
 
72
        sslHostnameSet          bool
71
73
}
72
74
 
73
75
type attrs map[string]interface{}
79
81
}
80
82
 
81
83
func (t configTest) check(c *gc.C) {
82
 
        envs := attrs{
83
 
                "environments": attrs{
84
 
                        "testenv": attrs{
85
 
                                "type":            "openstack",
86
 
                                "authorized-keys": "fakekey",
87
 
                        },
88
 
                },
89
 
        }
90
 
        testenv := envs["environments"].(attrs)["testenv"].(attrs)
91
 
        for k, v := range t.config {
92
 
                testenv[k] = v
93
 
        }
94
 
        if _, ok := testenv["control-bucket"]; !ok {
95
 
                testenv["control-bucket"] = "x"
96
 
        }
97
 
        data, err := goyaml.Marshal(envs)
 
84
        attrs := testing.FakeConfig().Merge(testing.Attrs{
 
85
                "type":           "openstack",
 
86
                "control-bucket": "x",
 
87
        }).Merge(t.config)
 
88
 
 
89
        cfg, err := config.New(config.NoDefaults, attrs)
98
90
        c.Assert(err, gc.IsNil)
99
91
 
100
 
        es, err := environs.ReadEnvironsBytes(data)
101
 
        c.Check(err, gc.IsNil)
102
 
 
103
92
        // Set environment variables if any.
104
93
        savedVars := make(map[string]string)
105
94
        if t.envVars != nil {
110
99
        }
111
100
        defer restoreEnvVars(savedVars)
112
101
 
113
 
        e, err := es.Open("testenv")
 
102
        e, err := environs.New(cfg)
114
103
        if t.change != nil {
115
104
                c.Assert(err, gc.IsNil)
116
105
 
153
142
                c.Assert(ecfg.password(), gc.Equals, t.password)
154
143
                c.Assert(ecfg.tenantName(), gc.Equals, t.tenantName)
155
144
                c.Assert(ecfg.authURL(), gc.Equals, t.authURL)
156
 
                expected := map[string]interface{}{
 
145
                expected := map[string]string{
157
146
                        "username":    t.username,
158
147
                        "password":    t.password,
159
148
                        "tenant-name": t.tenantName,
163
152
                c.Assert(err, gc.IsNil)
164
153
                c.Assert(expected, gc.DeepEquals, actual)
165
154
        }
166
 
        if t.pbucketURL != "" {
167
 
                c.Assert(ecfg.publicBucketURL(), gc.Equals, t.pbucketURL)
168
 
                c.Assert(ecfg.publicBucket(), gc.Equals, t.publicBucket)
 
155
        if t.toolsURL != "" {
 
156
                toolsURL, ok := ecfg.ToolsURL()
 
157
                c.Assert(ok, jc.IsTrue)
 
158
                c.Assert(toolsURL, gc.Equals, t.toolsURL)
169
159
        }
170
160
        if t.firewallMode != "" {
171
161
                c.Assert(ecfg.FirewallMode(), gc.Equals, t.firewallMode)
172
162
        }
173
163
        c.Assert(ecfg.useFloatingIP(), gc.Equals, t.useFloatingIP)
 
164
        // Default should be true
 
165
        expectedHostnameVerification := true
 
166
        if t.sslHostnameSet {
 
167
                expectedHostnameVerification = t.sslHostnameVerification
 
168
        }
 
169
        c.Assert(ecfg.SSLHostnameVerification(), gc.Equals, expectedHostnameVerification)
174
170
        for name, expect := range t.expect {
175
171
                actual, found := ecfg.UnknownAttrs()[name]
176
172
                c.Check(found, gc.Equals, true)
223
219
                config: attrs{
224
220
                        "region": 666,
225
221
                },
226
 
                err: ".*expected string, got 666",
 
222
                err: `.*expected string, got int\(666\)`,
227
223
        }, {
228
224
                summary: "missing region in environment",
229
225
                envVars: map[string]string{
236
232
                config: attrs{
237
233
                        "username": 666,
238
234
                },
239
 
                err: ".*expected string, got 666",
 
235
                err: `.*expected string, got int\(666\)`,
240
236
        }, {
241
237
                summary: "missing username in environment",
242
238
                err:     "required environment variable not set for credentials attribute: User",
249
245
                config: attrs{
250
246
                        "password": 666,
251
247
                },
252
 
                err: ".*expected string, got 666",
 
248
                err: `.*expected string, got int\(666\)`,
253
249
        }, {
254
250
                summary: "missing password in environment",
255
251
                err:     "required environment variable not set for credentials attribute: Secrets",
262
258
                config: attrs{
263
259
                        "tenant-name": 666,
264
260
                },
265
 
                err: ".*expected string, got 666",
 
261
                err: `.*expected string, got int\(666\)`,
266
262
        }, {
267
263
                summary: "missing tenant in environment",
268
264
                err:     "required environment variable not set for credentials attribute: TenantName",
275
271
                config: attrs{
276
272
                        "auth-url": 666,
277
273
                },
278
 
                err: ".*expected string, got 666",
 
274
                err: `.*expected string, got int\(666\)`,
279
275
        }, {
280
276
                summary: "missing auth-url in environment",
281
277
                err:     "required environment variable not set for credentials attribute: URL",
329
325
                config: attrs{
330
326
                        "control-bucket": 666,
331
327
                },
332
 
                err: ".*expected string, got 666",
 
328
                err: `.*expected string, got int\(666\)`,
333
329
        }, {
334
330
                summary: "changing control-bucket",
335
331
                change: attrs{
378
374
                },
379
375
                useFloatingIP: true,
380
376
        }, {
381
 
                summary: "public bucket URL",
382
 
                config: attrs{
383
 
                        "public-bucket":     "juju-dist-non-default",
384
 
                        "public-bucket-url": "http://some/url",
385
 
                },
386
 
                publicBucket: "juju-dist-non-default",
387
 
                pbucketURL:   "http://some/url",
388
 
        }, {
389
 
                summary: "public bucket URL with default bucket",
390
 
                config: attrs{
391
 
                        "public-bucket-url": "http://some/url",
392
 
                },
393
 
                publicBucket: "juju-dist",
394
 
                pbucketURL:   "http://some/url",
 
377
                summary: "public bucket URL with tools URL",
 
378
                config: attrs{
 
379
                        "public-bucket-url": "http://some/url",
 
380
                        "tools-url":         "http://tools/url",
 
381
                },
 
382
                toolsURL: "http://tools/url",
395
383
        }, {
396
384
                summary: "admin-secret given",
397
385
                config: attrs{
402
390
                config:       attrs{},
403
391
                firewallMode: config.FwInstance,
404
392
        }, {
405
 
                summary: "unset firewall-mode",
406
 
                config: attrs{
407
 
                        "firewall-mode": "",
408
 
                },
409
 
                firewallMode: config.FwInstance,
410
 
        }, {
411
393
                summary: "instance firewall-mode",
412
394
                config: attrs{
413
395
                        "firewall-mode": "instance",
433
415
                expect: attrs{
434
416
                        "future": "hammerstein",
435
417
                },
 
418
        }, {
 
419
                change: attrs{
 
420
                        "ssl-hostname-verification": false,
 
421
                },
 
422
                sslHostnameVerification: false,
 
423
                sslHostnameSet:          true,
 
424
        }, {
 
425
                change: attrs{
 
426
                        "ssl-hostname-verification": true,
 
427
                },
 
428
                sslHostnameVerification: true,
 
429
                sslHostnameSet:          true,
436
430
        },
437
431
}
438
432
 
444
438
        }
445
439
}
446
440
 
 
441
func (s *ConfigSuite) TestPrepareInsertsUniqueControlBucket(c *gc.C) {
 
442
        s.setupEnvCredentials()
 
443
        attrs := testing.FakeConfig().Merge(testing.Attrs{
 
444
                "type": "openstack",
 
445
        })
 
446
        cfg, err := config.New(config.NoDefaults, attrs)
 
447
        c.Assert(err, gc.IsNil)
 
448
 
 
449
        env0, err := providerInstance.Prepare(cfg)
 
450
        c.Assert(err, gc.IsNil)
 
451
        bucket0 := env0.(*environ).ecfg().controlBucket()
 
452
        c.Assert(bucket0, gc.Matches, "[a-f0-9]{32}")
 
453
 
 
454
        env1, err := providerInstance.Prepare(cfg)
 
455
        c.Assert(err, gc.IsNil)
 
456
        bucket1 := env1.(*environ).ecfg().controlBucket()
 
457
        c.Assert(bucket1, gc.Matches, "[a-f0-9]{32}")
 
458
 
 
459
        c.Assert(bucket1, gc.Not(gc.Equals), bucket0)
 
460
}
 
461
 
 
462
func (s *ConfigSuite) TestPrepareDoesNotTouchExistingControlBucket(c *gc.C) {
 
463
        s.setupEnvCredentials()
 
464
        attrs := testing.FakeConfig().Merge(testing.Attrs{
 
465
                "type":           "openstack",
 
466
                "control-bucket": "burblefoo",
 
467
        })
 
468
        cfg, err := config.New(config.NoDefaults, attrs)
 
469
        c.Assert(err, gc.IsNil)
 
470
 
 
471
        env, err := providerInstance.Prepare(cfg)
 
472
        c.Assert(err, gc.IsNil)
 
473
        bucket := env.(*environ).ecfg().controlBucket()
 
474
        c.Assert(bucket, gc.Equals, "burblefoo")
 
475
}
 
476
 
447
477
func (s *ConfigSuite) setupEnvCredentials() {
448
478
        os.Setenv("OS_USERNAME", "user")
449
479
        os.Setenv("OS_PASSWORD", "secret")
454
484
 
455
485
type ConfigDeprecationSuite struct {
456
486
        ConfigSuite
457
 
        writer    *testWriter
458
 
        oldWriter loggo.Writer
459
 
        oldLevel  loggo.Level
 
487
        writer *testWriter
460
488
}
461
489
 
462
490
var _ = gc.Suite(&ConfigDeprecationSuite{})
491
519
 
492
520
func (s *ConfigDeprecationSuite) setupEnv(c *gc.C, deprecatedKey, value string) {
493
521
        s.setupEnvCredentials()
494
 
        attrs := map[string]interface{}{
495
 
                "name":            "testenv",
496
 
                "type":            "openstack",
497
 
                "control-bucket":  "x",
498
 
                "authorized-keys": "fakekey",
499
 
                deprecatedKey:     value,
500
 
        }
 
522
        attrs := testing.FakeConfig().Merge(testing.Attrs{
 
523
                "name":           "testenv",
 
524
                "type":           "openstack",
 
525
                "control-bucket": "x",
 
526
                deprecatedKey:    value,
 
527
        })
501
528
        _, err := environs.NewFromAttrs(attrs)
502
529
        c.Assert(err, gc.IsNil)
503
530
}
506
533
        for attr, value := range map[string]string{
507
534
                "default-image-id":      "foo",
508
535
                "default-instance-type": "bar",
 
536
                "public-bucket-url":     "somewhere",
509
537
        } {
510
538
                s.setupLogger(c)
511
539
                s.setupEnv(c, attr, value)
512
540
                s.resetLogger(c)
513
541
                stripped := strings.Replace(s.writer.messages[0], "\n", "", -1)
514
 
                expected := fmt.Sprintf(`.*Config attribute "%s" \(%s\) is deprecated and ignored.*`, attr, value)
 
542
                expected := fmt.Sprintf(`.*Config attribute "%s" \(%s\) is deprecated.*`, attr, value)
515
543
                c.Assert(stripped, gc.Matches, expected)
516
544
        }
517
545
}