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