github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/ec2/config_test.go (about) 1 // Copyright 2011, 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ec2 5 6 // TODO: Clean this up so it matches environs/openstack/config_test.go. 7 8 import ( 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/cloud" 18 "github.com/juju/juju/environs" 19 "github.com/juju/juju/environs/config" 20 "github.com/juju/juju/environs/context" 21 "github.com/juju/juju/testing" 22 ) 23 24 // Use local suite since this file lives in the ec2 package 25 // for testing internals. 26 type ConfigSuite struct { 27 testing.BaseSuite 28 savedHome, savedAccessKey, savedSecretKey string 29 } 30 31 var _ = gc.Suite(&ConfigSuite{}) 32 33 // configTest specifies a config parsing test, checking that env when 34 // parsed as the ec2 section of a config file matches baseConfigResult 35 // when mutated by the mutate function, or that the parse matches the 36 // given error. 37 type configTest struct { 38 config map[string]interface{} 39 change map[string]interface{} 40 expect map[string]interface{} 41 vpcID string 42 forceVPCID bool 43 firewallMode string 44 blockStorageSource string 45 err string 46 } 47 48 type attrs map[string]interface{} 49 50 func (t configTest) check(c *gc.C) { 51 credential := cloud.NewCredential( 52 cloud.AccessKeyAuthType, 53 map[string]string{ 54 "access-key": "x", 55 "secret-key": "y", 56 }, 57 ) 58 cloudSpec := environs.CloudSpec{ 59 Type: "ec2", 60 Name: "ec2test", 61 Region: "us-east-1", 62 Credential: &credential, 63 } 64 attrs := testing.FakeConfig().Merge(testing.Attrs{ 65 "type": "ec2", 66 }).Merge(t.config) 67 cfg, err := config.New(config.NoDefaults, attrs) 68 c.Assert(err, jc.ErrorIsNil) 69 e, err := environs.New(environs.OpenParams{ 70 Cloud: cloudSpec, 71 Config: cfg, 72 }) 73 if t.change != nil { 74 c.Assert(err, jc.ErrorIsNil) 75 76 // Testing a change in configuration. 77 var old, changed, valid *config.Config 78 ec2env := e.(*environ) 79 old = ec2env.ecfg().Config 80 changed, err = old.Apply(t.change) 81 c.Assert(err, jc.ErrorIsNil) 82 83 // Keep err for validation below. 84 valid, err = providerInstance.Validate(changed, old) 85 if err == nil { 86 err = ec2env.SetConfig(valid) 87 } 88 } 89 if t.err != "" { 90 c.Check(err, gc.ErrorMatches, t.err) 91 return 92 } 93 c.Assert(err, jc.ErrorIsNil) 94 95 ecfg := e.(*environ).ecfg() 96 c.Assert(ecfg.Name(), gc.Equals, "testmodel") 97 c.Assert(ecfg.vpcID(), gc.Equals, t.vpcID) 98 c.Assert(ecfg.forceVPCID(), gc.Equals, t.forceVPCID) 99 100 if t.firewallMode != "" { 101 c.Assert(ecfg.FirewallMode(), gc.Equals, t.firewallMode) 102 } 103 for name, expect := range t.expect { 104 actual, found := ecfg.UnknownAttrs()[name] 105 c.Check(found, jc.IsTrue) 106 c.Check(actual, gc.Equals, expect) 107 } 108 } 109 110 var configTests = []configTest{ 111 { 112 config: attrs{}, 113 }, { 114 config: attrs{}, 115 vpcID: "", 116 forceVPCID: false, 117 }, { 118 config: attrs{ 119 "vpc-id": "invalid", 120 }, 121 err: `.*vpc-id: "invalid" is not a valid AWS VPC ID`, 122 vpcID: "", 123 forceVPCID: false, 124 }, { 125 config: attrs{ 126 "vpc-id": vpcIDNone, 127 }, 128 vpcID: "none", 129 forceVPCID: false, 130 }, { 131 config: attrs{ 132 "vpc-id": 42, 133 }, 134 err: `.*expected string, got int\(42\)`, 135 vpcID: "", 136 forceVPCID: false, 137 }, { 138 config: attrs{ 139 "vpc-id-force": "nonsense", 140 }, 141 err: `.*expected bool, got string\("nonsense"\)`, 142 vpcID: "", 143 forceVPCID: false, 144 }, { 145 config: attrs{ 146 "vpc-id": "vpc-anything", 147 "vpc-id-force": 999, 148 }, 149 err: `.*expected bool, got int\(999\)`, 150 vpcID: "", 151 forceVPCID: false, 152 }, { 153 config: attrs{ 154 "vpc-id": "", 155 "vpc-id-force": true, 156 }, 157 err: `.*cannot use vpc-id-force without specifying vpc-id as well`, 158 vpcID: "", 159 forceVPCID: true, 160 }, { 161 config: attrs{ 162 "vpc-id": "vpc-a1b2c3d4", 163 }, 164 vpcID: "vpc-a1b2c3d4", 165 forceVPCID: false, 166 }, { 167 config: attrs{ 168 "vpc-id": "vpc-some-id", 169 "vpc-id-force": true, 170 }, 171 vpcID: "vpc-some-id", 172 forceVPCID: true, 173 }, { 174 config: attrs{ 175 "vpc-id": "vpc-abcd", 176 "vpc-id-force": false, 177 }, 178 vpcID: "vpc-abcd", 179 forceVPCID: false, 180 }, { 181 config: attrs{ 182 "vpc-id": "vpc-unchanged", 183 "vpc-id-force": true, 184 }, 185 change: attrs{ 186 "vpc-id": "vpc-unchanged", 187 "vpc-id-force": false, 188 }, 189 err: `.*cannot change vpc-id-force from true to false`, 190 vpcID: "vpc-unchanged", 191 forceVPCID: true, 192 }, { 193 config: attrs{ 194 "vpc-id": "", 195 }, 196 change: attrs{ 197 "vpc-id": "none", 198 }, 199 err: `.*cannot change vpc-id from "" to "none"`, 200 vpcID: "", 201 forceVPCID: false, 202 }, { 203 config: attrs{ 204 "vpc-id": "", 205 }, 206 change: attrs{ 207 "vpc-id": "vpc-changed", 208 }, 209 err: `.*cannot change vpc-id from "" to "vpc-changed"`, 210 vpcID: "", 211 forceVPCID: false, 212 }, { 213 config: attrs{ 214 "vpc-id": "vpc-initial", 215 }, 216 change: attrs{ 217 "vpc-id": "", 218 }, 219 err: `.*cannot change vpc-id from "vpc-initial" to ""`, 220 vpcID: "vpc-initial", 221 forceVPCID: false, 222 }, { 223 config: attrs{ 224 "vpc-id": "vpc-old", 225 }, 226 change: attrs{ 227 "vpc-id": "vpc-new", 228 }, 229 err: `.*cannot change vpc-id from "vpc-old" to "vpc-new"`, 230 vpcID: "vpc-old", 231 forceVPCID: false, 232 }, { 233 config: attrs{ 234 "vpc-id": "vpc-foo", 235 "vpc-id-force": true, 236 }, 237 change: attrs{}, 238 vpcID: "vpc-foo", 239 forceVPCID: true, 240 }, { 241 config: attrs{}, 242 firewallMode: config.FwInstance, 243 }, { 244 config: attrs{}, 245 blockStorageSource: "ebs", 246 }, { 247 config: attrs{ 248 "storage-default-block-source": "ebs-fast", 249 }, 250 blockStorageSource: "ebs-fast", 251 }, { 252 config: attrs{ 253 "firewall-mode": "instance", 254 }, 255 firewallMode: config.FwInstance, 256 }, { 257 config: attrs{ 258 "firewall-mode": "global", 259 }, 260 firewallMode: config.FwGlobal, 261 }, { 262 config: attrs{ 263 "firewall-mode": "none", 264 }, 265 firewallMode: config.FwNone, 266 }, { 267 config: attrs{ 268 "ssl-hostname-verification": false, 269 }, 270 err: ".*disabling ssh-hostname-verification is not supported", 271 }, { 272 config: attrs{ 273 "future": "hammerstein", 274 }, 275 expect: attrs{ 276 "future": "hammerstein", 277 }, 278 }, { 279 change: attrs{ 280 "future": "hammerstein", 281 }, 282 expect: attrs{ 283 "future": "hammerstein", 284 }, 285 }, 286 } 287 288 func (s *ConfigSuite) SetUpTest(c *gc.C) { 289 s.BaseSuite.SetUpTest(c) 290 s.savedHome = utils.Home() 291 292 home := c.MkDir() 293 sshDir := filepath.Join(home, ".ssh") 294 err := os.Mkdir(sshDir, 0777) 295 c.Assert(err, jc.ErrorIsNil) 296 err = ioutil.WriteFile(filepath.Join(sshDir, "id_rsa.pub"), []byte("sshkey\n"), 0666) 297 c.Assert(err, jc.ErrorIsNil) 298 299 err = utils.SetHome(home) 300 c.Assert(err, jc.ErrorIsNil) 301 } 302 303 func (s *ConfigSuite) TearDownTest(c *gc.C) { 304 err := utils.SetHome(s.savedHome) 305 c.Assert(err, jc.ErrorIsNil) 306 s.BaseSuite.TearDownTest(c) 307 } 308 309 func (s *ConfigSuite) TestConfig(c *gc.C) { 310 for i, t := range configTests { 311 c.Logf("test %d: %v", i, t.config) 312 t.check(c) 313 } 314 } 315 316 func (s *ConfigSuite) TestPrepareConfigSetsDefaultBlockSource(c *gc.C) { 317 s.PatchValue(&verifyCredentials, func(*environ, context.ProviderCallContext) error { return nil }) 318 attrs := testing.FakeConfig().Merge(testing.Attrs{ 319 "type": "ec2", 320 }) 321 cfg, err := config.New(config.NoDefaults, attrs) 322 c.Assert(err, jc.ErrorIsNil) 323 324 credential := cloud.NewCredential( 325 cloud.AccessKeyAuthType, 326 map[string]string{ 327 "access-key": "x", 328 "secret-key": "y", 329 }, 330 ) 331 cfg, err = providerInstance.PrepareConfig(environs.PrepareConfigParams{ 332 Config: cfg, 333 Cloud: environs.CloudSpec{ 334 Type: "ec2", 335 Name: "aws", 336 Region: "test", 337 Credential: &credential, 338 }, 339 }) 340 c.Assert(err, jc.ErrorIsNil) 341 source, ok := cfg.StorageDefaultBlockSource() 342 c.Assert(ok, jc.IsTrue) 343 c.Assert(source, gc.Equals, "ebs") 344 } 345 346 func (s *ConfigSuite) TestPrepareSetsDefaultBlockSource(c *gc.C) { 347 s.PatchValue(&verifyCredentials, func(*environ, context.ProviderCallContext) error { return nil }) 348 attrs := testing.FakeConfig().Merge(testing.Attrs{ 349 "type": "ec2", 350 }) 351 baseConfig, err := config.New(config.NoDefaults, attrs) 352 c.Assert(err, jc.ErrorIsNil) 353 354 credential := cloud.NewCredential( 355 cloud.AccessKeyAuthType, 356 map[string]string{ 357 "access-key": "x", 358 "secret-key": "y", 359 }, 360 ) 361 cfg, err := providerInstance.PrepareConfig(environs.PrepareConfigParams{ 362 Config: baseConfig, 363 Cloud: environs.CloudSpec{ 364 Type: "ec2", 365 Name: "aws", 366 Region: "test", 367 Credential: &credential, 368 }, 369 }) 370 c.Assert(err, jc.ErrorIsNil) 371 372 source, ok := cfg.StorageDefaultBlockSource() 373 c.Assert(ok, jc.IsTrue) 374 c.Assert(source, gc.Equals, "ebs") 375 } 376 377 func (*ConfigSuite) TestSchema(c *gc.C) { 378 fields := providerInstance.Schema() 379 // Check that all the fields defined in environs/config 380 // are in the returned schema. 381 globalFields, err := config.Schema(nil) 382 c.Assert(err, gc.IsNil) 383 for name, field := range globalFields { 384 c.Check(fields[name], jc.DeepEquals, field) 385 } 386 }