~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

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

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
        "github.com/juju/testing"
11
11
        jc "github.com/juju/testing/checkers"
12
 
        "github.com/juju/utils"
13
12
        "github.com/juju/utils/ssh"
14
13
        gc "gopkg.in/check.v1"
15
14
 
16
15
        "github.com/juju/juju/cloud"
17
16
        "github.com/juju/juju/environs"
18
17
        "github.com/juju/juju/environs/config"
19
 
        envtesting "github.com/juju/juju/environs/testing"
20
18
        jp "github.com/juju/juju/provider/joyent"
21
19
        coretesting "github.com/juju/juju/testing"
22
20
)
30
28
 
31
29
func validAttrs() coretesting.Attrs {
32
30
        return coretesting.FakeConfig().Merge(coretesting.Attrs{
33
 
                "type":             "joyent",
34
 
                "sdc-user":         "test",
35
 
                "sdc-key-id":       "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff",
36
 
                "sdc-url":          "test://test.api.joyentcloud.com",
37
 
                "manta-user":       "test",
38
 
                "manta-key-id":     "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff",
39
 
                "manta-url":        "test://test.manta.joyent.com",
40
 
                "private-key-path": "~/.ssh/provider_id_rsa",
41
 
                "algorithm":        "rsa-sha256",
42
 
                "control-dir":      "juju-test",
 
31
                "type":        "joyent",
 
32
                "sdc-user":    "test",
 
33
                "sdc-key-id":  "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff",
 
34
                "sdc-url":     "test://test.api.joyentcloud.com",
 
35
                "private-key": testPrivateKey,
 
36
                "algorithm":   "rsa-sha256",
43
37
        })
44
38
}
45
39
 
57
51
        s.AddSuiteCleanup(func(*gc.C) { restoreSdcAccount() })
