OLD | NEW |
1 package state_test | 1 package state_test |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "labix.org/v2/mgo/bson" | 5 "labix.org/v2/mgo/bson" |
6 . "launchpad.net/gocheck" | 6 . "launchpad.net/gocheck" |
7 "launchpad.net/juju-core/charm" | 7 "launchpad.net/juju-core/charm" |
8 "launchpad.net/juju-core/environs/config" | 8 "launchpad.net/juju-core/environs/config" |
9 "launchpad.net/juju-core/state" | 9 "launchpad.net/juju-core/state" |
10 "launchpad.net/juju-core/testing" | 10 "launchpad.net/juju-core/testing" |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 392 |
393 func (s *StateSuite) TestEnvironConfig(c *C) { | 393 func (s *StateSuite) TestEnvironConfig(c *C) { |
394 initial := map[string]interface{}{ | 394 initial := map[string]interface{}{ |
395 "name": "test", | 395 "name": "test", |
396 "type": "test", | 396 "type": "test", |
397 "authorized-keys": "i-am-a-key", | 397 "authorized-keys": "i-am-a-key", |
398 "default-series": "precise", | 398 "default-series": "precise", |
399 "development": true, | 399 "development": true, |
400 "firewall-mode": "", | 400 "firewall-mode": "", |
401 "admin-secret": "", | 401 "admin-secret": "", |
402 "ca-cert": testing.CACertPEM, | |
403 "ca-private-key": "", | |
404 } | 402 } |
405 cfg, err := config.New(initial) | 403 cfg, err := config.New(initial) |
406 c.Assert(err, IsNil) | 404 c.Assert(err, IsNil) |
407 st, err := state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}
}, cfg) | 405 st, err := state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}
}, cfg) |
408 c.Assert(err, IsNil) | 406 c.Assert(err, IsNil) |
409 st.Close() | 407 st.Close() |
410 c.Assert(err, IsNil) | 408 c.Assert(err, IsNil) |
411 cfg, err = s.State.EnvironConfig() | 409 cfg, err = s.State.EnvironConfig() |
412 c.Assert(err, IsNil) | 410 c.Assert(err, IsNil) |
413 current := cfg.AllAttrs() | 411 current := cfg.AllAttrs() |
(...skipping 11 matching lines...) Expand all Loading... |
425 } | 423 } |
426 | 424 |
427 func (s *StateSuite) TestEnvironConfigWithAdminSecret(c *C) { | 425 func (s *StateSuite) TestEnvironConfigWithAdminSecret(c *C) { |
428 attrs := map[string]interface{}{ | 426 attrs := map[string]interface{}{ |
429 "name": "test", | 427 "name": "test", |
430 "type": "test", | 428 "type": "test", |
431 "authorized-keys": "i-am-a-key", | 429 "authorized-keys": "i-am-a-key", |
432 "default-series": "precise", | 430 "default-series": "precise", |
433 "development": true, | 431 "development": true, |
434 "admin-secret": "foo", | 432 "admin-secret": "foo", |
435 "ca-cert": testing.CACertPEM, | |
436 } | 433 } |
437 cfg, err := config.New(attrs) | 434 cfg, err := config.New(attrs) |
438 c.Assert(err, IsNil) | 435 c.Assert(err, IsNil) |
439 _, err = state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}},
cfg) | 436 _, err = state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}},
cfg) |
440 c.Assert(err, ErrorMatches, "admin-secret should never be written to the
state") | 437 c.Assert(err, ErrorMatches, "admin-secret should never be written to the
state") |
441 | 438 |
442 delete(attrs, "admin-secret") | 439 delete(attrs, "admin-secret") |
443 cfg, err = config.New(attrs) | 440 cfg, err = config.New(attrs) |
444 st, err := state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}
}, cfg) | 441 st, err := state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}
}, cfg) |
445 c.Assert(err, IsNil) | 442 c.Assert(err, IsNil) |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 | 783 |
787 func (s *StateSuite) TestInitialize(c *C) { | 784 func (s *StateSuite) TestInitialize(c *C) { |
788 m := map[string]interface{}{ | 785 m := map[string]interface{}{ |
789 "type": "dummy", | 786 "type": "dummy", |
790 "name": "lisboa", | 787 "name": "lisboa", |
791 "authorized-keys": "i-am-a-key", | 788 "authorized-keys": "i-am-a-key", |
792 "default-series": "precise", | 789 "default-series": "precise", |
793 "development": true, | 790 "development": true, |
794 "firewall-mode": "", | 791 "firewall-mode": "", |
795 "admin-secret": "", | 792 "admin-secret": "", |
796 "ca-cert": testing.CACertPEM, | |
797 "ca-private-key": "", | |
798 } | 793 } |
799 cfg, err := config.New(m) | 794 cfg, err := config.New(m) |
800 c.Assert(err, IsNil) | 795 c.Assert(err, IsNil) |
801 st, err := state.Initialize(s.StateInfo(c), cfg) | 796 st, err := state.Initialize(s.StateInfo(c), cfg) |
802 c.Assert(err, IsNil) | 797 c.Assert(err, IsNil) |
803 c.Assert(st, NotNil) | 798 c.Assert(st, NotNil) |
804 defer st.Close() | 799 defer st.Close() |
805 env, err := st.EnvironConfig() | 800 env, err := st.EnvironConfig() |
806 c.Assert(env.AllAttrs(), DeepEquals, m) | 801 c.Assert(env.AllAttrs(), DeepEquals, m) |
807 } | 802 } |
808 | 803 |
809 func (s *StateSuite) TestDoubleInitialize(c *C) { | 804 func (s *StateSuite) TestDoubleInitialize(c *C) { |
810 m := map[string]interface{}{ | 805 m := map[string]interface{}{ |
811 "type": "dummy", | 806 "type": "dummy", |
812 "name": "lisboa", | 807 "name": "lisboa", |
813 "authorized-keys": "i-am-a-key", | 808 "authorized-keys": "i-am-a-key", |
814 "default-series": "precise", | 809 "default-series": "precise", |
815 "development": true, | 810 "development": true, |
816 "firewall-mode": "", | 811 "firewall-mode": "", |
817 "admin-secret": "", | 812 "admin-secret": "", |
818 "ca-cert": testing.CACertPEM, | |
819 "ca-private-key": "", | |
820 } | 813 } |
821 cfg, err := config.New(m) | 814 cfg, err := config.New(m) |
822 c.Assert(err, IsNil) | 815 c.Assert(err, IsNil) |
823 st, err := state.Initialize(s.StateInfo(c), cfg) | 816 st, err := state.Initialize(s.StateInfo(c), cfg) |
824 c.Assert(err, IsNil) | 817 c.Assert(err, IsNil) |
825 c.Assert(st, NotNil) | 818 c.Assert(st, NotNil) |
826 env1, err := st.EnvironConfig() | 819 env1, err := st.EnvironConfig() |
827 st.Close() | 820 st.Close() |
828 | 821 |
829 // initialize again, there should be no error and the | 822 // initialize again, there should be no error and the |
830 // environ config should not change. | 823 // environ config should not change. |
831 m = map[string]interface{}{ | 824 m = map[string]interface{}{ |
832 "type": "dummy", | 825 "type": "dummy", |
833 "name": "sydney", | 826 "name": "sydney", |
834 "authorized-keys": "i-am-not-an-animal", | 827 "authorized-keys": "i-am-not-an-animal", |
835 "default-series": "xanadu", | 828 "default-series": "xanadu", |
836 "development": false, | 829 "development": false, |
837 "firewall-mode": "", | 830 "firewall-mode": "", |
838 "admin-secret": "", | 831 "admin-secret": "", |
839 "ca-cert": testing.CACertPEM, | |
840 "ca-private-key": "", | |
841 } | 832 } |
842 cfg, err = config.New(m) | 833 cfg, err = config.New(m) |
843 c.Assert(err, IsNil) | 834 c.Assert(err, IsNil) |
844 st, err = state.Initialize(s.StateInfo(c), cfg) | 835 st, err = state.Initialize(s.StateInfo(c), cfg) |
845 c.Assert(err, IsNil) | 836 c.Assert(err, IsNil) |
846 c.Assert(st, NotNil) | 837 c.Assert(st, NotNil) |
847 env2, err := st.EnvironConfig() | 838 env2, err := st.EnvironConfig() |
848 st.Close() | 839 st.Close() |
849 | 840 |
850 c.Assert(env1.AllAttrs(), DeepEquals, env2.AllAttrs()) | 841 c.Assert(env1.AllAttrs(), DeepEquals, env2.AllAttrs()) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 assertService("foo-2foo", true) | 876 assertService("foo-2foo", true) |
886 } | 877 } |
887 | 878 |
888 type attrs map[string]interface{} | 879 type attrs map[string]interface{} |
889 | 880 |
890 var watchEnvironConfigTests = []attrs{ | 881 var watchEnvironConfigTests = []attrs{ |
891 { | 882 { |
892 "type": "my-type", | 883 "type": "my-type", |
893 "name": "my-name", | 884 "name": "my-name", |
894 "authorized-keys": "i-am-a-key", | 885 "authorized-keys": "i-am-a-key", |
895 "ca-cert": testing.CACertPEM, | |
896 }, | 886 }, |
897 { | 887 { |
898 // Add an attribute. | 888 // Add an attribute. |
899 "type": "my-type", | 889 "type": "my-type", |
900 "name": "my-name", | 890 "name": "my-name", |
901 "default-series": "my-series", | 891 "default-series": "my-series", |
902 "authorized-keys": "i-am-a-key", | 892 "authorized-keys": "i-am-a-key", |
903 "ca-cert": testing.CACertPEM, | |
904 }, | 893 }, |
905 { | 894 { |
906 // Set a new attribute value. | 895 // Set a new attribute value. |
907 "type": "my-type", | 896 "type": "my-type", |
908 "name": "my-new-name", | 897 "name": "my-new-name", |
909 "default-series": "my-series", | 898 "default-series": "my-series", |
910 "authorized-keys": "i-am-a-key", | 899 "authorized-keys": "i-am-a-key", |
911 "ca-cert": testing.CACertPEM, | |
912 }, | 900 }, |
913 } | 901 } |
914 | 902 |
915 func (s *StateSuite) TestWatchEnvironConfig(c *C) { | 903 func (s *StateSuite) TestWatchEnvironConfig(c *C) { |
916 watcher := s.State.WatchEnvironConfig() | 904 watcher := s.State.WatchEnvironConfig() |
917 defer func() { | 905 defer func() { |
918 c.Assert(watcher.Stop(), IsNil) | 906 c.Assert(watcher.Stop(), IsNil) |
919 }() | 907 }() |
920 for i, test := range watchEnvironConfigTests { | 908 for i, test := range watchEnvironConfigTests { |
921 c.Logf("test %d", i) | 909 c.Logf("test %d", i) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 case <-time.After(500 * time.Millisecond): | 951 case <-time.After(500 * time.Millisecond): |
964 c.Fatalf("did not get change") | 952 c.Fatalf("did not get change") |
965 } | 953 } |
966 } | 954 } |
967 | 955 |
968 func (s *StateSuite) TestWatchEnvironConfigInvalidConfig(c *C) { | 956 func (s *StateSuite) TestWatchEnvironConfigInvalidConfig(c *C) { |
969 m := map[string]interface{}{ | 957 m := map[string]interface{}{ |
970 "type": "dummy", | 958 "type": "dummy", |
971 "name": "lisboa", | 959 "name": "lisboa", |
972 "authorized-keys": "i-am-a-key", | 960 "authorized-keys": "i-am-a-key", |
973 "ca-cert": testing.CACertPEM, | |
974 } | 961 } |
975 cfg1, err := config.New(m) | 962 cfg1, err := config.New(m) |
976 c.Assert(err, IsNil) | 963 c.Assert(err, IsNil) |
977 st, err := state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}
}, cfg1) | 964 st, err := state.Initialize(&state.Info{Addrs: []string{testing.MgoAddr}
}, cfg1) |
978 c.Assert(err, IsNil) | 965 c.Assert(err, IsNil) |
979 st.Close() | 966 st.Close() |
980 | 967 |
981 // Corrupt the environment configuration. | 968 // Corrupt the environment configuration. |
982 settings := s.Session.DB("juju").C("settings") | 969 settings := s.Session.DB("juju").C("settings") |
983 err = settings.UpdateId("e", bson.D{{"$unset", bson.D{{"name", 1}}}}) | 970 err = settings.UpdateId("e", bson.D{{"$unset", bson.D{{"name", 1}}}}) |
(...skipping 25 matching lines...) Expand all Loading... |
1009 case <-done: | 996 case <-done: |
1010 c.Fatalf("configuration returned too soon") | 997 c.Fatalf("configuration returned too soon") |
1011 case <-time.After(100 * time.Millisecond): | 998 case <-time.After(100 * time.Millisecond): |
1012 } | 999 } |
1013 | 1000 |
1014 // Fix the configuration. | 1001 // Fix the configuration. |
1015 cfg2, err := config.New(map[string]interface{}{ | 1002 cfg2, err := config.New(map[string]interface{}{ |
1016 "type": "dummy", | 1003 "type": "dummy", |
1017 "name": "lisboa", | 1004 "name": "lisboa", |
1018 "authorized-keys": "new-key", | 1005 "authorized-keys": "new-key", |
1019 "ca-cert": testing.CACertPEM, | |
1020 }) | 1006 }) |
1021 c.Assert(err, IsNil) | 1007 c.Assert(err, IsNil) |
1022 err = s.State.SetEnvironConfig(cfg2) | 1008 err = s.State.SetEnvironConfig(cfg2) |
1023 c.Assert(err, IsNil) | 1009 c.Assert(err, IsNil) |
1024 | 1010 |
1025 s.State.StartSync() | 1011 s.State.StartSync() |
1026 select { | 1012 select { |
1027 case cfg3 := <-done: | 1013 case cfg3 := <-done: |
1028 c.Assert(cfg3.AllAttrs(), DeepEquals, cfg2.AllAttrs()) | 1014 c.Assert(cfg3.AllAttrs(), DeepEquals, cfg2.AllAttrs()) |
1029 case <-time.After(5 * time.Second): | 1015 case <-time.After(5 * time.Second): |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1177 c.Assert(err, IsNil) | 1163 c.Assert(err, IsNil) |
1178 | 1164 |
1179 // Check that removing the password is idempotent. | 1165 // Check that removing the password is idempotent. |
1180 err = s.State.SetAdminPassword("") | 1166 err = s.State.SetAdminPassword("") |
1181 c.Assert(err, IsNil) | 1167 c.Assert(err, IsNil) |
1182 | 1168 |
1183 info.Password = "" | 1169 info.Password = "" |
1184 err = tryOpenState(info) | 1170 err = tryOpenState(info) |
1185 c.Assert(err, IsNil) | 1171 c.Assert(err, IsNil) |
1186 } | 1172 } |
OLD | NEW |