~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-03 09:22:46 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20140203092246-e03vg402vztzo4qa
Tags: 1.17.2-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
        stdtesting "testing"
10
10
        "time"
11
11
 
 
12
        "github.com/loggo/loggo"
12
13
        gc "launchpad.net/gocheck"
13
 
        "launchpad.net/loggo"
14
14
 
15
15
        "launchpad.net/juju-core/cert"
16
16
        "launchpad.net/juju-core/environs/config"
45
45
var sampleConfig = testing.Attrs{
46
46
        "type":                      "my-type",
47
47
        "name":                      "my-name",
48
 
        "authorized-keys":           "my-keys",
 
48
        "authorized-keys":           testing.FakeAuthKeys,
49
49
        "firewall-mode":             config.FwInstance,
50
50
        "admin-secret":              "foo",
51
51
        "unknown":                   "my-unknown",
129
129
                attrs: testing.Attrs{
130
130
                        "type":            "my-type",
131
131
                        "name":            "my-name",
132
 
                        "authorized-keys": "my-keys",
 
132
                        "authorized-keys": testing.FakeAuthKeys,
133
133
                },
134
134
        }, {
135
135
                about:       "Load authorized-keys from path",
276
276
                attrs: testing.Attrs{
277
277
                        "type":            "my-type",
278
278
                        "name":            "my-name",
279
 
                        "authorized-keys": "my-keys",
 
279
                        "authorized-keys": testing.FakeAuthKeys,
280
280
                        "agent-version":   "1.2.3",
281
281
                },
282
282
        }, {
285
285
                attrs: testing.Attrs{
286
286
                        "type":            "my-type",
287
287
                        "name":            "my-name",
288
 
                        "authorized-keys": "my-keys",
 
288
                        "authorized-keys": testing.FakeAuthKeys,
289
289
                        "development":     true,
290
290
                },
291
291
        }, {
294
294
                attrs: testing.Attrs{
295
295
                        "type":            "my-type",
296
296
                        "name":            "my-name",
297
 
                        "authorized-keys": "my-keys",
 
297
                        "authorized-keys": testing.FakeAuthKeys,
298
298
                        "development":     false,
299
299
                        "admin-secret":    "pork",
300
300
                },
304
304
                attrs: testing.Attrs{
305
305
                        "type":            "my-type",
306
306
                        "name":            "my-name",
307
 
                        "authorized-keys": "my-keys",
 
307
                        "authorized-keys": testing.FakeAuthKeys,
308
308
                        "development":     "invalid",
309
309
                },
310
310
                err: `development: expected bool, got string\("invalid"\)`,
314
314
                attrs: testing.Attrs{
315
315
                        "type":            "my-type",
316
316
                        "name":            "my-name",
317
 
                        "authorized-keys": "my-keys",
 
317
                        "authorized-keys": testing.FakeAuthKeys,
318
318
                        "agent-version":   "2",
319
319
                },
320
320
                err: `invalid agent version in environment configuration: "2"`,
447
447
                },
448
448
                err: `provisioner-safe-mode: expected bool, got string\("yes please"\)`,
