LEFT | RIGHT |
1 package openstack | 1 package openstack |
2 | 2 |
3 import ( | 3 import ( |
4 . "launchpad.net/gocheck" | 4 . "launchpad.net/gocheck" |
5 "launchpad.net/goyaml" | 5 "launchpad.net/goyaml" |
6 "launchpad.net/juju-core/environs" | 6 "launchpad.net/juju-core/environs" |
7 "launchpad.net/juju-core/environs/config" | 7 "launchpad.net/juju-core/environs/config" |
8 "os" | 8 "os" |
9 ) | 9 ) |
10 | 10 |
11 type ConfigSuite struct { | 11 type ConfigSuite struct { |
12 savedVars map[string]string | 12 savedVars map[string]string |
13 } | 13 } |
14 | 14 |
| 15 // Ensure any environment variables a user may have set locally are reset. |
15 var envVars = map[string]string{ | 16 var envVars = map[string]string{ |
16 » "OS_PASSWORD": "testpass", | 17 » "OS_USERNAME": "", |
| 18 » "OS_PASSWORD": "", |
| 19 » "OS_TENANT_NAME": "", |
| 20 » "OS_AUTH_URL": "", |
| 21 » "OS_REGION_NAME": "", |
| 22 » "NOVA_USERNAME": "", |
| 23 » "NOVA_PASSWORD": "", |
| 24 » "NOVA_PROJECT_ID": "", |
| 25 » "NOVA_REGION": "", |
17 } | 26 } |
18 | 27 |
19 var _ = Suite(&ConfigSuite{}) | 28 var _ = Suite(&ConfigSuite{}) |
20 | 29 |
21 // configTest specifies a config parsing test, checking that env when | 30 // configTest specifies a config parsing test, checking that env when |
22 // parsed as the openstack section of a config file matches | 31 // parsed as the openstack section of a config file matches |
23 // baseConfigResult when mutated by the mutate function, or that the | 32 // baseConfigResult when mutated by the mutate function, or that the |
24 // parse matches the given error. | 33 // parse matches the given error. |
25 type configTest struct { | 34 type configTest struct { |
26 summary string | 35 summary string |
27 config attrs | 36 config attrs |
28 change attrs | 37 change attrs |
29 region string | 38 region string |
30 controlBucket string | 39 controlBucket string |
31 username string | 40 username string |
32 password string | 41 password string |
33 tenantName string | 42 tenantName string |
34 authURL string | 43 authURL string |
35 firewallMode config.FirewallMode | 44 firewallMode config.FirewallMode |
36 err string | 45 err string |
37 } | 46 } |
38 | 47 |
39 type attrs map[string]interface{} | 48 type attrs map[string]interface{} |
40 | 49 |
41 func (t configTest) check(c *C) { | 50 func (t configTest) check(c *C) { |
42 envs := attrs{ | 51 envs := attrs{ |
43 "environments": attrs{ | 52 "environments": attrs{ |
44 "testenv": attrs{ | 53 "testenv": attrs{ |
45 » » » » "username": "testuser", | 54 » » » » "type": "openstack", |
46 » » » » "tenant-name": "sometenant", | |
47 » » » » "auth-url": "http://somehost", | |
48 » » » » "region": "someregion", | |
49 » » » » "type": "openstack", | |
50 }, | 55 }, |
51 }, | 56 }, |
52 } | 57 } |
53 testenv := envs["environments"].(attrs)["testenv"].(attrs) | 58 testenv := envs["environments"].(attrs)["testenv"].(attrs) |
54 for k, v := range t.config { | 59 for k, v := range t.config { |
55 testenv[k] = v | 60 testenv[k] = v |
56 } | 61 } |
57 if _, ok := testenv["control-bucket"]; !ok { | 62 if _, ok := testenv["control-bucket"]; !ok { |
58 testenv["control-bucket"] = "x" | 63 testenv["control-bucket"] = "x" |
59 } | 64 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 }, { | 237 }, { |
233 summary: "global firewall-mode", | 238 summary: "global firewall-mode", |
234 config: attrs{ | 239 config: attrs{ |
235 "firewall-mode": "global", | 240 "firewall-mode": "global", |
236 }, | 241 }, |
237 firewallMode: config.FwGlobal, | 242 firewallMode: config.FwGlobal, |
238 }, | 243 }, |
239 } | 244 } |
240 | 245 |
241 func (s *ConfigSuite) TestConfig(c *C) { | 246 func (s *ConfigSuite) TestConfig(c *C) { |
| 247 s.setupEnvCredentials() |
242 for i, t := range configTests { | 248 for i, t := range configTests { |
243 c.Logf("test %d: %s (%v)", i, t.summary, t.config) | 249 c.Logf("test %d: %s (%v)", i, t.summary, t.config) |
244 t.check(c) | 250 t.check(c) |
245 } | 251 } |
246 } | 252 } |
247 | 253 |
| 254 func (s *ConfigSuite) setupEnvCredentials() { |
| 255 os.Setenv("OS_USERNAME", "user") |
| 256 os.Setenv("OS_PASSWORD", "secret") |
| 257 os.Setenv("OS_AUTH_URL", "http://auth") |
| 258 os.Setenv("OS_TENANT_NAME", "sometenant") |
| 259 os.Setenv("OS_REGION_NAME", "region") |
| 260 } |
| 261 |
| 262 var regionTestConfig = configTests[0] |
| 263 var credentialsTestConfig = configTests[11] |
| 264 |
| 265 func (s *ConfigSuite) TestMissingRegion(c *C) { |
| 266 s.setupEnvCredentials() |
| 267 os.Setenv("OS_REGION_NAME", "") |
| 268 os.Setenv("NOVA_REGION", "") |
| 269 test := credentialsTestConfig |
| 270 test.err = "required environment variable not set for credentials attrib
ute: Region" |
| 271 test.check(c) |
| 272 } |
| 273 |
| 274 func (s *ConfigSuite) TestMissingUsername(c *C) { |
| 275 s.setupEnvCredentials() |
| 276 os.Setenv("OS_USERNAME", "") |
| 277 os.Setenv("NOVA_USERNAME", "") |
| 278 test := regionTestConfig |
| 279 test.err = "required environment variable not set for credentials attrib
ute: User" |
| 280 test.check(c) |
| 281 } |
| 282 |
248 func (s *ConfigSuite) TestMissingPassword(c *C) { | 283 func (s *ConfigSuite) TestMissingPassword(c *C) { |
| 284 s.setupEnvCredentials() |
249 os.Setenv("OS_PASSWORD", "") | 285 os.Setenv("OS_PASSWORD", "") |
250 os.Setenv("NOVA_PASSWORD", "") | 286 os.Setenv("NOVA_PASSWORD", "") |
251 » test := configTests[0] | 287 » test := regionTestConfig |
252 test.err = "required environment variable not set for credentials attrib
ute: Secrets" | 288 test.err = "required environment variable not set for credentials attrib
ute: Secrets" |
253 test.check(c) | 289 test.check(c) |
254 } | 290 } |
| 291 func (s *ConfigSuite) TestMissingTenant(c *C) { |
| 292 s.setupEnvCredentials() |
| 293 os.Setenv("OS_TENANT_NAME", "") |
| 294 os.Setenv("NOVA_PROJECT_ID", "") |
| 295 test := regionTestConfig |
| 296 test.err = "required environment variable not set for credentials attrib
ute: TenantName" |
| 297 test.check(c) |
| 298 } |
| 299 |
| 300 func (s *ConfigSuite) TestMissingAuthUrl(c *C) { |
| 301 s.setupEnvCredentials() |
| 302 os.Setenv("OS_AUTH_URL", "") |
| 303 test := regionTestConfig |
| 304 test.err = "required environment variable not set for credentials attrib
ute: URL" |
| 305 test.check(c) |
| 306 } |
| 307 |
| 308 func (s *ConfigSuite) TestCredentialsFromEnv(c *C) { |
| 309 // Specify a basic configuration without credentials. |
| 310 envs := attrs{ |
| 311 "environments": attrs{ |
| 312 "testenv": attrs{ |
| 313 "type": "openstack", |
| 314 }, |
| 315 }, |
| 316 } |
| 317 data, err := goyaml.Marshal(envs) |
| 318 c.Assert(err, IsNil) |
| 319 // Poke the credentials into the environment. |
| 320 s.setupEnvCredentials() |
| 321 es, err := environs.ReadEnvironsBytes(data) |
| 322 c.Check(err, IsNil) |
| 323 e, err := es.Open("testenv") |
| 324 ecfg := e.(*environ).ecfg() |
| 325 // The credentials below come from environment variables set during test
setup. |
| 326 c.Assert(ecfg.username(), Equals, "user") |
| 327 c.Assert(ecfg.password(), Equals, "secret") |
| 328 c.Assert(ecfg.authURL(), Equals, "http://auth") |
| 329 c.Assert(ecfg.region(), Equals, "region") |
| 330 c.Assert(ecfg.tenantName(), Equals, "sometenant") |
| 331 } |
LEFT | RIGHT |