| OLD | NEW |
| 1 package state_test | 1 package state_test |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "fmt" | 4 "fmt" |
| 5 . "launchpad.net/gocheck" | 5 . "launchpad.net/gocheck" |
| 6 "launchpad.net/gozk/zookeeper" | 6 "launchpad.net/gozk/zookeeper" |
| 7 "launchpad.net/juju-core/charm" | 7 "launchpad.net/juju-core/charm" |
| 8 "launchpad.net/juju-core/juju/testing" | 8 "launchpad.net/juju-core/juju/testing" |
| 9 "launchpad.net/juju-core/state" | 9 "launchpad.net/juju-core/state" |
| 10 coretesting "launchpad.net/juju-core/testing" | 10 coretesting "launchpad.net/juju-core/testing" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 } | 36 } |
| 37 | 37 |
| 38 type StateSuite struct { | 38 type StateSuite struct { |
| 39 ConnSuite | 39 ConnSuite |
| 40 } | 40 } |
| 41 | 41 |
| 42 var _ = Suite(&StateSuite{}) | 42 var _ = Suite(&StateSuite{}) |
| 43 | 43 |
| 44 func (s *StateSuite) TestInitialize(c *C) { | 44 func (s *StateSuite) TestInitialize(c *C) { |
| 45 // Check that initialization of an already-initialized state succeeds. | 45 // Check that initialization of an already-initialized state succeeds. |
| 46 » st, err := state.Initialize(s.StateInfo(c)) | 46 » st, err := state.Initialize(s.StateInfo(c), nil) |
| 47 c.Assert(err, IsNil) | 47 c.Assert(err, IsNil) |
| 48 c.Assert(st, NotNil) | 48 c.Assert(st, NotNil) |
| 49 st.Close() | 49 st.Close() |
| 50 | 50 |
| 51 // Check that Open blocks until Initialize has succeeded. | 51 // Check that Open blocks until Initialize has succeeded. |
| 52 coretesting.ZkRemoveTree(s.zkConn, "/") | 52 coretesting.ZkRemoveTree(s.zkConn, "/") |
| 53 | 53 |
| 54 errc := make(chan error) | 54 errc := make(chan error) |
| 55 go func() { | 55 go func() { |
| 56 st, err := state.Open(s.StateInfo(c)) | 56 st, err := state.Open(s.StateInfo(c)) |
| 57 errc <- err | 57 errc <- err |
| 58 if st != nil { | 58 if st != nil { |
| 59 st.Close() | 59 st.Close() |
| 60 } | 60 } |
| 61 }() | 61 }() |
| 62 | 62 |
| 63 // Wait a little while to verify that it's actually blocking. | 63 // Wait a little while to verify that it's actually blocking. |
| 64 time.Sleep(200 * time.Millisecond) | 64 time.Sleep(200 * time.Millisecond) |
| 65 select { | 65 select { |
| 66 case err := <-errc: | 66 case err := <-errc: |
| 67 c.Fatalf("state.Open did not block (returned error %v)", err) | 67 c.Fatalf("state.Open did not block (returned error %v)", err) |
| 68 default: | 68 default: |
| 69 } | 69 } |
| 70 | 70 |
| 71 » st, err = state.Initialize(s.StateInfo(c)) | 71 » st, err = state.Initialize(s.StateInfo(c), nil) |
| 72 c.Assert(err, IsNil) | 72 c.Assert(err, IsNil) |
| 73 c.Assert(st, NotNil) | 73 c.Assert(st, NotNil) |
| 74 defer st.Close() | 74 defer st.Close() |
| 75 | 75 |
| 76 select { | 76 select { |
| 77 case err := <-errc: | 77 case err := <-errc: |
| 78 c.Assert(err, IsNil) | 78 c.Assert(err, IsNil) |
| 79 case <-time.After(1e9): | 79 case <-time.After(1e9): |
| 80 c.Fatalf("state.Open blocked forever") | 80 c.Fatalf("state.Open blocked forever") |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 func (s *StateSuite) TestEnvironConfig(c *C) { | 84 func (s *StateSuite) TestInitalizeWithConfig(c *C) { |
| 85 » _, err := s.zkConn.Set("/environment", "type: dummy\nname: foo\n", -1) | 85 » // clean existing state |
| 86 » coretesting.ZkRemoveTree(s.zkConn, "/") |
| 87 |
| 88 » m := map[string]interface{}{ |
| 89 » » "name": "only", |
| 90 » » "type": "dummy", |
| 91 » » "zookeeper": true, |
| 92 » » "authorized-keys": "i-am-a-key", |
| 93 » } |
| 94 » st, err := state.Initialize(s.StateInfo(c), m) |
| 86 c.Assert(err, IsNil) | 95 c.Assert(err, IsNil) |
| 87 | 96 » c.Assert(st, NotNil) |
| 88 » env, err := s.State.EnvironConfig() | 97 » defer st.Close() |
| 98 » env, err := st.EnvironConfig() |
| 89 env.Read() | 99 env.Read() |
| 90 c.Assert(err, IsNil) | 100 c.Assert(err, IsNil) |
| 91 » c.Assert(env.Map(), DeepEquals, map[string]interface{}{"type": "dummy",
"name": "foo"}) | 101 » c.Assert(env.Map(), DeepEquals, m) |
| 92 } | 102 } |
| 93 | 103 |
| 94 type environConfig map[string]interface{} | 104 type environConfig map[string]interface{} |
| 95 | 105 |
| 96 var environmentWatchTests = []struct { | 106 var environmentWatchTests = []struct { |
| 97 key string | 107 key string |
| 98 value interface{} | 108 value interface{} |
| 99 want map[string]interface{} | 109 want map[string]interface{} |
| 100 }{ | 110 }{ |
| 101 {"provider", "dummy", environConfig{"provider": "dummy"}}, | 111 {"provider", "dummy", environConfig{"provider": "dummy"}}, |
| 102 {"secret", "shhh", environConfig{"provider": "dummy", "secret": "shhh"}}
, | 112 {"secret", "shhh", environConfig{"provider": "dummy", "secret": "shhh"}}
, |
| 103 {"provider", "aws", environConfig{"provider": "aws", "secret": "shhh"}}, | 113 {"provider", "aws", environConfig{"provider": "aws", "secret": "shhh"}}, |
| 104 } | 114 } |
| 105 | 115 |
| 106 func (s *StateSuite) TestWatchEnvironment(c *C) { | 116 func (s *StateSuite) TestWatchEnvironment(c *C) { |
| 107 // Blank out the environment created by JujuConnSuite, | 117 // Blank out the environment created by JujuConnSuite, |
| 108 // so that we know what we have. | 118 // so that we know what we have. |
| 109 _, err := s.zkConn.Set("/environment", "", -1) | 119 _, err := s.zkConn.Set("/environment", "", -1) |
| 110 c.Assert(err, IsNil) | 120 c.Assert(err, IsNil) |
| 111 | |
| 112 // fetch the /environment key as a *ConfigNode | 121 // fetch the /environment key as a *ConfigNode |
| 113 environConfigWatcher := s.State.WatchEnvironConfig() | 122 environConfigWatcher := s.State.WatchEnvironConfig() |
| 114 defer func() { | 123 defer func() { |
| 115 c.Assert(environConfigWatcher.Stop(), IsNil) | 124 c.Assert(environConfigWatcher.Stop(), IsNil) |
| 116 }() | 125 }() |
| 117 | 126 |
| 118 config, ok := <-environConfigWatcher.Changes() | 127 config, ok := <-environConfigWatcher.Changes() |
| 119 c.Assert(ok, Equals, true) | 128 c.Assert(ok, Equals, true) |
| 120 | 129 |
| 121 for i, test := range environmentWatchTests { | 130 for i, test := range environmentWatchTests { |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 func (*StateSuite) TestSortPorts(c *C) { | 459 func (*StateSuite) TestSortPorts(c *C) { |
| 451 for _, t := range sortPortsTests { | 460 for _, t := range sortPortsTests { |
| 452 p := make([]state.Port, len(t.have)) | 461 p := make([]state.Port, len(t.have)) |
| 453 copy(p, t.have) | 462 copy(p, t.have) |
| 454 state.SortPorts(p) | 463 state.SortPorts(p) |
| 455 c.Check(p, DeepEquals, t.want) | 464 c.Check(p, DeepEquals, t.want) |
| 456 state.SortPorts(p) | 465 state.SortPorts(p) |
| 457 c.Check(p, DeepEquals, t.want) | 466 c.Check(p, DeepEquals, t.want) |
| 458 } | 467 } |
| 459 } | 468 } |
| OLD | NEW |