449
449
        }, {
 
450
                about:       "default image stream",
 
451
                useDefaults: config.UseDefaults,
 
452
                attrs: testing.Attrs{
 
453
                        "type": "my-type",
 
454
                        "name": "my-name",
 
455
                },
 
456
        }, {
 
457
                about:       "explicit image stream",
 
458
                useDefaults: config.UseDefaults,
 
459
                attrs: testing.Attrs{
 
460
                        "type":         "my-type",
 
461
                        "name":         "my-name",
 
462
                        "image-stream": "daily",
 
463
                },
 
464
        }, {
450
465
                about:       "Explicit state port",
451
466
                useDefaults: config.UseDefaults,
452
467
                attrs: testing.Attrs{
498
513
                },
499
514
                err: `syslog-port: expected number, got string\("illegal"\)`,
500
515
        }, {
 
516
                about:       "Explicit bootstrap timeout",
 
517
                useDefaults: config.UseDefaults,
 
518
                attrs: testing.Attrs{
 
519
                        "type":              "my-type",
 
520
                        "name":              "my-name",
 
521
                        "bootstrap-timeout": 300,
 
522
                },
 
523
        }, {
 
524
                about:       "Invalid bootstrap timeout",
 
525
                useDefaults: config.UseDefaults,
 
526
                attrs: testing.Attrs{
 
527
                        "type":              "my-type",
 
528
                        "name":              "my-name",
 
529
                        "bootstrap-timeout": "illegal",
 
530
                },
 
531
                err: `bootstrap-timeout: expected number, got string\("illegal"\)`,
 
532
        }, {
 
533
                about:       "Explicit bootstrap retry delay",
 
534
                useDefaults: config.UseDefaults,
 
535
                attrs: testing.Attrs{
 
536
                        "type":                  "my-type",
 
537
                        "name":                  "my-name",
 
538
                        "bootstrap-retry-delay": 5,
 
539
                },
 
540
        }, {
 
541
                about:       "Invalid bootstrap retry delay",
 
542
                useDefaults: config.UseDefaults,
 
543
                attrs: testing.Attrs{
 
544
                        "type":                  "my-type",
 
545
                        "name":                  "my-name",
 
546
                        "bootstrap-retry-delay": "illegal",
 
547
                },
 
548
                err: `bootstrap-retry-delay: expected number, got string\("illegal"\)`,
 
549
        }, {
 
550
                about:       "Explicit bootstrap addresses delay",
 
551
                useDefaults: config.UseDefaults,
 
552
                attrs: testing.Attrs{
 
553
                        "type": "my-type",
 
554
                        "name": "my-name",
 
555
                        "bootstrap-addresses-delay": 15,
 
556
                },
 
557
        }, {
 
558
                about:       "Invalid bootstrap addresses delay",
 
559
                useDefaults: config.UseDefaults,
 
560
                attrs: testing.Attrs{
 
561
                        "type": "my-type",
 
562
                        "name": "my-name",
 
563
                        "bootstrap-addresses-delay": "illegal",
 
564
                },
 
565
                err: `bootstrap-addresses-delay: expected number, got string\("illegal"\)`,
 
566
        }, {
501
567
                about:       "Invalid logging configuration",
502
568
                useDefaults: config.UseDefaults,
503
569
                attrs: testing.Attrs{
556
622
                        "firewall-mode":             "instance",
557
623
                        "type":                      "ec2",
558
624
                },
 
625
        }, {
 
626
                about:       "Provider type null is replaced with manual",
 
627
                useDefaults: config.UseDefaults,
 
628
                attrs: testing.Attrs{
 
629
                        "type": "null",
 
630
                        "name": "my-name",
 
631
                },
559
632
        },
560
633
        authTokenConfigTest("token=value, tokensecret=value", true),
561
634
        authTokenConfigTest("token=value, ", true),
646
719
                attrs: testing.Attrs{
647
720
                        "type":            "my-type",
648
721
                        "name":            "my-name",
649
 
                        "authorized-keys": "my-keys",
 
722
                        "authorized-keys": testing.FakeAuthKeys,
650
723
                },
