github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/manual/config_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package manual 5 6 import ( 7 "fmt" 8 "regexp" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/environs/config" 14 coretesting "github.com/juju/juju/testing" 15 ) 16 17 type configSuite struct { 18 coretesting.FakeJujuHomeSuite 19 } 20 21 var _ = gc.Suite(&configSuite{}) 22 23 func MinimalConfigValues() map[string]interface{} { 24 return map[string]interface{}{ 25 "name": "test", 26 "type": "manual", 27 "bootstrap-host": "hostname", 28 "bootstrap-user": "", 29 "storage-auth-key": "whatever", 30 "storage-port": 8040, 31 "storage-listen-ip": "", 32 // Not strictly necessary, but simplifies testing by disabling 33 // ssh storage by default. 34 "use-sshstorage": false, 35 // While the ca-cert bits aren't entirely minimal, they avoid the need 36 // to set up a fake home. 37 "ca-cert": coretesting.CACert, 38 "ca-private-key": coretesting.CAKey, 39 } 40 } 41 42 func MinimalConfig(c *gc.C) *config.Config { 43 minimal := MinimalConfigValues() 44 testConfig, err := config.New(config.UseDefaults, minimal) 45 c.Assert(err, jc.ErrorIsNil) 46 return testConfig 47 } 48 49 func getEnvironConfig(c *gc.C, attrs map[string]interface{}) *environConfig { 50 testConfig, err := config.New(config.UseDefaults, attrs) 51 c.Assert(err, jc.ErrorIsNil) 52 envConfig, err := manualProvider{}.validate(testConfig, nil) 53 c.Assert(err, jc.ErrorIsNil) 54 return envConfig 55 } 56 57 func (s *configSuite) TestValidateConfig(c *gc.C) { 58 testConfig := MinimalConfig(c) 59 testConfig, err := testConfig.Apply(map[string]interface{}{"bootstrap-host": ""}) 60 c.Assert(err, jc.ErrorIsNil) 61 _, err = manualProvider{}.Validate(testConfig, nil) 62 c.Assert(err, gc.ErrorMatches, "bootstrap-host must be specified") 63 64 testConfig, err = testConfig.Apply(map[string]interface{}{"storage-auth-key": nil}) 65 c.Assert(err, jc.ErrorIsNil) 66 _, err = manualProvider{}.Validate(testConfig, nil) 67 c.Assert(err, gc.ErrorMatches, "storage-auth-key: expected string, got nothing") 68 69 values := MinimalConfigValues() 70 delete(values, "bootstrap-user") 71 delete(values, "storage-listen-ip") 72 delete(values, "storage-port") 73 testConfig, err = config.New(config.UseDefaults, values) 74 c.Assert(err, jc.ErrorIsNil) 75 76 valid, err := manualProvider{}.Validate(testConfig, nil) 77 c.Assert(err, jc.ErrorIsNil) 78 unknownAttrs := valid.UnknownAttrs() 79 c.Assert(unknownAttrs["bootstrap-host"], gc.Equals, "hostname") 80 c.Assert(unknownAttrs["bootstrap-user"], gc.Equals, "") 81 c.Assert(unknownAttrs["storage-listen-ip"], gc.Equals, "") 82 c.Assert(unknownAttrs["storage-port"], gc.Equals, int(8040)) 83 } 84 85 func (s *configSuite) TestConfigMutability(c *gc.C) { 86 testConfig := MinimalConfig(c) 87 valid, err := manualProvider{}.Validate(testConfig, nil) 88 c.Assert(err, jc.ErrorIsNil) 89 unknownAttrs := valid.UnknownAttrs() 90 91 // Make sure the immutable values can't be changed. It'd be nice to be 92 // able to change these, but that would involve somehow updating the 93 // machine agent's config/upstart config. 94 oldConfig := testConfig 95 for k, v := range map[string]interface{}{ 96 "bootstrap-host": "new-hostname", 97 "bootstrap-user": "new-username", 98 "storage-listen-ip": "10.0.0.123", 99 "storage-port": 1234, 100 } { 101 testConfig = MinimalConfig(c) 102 testConfig, err = testConfig.Apply(map[string]interface{}{k: v}) 103 c.Assert(err, jc.ErrorIsNil) 104 _, err := manualProvider{}.Validate(testConfig, oldConfig) 105 oldv := unknownAttrs[k] 106 errmsg := fmt.Sprintf("cannot change %s from %q to %q", k, oldv, v) 107 c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(errmsg)) 108 } 109 } 110 111 func (s *configSuite) TestBootstrapHostUser(c *gc.C) { 112 values := MinimalConfigValues() 113 testConfig := getEnvironConfig(c, values) 114 c.Assert(testConfig.bootstrapHost(), gc.Equals, "hostname") 115 c.Assert(testConfig.bootstrapUser(), gc.Equals, "") 116 values["bootstrap-host"] = "127.0.0.1" 117 values["bootstrap-user"] = "ubuntu" 118 testConfig = getEnvironConfig(c, values) 119 c.Assert(testConfig.bootstrapHost(), gc.Equals, "127.0.0.1") 120 c.Assert(testConfig.bootstrapUser(), gc.Equals, "ubuntu") 121 } 122 123 func (s *configSuite) TestStorageParams(c *gc.C) { 124 values := MinimalConfigValues() 125 testConfig := getEnvironConfig(c, values) 126 c.Assert(testConfig.storageAddr(), gc.Equals, "hostname:8040") 127 c.Assert(testConfig.storageListenAddr(), gc.Equals, ":8040") 128 values["storage-listen-ip"] = "10.0.0.123" 129 values["storage-port"] = 1234 130 testConfig = getEnvironConfig(c, values) 131 c.Assert(testConfig.storageAddr(), gc.Equals, "hostname:1234") 132 c.Assert(testConfig.storageListenAddr(), gc.Equals, "10.0.0.123:1234") 133 } 134 135 func (s *configSuite) TestStorageCompat(c *gc.C) { 136 // Older environment configurations will not have the 137 // use-sshstorage attribute. We treat them as if they 138 // have use-sshstorage=false. 139 values := MinimalConfigValues() 140 delete(values, "use-sshstorage") 141 cfg, err := config.New(config.UseDefaults, values) 142 c.Assert(err, jc.ErrorIsNil) 143 envConfig := newEnvironConfig(cfg, values) 144 c.Assert(err, jc.ErrorIsNil) 145 c.Assert(envConfig.useSSHStorage(), jc.IsFalse) 146 } 147 148 func (s *configSuite) TestConfigWithFloatStoragePort(c *gc.C) { 149 // When the config values get serialized through JSON, the integers 150 // get coerced to float64 values. The parsing needs to handle this. 151 values := MinimalConfigValues() 152 values["storage-port"] = float64(8040) 153 154 cfg, err := config.New(config.UseDefaults, values) 155 c.Assert(err, jc.ErrorIsNil) 156 valid, err := ProviderInstance.Validate(cfg, nil) 157 c.Assert(err, jc.ErrorIsNil) 158 unknownAttrs := valid.UnknownAttrs() 159 c.Assert(unknownAttrs["storage-port"], gc.Equals, int(8040)) 160 161 env, err := ProviderInstance.Open(cfg) 162 c.Assert(err, jc.ErrorIsNil) 163 // really, we're asserting that this doesn't panic :) 164 c.Assert(env.(*manualEnviron).cfg.storagePort(), gc.Equals, int(8040)) 165 }