1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
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"
18
type ConfigSuite struct {
24
var _ = gc.Suite(&ConfigSuite{})
26
func (s *ConfigSuite) SetUpTest(c *gc.C) {
27
s.BaseSuite.SetUpTest(c)
29
cfg, err := testing.EnvironConfig(c).Apply(vsphere.ConfigAttrs)
30
c.Assert(err, jc.ErrorIsNil)
34
// configTestSpec defines a subtest to run in a table driven test.
35
type configTestSpec struct {
36
// info describes the subtest.
38
// insert holds attrs that should be merged into the config.
40
// remove has the names of attrs that should be removed.
42
// expect defines the expected attributes in a success case.
44
// err is the error message to expect in a failure case.
48
func (ts configTestSpec) checkSuccess(c *gc.C, value interface{}, err error) {
49
if !c.Check(err, jc.ErrorIsNil) {
53
var cfg *config.Config
54
switch typed := value.(type) {
57
case environs.Environ:
61
attrs := cfg.AllAttrs()
62
for field, value := range ts.expect {
63
c.Check(attrs[field], gc.Equals, value)
67
func (ts configTestSpec) checkFailure(c *gc.C, err error, msg string) {
68
c.Check(err, gc.ErrorMatches, msg+": "+ts.err)
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)
77
func (ts configTestSpec) attrs() testing.Attrs {
78
return vsphere.ConfigAttrs.Merge(ts.insert).Delete(ts.remove...)
81
func (ts configTestSpec) newConfig(c *gc.C) *config.Config {
83
cfg, err := testing.EnvironConfig(c).Apply(attrs)
84
c.Assert(err, jc.ErrorIsNil)
88
var newConfigTests = []configTestSpec{{
89
info: "datacenter is required",
90
remove: []string{"datacenter"},
91
err: "datacenter: expected string, got nothing",
93
info: "datacenter cannot be empty",
94
insert: testing.Attrs{"datacenter": ""},
95
err: "datacenter: must not be empty",
97
info: "host is required",
98
remove: []string{"host"},
99
err: "host: expected string, got nothing",
101
info: "host cannot be empty",
102
insert: testing.Attrs{"host": ""},
103
err: "host: must not be empty",
105
info: "user is required",
106
remove: []string{"user"},
107
err: "user: expected string, got nothing",
109
info: "user cannot be empty",
110
insert: testing.Attrs{"user": ""},
111
err: "user: must not be empty",
113
info: "password is required",
114
remove: []string{"password"},
115
err: "password: expected string, got nothing",
117
info: "password cannot be empty",
118
insert: testing.Attrs{"password": ""},
119
err: "password: must not be empty",
121
info: "unknown field is not touched",
122
insert: testing.Attrs{"unknown-field": "12345"},
123
expect: testing.Attrs{"unknown-field": "12345"},
126
func (*ConfigSuite) TestNewEnvironConfig(c *gc.C) {
127
for i, test := range newConfigTests {
128
c.Logf("test %d: %s", i, test.info)
130
testConfig := test.newConfig(c)
131
environ, err := environs.New(testConfig)
135
test.checkFailure(c, err, "invalid config")
137
test.checkSuccess(c, environ, err)
142
func (*ConfigSuite) TestValidateNewConfig(c *gc.C) {
143
for i, test := range newConfigTests {
144
c.Logf("test %d: %s", i, test.info)
146
testConfig := test.newConfig(c)
147
validatedConfig, err := vsphere.Provider.Validate(testConfig, nil)
151
test.checkFailure(c, err, "invalid config")
153
c.Check(validatedConfig, gc.NotNil)
154
test.checkSuccess(c, validatedConfig, err)
159
func (s *ConfigSuite) TestValidateOldConfig(c *gc.C) {
160
for i, test := range newConfigTests {
161
c.Logf("test %d: %s", i, test.info)
163
oldcfg := test.newConfig(c)
165
expected := vsphere.ConfigAttrs
167
// Validate the new config (relative to the old one) using the
169
validatedConfig, err := vsphere.Provider.Validate(newcfg, oldcfg)
173
test.checkFailure(c, err, "invalid base config")
175
if test.remove != nil {
176
// No defaults are set on the old config.
177
c.Check(err, gc.ErrorMatches, "invalid base config: .*")
181
c.Check(err, jc.ErrorIsNil)
182
// We verify that Validate filled in the defaults
184
c.Check(validatedConfig, gc.NotNil)
185
test.checkAttrs(c, expected, validatedConfig)
190
var changeConfigTests = []configTestSpec{{
191
info: "no change, no error",
192
expect: vsphere.ConfigAttrs,
194
info: "cannot change datacenter",
195
insert: testing.Attrs{"datacenter": "/datacenter2"},
196
err: "datacenter: cannot change from /datacenter1 to /datacenter2",
198
info: "cannot change host",
199
insert: testing.Attrs{"host": "host2"},
200
err: "host: cannot change from host1 to host2",
202
info: "cannot change user",
203
insert: testing.Attrs{"user": "user2"},
204
expect: testing.Attrs{"user": "user2"},
206
info: "cannot change password",
207
insert: testing.Attrs{"password": "password2"},
208
expect: testing.Attrs{"password": "password2"},
210
info: "can insert unknown field",
211
insert: testing.Attrs{"unknown": "ignoti"},
212
expect: testing.Attrs{"unknown": "ignoti"},
215
func (s *ConfigSuite) TestValidateChange(c *gc.C) {
216
for i, test := range changeConfigTests {
217
c.Logf("test %d: %s", i, test.info)
219
testConfig := test.newConfig(c)
220
validatedConfig, err := vsphere.Provider.Validate(testConfig, s.config)
224
test.checkFailure(c, err, "invalid config change")
226
test.checkSuccess(c, validatedConfig, err)
231
func (s *ConfigSuite) TestSetConfig(c *gc.C) {
232
for i, test := range changeConfigTests {
233
c.Logf("test %d: %s", i, test.info)
235
environ, err := environs.New(s.config)
236
c.Assert(err, jc.ErrorIsNil)
238
testConfig := test.newConfig(c)
239
err = environ.SetConfig(testConfig)
243
test.checkFailure(c, err, "invalid config change")
244
test.checkAttrs(c, environ.Config().AllAttrs(), s.config)
246
test.checkSuccess(c, environ.Config(), err)