651
724
        }, {
652
725
                about:       "Unspecified certificate, specified key",
654
727
                attrs: testing.Attrs{
655
728
                        "type":            "my-type",
656
729
                        "name":            "my-name",
657
 
                        "authorized-keys": "my-keys",
 
730
                        "authorized-keys": testing.FakeAuthKeys,
658
731
                        "ca-private-key":  caKey,
659
732
                },
660
733
                err: "bad CA certificate/key in configuration: crypto/tls:.*",
677
750
                attrs: testing.Attrs{
678
751
                        "type":            "my-type",
679
752
                        "name":            "my-name",
680
 
                        "authorized-keys": "my-keys",
 
753
                        "authorized-keys": testing.FakeAuthKeys,
681
754
                        "ca-private-key":  caKey,
682
755
                },
683
756
                err: `file ".*/my-name-cert.pem" is empty`,
687
760
                attrs: testing.Attrs{
688
761
                        "type":            "my-type",
689
762
                        "name":            "my-name",
690
 
                        "authorized-keys": "my-keys",
 
763
                        "authorized-keys": testing.FakeAuthKeys,
691
764
                },
692
765
                err: `file ".*/my-name-cert.pem" is empty`,
693
766
        }, {
696
769
                attrs: testing.Attrs{
697
770
                        "type":            "my-type",
698
771
                        "name":            "my-name",
699
 
                        "authorized-keys": "my-keys",
 
772
                        "authorized-keys": testing.FakeAuthKeys,
700
773
                        "ca-cert":         caCert,
701
774
                },
702
775
                err: `file ".*/my-name-private-key.pem" is empty`,
706
779
                attrs: testing.Attrs{
707
780
                        "type":            "my-type",
708
781
                        "name":            "my-name",
709
 
                        "authorized-keys": "my-keys",
 
782
                        "authorized-keys": testing.FakeAuthKeys,
710
783
                        "ca-cert":         "",
711
784
                        "ca-private-key":  "",
712
785
                },
716
789
                attrs: testing.Attrs{
717
790
                        "type":            "my-type",
718
791
                        "name":            "my-name",
719
 
                        "authorized-keys": "my-keys",
 
792
                        "authorized-keys": testing.FakeAuthKeys,
720
793
                        "ca-cert":         "",
721
794
                },
722
795
                err: "bad CA certificate/key in configuration: crypto/tls: .*",
747
820
        c.Assert(err, gc.IsNil)
748
821
 
749
822
        typ, _ := test.attrs["type"].(string)
 
823
        // "null" has been deprecated in favour of "manual",
 
824
        // and is automatically switched.
 
825
        if typ == "null" {
 
826
                typ = "manual"
 
827
        }
750
828
        name, _ := test.attrs["name"].(string)
751
829
        c.Assert(cfg.Type(), gc.Equals, typ)
752
830
        c.Assert(cfg.Name(), gc.Equals, name)
839
917
        } else {
840
918
                c.Assert(cfg.ProvisionerSafeMode(), gc.Equals, false)
841
919
        }
 
920
        sshOpts := cfg.BootstrapSSHOpts()
 
921
        test.assertDuration(
 
922
                c,
 
923
                "bootstrap-timeout",
 
924
                sshOpts.Timeout,
 
925
                config.DefaultBootstrapSSHTimeout,
 
926
        )
 
927
        test.assertDuration(
 
928
                c,
 
929
                "bootstrap-retry-delay",
 
930
                sshOpts.RetryDelay,
 
931
                config.DefaultBootstrapSSHRetryDelay,
 
932
        )
 
933
        test.assertDuration(
 
934
                c,
 
935
                "bootstrap-addresses-delay",
 
936
                sshOpts.AddressesDelay,
 
937
                config.DefaultBootstrapSSHAddressesDelay,
 
938
        )
 
939
 
 
940
        if v, ok := test.attrs["image-stream"]; ok {
 
941
                c.Assert(cfg.ImageStream(), gc.Equals, v)
 
942
        } else {
 
943
                c.Assert(cfg.ImageStream(), gc.Equals, "released")
 
944
        }
842
945
 
843
946
        url, urlPresent := cfg.ImageMetadataURL()
844
947
        if v, _ := test.attrs["image-metadata-url"].(string); v != "" {
867
970
        }
868
971
}
869
972
 
 
973
func (test configTest) assertDuration(c *gc.C, name string, actual time.Duration, defaultInSeconds int) {
 
974
        value, ok := test.attrs[name].(int)
 
975
        if !ok || value == 0 {
 
976
                c.Assert(actual, gc.Equals, time.Duration(defaultInSeconds)*time.Second)
 
977
        } else {
 
978
                c.Assert(actual, gc.Equals, time.Duration(value)*time.Second)
 
979
        }
 
980
}
 
981
 
