~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/vsphere/config_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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:   "unknown field is not touched",
 
90
        insert: testing.Attrs{"unknown-field": "12345"},
 
91
        expect: testing.Attrs{"unknown-field": "12345"},
 
92
}}
 
93
 
 
94
func (*ConfigSuite) TestNewModelConfig(c *gc.C) {
 
95
        for i, test := range newConfigTests {
 
96
                c.Logf("test %d: %s", i, test.info)
 
97
 
 
98
                testConfig := test.newConfig(c)
 
99
                environ, err := environs.New(environs.OpenParams{
 
100
                        Cloud:  vsphere.FakeCloudSpec(),
 
101
                        Config: testConfig,
 
102
                })
 
103
 
 
104
                // Check the result
 
105
                if test.err != "" {
 
106
                        test.checkFailure(c, err, "invalid config")
 
107
                } else {
 
108
                        test.checkSuccess(c, environ, err)
 
109
                }
 
110
        }
 
111
}
 
112
 
 
113
func (*ConfigSuite) TestValidateNewConfig(c *gc.C) {
 
114
        for i, test := range newConfigTests {
 
115
                c.Logf("test %d: %s", i, test.info)
 
116
 
 
117
                testConfig := test.newConfig(c)
 
118
                validatedConfig, err := vsphere.Provider.Validate(testConfig, nil)
 
119
 
 
120
                // Check the result
 
121
                if test.err != "" {
 
122
                        test.checkFailure(c, err, "invalid config")
 
123
                } else {
 
124
                        c.Check(validatedConfig, gc.NotNil)
 
125
                        test.checkSuccess(c, validatedConfig, err)
 
126
                }
 
127
        }
 
128
}
 
129
 
 
130
func (s *ConfigSuite) TestValidateOldConfig(c *gc.C) {
 
131
        for i, test := range newConfigTests {
 
132
                c.Logf("test %d: %s", i, test.info)
 
133
 
 
134
                oldcfg := test.newConfig(c)
 
135
                newcfg := s.config
 
136
                expected := vsphere.ConfigAttrs()
 
137
 
 
138
                // Validate the new config (relative to the old one) using the
 
139
                // provider.
 
140
                validatedConfig, err := vsphere.Provider.Validate(newcfg, oldcfg)
 
141
 
 
142
                // Check the result.
 
143
                if test.err != "" {
 
144
                        test.checkFailure(c, err, "invalid base config")
 
145
                } else {
 
146
                        if test.remove != nil {
 
147
                                // No defaults are set on the old config.
 
148
                                c.Check(err, gc.ErrorMatches, "invalid base config: .*")
 
149
                                continue
 
150
                        }
 
151
 
 
152
                        c.Check(err, jc.ErrorIsNil)
 
153
                        // We verify that Validate filled in the defaults
 
154
                        // appropriately.
 
155
                        c.Check(validatedConfig, gc.NotNil)
 
156
                        test.checkAttrs(c, expected, validatedConfig)
 
157
                }
 
158
        }
 
159
}
 
160
 
 
161
var changeConfigTests = []configTestSpec{{
 
162
        info:   "no change, no error",
 
163
        expect: vsphere.ConfigAttrs(),
 
164
}, {
 
165
        info:   "can insert unknown field",
 
166
        insert: testing.Attrs{"unknown": "ignoti"},
 
167
        expect: testing.Attrs{"unknown": "ignoti"},
 
168
}}
 
169
 
 
170
func (s *ConfigSuite) TestValidateChange(c *gc.C) {
 
171
        for i, test := range changeConfigTests {
 
172
                c.Logf("test %d: %s", i, test.info)
 
173
 
 
174
                testConfig := test.newConfig(c)
 
175
                validatedConfig, err := vsphere.Provider.Validate(testConfig, s.config)
 
176
 
 
177
                // Check the result.
 
178
                if test.err != "" {
 
179
                        test.checkFailure(c, err, "invalid config change")
 
180
                } else {
 
181
                        test.checkSuccess(c, validatedConfig, err)
 
182
                }
 
183
        }
 
184
}
 
185
 
 
186
func (s *ConfigSuite) TestSetConfig(c *gc.C) {
 
187
        for i, test := range changeConfigTests {
 
188
                c.Logf("test %d: %s", i, test.info)
 
189
 
 
190
                environ, err := environs.New(environs.OpenParams{
 
191
                        Cloud:  vsphere.FakeCloudSpec(),
 
192
                        Config: s.config,
 
193
                })
 
194
                c.Assert(err, jc.ErrorIsNil)
 
195
 
 
196
                testConfig := test.newConfig(c)
 
197
                err = environ.SetConfig(testConfig)
 
198
 
 
199
                // Check the result.
 
200
                if test.err != "" {
 
201
                        test.checkFailure(c, err, "invalid config change")
 
202
                        test.checkAttrs(c, environ.Config().AllAttrs(), s.config)
 
203
                } else {
 
204
                        test.checkSuccess(c, environ.Config(), err)
 
205
                }
 
206
        }
 
207
}