58
52
        restoreSdcKeyId := testing.PatchEnvironment(jp.SdcKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00")
59
53
        s.AddSuiteCleanup(func(*gc.C) { restoreSdcKeyId() })
60
 
        restoreMantaUser := testing.PatchEnvironment(jp.MantaUser, "tester")
61
 
        s.AddSuiteCleanup(func(*gc.C) { restoreMantaUser() })
62
 
        restoreMantaKeyId := testing.PatchEnvironment(jp.MantaKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00")
63
 
        s.AddSuiteCleanup(func(*gc.C) { restoreMantaKeyId() })
64
54
        s.privateKeyData = generatePrivateKey(c)
65
55
        jp.RegisterMachinesEndpoint()
66
56
        s.AddSuiteCleanup(func(*gc.C) { jp.UnregisterMachinesEndpoint() })
79
69
 
80
70
func (s *ConfigSuite) SetUpTest(c *gc.C) {
81
71
        s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
82
 
        s.AddCleanup(CreateTestKey(c))
83
72
        for _, envVar := range jp.EnvironmentVariables {
84
73
                s.PatchEnvironment(envVar, "")
85
74
        }
150
139
        insert: coretesting.Attrs{"sdc-url": "test://test.api.joyentcloud.com"},
151
140
        expect: coretesting.Attrs{"sdc-url": "test://test.api.joyentcloud.com"},
152
141
}, {
153
 
        info:   "manta-user is required",
154
 
        remove: []string{"manta-user"},
155
 
        err:    ".* cannot get manta-user value from model variable .*",
156
 
}, {
157
 
        info:   "manta-user cannot be empty",
158
 
        insert: coretesting.Attrs{"manta-user": ""},
159
 
        err:    ".* cannot get manta-user value from model variable .*",
160
 
}, {
161
 
        info:   "can get manta-user from model variable",
162
 
        insert: coretesting.Attrs{"manta-user": ""},
163
 
        expect: coretesting.Attrs{"manta-user": "tester"},
164
 
        envVars: map[string]string{
165
 
                "MANTA_USER": "tester",
166
 
        },
167
 
}, {
168
 
        info:   "can get manta-user from model variable, missing from config",
169
 
        remove: []string{"manta-user"},
170
 
        expect: coretesting.Attrs{"manta-user": "tester"},
171
 
        envVars: map[string]string{
172
 
                "MANTA_USER": "tester",
173
 
        },
174
 
}, {
175
 
        info:   "manta-key-id is required",
176
 
        remove: []string{"manta-key-id"},
177
 
        err:    ".* cannot get manta-key-id value from model variable .*",
178
 
}, {
179
 
        info:   "manta-key-id cannot be empty",
180
 
        insert: coretesting.Attrs{"manta-key-id": ""},
181
 
        err:    ".* cannot get manta-key-id value from model variable .*",
182
 
}, {
183
 
        info:   "can get manta-key-id from env variable",
184
 
        insert: coretesting.Attrs{"manta-key-id": ""},
185
 
        expect: coretesting.Attrs{"manta-key-id": "key"},
186
 
        envVars: map[string]string{
187
 
                "MANTA_KEY_ID": "key",
188
 
        },
189
 
}, {
190
 
        info:   "can get manta-key-id from model variable, missing from config",
191
 
        remove: []string{"manta-key-id"},
192
 
        expect: coretesting.Attrs{"manta-key-id": "key"},
193
 
        envVars: map[string]string{
194
 
                "MANTA_KEY_ID": "key",
195
 
        },
196
 
}, {
197
 
        info:   "manta-url is inserted if missing",
198
 
        expect: coretesting.Attrs{"manta-url": "test://test.manta.joyent.com"},
199
 
}, {
200
 
        info:   "manta-url cannot be empty",
201
 
        insert: coretesting.Attrs{"manta-url": ""},
202
 
        err:    ".* cannot get manta-url value from model variable .*",
203
 
}, {
204
 
        info:   "manta-url is untouched if present",
205
 
        insert: coretesting.Attrs{"manta-url": "test://test.manta.joyent.com"},
206
 
        expect: coretesting.Attrs{"manta-url": "test://test.manta.joyent.com"},
207
 
}, {
208
 
        info:   "can get private-key-path from env variable",
209
 
        insert: coretesting.Attrs{"private-key-path": ""},
210
 
        expect: coretesting.Attrs{"private-key-path": "some-file"},
211
 
        envVars: map[string]string{
212
 
                "MANTA_PRIVATE_KEY_FILE": "some-file",
213
 
        },
214
 
}, {
215
 
        info:   "can get private-key-path from model variable, missing from config",
216
 
        remove: []string{"private-key-path"},
217
 
        expect: coretesting.Attrs{"private-key-path": "some-file"},
218
 
        envVars: map[string]string{
219
 
                "MANTA_PRIVATE_KEY_FILE": "some-file",
220
 
        },
221
 
}, {
222
142
        info:   "algorithm is inserted if missing",
223
143
        expect: coretesting.Attrs{"algorithm": "rsa-sha256"},
224
144
}, {
229
149
        info:   "unknown field is not touched",
230
150
        insert: coretesting.Attrs{"unknown-field": 12345},
231
151
        expect: coretesting.Attrs{"unknown-field": 12345},
232
 
}, {
233
 
        info:   "can specify just private-key",
234
 
        remove: []string{"private-key-path"},
235
 
        insert: coretesting.Attrs{"private-key": "foo"},
236
152
}}
237
153
 
238
154
func (s *ConfigSuite) TestNewModelConfig(c *gc.C) {
288
204
        insert: coretesting.Attrs{"sdc-url": "test://test.api.joyentcloud.com"},
289
205
        expect: coretesting.Attrs{"sdc-url": "test://test.api.joyentcloud.com"},
290
206
}, {
291
 
        info:   "can change manta-user",
292
 
        insert: coretesting.Attrs{"manta-user": "manta_user"},
293
 
        expect: coretesting.Attrs{"manta-user": "manta_user"},
294
 
}, {
295
 
        info:   "can change manta-key-id",
296
 
        insert: coretesting.Attrs{"manta-key-id": "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00"},
297
 
        expect: coretesting.Attrs{"manta-key-id": "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00"},
298
 
}, {
299
 
        info:   "can change manta-url",
300
 
        insert: coretesting.Attrs{"manta-url": "test://test.manta.joyent.com"},
301
 
        expect: coretesting.Attrs{"manta-url": "test://test.manta.joyent.com"},
302
 
}, {
303
207
        info:   "can insert unknown field",
304
208
        insert: coretesting.Attrs{"unknown": "ignoti"},
305
209
        expect: coretesting.Attrs{"unknown": "ignoti"},
307
211
 
308
212
func (s *ConfigSuite) TestValidateChange(c *gc.C) {
309
213
        attrs := validAttrs()
310
 
        attrs["private-key"] = s.privateKeyData
311
214
        baseConfig := newConfig(c, attrs)
312
215
        for i, test := range changeConfigTests {
313
216
                c.Logf("test %d: %s", i, test.info)
314
217
                attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
315
 
                attrs["private-key"] = s.privateKeyData
316
218
                testConfig := newConfig(c, attrs)
317
219
                validatedConfig, err := jp.Provider.Validate(testConfig, baseConfig)
318
220
                if test.err == "" {
355
257
        }
356
258
}
357
259
 
358
 
func validPrepareAttrs() coretesting.Attrs {
359
 
        return validAttrs().Delete("private-key")
360
 
}
361
 
 
362
260
// TODO(wallyworld) - add tests for cloud endpoint passed in via bootstrap args
363
 
var prepareConfigTests = []struct {
 
261
var bootstrapConfigTests = []struct {
364
262
        info   string
365
263
        insert coretesting.Attrs
366
264
        remove []string
367
265
        expect coretesting.Attrs
368
266
        err    string
369
267
}{{
370
 
        info:   "All value provided, nothig to do",
371
 
        expect: validPrepareAttrs(),
372
 
}, {
373
 
        info:   "private key is loaded from key file",
374
 
        insert: coretesting.Attrs{"private-key-path": fmt.Sprintf("~/.ssh/%s", testKeyFileName)},
375
 
        expect: coretesting.Attrs{"private-key": testPrivateKey},
376
 
}, {
377
 
        info:   "bad private-key-path errors, not panics",
378
 
        insert: coretesting.Attrs{"private-key-path": "~/.ssh/no-such-file"},
379
 
        err:    "invalid Joyent provider config: open .*: " + utils.NoSuchFileErrRegexp,
 
268
        info:   "All value provided, nothing to do",
 
269
        expect: validAttrs(),
380
270
}}
381
271
 
382
 
func (s *ConfigSuite) TestPrepareForBootstrap(c *gc.C) {
383
 
        ctx := envtesting.BootstrapContext(c)
384
 
        for i, test := range prepareConfigTests {
 
272
func (s *ConfigSuite) TestBootstrapConfig(c *gc.C) {
 
273
        for i, test := range bootstrapConfigTests {
385
274
                c.Logf("test %d: %s", i, test.info)
386
 
                attrs := validPrepareAttrs().Merge(test.insert).Delete(test.remove...)
 
275
                attrs := validAttrs().Merge(test.insert).Delete(test.remove...)
387
276
                credentialAttrs := make(map[string]string, len(attrs))
388
 
                for k, v := range attrs.Delete("type", "control-dir") {
 
277
                for k, v := range attrs.Delete("type") {
389
278
                        credentialAttrs[k] = fmt.Sprintf("%v", v)
390
279
                }
391
280
                testConfig := newConfig(c, attrs)
392
 
                preparedConfig, err := jp.Provider.PrepareForBootstrap(ctx, environs.PrepareForBootstrapParams{
 
281
                preparedConfig, err := jp.Provider.BootstrapConfig(environs.BootstrapConfigParams{
393
282
                        Config: testConfig,
394
283
                        Credentials: cloud.NewCredential(
395
284
                                cloud.UserPassAuthType,
398
287
                })
399
288
                if test.err == "" {
400
289
                        c.Check(err, jc.ErrorIsNil)
401
 
                        attrs := preparedConfig.Config().AllAttrs()
 
290
                        attrs := preparedConfig.AllAttrs()
402
291
                        for field, value := range test.expect {
403
292
                                c.Check(attrs[field], gc.Equals, value)
404
293
                        }