870
982
func (s *ConfigSuite) TestConfigAttrs(c *gc.C) {
871
983
        // Normally this is handled by testing.FakeHome
872
984
        s.PatchEnvironment(osenv.JujuLoggingConfigEnvKey, "")
873
985
        attrs := map[string]interface{}{
874
986
                "type":                      "my-type",
875
987
                "name":                      "my-name",
876
 
                "authorized-keys":           "my-keys",
 
988
                "authorized-keys":           testing.FakeAuthKeys,
877
989
                "firewall-mode":             config.FwInstance,
878
990
                "admin-secret":              "foo",
879
991
                "unknown":                   "my-unknown",
884
996
                "state-port":                1234,
885
997
                "api-port":                  4321,
886
998
                "syslog-port":               2345,
 
999
                "bootstrap-timeout":         3600,
 
1000
                "bootstrap-retry-delay":     30,
 
1001
                "bootstrap-addresses-delay": 10,
887
1002
                "default-series":            "precise",
888
1003
                "charm-store-auth":          "token=auth",
889
1004
        }
900
1015
        attrs["tools-url"] = ""
901
1016
        // Default firewall mode is instance
902
1017
        attrs["firewall-mode"] = string(config.FwInstance)
903
 
        c.Assert(cfg.AllAttrs(), gc.DeepEquals, attrs)
904
 
        c.Assert(cfg.UnknownAttrs(), gc.DeepEquals, map[string]interface{}{"unknown": "my-unknown"})
 
1018
        c.Assert(cfg.AllAttrs(), jc.DeepEquals, attrs)
 
1019
        c.Assert(cfg.UnknownAttrs(), jc.DeepEquals, map[string]interface{}{"unknown": "my-unknown"})
905
1020
 
906
1021
        newcfg, err := cfg.Apply(map[string]interface{}{
907
1022
                "name":        "new-name",
908
1023
                "new-unknown": "my-new-unknown",
909
1024
        })
 
1025
        c.Assert(err, gc.IsNil)
910
1026
 
911
1027
        attrs["name"] = "new-name"
912
1028
        attrs["new-unknown"] = "my-new-unknown"
913
 
        c.Assert(newcfg.AllAttrs(), gc.DeepEquals, attrs)
 
1029
        c.Assert(newcfg.AllAttrs(), jc.DeepEquals, attrs)
914
1030
}
915
1031
 
916
1032
type validationTest struct {
1137
1253
        c.Assert(config.AptFtpProxy(), gc.Equals, "")
1138
1254
}
1139
1255
 
 
1256
func (*ConfigSuite) TestProxyConfigMap(c *gc.C) {
 
1257
        defer makeFakeHome(c).Restore()
 
1258
 
 
1259
        cfg := newTestConfig(c, testing.Attrs{})
 
1260
        proxy := osenv.ProxySettings{
 
1261
                Http:  "http proxy",
 
1262
                Https: "https proxy",
 
1263
                Ftp:   "ftp proxy",
 
1264
        }
 
1265
        cfg, err := cfg.Apply(config.ProxyConfigMap(proxy))
 
1266
        c.Assert(err, gc.IsNil)
 
1267
        c.Assert(cfg.ProxySettings(), gc.DeepEquals, proxy)
 
1268
        c.Assert(cfg.AptProxySettings(), gc.DeepEquals, proxy)
 
1269
}
 
1270
 
 
1271
func (*ConfigSuite) TestAptProxyConfigMap(c *gc.C) {
 
1272
        defer makeFakeHome(c).Restore()
 
1273
 
 
1274
        cfg := newTestConfig(c, testing.Attrs{})
 
1275
        proxy := osenv.ProxySettings{
 
1276
                Http:  "http proxy",
 
1277
                Https: "https proxy",
 
1278
                Ftp:   "ftp proxy",
 
1279
        }
 
1280
        cfg, err := cfg.Apply(config.AptProxyConfigMap(proxy))
 
1281
        c.Assert(err, gc.IsNil)
 
1282
        // The default proxy settings should still be empty.
 
1283
        c.Assert(cfg.ProxySettings(), gc.DeepEquals, osenv.ProxySettings{})
 
1284
        c.Assert(cfg.AptProxySettings(), gc.DeepEquals, proxy)
 
1285
}
 
1286
 
1140
1287
func (*ConfigSuite) TestGenerateStateServerCertAndKey(c *gc.C) {
1141
1288
        // In order to test missing certs, it checks the JUJU_HOME dir, so we need
1142
1289
        // a fake home.