~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/ec2/config_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package ec2
 
2
 
 
3
// TODO: Clean this up so it matches environs/openstack/config_test.go.
 
4
 
 
5
import (
 
6
        "io/ioutil"
 
7
        "launchpad.net/goamz/aws"
 
8
        . "launchpad.net/gocheck"
 
9
        "launchpad.net/goyaml"
 
10
        "launchpad.net/juju-core/environs"
 
11
        "launchpad.net/juju-core/environs/config"
 
12
        "launchpad.net/juju-core/testing"
 
13
        "os"
 
14
        "path/filepath"
 
15
        "strings"
 
16
)
 
17
 
 
18
// Use local suite since this file lives in the ec2 package
 
19
// for testing internals.
 
20
type ConfigSuite struct {
 
21
        savedHome, savedAccessKey, savedSecretKey string
 
22
}
 
23
 
 
24
var _ = Suite(&ConfigSuite{})
 
25
 
 
26
var configTestRegion = aws.Region{
 
27
        Name:        "configtest",
 
28
        EC2Endpoint: "testregion.nowhere:1234",
 
29
}
 
30
 
 
31
var testAuth = aws.Auth{"gopher", "long teeth"}
 
32
 
 
33
// configTest specifies a config parsing test, checking that env when
 
34
// parsed as the ec2 section of a config file matches baseConfigResult
 
35
// when mutated by the mutate function, or that the parse matches the
 
36
// given error.
 
37
type configTest struct {
 
38
        config        attrs
 
39
        change        attrs
 
40
        region        string
 
41
        cbucket       string
 
42
        pbucket       string
 
43
        pbucketRegion string
 
44
        accessKey     string
 
45
        secretKey     string
 
46
        firewallMode  config.FirewallMode
 
47
        err           string
 
48
}
 
49
 
 
50
type attrs map[string]interface{}
 
51
 
 
52
func (t configTest) check(c *C) {
 
53
        envs := attrs{
 
54
                "environments": attrs{
 
55
                        "testenv": attrs{
 
56
                                "type":           "ec2",
 
57
                                "ca-cert":        testing.CACert,
 
58
                                "ca-private-key": testing.CAKey,
 
59
                        },
 
60
                },
 
61
        }
 
62
        testenv := envs["environments"].(attrs)["testenv"].(attrs)
 
63
        for k, v := range t.config {
 
64
                testenv[k] = v
 
65
        }
 
66
        if _, ok := testenv["control-bucket"]; !ok {
 
67
                testenv["control-bucket"] = "x"
 
68
        }
 
69
        data, err := goyaml.Marshal(envs)
 
70
        c.Assert(err, IsNil)
 
71
 
 
72
        es, err := environs.ReadEnvironsBytes(data)
 
73
        c.Check(err, IsNil)
 
74
 
 
75
        e, err := es.Open("testenv")
 
76
        if t.change != nil {
 
77
                c.Assert(err, IsNil)
 
78
 
 
79
                // Testing a change in configuration.
 
80
                var old, changed, valid *config.Config
 
81
                ec2env := e.(*environ)
 
82
                old = ec2env.ecfg().Config
 
83
                changed, err = old.Apply(t.change)
 
84
                c.Assert(err, IsNil)
 
85
 
 
86
                // Keep err for validation below.
 
87
                valid, err = providerInstance.Validate(changed, old)
 
88
                if err == nil {
 
89
                        err = ec2env.SetConfig(valid)
 
90
                }
 
91
        }
 
92
        if t.err != "" {
 
93
                c.Check(err, ErrorMatches, t.err)
 
94
                return
 
95
        }
 
96
        c.Assert(err, IsNil)
 
97
 
 
98
        ecfg := e.(*environ).ecfg()
 
99
        c.Assert(ecfg.Name(), Equals, "testenv")
 
100
        c.Assert(ecfg.controlBucket(), Equals, "x")
 
101
        if t.region != "" {
 
102
                c.Assert(ecfg.region(), Equals, t.region)
 
103
        }
 
104
        if t.pbucket != "" {
 
105
                c.Assert(ecfg.publicBucket(), Equals, t.pbucket)
 
106
        }
 
107
        if t.accessKey != "" {
 
108
                c.Assert(ecfg.accessKey(), Equals, t.accessKey)
 
109
                c.Assert(ecfg.secretKey(), Equals, t.secretKey)
 
110
                expected := map[string]interface{}{
 
111
                        "access-key": t.accessKey,
 
112
                        "secret-key": t.secretKey,
 
113
                }
 
114
                c.Assert(err, IsNil)
 
115
                actual, err := e.Provider().SecretAttrs(ecfg.Config)
 
116
                c.Assert(err, IsNil)
 
117
                c.Assert(expected, DeepEquals, actual)
 
118
        } else {
 
119
                c.Assert(ecfg.accessKey(), DeepEquals, testAuth.AccessKey)
 
120
                c.Assert(ecfg.secretKey(), DeepEquals, testAuth.SecretKey)
 
121
        }
 
122
        if t.firewallMode != "" {
 
123
                c.Assert(ecfg.FirewallMode(), Equals, t.firewallMode)
 
124
        }
 
125
 
 
126
        // check storage buckets are configured correctly
 
127
        env := e.(*environ)
 
128
        c.Assert(env.Storage().(*storage).bucket.Region.Name, Equals, ecfg.region())
 
129
        c.Assert(env.PublicStorage().(*storage).bucket.Region.Name, Equals, ecfg.publicBucketRegion())
 
130
}
 
