LEFT | RIGHT |
(no file at all) | |
| 1 package openstack |
| 2 |
| 3 import ( |
| 4 . "launchpad.net/gocheck" |
| 5 "launchpad.net/goyaml" |
| 6 "launchpad.net/juju-core/environs" |
| 7 "launchpad.net/juju-core/environs/config" |
| 8 "os" |
| 9 "testing" |
| 10 ) |
| 11 |
| 12 type ConfigSuite struct { |
| 13 savedVars map[string]string |
| 14 } |
| 15 |
| 16 var envVars = map[string]string{ |
| 17 "OS_USERNAME": "testuser", |
| 18 "OS_PASSWORD": "testpass", |
| 19 "OS_TENANT_NAME": "testtenant", |
| 20 "OS_AUTH_URL": "http://somehost", |
| 21 "OS_REGION_NAME": "testreg", |
| 22 } |
| 23 |
| 24 var _ = Suite(&ConfigSuite{}) |
| 25 |
| 26 func Test(t *testing.T) { |
| 27 TestingT(t) |
| 28 } |
| 29 |
| 30 // configTest specifies a config parsing test, checking that env when |
| 31 // parsed as the openstack section of a config file matches |
| 32 // baseConfigResult when mutated by the mutate function, or that the |
| 33 // parse matches the given error. |
| 34 type configTest struct { |
| 35 summary string |
| 36 config attrs |
| 37 change attrs |
| 38 region string |
| 39 controlBucket string |
| 40 username string |
| 41 password string |
| 42 tenantName string |
| 43 authURL string |
| 44 firewallMode config.FirewallMode |
| 45 err string |
| 46 } |
| 47 |
| 48 type attrs map[string]interface{} |
| 49 |
| 50 func (t configTest) check(c *C) { |
| 51 envs := attrs{ |
| 52 "environments": attrs{ |
| 53 "testenv": attrs{ |
| 54 "type": "openstack", |
| 55 }, |
| 56 }, |
| 57 } |
| 58 testenv := envs["environments"].(attrs)["testenv"].(attrs) |
| 59 for k, v := range t.config { |
| 60 testenv[k] = v |
| 61 } |
| 62 if _, ok := testenv["control-bucket"]; !ok { |
| 63 testenv["control-bucket"] = "x" |
| 64 } |
| 65 data, err := goyaml.Marshal(envs) |
| 66 c.Assert(err, IsNil) |
| 67 |
| 68 es, err := environs.ReadEnvironsBytes(data) |
| 69 c.Check(err, IsNil) |
| 70 |
| 71 e, err := es.Open("testenv") |
| 72 if t.change != nil { |
| 73 c.Assert(err, IsNil) |
| 74 |
| 75 // Testing a change in configuration. |
| 76 var old, changed, valid *config.Config |
| 77 osenv := e.(*environ) |
| 78 old = osenv.ecfg().Config |
| 79 changed, err = old.Apply(t.change) |
| 80 c.Assert(err, IsNil) |
| 81 |
| 82 // Keep err for validation below. |
| 83 valid, err = providerInstance.Validate(changed, old) |
| 84 if err == nil { |
| 85 err = osenv.SetConfig(valid) |
| 86 } |
| 87 } |
| 88 if t.err != "" { |
| 89 c.Check(err, ErrorMatches, t.err) |
| 90 return |
| 91 } |
| 92 c.Assert(err, IsNil) |
| 93 |
| 94 ecfg := e.(*environ).ecfg() |
| 95 c.Assert(ecfg.Name(), Equals, "testenv") |
| 96 c.Assert(ecfg.controlBucket(), Equals, "x") |
| 97 if t.region != "" { |
| 98 c.Assert(ecfg.region(), Equals, t.region) |
| 99 } |
| 100 if t.username != "" { |
| 101 c.Assert(ecfg.username(), Equals, t.username) |
| 102 c.Assert(ecfg.password(), Equals, t.password) |
| 103 c.Assert(ecfg.tenantName(), Equals, t.tenantName) |
| 104 c.Assert(ecfg.authURL(), Equals, t.authURL) |
| 105 expected := map[string]interface{}{ |
| 106 "username": t.username, |
| 107 "password": t.password, |
| 108 "tenant-name": t.tenantName, |
| 109 } |
| 110 c.Assert(err, IsNil) |
| 111 actual, err := e.Provider().SecretAttrs(ecfg.Config) |
| 112 c.Assert(err, IsNil) |
| 113 c.Assert(expected, DeepEquals, actual) |
| 114 } |
| 115 if t.firewallMode != "" { |
| 116 c.Assert(ecfg.FirewallMode(), Equals, t.firewallMode) |
| 117 } |
| 118 } |
| 119 |
| 120 func (s *ConfigSuite) SetUpTest(c *C) { |
| 121 s.savedVars = make(map[string]string) |
| 122 for v, val := range envVars { |
| 123 s.savedVars[v] = os.Getenv(v) |
| 124 os.Setenv(v, val) |
| 125 } |
| 126 } |
| 127 |
| 128 func (s *ConfigSuite) TearDownTest(c *C) { |
| 129 for v, val := range envVars { |
| 130 os.Setenv(v, val) |
| 131 } |
| 132 } |
| 133 |
| 134 var configTests = []configTest{ |
| 135 { |
| 136 summary: "setting region", |
| 137 config: attrs{ |
| 138 "region": "somereg", |
| 139 }, |
| 140 region: "somereg", |
| 141 }, { |
| 142 summary: "setting region (2)", |
| 143 config: attrs{ |
| 144 "region": "configtest", |
| 145 }, |
| 146 region: "configtest", |
| 147 }, { |
| 148 summary: "changing region", |
| 149 config: attrs{ |
| 150 "region": "configtest", |
| 151 }, |
| 152 change: attrs{ |
| 153 "region": "somereg", |
| 154 }, |
| 155 err: `cannot change region from "configtest" to "somereg"`, |
| 156 }, { |
| 157 summary: "invalid region", |
| 158 config: attrs{ |
| 159 "region": 666, |
| 160 }, |
| 161 err: ".*expected string, got 666", |
| 162 }, { |
| 163 summary: "invalid username", |
| 164 config: attrs{ |
| 165 "username": 666, |
| 166 }, |
| 167 err: ".*expected string, got 666", |
| 168 }, { |
| 169 summary: "invalid password", |
| 170 config: attrs{ |
| 171 "password": 666, |
| 172 }, |
| 173 err: ".*expected string, got 666", |
| 174 }, { |
| 175 summary: "invalid tenant-name", |
| 176 config: attrs{ |
| 177 "tenant-name": 666, |
| 178 }, |
| 179 err: ".*expected string, got 666", |
| 180 }, { |
| 181 summary: "invalid auth-url type", |
| 182 config: attrs{ |
| 183 "auth-url": 666, |
| 184 }, |
| 185 err: ".*expected string, got 666", |
| 186 }, { |
| 187 summary: "invalid auth-url format", |
| 188 config: attrs{ |
| 189 "auth-url": "invalid", |
| 190 }, |
| 191 err: `invalid auth-url value "invalid"`, |
| 192 }, { |
| 193 summary: "invalid control-bucket", |
| 194 config: attrs{ |
| 195 "control-bucket": 666, |
| 196 }, |
| 197 err: ".*expected string, got 666", |
| 198 }, { |
| 199 summary: "changing control-bucket", |
| 200 change: attrs{ |
| 201 "control-bucket": "new-x", |
| 202 }, |
| 203 err: `cannot change control-bucket from "x" to "new-x"`, |
| 204 }, { |
| 205 summary: "valid auth args", |
| 206 config: attrs{ |
| 207 "username": "jujuer", |
| 208 "password": "open sesame", |
| 209 "tenant-name": "juju tenant", |
| 210 "auth-url": "http://some/url", |
| 211 }, |
| 212 username: "jujuer", |
| 213 password: "open sesame", |
| 214 tenantName: "juju tenant", |
| 215 authURL: "http://some/url", |
| 216 }, { |
| 217 summary: "admin-secret given", |
| 218 config: attrs{ |
| 219 "admin-secret": "Futumpsh", |
| 220 }, |
| 221 }, { |
| 222 summary: "default firewall-mode", |
| 223 config: attrs{}, |
| 224 firewallMode: config.FwInstance, |
| 225 }, { |
| 226 summary: "unset firewall-mode", |
| 227 config: attrs{ |
| 228 "firewall-mode": "", |
| 229 }, |
| 230 firewallMode: config.FwInstance, |
| 231 }, { |
| 232 summary: "instance firewall-mode", |
| 233 config: attrs{ |
| 234 "firewall-mode": "instance", |
| 235 }, |
| 236 firewallMode: config.FwInstance, |
| 237 }, { |
| 238 summary: "global firewall-mode", |
| 239 config: attrs{ |
| 240 "firewall-mode": "global", |
| 241 }, |
| 242 firewallMode: config.FwGlobal, |
| 243 }, |
| 244 } |
| 245 |
| 246 func (s *ConfigSuite) TestConfig(c *C) { |
| 247 for i, t := range configTests { |
| 248 c.Logf("test %d: %s (%v)", i, t.summary, t.config) |
| 249 t.check(c) |
| 250 } |
| 251 } |
| 252 |
| 253 func (s *ConfigSuite) TestMissingRegion(c *C) { |
| 254 os.Setenv("OS_REGION_NAME", "") |
| 255 test := configTests[0] |
| 256 delete(test.config, "region") |
| 257 test.err = ".*environment has no region" |
| 258 test.check(c) |
| 259 } |
| 260 |
| 261 func (s *ConfigSuite) TestMissingUsername(c *C) { |
| 262 os.Setenv("OS_USERNAME", "") |
| 263 test := configTests[0] |
| 264 test.err = ".*environment has no username, password, tenant-name, or aut
h-url" |
| 265 test.check(c) |
| 266 } |
| 267 |
| 268 func (s *ConfigSuite) TestMissingPassword(c *C) { |
| 269 os.Setenv("OS_PASSWORD", "") |
| 270 test := configTests[0] |
| 271 test.err = ".*environment has no username, password, tenant-name, or aut
h-url" |
| 272 test.check(c) |
| 273 } |
| 274 |
| 275 func (s *ConfigSuite) TestMissinTenant(c *C) { |
| 276 os.Setenv("OS_TENANT_NAME", "") |
| 277 test := configTests[0] |
| 278 test.err = ".*environment has no username, password, tenant-name, or aut
h-url" |
| 279 test.check(c) |
| 280 } |
| 281 |
| 282 func (s *ConfigSuite) TestMissingAuthUrl(c *C) { |
| 283 os.Setenv("OS_AUTH_URL", "") |
| 284 test := configTests[0] |
| 285 test.err = ".*environment has no username, password, tenant-name, or aut
h-url" |
| 286 test.check(c) |
| 287 } |
LEFT | RIGHT |