Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(379)

Side by Side Diff: environs/openstack/config_test.go

Issue 11659043: Improve Openstack config deprecation warnings. (Closed)
Patch Set: Created 10 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « environs/openstack/config.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012, 2013 Canonical Ltd. 1 // Copyright 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 openstack 4 package openstack
5 5
6 import ( 6 import (
7 » . "launchpad.net/gocheck" 7 » "bytes"
8 » "fmt"
9 » gc "launchpad.net/gocheck"
8 "launchpad.net/goyaml" 10 "launchpad.net/goyaml"
9 "launchpad.net/juju-core/environs" 11 "launchpad.net/juju-core/environs"
10 "launchpad.net/juju-core/environs/config" 12 "launchpad.net/juju-core/environs/config"
13 "launchpad.net/loggo"
thumper 2013/07/22 04:43:02 header grouping
wallyworld 2013/07/22 05:41:12 Done.
11 "os" 14 "os"
15 "strings"
16 "time"
12 ) 17 )
13 18
14 type ConfigSuite struct { 19 type ConfigSuite struct {
15 savedVars map[string]string 20 savedVars map[string]string
16 oldJujuHome string 21 oldJujuHome string
17 } 22 }
18 23
19 // Ensure any environment variables a user may have set locally are reset. 24 // Ensure any environment variables a user may have set locally are reset.
20 var envVars = map[string]string{ 25 var envVars = map[string]string{
21 "AWS_SECRET_ACCESS_KEY": "", 26 "AWS_SECRET_ACCESS_KEY": "",
22 "EC2_SECRET_KEYS": "", 27 "EC2_SECRET_KEYS": "",
23 "NOVA_API_KEY": "", 28 "NOVA_API_KEY": "",
24 "NOVA_PASSWORD": "", 29 "NOVA_PASSWORD": "",
25 "NOVA_PROJECT_ID": "", 30 "NOVA_PROJECT_ID": "",
26 "NOVA_REGION": "", 31 "NOVA_REGION": "",
27 "NOVA_USERNAME": "", 32 "NOVA_USERNAME": "",
28 "OS_ACCESS_KEY": "", 33 "OS_ACCESS_KEY": "",
29 "OS_AUTH_URL": "", 34 "OS_AUTH_URL": "",
30 "OS_PASSWORD": "", 35 "OS_PASSWORD": "",
31 "OS_REGION_NAME": "", 36 "OS_REGION_NAME": "",
32 "OS_SECRET_KEY": "", 37 "OS_SECRET_KEY": "",
33 "OS_TENANT_NAME": "", 38 "OS_TENANT_NAME": "",
34 "OS_USERNAME": "", 39 "OS_USERNAME": "",
35 } 40 }
36 41
37 var _ = Suite(&ConfigSuite{}) 42 var _ = gc.Suite(&ConfigSuite{})
38 43
39 // configTest specifies a config parsing test, checking that env when 44 // configTest specifies a config parsing test, checking that env when
40 // parsed as the openstack section of a config file matches 45 // parsed as the openstack section of a config file matches
41 // baseConfigResult when mutated by the mutate function, or that the 46 // baseConfigResult when mutated by the mutate function, or that the
42 // parse matches the given error. 47 // parse matches the given error.
43 type configTest struct { 48 type configTest struct {
44 summary string 49 summary string
45 config attrs 50 config attrs
46 change attrs 51 change attrs
47 expect attrs 52 expect attrs
(...skipping 15 matching lines...) Expand all
63 } 68 }
64 69
65 type attrs map[string]interface{} 70 type attrs map[string]interface{}
66 71
67 func restoreEnvVars(envVars map[string]string) { 72 func restoreEnvVars(envVars map[string]string) {
68 for k, v := range envVars { 73 for k, v := range envVars {
69 os.Setenv(k, v) 74 os.Setenv(k, v)
70 } 75 }
71 } 76 }
72 77
73 func (t configTest) check(c *C) { 78 func (t configTest) check(c *gc.C) {
74 envs := attrs{ 79 envs := attrs{
75 "environments": attrs{ 80 "environments": attrs{
76 "testenv": attrs{ 81 "testenv": attrs{
77 "type": "openstack", 82 "type": "openstack",
78 "authorized-keys": "fakekey", 83 "authorized-keys": "fakekey",
79 }, 84 },
80 }, 85 },
81 } 86 }
82 testenv := envs["environments"].(attrs)["testenv"].(attrs) 87 testenv := envs["environments"].(attrs)["testenv"].(attrs)
83 for k, v := range t.config { 88 for k, v := range t.config {
84 testenv[k] = v 89 testenv[k] = v
85 } 90 }
86 if _, ok := testenv["control-bucket"]; !ok { 91 if _, ok := testenv["control-bucket"]; !ok {
87 testenv["control-bucket"] = "x" 92 testenv["control-bucket"] = "x"
88 } 93 }
89 data, err := goyaml.Marshal(envs) 94 data, err := goyaml.Marshal(envs)
90 » c.Assert(err, IsNil) 95 » c.Assert(err, gc.IsNil)
91 96
92 es, err := environs.ReadEnvironsBytes(data) 97 es, err := environs.ReadEnvironsBytes(data)
93 » c.Check(err, IsNil) 98 » c.Check(err, gc.IsNil)
94 99
95 // Set environment variables if any. 100 // Set environment variables if any.
96 savedVars := make(map[string]string) 101 savedVars := make(map[string]string)
97 if t.envVars != nil { 102 if t.envVars != nil {
98 for k, v := range t.envVars { 103 for k, v := range t.envVars {
99 savedVars[k] = os.Getenv(k) 104 savedVars[k] = os.Getenv(k)
100 os.Setenv(k, v) 105 os.Setenv(k, v)
101 } 106 }
102 } 107 }
103 defer restoreEnvVars(savedVars) 108 defer restoreEnvVars(savedVars)
104 109
105 e, err := es.Open("testenv") 110 e, err := es.Open("testenv")
106 if t.change != nil { 111 if t.change != nil {
107 » » c.Assert(err, IsNil) 112 » » c.Assert(err, gc.IsNil)
108 113
109 // Testing a change in configuration. 114 // Testing a change in configuration.
110 var old, changed, valid *config.Config 115 var old, changed, valid *config.Config
111 osenv := e.(*environ) 116 osenv := e.(*environ)
112 old = osenv.ecfg().Config 117 old = osenv.ecfg().Config
113 changed, err = old.Apply(t.change) 118 changed, err = old.Apply(t.change)
114 » » c.Assert(err, IsNil) 119 » » c.Assert(err, gc.IsNil)
115 120
116 // Keep err for validation below. 121 // Keep err for validation below.
117 valid, err = providerInstance.Validate(changed, old) 122 valid, err = providerInstance.Validate(changed, old)
118 if err == nil { 123 if err == nil {
119 err = osenv.SetConfig(valid) 124 err = osenv.SetConfig(valid)
120 } 125 }
121 } 126 }
122 if t.err != "" { 127 if t.err != "" {
123 » » c.Check(err, ErrorMatches, t.err) 128 » » c.Check(err, gc.ErrorMatches, t.err)
124 return 129 return
125 } 130 }
126 » c.Assert(err, IsNil) 131 » c.Assert(err, gc.IsNil)
127 132
128 ecfg := e.(*environ).ecfg() 133 ecfg := e.(*environ).ecfg()
129 » c.Assert(ecfg.Name(), Equals, "testenv") 134 » c.Assert(ecfg.Name(), gc.Equals, "testenv")
130 » c.Assert(ecfg.controlBucket(), Equals, "x") 135 » c.Assert(ecfg.controlBucket(), gc.Equals, "x")
131 if t.region != "" { 136 if t.region != "" {
132 » » c.Assert(ecfg.region(), Equals, t.region) 137 » » c.Assert(ecfg.region(), gc.Equals, t.region)
133 } 138 }
134 if t.authMode != "" { 139 if t.authMode != "" {
135 » » c.Assert(ecfg.authMode(), Equals, t.authMode) 140 » » c.Assert(ecfg.authMode(), gc.Equals, t.authMode)
136 } 141 }
137 if t.accessKey != "" { 142 if t.accessKey != "" {
138 » » c.Assert(ecfg.accessKey(), Equals, t.accessKey) 143 » » c.Assert(ecfg.accessKey(), gc.Equals, t.accessKey)
139 } 144 }
140 if t.secretKey != "" { 145 if t.secretKey != "" {
141 » » c.Assert(ecfg.secretKey(), Equals, t.secretKey) 146 » » c.Assert(ecfg.secretKey(), gc.Equals, t.secretKey)
142 } 147 }
143 if t.username != "" { 148 if t.username != "" {
144 » » c.Assert(ecfg.username(), Equals, t.username) 149 » » c.Assert(ecfg.username(), gc.Equals, t.username)
145 » » c.Assert(ecfg.password(), Equals, t.password) 150 » » c.Assert(ecfg.password(), gc.Equals, t.password)
146 » » c.Assert(ecfg.tenantName(), Equals, t.tenantName) 151 » » c.Assert(ecfg.tenantName(), gc.Equals, t.tenantName)
147 » » c.Assert(ecfg.authURL(), Equals, t.authURL) 152 » » c.Assert(ecfg.authURL(), gc.Equals, t.authURL)
148 expected := map[string]interface{}{ 153 expected := map[string]interface{}{
149 "username": t.username, 154 "username": t.username,
150 "password": t.password, 155 "password": t.password,
151 "tenant-name": t.tenantName, 156 "tenant-name": t.tenantName,
152 } 157 }
153 » » c.Assert(err, IsNil) 158 » » c.Assert(err, gc.IsNil)
154 actual, err := e.Provider().SecretAttrs(ecfg.Config) 159 actual, err := e.Provider().SecretAttrs(ecfg.Config)
155 » » c.Assert(err, IsNil) 160 » » c.Assert(err, gc.IsNil)
156 » » c.Assert(expected, DeepEquals, actual) 161 » » c.Assert(expected, gc.DeepEquals, actual)
157 } 162 }
158 if t.pbucketURL != "" { 163 if t.pbucketURL != "" {
159 » » c.Assert(ecfg.publicBucketURL(), Equals, t.pbucketURL) 164 » » c.Assert(ecfg.publicBucketURL(), gc.Equals, t.pbucketURL)
160 » » c.Assert(ecfg.publicBucket(), Equals, t.publicBucket) 165 » » c.Assert(ecfg.publicBucket(), gc.Equals, t.publicBucket)
161 } 166 }
162 if t.firewallMode != "" { 167 if t.firewallMode != "" {
163 » » c.Assert(ecfg.FirewallMode(), Equals, t.firewallMode) 168 » » c.Assert(ecfg.FirewallMode(), gc.Equals, t.firewallMode)
164 } 169 }
165 » c.Assert(ecfg.useFloatingIP(), Equals, t.useFloatingIP) 170 » c.Assert(ecfg.useFloatingIP(), gc.Equals, t.useFloatingIP)
166 for name, expect := range t.expect { 171 for name, expect := range t.expect {
167 actual, found := ecfg.UnknownAttrs()[name] 172 actual, found := ecfg.UnknownAttrs()[name]
168 » » c.Check(found, Equals, true) 173 » » c.Check(found, gc.Equals, true)
169 » » c.Check(actual, Equals, expect) 174 » » c.Check(actual, gc.Equals, expect)
170 } 175 }
171 } 176 }
172 177
173 func (s *ConfigSuite) SetUpTest(c *C) { 178 func (s *ConfigSuite) SetUpTest(c *gc.C) {
174 s.oldJujuHome = config.SetJujuHome(c.MkDir()) 179 s.oldJujuHome = config.SetJujuHome(c.MkDir())
175 s.savedVars = make(map[string]string) 180 s.savedVars = make(map[string]string)
176 for v, val := range envVars { 181 for v, val := range envVars {
177 s.savedVars[v] = os.Getenv(v) 182 s.savedVars[v] = os.Getenv(v)
178 os.Setenv(v, val) 183 os.Setenv(v, val)
179 } 184 }
180 } 185 }
181 186
182 func (s *ConfigSuite) TearDownTest(c *C) { 187 func (s *ConfigSuite) TearDownTest(c *gc.C) {
183 for k, v := range s.savedVars { 188 for k, v := range s.savedVars {
184 os.Setenv(k, v) 189 os.Setenv(k, v)
185 } 190 }
186 config.SetJujuHome(s.oldJujuHome) 191 config.SetJujuHome(s.oldJujuHome)
187 } 192 }
188 193
189 var configTests = []configTest{ 194 var configTests = []configTest{
190 { 195 {
191 summary: "setting region", 196 summary: "setting region",
192 config: attrs{ 197 config: attrs{
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 }, { 424 }, {
420 change: attrs{ 425 change: attrs{
421 "future": "hammerstein", 426 "future": "hammerstein",
422 }, 427 },
423 expect: attrs{ 428 expect: attrs{
424 "future": "hammerstein", 429 "future": "hammerstein",
425 }, 430 },
426 }, 431 },
427 } 432 }
428 433
429 func (s *ConfigSuite) TestConfig(c *C) { 434 func (s *ConfigSuite) TestConfig(c *gc.C) {
430 s.setupEnvCredentials() 435 s.setupEnvCredentials()
431 for i, t := range configTests { 436 for i, t := range configTests {
432 c.Logf("test %d: %s (%v)", i, t.summary, t.config) 437 c.Logf("test %d: %s (%v)", i, t.summary, t.config)
433 t.check(c) 438 t.check(c)
434 } 439 }
435 } 440 }
436 441
437 func (s *ConfigSuite) setupEnvCredentials() { 442 func (s *ConfigSuite) setupEnvCredentials() {
438 os.Setenv("OS_USERNAME", "user") 443 os.Setenv("OS_USERNAME", "user")
439 os.Setenv("OS_PASSWORD", "secret") 444 os.Setenv("OS_PASSWORD", "secret")
440 os.Setenv("OS_AUTH_URL", "http://auth") 445 os.Setenv("OS_AUTH_URL", "http://auth")
441 os.Setenv("OS_TENANT_NAME", "sometenant") 446 os.Setenv("OS_TENANT_NAME", "sometenant")
442 os.Setenv("OS_REGION_NAME", "region") 447 os.Setenv("OS_REGION_NAME", "region")
443 } 448 }
449
450 type ConfigDeprecationSuite struct {
451 oldJujuHome string
452 writer *testWriter
453 oldWriter loggo.Writer
454 oldLevel loggo.Level
455 }
456
457 var _ = gc.Suite(&ConfigDeprecationSuite{})
458
459 func (s *ConfigDeprecationSuite) SetUpTest(c *gc.C) {
460 s.oldJujuHome = config.SetJujuHome(c.MkDir())
thumper 2013/07/22 04:43:02 just use a testing.FakeHome.
wallyworld 2013/07/22 05:41:12 I forgot about fake home. I just copied an existin
461 var err error
462 s.writer = &testWriter{}
463 s.oldWriter, s.oldLevel, err = loggo.RemoveWriter("default")
464 c.Assert(err, gc.IsNil)
465 err = loggo.RegisterWriter("test", s.writer, loggo.TRACE)
466 c.Assert(err, gc.IsNil)
467 }
468
469 func (s *ConfigDeprecationSuite) TearDownTest(c *gc.C) {
470 _, _, err := loggo.RemoveWriter("test")
471 c.Assert(err, gc.IsNil)
472 err = loggo.RegisterWriter("default", s.oldWriter, s.oldLevel)
473 c.Assert(err, gc.IsNil)
474 config.SetJujuHome(s.oldJujuHome)
475 }
476
477 type testWriter struct {
478 bytes.Buffer
479 }
480
481 func (t *testWriter) Write(level loggo.Level, module, filename string, line int, timestamp time.Time, message string) {
482 t.Buffer.WriteString(fmt.Sprintf("%s %s %s", level, module, message))
483 }
484
485 func (s *ConfigDeprecationSuite) setupEnv(c *gc.C, deprecatedKey, value string) *environ {
486 envs := attrs{
487 "environments": attrs{
488 "testenv": attrs{
489 "type": "openstack",
490 "control-bucket": "x",
491 "authorized-keys": "fakekey",
492 },
493 },
494 }
495 testenv := envs["environments"].(attrs)["testenv"].(attrs)
496 testenv[deprecatedKey] = value
497 data, err := goyaml.Marshal(envs)
498 c.Assert(err, gc.IsNil)
499
500 es, err := environs.ReadEnvironsBytes(data)
501 c.Check(err, gc.IsNil)
502
503 e, err := es.Open("testenv")
504 return e.(*environ)
505 }
506
507 func (s *ConfigDeprecationSuite) TestDeprecationWarnings(c *gc.C) {
508 for attr, value := range map[string]string{
509 "default-image-id": "foo",
510 "default-instance-type": "bar",
511 } {
512 osenv := s.setupEnv(c, attr, value)
513 _, err := providerInstance.Validate(osenv.ecfg().Config, nil)
514 c.Assert(err, gc.IsNil)
515 stripped := strings.Replace(s.writer.String(), "\n", "", -1)
516 expected := fmt.Sprintf(`.*WARNING juju Config attribute "%s" \( %s\) is deprecated and ignored.*`, attr, value)
517 c.Assert(stripped, gc.Matches, expected)
518 }
519 }
OLDNEW
« no previous file with comments | « environs/openstack/config.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b