github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/vsphere/config_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build !gccgo 5 6 package vsphere_test 7 8 import ( 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/environs" 13 "github.com/juju/juju/environs/config" 14 "github.com/juju/juju/provider/vsphere" 15 "github.com/juju/juju/testing" 16 ) 17 18 type ConfigSuite struct { 19 vsphere.BaseSuite 20 21 config *config.Config 22 } 23 24 var _ = gc.Suite(&ConfigSuite{}) 25 26 func (s *ConfigSuite) SetUpTest(c *gc.C) { 27 s.BaseSuite.SetUpTest(c) 28 29 cfg, err := testing.ModelConfig(c).Apply(vsphere.ConfigAttrs) 30 c.Assert(err, jc.ErrorIsNil) 31 s.config = cfg 32 } 33 34 // configTestSpec defines a subtest to run in a table driven test. 35 type configTestSpec struct { 36 // info describes the subtest. 37 info string 38 // insert holds attrs that should be merged into the config. 39 insert testing.Attrs 40 // remove has the names of attrs that should be removed. 41 remove []string 42 // expect defines the expected attributes in a success case. 43 expect testing.Attrs 44 // err is the error message to expect in a failure case. 45 err string 46 } 47 48 func (ts configTestSpec) checkSuccess(c *gc.C, value interface{}, err error) { 49 if !c.Check(err, jc.ErrorIsNil) { 50 return 51 } 52 53 var cfg *config.Config 54 switch typed := value.(type) { 55 case *config.Config: 56 cfg = typed 57 case environs.Environ: 58 cfg = typed.Config() 59 } 60 61 attrs := cfg.AllAttrs() 62 for field, value := range ts.expect { 63 c.Check(attrs[field], gc.Equals, value) 64 } 65 } 66 67 func (ts configTestSpec) checkFailure(c *gc.C, err error, msg string) { 68 c.Check(err, gc.ErrorMatches, msg+": "+ts.err) 69 } 70 71 func (ts configTestSpec) checkAttrs(c *gc.C, attrs map[string]interface{}, cfg *config.Config) { 72 for field, value := range cfg.UnknownAttrs() { 73 c.Check(attrs[field], gc.Equals, value) 74 } 75 } 76 77 func (ts configTestSpec) attrs() testing.Attrs { 78 return vsphere.ConfigAttrs.Merge(ts.insert).Delete(ts.remove...) 79 } 80 81 func (ts configTestSpec) newConfig(c *gc.C) *config.Config { 82 attrs := ts.attrs() 83 cfg, err := testing.ModelConfig(c).Apply(attrs) 84 c.Assert(err, jc.ErrorIsNil) 85 return cfg 86 } 87 88 var newConfigTests = []configTestSpec{{ 89 info: "datacenter is required", 90 remove: []string{"datacenter"}, 91 err: "datacenter: expected string, got nothing", 92 }, { 93 info: "datacenter cannot be empty", 94 insert: testing.Attrs{"datacenter": ""}, 95 err: "datacenter: must not be empty", 96 }, { 97 info: "host is required", 98 remove: []string{"host"}, 99 err: "host: expected string, got nothing", 100 }, { 101 info: "host cannot be empty", 102 insert: testing.Attrs{"host": ""}, 103 err: "host: must not be empty", 104 }, { 105 info: "user is required", 106 remove: []string{"user"}, 107 err: "user: expected string, got nothing", 108 }, { 109 info: "user cannot be empty", 110 insert: testing.Attrs{"user": ""}, 111 err: "user: must not be empty", 112 }, { 113 info: "password is required", 114 remove: []string{"password"}, 115 err: "password: expected string, got nothing", 116 }, { 117 info: "password cannot be empty", 118 insert: testing.Attrs{"password": ""}, 119 err: "password: must not be empty", 120 }, { 121 info: "unknown field is not touched", 122 insert: testing.Attrs{"unknown-field": "12345"}, 123 expect: testing.Attrs{"unknown-field": "12345"}, 124 }} 125 126 func (*ConfigSuite) TestNewModelConfig(c *gc.C) { 127 for i, test := range newConfigTests { 128 c.Logf("test %d: %s", i, test.info) 129 130 testConfig := test.newConfig(c) 131 environ, err := environs.New(testConfig) 132 133 // Check the result 134 if test.err != "" { 135 test.checkFailure(c, err, "invalid config") 136 } else { 137 test.checkSuccess(c, environ, err) 138 } 139 } 140 } 141 142 func (*ConfigSuite) TestValidateNewConfig(c *gc.C) { 143 for i, test := range newConfigTests { 144 c.Logf("test %d: %s", i, test.info) 145 146 testConfig := test.newConfig(c) 147 validatedConfig, err := vsphere.Provider.Validate(testConfig, nil) 148 149 // Check the result 150 if test.err != "" { 151 test.checkFailure(c, err, "invalid config") 152 } else { 153 c.Check(validatedConfig, gc.NotNil) 154 test.checkSuccess(c, validatedConfig, err) 155 } 156 } 157 } 158 159 func (s *ConfigSuite) TestValidateOldConfig(c *gc.C) { 160 for i, test := range newConfigTests { 161 c.Logf("test %d: %s", i, test.info) 162 163 oldcfg := test.newConfig(c) 164 newcfg := s.config 165 expected := vsphere.ConfigAttrs 166 167 // Validate the new config (relative to the old one) using the 168 // provider. 169 validatedConfig, err := vsphere.Provider.Validate(newcfg, oldcfg) 170 171 // Check the result. 172 if test.err != "" { 173 test.checkFailure(c, err, "invalid base config") 174 } else { 175 if test.remove != nil { 176 // No defaults are set on the old config. 177 c.Check(err, gc.ErrorMatches, "invalid base config: .*") 178 continue 179 } 180 181 c.Check(err, jc.ErrorIsNil) 182 // We verify that Validate filled in the defaults 183 // appropriately. 184 c.Check(validatedConfig, gc.NotNil) 185 test.checkAttrs(c, expected, validatedConfig) 186 } 187 } 188 } 189 190 var changeConfigTests = []configTestSpec{{ 191 info: "no change, no error", 192 expect: vsphere.ConfigAttrs, 193 }, { 194 info: "cannot change datacenter", 195 insert: testing.Attrs{"datacenter": "/datacenter2"}, 196 err: "datacenter: cannot change from /datacenter1 to /datacenter2", 197 }, { 198 info: "cannot change host", 199 insert: testing.Attrs{"host": "host2"}, 200 err: "host: cannot change from host1 to host2", 201 }, { 202 info: "cannot change user", 203 insert: testing.Attrs{"user": "user2"}, 204 expect: testing.Attrs{"user": "user2"}, 205 }, { 206 info: "cannot change password", 207 insert: testing.Attrs{"password": "password2"}, 208 expect: testing.Attrs{"password": "password2"}, 209 }, { 210 info: "can insert unknown field", 211 insert: testing.Attrs{"unknown": "ignoti"}, 212 expect: testing.Attrs{"unknown": "ignoti"}, 213 }} 214 215 func (s *ConfigSuite) TestValidateChange(c *gc.C) { 216 for i, test := range changeConfigTests { 217 c.Logf("test %d: %s", i, test.info) 218 219 testConfig := test.newConfig(c) 220 validatedConfig, err := vsphere.Provider.Validate(testConfig, s.config) 221 222 // Check the result. 223 if test.err != "" { 224 test.checkFailure(c, err, "invalid config change") 225 } else { 226 test.checkSuccess(c, validatedConfig, err) 227 } 228 } 229 } 230 231 func (s *ConfigSuite) TestSetConfig(c *gc.C) { 232 for i, test := range changeConfigTests { 233 c.Logf("test %d: %s", i, test.info) 234 235 environ, err := environs.New(s.config) 236 c.Assert(err, jc.ErrorIsNil) 237 238 testConfig := test.newConfig(c) 239 err = environ.SetConfig(testConfig) 240 241 // Check the result. 242 if test.err != "" { 243 test.checkFailure(c, err, "invalid config change") 244 test.checkAttrs(c, environ.Config().AllAttrs(), s.config) 245 } else { 246 test.checkSuccess(c, environ.Config(), err) 247 } 248 } 249 }