131
 
 
132
var configTests = []configTest{
 
133
        {
 
134
                config:  attrs{},
 
135
                pbucket: "juju-dist",
 
136
        }, {
 
137
                // check that region defaults to us-east-1
 
138
                config: attrs{},
 
139
                region: "us-east-1",
 
140
        }, {
 
141
                config: attrs{
 
142
                        "region": "eu-west-1",
 
143
                },
 
144
                region: "eu-west-1",
 
145
        }, {
 
146
                config: attrs{
 
147
                        "region": "unknown",
 
148
                },
 
149
                err: ".*invalid region name.*",
 
150
        }, {
 
151
                config: attrs{
 
152
                        "region": "configtest",
 
153
                },
 
154
                region: "configtest",
 
155
        }, {
 
156
                config: attrs{
 
157
                        "region": "configtest",
 
158
                },
 
159
                change: attrs{
 
160
                        "region": "us-east-1",
 
161
                },
 
162
                err: `cannot change region from "configtest" to "us-east-1"`,
 
163
        }, {
 
164
                config: attrs{
 
165
                        "region": 666,
 
166
                },
 
167
                err: ".*expected string, got 666",
 
168
        }, {
 
169
                config: attrs{
 
170
                        "access-key": 666,
 
171
                },
 
172
                err: ".*expected string, got 666",
 
173
        }, {
 
174
                config: attrs{
 
175
                        "secret-key": 666,
 
176
                },
 
177
                err: ".*expected string, got 666",
 
178
        }, {
 
179
                config: attrs{
 
180
                        "control-bucket": 666,
 
181
                },
 
182
                err: ".*expected string, got 666",
 
183
        }, {
 
184
                change: attrs{
 
185
                        "control-bucket": "new-x",
 
186
                },
 
187
                err: `cannot change control-bucket from "x" to "new-x"`,
 
188
        }, {
 
189
                config: attrs{
 
190
                        "public-bucket": 666,
 
191
                },
 
192
                err: ".*expected string, got 666",
 
193
        }, {
 
194
                // check that the public-bucket defaults to juju-dist
 
195
                config:  attrs{},
 
196
                pbucket: "juju-dist",
 
197
        }, {
 
198
                config: attrs{
 
199
                        "public-bucket": "foo",
 
200
                },
 
201
                pbucket: "foo",
 
202
        }, {
 
203
                // check that public-bucket-region defaults to
 
204
                // us-east-1, the S3 endpoint that owns juju-dist
 
205
                config:        attrs{},
 
206
                pbucketRegion: "us-east-1",
 
207
        }, {
 
208
                config: attrs{
 
209
                        "public-bucket-region": "foo",
 
210
                },
 
211
                err: ".*invalid public-bucket-region name.*",
 
212
        }, {
 
213
                config: attrs{
 
214
                        "public-bucket-region": "ap-southeast-1",
 
215
                },
 
216
                pbucketRegion: "ap-southeast-1",
 
217
        }, {
 
218
                config: attrs{
 
219
                        "region":               "us-west-1",
 
220
                        "public-bucket-region": "ap-southeast-1",
 
221
                },
 
222
                region:        "us-west-1",
 
223
                pbucketRegion: "us-east-1",
 
224
        }, {
 
225
                config: attrs{
 
226
                        "access-key": "jujuer",
 
227
                        "secret-key": "open sesame",
 
228
                },
 
229
                accessKey: "jujuer",
 
230
                secretKey: "open sesame",
 
231
        }, {
 
232
                config: attrs{
 
233
                        "access-key": "jujuer",
 
234
                },
 
235
                err: ".*environment has no access-key or secret-key",
 
236
        }, {
 
237
                config: attrs{
 
238
                        "secret-key": "badness",
 
239
                },
 
240
                err: ".*environment has no access-key or secret-key",
 
241
        }, {
 
242
                config: attrs{
 
243
                        "admin-secret": "Futumpsh",
 
244
                },
 
245
        }, {
 
246
                config:       attrs{},
 
247
                firewallMode: config.FwInstance,
 
248
        }, {
 
249
                config: attrs{
 
250
                        "firewall-mode": "",
 
251
                },
 
252
                firewallMode: config.FwInstance,
 
253
        }, {
 
254
                config: attrs{
 
255
                        "firewall-mode": "instance",
 
256
                },
 
257
                firewallMode: config.FwInstance,
 
258
        }, {
 
259
                config: attrs{
 
260
                        "firewall-mode": "global",
 
261
                },
 
262
                firewallMode: config.FwGlobal,
 
263
        }, {
 
264
                config: attrs{
 
265
                        "ssl-hostname-verification": false,
 
266
                },
 
267
                err: "disabling ssh-hostname-verification is not supported",
 
268
        },
 
269
}
 
