github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/cloudsigma/config_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloudsigma 5 6 import ( 7 "github.com/juju/schema" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/juju/environs" 11 "github.com/juju/juju/environs/config" 12 "github.com/juju/juju/testing" 13 ) 14 15 func newConfig(c *gc.C, attrs testing.Attrs) *config.Config { 16 attrs = testing.FakeConfig().Merge(attrs) 17 cfg, err := config.New(config.NoDefaults, attrs) 18 c.Assert(err, gc.IsNil) 19 return cfg 20 } 21 22 func validAttrs() testing.Attrs { 23 return testing.FakeConfig().Merge(testing.Attrs{ 24 "type": "cloudsigma", 25 "username": "user", 26 "password": "password", 27 "region": "zrh", 28 "uuid": "f54aac3a-9dcd-4a0c-86b5-24091478478c", 29 }) 30 } 31 32 type configSuite struct { 33 testing.BaseSuite 34 } 35 36 func (s *configSuite) SetUpSuite(c *gc.C) { 37 s.BaseSuite.SetUpSuite(c) 38 } 39 40 func (s *configSuite) SetUpTest(c *gc.C) { 41 s.BaseSuite.SetUpTest(c) 42 // speed up tests, do not create heavy stuff inside providers created withing this test suite 43 s.PatchValue(&newClient, func(cfg *environConfig) (*environClient, error) { 44 return nil, nil 45 }) 46 } 47 48 var _ = gc.Suite(&configSuite{}) 49 50 func (s *configSuite) TestNewEnvironConfig(c *gc.C) { 51 52 type checker struct { 53 checker gc.Checker 54 value interface{} 55 } 56 57 var newConfigTests = []struct { 58 info string 59 insert testing.Attrs 60 remove []string 61 expect testing.Attrs 62 err string 63 }{{ 64 info: "username is required", 65 remove: []string{"username"}, 66 err: "username: must not be empty", 67 }, { 68 info: "username cannot be empty", 69 insert: testing.Attrs{"username": ""}, 70 err: "username: must not be empty", 71 }, { 72 info: "password is required", 73 remove: []string{"password"}, 74 err: "password: must not be empty", 75 }, { 76 info: "password cannot be empty", 77 insert: testing.Attrs{"password": ""}, 78 err: "password: must not be empty", 79 }, { 80 info: "region is inserted if missing", 81 remove: []string{"region"}, 82 expect: testing.Attrs{"region": "zrh"}, 83 }, { 84 info: "region must not be empty", 85 insert: testing.Attrs{"region": ""}, 86 err: "region: must not be empty", 87 }} 88 89 for i, test := range newConfigTests { 90 c.Logf("test %d: %s", i, test.info) 91 attrs := validAttrs().Merge(test.insert).Delete(test.remove...) 92 testConfig := newConfig(c, attrs) 93 environ, err := environs.New(testConfig) 94 if test.err == "" { 95 c.Check(err, gc.IsNil) 96 attrs := environ.Config().AllAttrs() 97 for field, value := range test.expect { 98 if chk, ok := value.(checker); ok { 99 c.Check(attrs[field], chk.checker, chk.value) 100 } else { 101 c.Check(attrs[field], gc.Equals, value) 102 } 103 } 104 } else { 105 c.Check(environ, gc.IsNil) 106 c.Check(err, gc.ErrorMatches, test.err) 107 } 108 } 109 } 110 111 var changeConfigTests = []struct { 112 info string 113 insert testing.Attrs 114 remove []string 115 expect testing.Attrs 116 err string 117 }{{ 118 info: "no change, no error", 119 expect: validAttrs(), 120 }, { 121 info: "can change username", 122 insert: testing.Attrs{"username": "cloudsigma_user"}, 123 expect: testing.Attrs{"username": "cloudsigma_user"}, 124 }, { 125 info: "can not change username to empty", 126 insert: testing.Attrs{"username": ""}, 127 err: "username: must not be empty", 128 }, { 129 info: "can change password", 130 insert: testing.Attrs{"password": "cloudsigma_password"}, 131 expect: testing.Attrs{"password": "cloudsigma_password"}, 132 }, { 133 info: "can not change password to empty", 134 insert: testing.Attrs{"password": ""}, 135 err: "password: must not be empty", 136 }, { 137 info: "can change region", 138 insert: testing.Attrs{"region": "lvs"}, 139 err: "region: cannot change from .* to .*", 140 }} 141 142 func (s *configSuite) TestValidateChange(c *gc.C) { 143 baseConfig := newConfig(c, validAttrs()) 144 for i, test := range changeConfigTests { 145 c.Logf("test %d: %s", i, test.info) 146 attrs := validAttrs().Merge(test.insert).Delete(test.remove...) 147 testConfig := newConfig(c, attrs) 148 validatedConfig, err := providerInstance.Validate(testConfig, baseConfig) 149 if test.err == "" { 150 c.Check(err, gc.IsNil) 151 attrs := validatedConfig.AllAttrs() 152 for field, value := range test.expect { 153 c.Check(attrs[field], gc.Equals, value) 154 } 155 } else { 156 c.Check(validatedConfig, gc.IsNil) 157 c.Check(err, gc.ErrorMatches, "invalid config.*: "+test.err) 158 } 159 160 // reverse change 161 validatedConfig, err = providerInstance.Validate(baseConfig, testConfig) 162 if test.err == "" { 163 c.Check(err, gc.IsNil) 164 attrs := validatedConfig.AllAttrs() 165 for field, value := range validAttrs() { 166 c.Check(attrs[field], gc.Equals, value) 167 } 168 } else { 169 c.Check(validatedConfig, gc.IsNil) 170 c.Check(err, gc.ErrorMatches, "invalid .*config.*: "+test.err) 171 } 172 } 173 } 174 175 func (s *configSuite) TestSetConfig(c *gc.C) { 176 baseConfig := newConfig(c, validAttrs()) 177 for i, test := range changeConfigTests { 178 c.Logf("test %d: %s", i, test.info) 179 environ, err := environs.New(baseConfig) 180 c.Assert(err, gc.IsNil) 181 attrs := validAttrs().Merge(test.insert).Delete(test.remove...) 182 testConfig := newConfig(c, attrs) 183 err = environ.SetConfig(testConfig) 184 newAttrs := environ.Config().AllAttrs() 185 if test.err == "" { 186 c.Check(err, gc.IsNil) 187 for field, value := range test.expect { 188 c.Check(newAttrs[field], gc.Equals, value) 189 } 190 } else { 191 c.Check(err, gc.ErrorMatches, test.err) 192 for field, value := range baseConfig.UnknownAttrs() { 193 c.Check(newAttrs[field], gc.Equals, value) 194 } 195 } 196 } 197 } 198 199 func (s *configSuite) TestConfigName(c *gc.C) { 200 baseConfig := newConfig(c, validAttrs().Merge(testing.Attrs{"name": "testname"})) 201 environ, err := environs.New(baseConfig) 202 c.Assert(err, gc.IsNil) 203 c.Check(environ.Config().Name(), gc.Equals, "testname") 204 } 205 206 func (s *configSuite) TestEnvironConfig(c *gc.C) { 207 testConfig := newConfig(c, validAttrs()) 208 ecfg, err := validateConfig(testConfig, nil) 209 c.Assert(ecfg, gc.NotNil) 210 c.Assert(err, gc.IsNil) 211 c.Check(ecfg.username(), gc.Equals, "user") 212 c.Check(ecfg.password(), gc.Equals, "password") 213 c.Check(ecfg.region(), gc.Equals, "zrh") 214 } 215 216 func (s *configSuite) TestInvalidConfigChange(c *gc.C) { 217 oldAttrs := validAttrs().Merge(testing.Attrs{"name": "123"}) 218 oldConfig := newConfig(c, oldAttrs) 219 newAttrs := validAttrs().Merge(testing.Attrs{"name": "321"}) 220 newConfig := newConfig(c, newAttrs) 221 222 oldecfg, _ := providerInstance.Validate(oldConfig, nil) 223 c.Assert(oldecfg, gc.NotNil) 224 225 newecfg, err := providerInstance.Validate(newConfig, oldecfg) 226 c.Assert(newecfg, gc.IsNil) 227 c.Assert(err, gc.NotNil) 228 } 229 230 var secretAttrsConfigTests = []struct { 231 info string 232 insert testing.Attrs 233 remove []string 234 expect map[string]string 235 err string 236 }{{ 237 info: "no change, no error", 238 expect: map[string]string{"password": "password"}, 239 }, { 240 info: "invalid config", 241 insert: testing.Attrs{"username": ""}, 242 err: ".* must not be empty.*", 243 }} 244 245 func (s *configSuite) TestSecretAttrs(c *gc.C) { 246 for i, test := range secretAttrsConfigTests { 247 c.Logf("test %d: %s", i, test.info) 248 attrs := validAttrs().Merge(test.insert).Delete(test.remove...) 249 testConfig := newConfig(c, attrs) 250 sa, err := providerInstance.SecretAttrs(testConfig) 251 if test.err == "" { 252 c.Check(sa, gc.HasLen, len(test.expect)) 253 for field, value := range test.expect { 254 c.Check(sa[field], gc.Equals, value) 255 } 256 c.Check(err, gc.IsNil) 257 } else { 258 c.Check(sa, gc.IsNil) 259 c.Check(err, gc.ErrorMatches, test.err) 260 } 261 } 262 } 263 264 func (s *configSuite) TestSecretAttrsAreStrings(c *gc.C) { 265 for i, field := range configSecretFields { 266 c.Logf("test %d: %s", i, field) 267 attrs := validAttrs().Merge(testing.Attrs{field: 0}) 268 269 if v, ok := configFields[field]; ok { 270 configFields[field] = schema.ForceInt() 271 defer func(c schema.Checker) { 272 configFields[field] = c 273 }(v) 274 } else { 275 c.Errorf("secrect field %s not found in configFields", field) 276 continue 277 } 278 279 testConfig := newConfig(c, attrs) 280 sa, err := providerInstance.SecretAttrs(testConfig) 281 c.Check(sa, gc.IsNil) 282 c.Check(err, gc.ErrorMatches, "secret .* field must have a string value; got .*") 283 } 284 } 285 286 func (s *configSuite) TestClientConfigChanged(c *gc.C) { 287 ecfg := &environConfig{ 288 Config: newConfig(c, testing.Attrs{"name": "client-test"}), 289 attrs: map[string]interface{}{ 290 "region": "https://testing.invalid", 291 "username": "user", 292 "password": "password", 293 }, 294 } 295 296 oldConfig := &environConfig{ 297 Config: newConfig(c, testing.Attrs{"name": "client-test"}), 298 attrs: map[string]interface{}{ 299 "region": "https://testing.invalid", 300 "username": "user", 301 "password": "password", 302 }, 303 } 304 305 rc := oldConfig.clientConfigChanged(ecfg) 306 c.Check(rc, gc.Equals, false) 307 308 ecfg.attrs["region"] = "" 309 rc = oldConfig.clientConfigChanged(ecfg) 310 c.Check(rc, gc.Equals, true) 311 312 ecfg.attrs["region"] = "https://testing.invalid" 313 ecfg.attrs["username"] = "user1" 314 rc = oldConfig.clientConfigChanged(ecfg) 315 c.Check(rc, gc.Equals, true) 316 317 ecfg.attrs["username"] = "user" 318 ecfg.attrs["password"] = "password1" 319 rc = oldConfig.clientConfigChanged(ecfg) 320 c.Check(rc, gc.Equals, true) 321 }