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.ModelConfig(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.ModelConfig(c).Apply(attrs)
84
c.Assert(err, jc.ErrorIsNil)
88
var newConfigTests = []configTestSpec{{
89
info: "unknown field is not touched",
90
insert: testing.Attrs{"unknown-field": "12345"},
91
expect: testing.Attrs{"unknown-field": "12345"},
94
func (*ConfigSuite) TestNewModelConfig(c *gc.C) {
95
for i, test := range newConfigTests {
96
c.Logf("test %d: %s", i, test.info)
98
testConfig := test.newConfig(c)
99
environ, err := environs.New(environs.OpenParams{
100
Cloud: vsphere.FakeCloudSpec(),
106
test.checkFailure(c, err, "invalid config")
108
test.checkSuccess(c, environ, err)
113
func (*ConfigSuite) TestValidateNewConfig(c *gc.C) {
114
for i, test := range newConfigTests {
115
c.Logf("test %d: %s", i, test.info)
117
testConfig := test.newConfig(c)
118
validatedConfig, err := vsphere.Provider.Validate(testConfig, nil)
122
test.checkFailure(c, err, "invalid config")
124
c.Check(validatedConfig, gc.NotNil)
125
test.checkSuccess(c, validatedConfig, err)
130
func (s *ConfigSuite) TestValidateOldConfig(c *gc.C) {
131
for i, test := range newConfigTests {
132
c.Logf("test %d: %s", i, test.info)
134
oldcfg := test.newConfig(c)
136
expected := vsphere.ConfigAttrs()
138
// Validate the new config (relative to the old one) using the
140
validatedConfig, err := vsphere.Provider.Validate(newcfg, oldcfg)
144
test.checkFailure(c, err, "invalid base config")
146
if test.remove != nil {
147
// No defaults are set on the old config.
148
c.Check(err, gc.ErrorMatches, "invalid base config: .*")
152
c.Check(err, jc.ErrorIsNil)
153
// We verify that Validate filled in the defaults
155
c.Check(validatedConfig, gc.NotNil)
156
test.checkAttrs(c, expected, validatedConfig)
161
var changeConfigTests = []configTestSpec{{
162
info: "no change, no error",
163
expect: vsphere.ConfigAttrs(),
165
info: "can insert unknown field",
166
insert: testing.Attrs{"unknown": "ignoti"},
167
expect: testing.Attrs{"unknown": "ignoti"},
170
func (s *ConfigSuite) TestValidateChange(c *gc.C) {
171
for i, test := range changeConfigTests {
172
c.Logf("test %d: %s", i, test.info)
174
testConfig := test.newConfig(c)
175
validatedConfig, err := vsphere.Provider.Validate(testConfig, s.config)
179
test.checkFailure(c, err, "invalid config change")
181
test.checkSuccess(c, validatedConfig, err)
186
func (s *ConfigSuite) TestSetConfig(c *gc.C) {
187
for i, test := range changeConfigTests {
188
c.Logf("test %d: %s", i, test.info)
190
environ, err := environs.New(environs.OpenParams{
191
Cloud: vsphere.FakeCloudSpec(),
194
c.Assert(err, jc.ErrorIsNil)
196
testConfig := test.newConfig(c)
197
err = environ.SetConfig(testConfig)
201
test.checkFailure(c, err, "invalid config change")
202
test.checkAttrs(c, environ.Config().AllAttrs(), s.config)
204
test.checkSuccess(c, environ.Config(), err)