270
 
 
271
func indent(s string, with string) string {
 
272
        var r string
 
273
        lines := strings.Split(s, "\n")
 
274
        for _, l := range lines {
 
275
                r += with + l + "\n"
 
276
        }
 
277
        return r
 
278
}
 
279
 
 
280
func (s *ConfigSuite) SetUpTest(c *C) {
 
281
        s.savedHome = os.Getenv("HOME")
 
282
        s.savedAccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
 
283
        s.savedSecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
 
284
 
 
285
        home := c.MkDir()
 
286
        sshDir := filepath.Join(home, ".ssh")
 
287
        err := os.Mkdir(sshDir, 0777)
 
288
        c.Assert(err, IsNil)
 
289
        err = ioutil.WriteFile(filepath.Join(sshDir, "id_rsa.pub"), []byte("sshkey\n"), 0666)
 
290
        c.Assert(err, IsNil)
 
291
 
 
292
        os.Setenv("HOME", home)
 
293
        os.Setenv("AWS_ACCESS_KEY_ID", testAuth.AccessKey)
 
294
        os.Setenv("AWS_SECRET_ACCESS_KEY", testAuth.SecretKey)
 
295
        aws.Regions["configtest"] = configTestRegion
 
296
}
 
297
 
 
298
func (s *ConfigSuite) TearDownTest(c *C) {
 
299
        os.Setenv("HOME", s.savedHome)
 
300
        os.Setenv("AWS_ACCESS_KEY_ID", s.savedAccessKey)
 
301
        os.Setenv("AWS_SECRET_ACCESS_KEY", s.savedSecretKey)
 
302
        delete(aws.Regions, "configtest")
 
303
}
 
304
 
 
305
func (s *ConfigSuite) TestConfig(c *C) {
 
306
        for i, t := range configTests {
 
307
                c.Logf("test %d: %v", i, t.config)
 
308
                t.check(c)
 
309
        }
 
310
}
 
311
 
 
312
func (s *ConfigSuite) TestMissingAuth(c *C) {
 
313
        os.Setenv("AWS_ACCESS_KEY_ID", "")
 
314
        os.Setenv("AWS_SECRET_ACCESS_KEY", "")
 
315
        test := configTests[0]
 
316
        test.err = "environment has no access-key or secret-key"
 
317
        test.check(c)
 
318
}