1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs_test
import (
. "launchpad.net/gocheck"
"launchpad.net/juju-core/environs"
"launchpad.net/juju-core/environs/config"
_ "launchpad.net/juju-core/environs/dummy"
"launchpad.net/juju-core/testing"
"path/filepath"
)
type suite struct{}
var _ = Suite(suite{})
var invalidConfigTests = []struct {
env string
err string
}{
{"'", "YAML error:.*"},
{`
default: unknown
environments:
only:
type: unknown
`, `default environment .* does not exist`,
},
}
func (suite) TestInvalidConfig(c *C) {
for i, t := range invalidConfigTests {
c.Logf("running test %v", i)
_, err := environs.ReadEnvironsBytes([]byte(t.env))
c.Check(err, ErrorMatches, t.err)
}
}
var invalidEnvTests = []struct {
env string
name string
err string
}{
{`
environments:
only:
foo: bar
`, "", `environment "only" has no type`,
}, {`
environments:
only:
foo: bar
`, "only", `environment "only" has no type`,
}, {`
environments:
only:
foo: bar
type: crazy
`, "only", `environment "only" has an unknown provider type "crazy"`,
}, {`
environments:
only:
type: dummy
`, "only", `.*state-server: expected bool, got nothing`,
}, {`
environments:
only:
type: dummy
state-server: false
unknown-value: causes-an-error
`, "only", `.*unknown-value: expected nothing, got "causes-an-error"`,
},
}
func (suite) TestInvalidEnv(c *C) {
defer testing.MakeFakeHomeNoEnvironments(c, "only").Restore()
for i, t := range invalidEnvTests {
c.Logf("running test %v", i)
es, err := environs.ReadEnvironsBytes([]byte(t.env))
c.Check(err, IsNil)
e, err := es.Open(t.name)
c.Check(err, ErrorMatches, t.err)
c.Check(e, IsNil)
}
}
var configTests = []struct {
env string
check func(c *C, es *environs.Environs)
}{
{`
environments:
only:
type: dummy
state-server: false
`, func(c *C, es *environs.Environs) {
e, err := es.Open("")
c.Assert(err, IsNil)
c.Assert(e.Name(), Equals, "only")
}}, {`
default:
invalid
environments:
valid:
type: dummy
state-server: false
invalid:
type: crazy
`, func(c *C, es *environs.Environs) {
e, err := es.Open("")
c.Assert(err, ErrorMatches, `environment "invalid" has an unknown provider type "crazy"`)
c.Assert(e, IsNil)
e, err = es.Open("valid")
c.Assert(err, IsNil)
c.Assert(e.Name(), Equals, "valid")
}}, {`
environments:
one:
type: dummy
state-server: false
two:
type: dummy
state-server: false
`, func(c *C, es *environs.Environs) {
e, err := es.Open("")
c.Assert(err, ErrorMatches, `no default environment found`)
c.Assert(e, IsNil)
}},
}
func (suite) TestConfig(c *C) {
defer testing.MakeFakeHomeNoEnvironments(c, "only", "valid", "one", "two").Restore()
for i, t := range configTests {
c.Logf("running test %v", i)
es, err := environs.ReadEnvironsBytes([]byte(t.env))
c.Assert(err, IsNil)
t.check(c, es)
}
}
func (suite) TestDefaultConfigFile(c *C) {
defer testing.MakeEmptyFakeHome(c).Restore()
env := `
environments:
only:
type: dummy
state-server: false
authorized-keys: i-am-a-key
`
outfile, err := environs.WriteEnvirons("", env)
c.Assert(err, IsNil)
path := testing.HomePath(".juju", "environments.yaml")
c.Assert(path, Equals, outfile)
es, err := environs.ReadEnvirons("")
c.Assert(err, IsNil)
e, err := es.Open("")
c.Assert(err, IsNil)
c.Assert(e.Name(), Equals, "only")
}
func (suite) TestNamedConfigFile(c *C) {
defer testing.MakeFakeHomeNoEnvironments(c, "only").Restore()
env := `
environments:
only:
type: dummy
state-server: false
authorized-keys: i-am-a-key
`
path := filepath.Join(c.MkDir(), "a-file")
outfile, err := environs.WriteEnvirons(path, env)
c.Assert(err, IsNil)
c.Assert(path, Equals, outfile)
es, err := environs.ReadEnvirons(path)
c.Assert(err, IsNil)
e, err := es.Open("")
c.Assert(err, IsNil)
c.Assert(e.Name(), Equals, "only")
}
func (suite) TestConfigRoundTrip(c *C) {
cfg, err := config.New(map[string]interface{}{
"name": "bladaam",
"type": "dummy",
"state-server": false,
"authorized-keys": "i-am-a-key",
"ca-cert": testing.CACert,
"ca-private-key": "",
})
c.Assert(err, IsNil)
provider, err := environs.Provider(cfg.Type())
c.Assert(err, IsNil)
cfg, err = provider.Validate(cfg, nil)
c.Assert(err, IsNil)
env, err := environs.New(cfg)
c.Assert(err, IsNil)
c.Assert(cfg.AllAttrs(), DeepEquals, env.Config().AllAttrs())
}
func (suite) TestBootstrapConfig(c *C) {
defer testing.MakeFakeHomeNoEnvironments(c, "bladaam").Restore()
cfg, err := config.New(map[string]interface{}{
"name": "bladaam",
"type": "dummy",
"state-server": false,
"admin-secret": "highly",
"secret": "um",
"authorized-keys": "i-am-a-key",
"ca-cert": testing.CACert,
"ca-private-key": testing.CAKey,
"agent-version": "1.2.3",
})
c.Assert(err, IsNil)
cfg1, err := environs.BootstrapConfig(cfg)
c.Assert(err, IsNil)
expect := cfg.AllAttrs()
delete(expect, "secret")
expect["admin-secret"] = ""
expect["ca-private-key"] = ""
c.Assert(cfg1.AllAttrs(), DeepEquals, expect)
}
|