Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(963)

Side by Side Diff: environs/config/config_test.go

Issue 6782094: environs/config: error for cert not found
Patch Set: environs/config: error for cert not found Created 12 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« environs/config/config.go ('K') | « environs/config/config.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package config_test 1 package config_test
2 2
3 import ( 3 import (
4 "io/ioutil" 4 "io/ioutil"
5 . "launchpad.net/gocheck" 5 . "launchpad.net/gocheck"
6 "launchpad.net/juju-core/environs/config" 6 "launchpad.net/juju-core/environs/config"
7 "launchpad.net/juju-core/testing" 7 "launchpad.net/juju-core/testing"
8 "launchpad.net/juju-core/version" 8 "launchpad.net/juju-core/version"
9 "os" 9 "os"
10 "path/filepath" 10 "path/filepath"
11 stdtesting "testing" 11 stdtesting "testing"
12 ) 12 )
13 13
14 func Test(t *stdtesting.T) { 14 func Test(t *stdtesting.T) {
15 TestingT(t) 15 TestingT(t)
16 } 16 }
17 17
18 type ConfigSuite struct { 18 type ConfigSuite struct {
19 testing.LoggingSuite 19 testing.LoggingSuite
20 home string 20 home string
21 } 21 }
22 22
23 var _ = Suite(&ConfigSuite{}) 23 var _ = Suite(&ConfigSuite{})
24 24
25 type attrs map[string]interface{} 25 type attrs map[string]interface{}
26 26
27 var configTests = []struct { 27 type configTest struct {
28 about string 28 about string
29 attrs map[string]interface{} 29 attrs map[string]interface{}
30 err string 30 err string
31 }{ 31 }
32
33 var configTests = []configTest{
32 { 34 {
33 about: "The minimum good configuration", 35 about: "The minimum good configuration",
34 attrs: attrs{ 36 attrs: attrs{
35 "type": "my-type", 37 "type": "my-type",
36 "name": "my-name", 38 "name": "my-name",
37 }, 39 },
38 }, { 40 }, {
39 about: "Explicit series", 41 about: "Explicit series",
40 attrs: attrs{ 42 attrs: attrs{
41 "type": "my-type", 43 "type": "my-type",
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 about: "Illegal firewall mode", 241 about: "Illegal firewall mode",
240 attrs: attrs{ 242 attrs: attrs{
241 "type": "my-type", 243 "type": "my-type",
242 "name": "my-name", 244 "name": "my-name",
243 "firewall-mode": "illegal", 245 "firewall-mode": "illegal",
244 }, 246 },
245 err: "invalid firewall mode in environment configuration: .*", 247 err: "invalid firewall mode in environment configuration: .*",
246 }, 248 },
247 } 249 }
248 250
249 var configTestFiles = []struct { 251 type configTestFile struct {
250 name, data string 252 name, data string
251 }{ 253 }
254
255 var configTestFiles = []configTestFile{
252 {".ssh/id_dsa.pub", "dsa"}, 256 {".ssh/id_dsa.pub", "dsa"},
253 {".ssh/id_rsa.pub", "rsa\n"}, 257 {".ssh/id_rsa.pub", "rsa\n"},
254 {".ssh/identity.pub", "identity"}, 258 {".ssh/identity.pub", "identity"},
255 {".ssh/authorized_keys", "auth0\n# first\nauth1\n\n"}, 259 {".ssh/authorized_keys", "auth0\n# first\nauth1\n\n"},
256 {".ssh/authorized_keys2", "auth2\nauth3\n"}, 260 {".ssh/authorized_keys2", "auth2\nauth3\n"},
257 261
258 {".juju/my-name-cert.pem", caCert}, 262 {".juju/my-name-cert.pem", caCert},
259 {".juju/my-name-private-key.pem", caKey}, 263 {".juju/my-name-private-key.pem", caKey},
260 {".juju/cacert2.pem", caCert2}, 264 {".juju/cacert2.pem", caCert2},
261 {".juju/cakey2.pem", caKey2}, 265 {".juju/cakey2.pem", caKey2},
262 {"othercert.pem", caCert3}, 266 {"othercert.pem", caCert3},
263 {"otherkey.pem", caKey3}, 267 {"otherkey.pem", caKey3},
264 } 268 }
265 269
266 func (*ConfigSuite) TestConfig(c *C) { 270 func (*ConfigSuite) TestConfig(c *C) {
267 homeDir := filepath.Join(c.MkDir(), "me") 271 homeDir := filepath.Join(c.MkDir(), "me")
268 defer os.Setenv("HOME", os.Getenv("HOME")) 272 defer os.Setenv("HOME", os.Getenv("HOME"))
269 os.Setenv("HOME", homeDir) 273 os.Setenv("HOME", homeDir)
270 274 » writeFiles(c, configTestFiles)
271 » for _, f := range configTestFiles {
272 » » path := filepath.Join(homeDir, f.name)
273 » » err := os.MkdirAll(filepath.Dir(path), 0700)
274 » » c.Assert(err, IsNil)
275 » » err = ioutil.WriteFile(path, []byte(f.data), 0666)
276 » » c.Assert(err, IsNil)
277 » }
278 275
279 for i, test := range configTests { 276 for i, test := range configTests {
280 c.Logf("test %d. %s", i, test.about) 277 c.Logf("test %d. %s", i, test.about)
281 278
282 cfg, err := config.New(test.attrs) 279 cfg, err := config.New(test.attrs)
283 if test.err != "" { 280 if test.err != "" {
284 c.Assert(err, ErrorMatches, test.err) 281 c.Assert(err, ErrorMatches, test.err)
285 continue 282 continue
286 } 283 }
287 c.Assert(err, IsNil) 284 c.Assert(err, IsNil)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 if path, _ := test.attrs["ca-private-key-path"].(string); path ! = "" { 334 if path, _ := test.attrs["ca-private-key-path"].(string); path ! = "" {
338 c.Assert(cfg.CAPrivateKeyPEM(), Equals, fileContents(c, path)) 335 c.Assert(cfg.CAPrivateKeyPEM(), Equals, fileContents(c, path))
339 } else if k, ok := test.attrs["ca-private-key"]; ok { 336 } else if k, ok := test.attrs["ca-private-key"]; ok {
340 c.Assert(cfg.CAPrivateKeyPEM(), Equals, k) 337 c.Assert(cfg.CAPrivateKeyPEM(), Equals, k)
341 } else { 338 } else {
342 c.Assert(cfg.CAPrivateKeyPEM(), Equals, caKey) 339 c.Assert(cfg.CAPrivateKeyPEM(), Equals, caKey)
343 } 340 }
344 } 341 }
345 } 342 }
346 343
344 // We use a different set of test files for testing behaviour
345 // when the CA certificate is not found, as the files
346 // can't contain the default CA file.
347 var certNotFoundTestFiles = []configTestFile{
348 {".ssh/id_rsa.pub", "rsa\n"},
349 {"othercert.pem", caCert3},
350 {"otherkey.pem", caKey3},
351 }
352
353 var caCertNotFoundTests = []configTest{
354 {
355 about: "CA cert not found",
356 attrs: attrs{
357 "type": "my-type",
358 "name": "my-name",
359 },
360 err: config.ErrDefaultCACertNotFound.Error(),
361 }, {
362 about: "CA cert not there but private key specified",
363 attrs: attrs{
364 "type": "my-type",
365 "name": "my-name",
366 "ca-private-key": caKey,
367 },
368 err: "open .*my-name-cert.pem:.*",
369 }, {
370 about: "CA cert not there but private key specified as non-exist ent path",
371 attrs: attrs{
372 "type": "my-type",
373 "name": "my-name",
374 "ca-private-key": "blah.pem",
375 },
376 err: "open .*my-name-cert.pem:.*",
377 }, {
378 about: "CA cert specified as non-existent path",
379 attrs: attrs{
380 "type": "my-type",
381 "name": "my-name",
382 "ca-cert-path": ".juju/my-name-cert.pem",
383 },
384 err: "open .*my-name-cert.pem:.*",
385 },
386 }
387
388 func (*ConfigSuite) TestCACertNotFound(c *C) {
389 homeDir := filepath.Join(c.MkDir(), "me")
390 defer os.Setenv("HOME", os.Getenv("HOME"))
391 os.Setenv("HOME", homeDir)
392 writeFiles(c, certNotFoundTestFiles)
393
394 for i, test := range caCertNotFoundTests {
395 c.Logf("test %d. %s", i, test.about)
396
397 cfg, err := config.New(test.attrs)
398 c.Assert(err, ErrorMatches, test.err)
399 c.Assert(cfg, IsNil)
400 }
401 }
402
403 func writeFiles(c *C, files []configTestFile) {
404 homeDir := os.Getenv("HOME")
405 for _, f := range files {
406 path := filepath.Join(homeDir, f.name)
407 err := os.MkdirAll(filepath.Dir(path), 0700)
408 c.Assert(err, IsNil)
409 err = ioutil.WriteFile(path, []byte(f.data), 0666)
410 c.Assert(err, IsNil)
411 }
412 }
413
347 // fileContents returns the test file contents for the 414 // fileContents returns the test file contents for the
348 // given specified path (which may be relative, so 415 // given specified path (which may be relative, so
349 // we compare with the base filename only). 416 // we compare with the base filename only).
350 func fileContents(c *C, path string) string { 417 func fileContents(c *C, path string) string {
351 for _, f := range configTestFiles { 418 for _, f := range configTestFiles {
352 if filepath.Base(f.name) == filepath.Base(path) { 419 if filepath.Base(f.name) == filepath.Base(path) {
353 return f.data 420 return f.data
354 } 421 }
355 } 422 }
356 c.Fatalf("path attribute holds unknown test file: %q", path) 423 c.Fatalf("path attribute holds unknown test file: %q", path)
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 -----BEGIN RSA PRIVATE KEY----- 535 -----BEGIN RSA PRIVATE KEY-----
469 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw 536 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw
470 -----END RSA PRIVATE KEY----- 537 -----END RSA PRIVATE KEY-----
471 `[1:] 538 `[1:]
472 539
473 var invalidCACert = ` 540 var invalidCACert = `
474 -----BEGIN CERTIFICATE----- 541 -----BEGIN CERTIFICATE-----
475 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw 542 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw
476 -----END CERTIFICATE----- 543 -----END CERTIFICATE-----
477 `[1:] 544 `[1:]
OLDNEW
« environs/config/config.go ('K') | « environs/config/config.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b