LEFT | RIGHT |
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" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 "type": "", | 238 "type": "", |
239 }, | 239 }, |
240 err: "empty type in environment configuration", | 240 err: "empty type in environment configuration", |
241 }, { | 241 }, { |
242 about: "Missing name", | 242 about: "Missing name", |
243 attrs: attrs{ | 243 attrs: attrs{ |
244 "type": "my-type", | 244 "type": "my-type", |
245 }, | 245 }, |
246 err: "name: expected string, got nothing", | 246 err: "name: expected string, got nothing", |
247 }, { | 247 }, { |
248 » » about: "Bad name", | 248 » » about: "Bad name, no slash", |
249 attrs: attrs{ | 249 attrs: attrs{ |
250 "name": "foo/bar", | 250 "name": "foo/bar", |
| 251 "type": "my-type", |
| 252 }, |
| 253 err: "environment name contains unsafe characters", |
| 254 }, { |
| 255 about: "Bad name, no backslash", |
| 256 attrs: attrs{ |
| 257 "name": "foo\\bar", |
251 "type": "my-type", | 258 "type": "my-type", |
252 }, | 259 }, |
253 err: "environment name contains unsafe characters", | 260 err: "environment name contains unsafe characters", |
254 }, { | 261 }, { |
255 about: "Empty name", | 262 about: "Empty name", |
256 attrs: attrs{ | 263 attrs: attrs{ |
257 "type": "my-type", | 264 "type": "my-type", |
258 "name": "", | 265 "name": "", |
259 }, | 266 }, |
260 err: "empty name in environment configuration", | 267 err: "empty name in environment configuration", |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 "ca-private-key": "", | 532 "ca-private-key": "", |
526 "ca-cert": caCert, | 533 "ca-cert": caCert, |
527 "ssl-hostname-verification": true, | 534 "ssl-hostname-verification": true, |
528 } | 535 } |
529 cfg, err := config.New(attrs) | 536 cfg, err := config.New(attrs) |
530 c.Assert(err, IsNil) | 537 c.Assert(err, IsNil) |
531 | 538 |
532 // These attributes are added if not set. | 539 // These attributes are added if not set. |
533 attrs["development"] = false | 540 attrs["development"] = false |
534 attrs["default-series"] = config.DefaultSeries | 541 attrs["default-series"] = config.DefaultSeries |
| 542 // Default firewall mode is instance |
| 543 attrs["firewall-mode"] = string(config.FwInstance) |
535 c.Assert(cfg.AllAttrs(), DeepEquals, attrs) | 544 c.Assert(cfg.AllAttrs(), DeepEquals, attrs) |
536 c.Assert(cfg.UnknownAttrs(), DeepEquals, map[string]interface{}{"unknown
": "my-unknown"}) | 545 c.Assert(cfg.UnknownAttrs(), DeepEquals, map[string]interface{}{"unknown
": "my-unknown"}) |
537 | 546 |
538 newcfg, err := cfg.Apply(map[string]interface{}{ | 547 newcfg, err := cfg.Apply(map[string]interface{}{ |
539 "name": "new-name", | 548 "name": "new-name", |
540 "new-unknown": "my-new-unknown", | 549 "new-unknown": "my-new-unknown", |
541 }) | 550 }) |
542 | 551 |
543 attrs["name"] = "new-name" | 552 attrs["name"] = "new-name" |
544 attrs["new-unknown"] = "my-new-unknown" | 553 attrs["new-unknown"] = "my-new-unknown" |
545 c.Assert(newcfg.AllAttrs(), DeepEquals, attrs) | 554 c.Assert(newcfg.AllAttrs(), DeepEquals, attrs) |
| 555 } |
| 556 |
| 557 type validationTest struct { |
| 558 about string |
| 559 new attrs |
| 560 old attrs |
| 561 err string |
| 562 } |
| 563 |
| 564 var validationTests = []validationTest{ |
| 565 { |
| 566 about: "Can't change the type", |
| 567 new: attrs{ |
| 568 "type": "type2", |
| 569 "name": "my-name", |
| 570 }, |
| 571 old: attrs{ |
| 572 "type": "my-type", |
| 573 "name": "my-name", |
| 574 }, |
| 575 err: `cannot change type from "my-type" to "type2"`, |
| 576 }, { |
| 577 about: "Can't change the name", |
| 578 new: attrs{ |
| 579 "type": "my-type", |
| 580 "name": "new-name", |
| 581 }, |
| 582 old: attrs{ |
| 583 "type": "my-type", |
| 584 "name": "my-name", |
| 585 }, |
| 586 err: `cannot change name from "my-name" to "new-name"`, |
| 587 }, { |
| 588 about: "Can set agent version", |
| 589 new: attrs{ |
| 590 "type": "my-type", |
| 591 "name": "my-name", |
| 592 "agent-version": "1.9.13", |
| 593 }, |
| 594 old: attrs{ |
| 595 "type": "my-type", |
| 596 "name": "my-name", |
| 597 }, |
| 598 }, { |
| 599 about: "Can't clear agent version", |
| 600 new: attrs{ |
| 601 "type": "my-type", |
| 602 "name": "my-name", |
| 603 }, |
| 604 old: attrs{ |
| 605 "type": "my-type", |
| 606 "name": "my-name", |
| 607 "agent-version": "1.9.13", |
| 608 }, |
| 609 err: `cannot clear agent-version`, |
| 610 }, { |
| 611 about: "Can't change the firewall-mode", |
| 612 new: attrs{ |
| 613 "type": "my-type", |
| 614 "name": "my-name", |
| 615 "firewall-mode": config.FwInstance, |
| 616 }, |
| 617 old: attrs{ |
| 618 "type": "my-type", |
| 619 "name": "my-name", |
| 620 "firewall-mode": config.FwGlobal, |
| 621 }, |
| 622 err: `cannot change firewall-mode from "global" to "instance"`, |
| 623 }, |
| 624 } |
| 625 |
| 626 func (*ConfigSuite) TestValidateChange(c *C) { |
| 627 files := []testFile{ |
| 628 {".ssh/identity.pub", "identity"}, |
| 629 } |
| 630 h := makeFakeHome(c, files) |
| 631 defer h.restore() |
| 632 |
| 633 for i, test := range validationTests { |
| 634 c.Logf("test %d. %s", i, test.about) |
| 635 newConfig, err := config.New(test.new) |
| 636 c.Assert(err, IsNil) |
| 637 oldConfig, err := config.New(test.old) |
| 638 c.Assert(err, IsNil) |
| 639 |
| 640 err = config.Validate(newConfig, oldConfig) |
| 641 if test.err == "" { |
| 642 c.Assert(err, IsNil) |
| 643 } else { |
| 644 c.Assert(err, ErrorMatches, test.err) |
| 645 } |
| 646 } |
546 } | 647 } |
547 | 648 |
548 type fakeHome struct { | 649 type fakeHome struct { |
549 oldHome string | 650 oldHome string |
550 oldJujuHome string | 651 oldJujuHome string |
551 files []testFile | 652 files []testFile |
552 } | 653 } |
553 | 654 |
554 func makeFakeHome(c *C, files []testFile) fakeHome { | 655 func makeFakeHome(c *C, files []testFile) fakeHome { |
555 oldHome := os.Getenv("HOME") | 656 oldHome := os.Getenv("HOME") |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 -----BEGIN RSA PRIVATE KEY----- | 778 -----BEGIN RSA PRIVATE KEY----- |
678 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw | 779 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw |
679 -----END RSA PRIVATE KEY----- | 780 -----END RSA PRIVATE KEY----- |
680 `[1:] | 781 `[1:] |
681 | 782 |
682 var invalidCACert = ` | 783 var invalidCACert = ` |
683 -----BEGIN CERTIFICATE----- | 784 -----BEGIN CERTIFICATE----- |
684 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw | 785 MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw |
685 -----END CERTIFICATE----- | 786 -----END CERTIFICATE----- |
686 `[1:] | 787 `[1:] |
LEFT | RIGHT |