~dave-cheney/juju-core/089-state-unit-assigned-machine

« back to all changes in this revision

Viewing changes to state/state_test.go

  • Committer: Frank Mueller
  • Author(s): Frank Mueller
  • Date: 2012-08-30 14:59:28 UTC
  • mfrom: (437.1.4 juju-core)
  • Revision ID: themue@gmail.com-20120830145928-uh7do9b89do20vum
state: changed environ config to be config.Config

state.EnvironConfig() and the according watcher now
return environs/config.Config. Additionally the new
function state.SetEnvironConfig() replaces the current
configuration. Depending code - mostly tests - has 
been changed.

R=niemeyer
CC=
https://codereview.appspot.com/6493055

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
        . "launchpad.net/gocheck"
6
6
        "launchpad.net/gozk/zookeeper"
7
7
        "launchpad.net/juju-core/charm"
 
8
        "launchpad.net/juju-core/environs/config"
8
9
        "launchpad.net/juju-core/juju/testing"
9
10
        "launchpad.net/juju-core/state"
10
11
        coretesting "launchpad.net/juju-core/testing"
90
91
                "type":            "dummy",
91
92
                "zookeeper":       true,
92
93
                "authorized-keys": "i-am-a-key",
 
94
                "default-series":  "precise",
93
95
        }
94
96
        st, err := state.Initialize(s.StateInfo(c), m)
95
97
        c.Assert(err, IsNil)
96
98
        c.Assert(st, NotNil)
97
99
        defer st.Close()
98
100
        env, err := st.EnvironConfig()
99
 
        env.Read()
100
 
        c.Assert(err, IsNil)
101
 
        c.Assert(env.Map(), DeepEquals, m)
102
 
}
103
 
 
104
 
type environConfig map[string]interface{}
105
 
 
106
 
var environmentWatchTests = []struct {
107
 
        key   string
108
 
        value interface{}
109
 
        want  map[string]interface{}
110
 
}{
111
 
        {"provider", "dummy", environConfig{"provider": "dummy"}},
112
 
        {"secret", "shhh", environConfig{"provider": "dummy", "secret": "shhh"}},
113
 
        {"provider", "aws", environConfig{"provider": "aws", "secret": "shhh"}},
114
 
}
 
101
        c.Assert(env.AllAttrs(), DeepEquals, m)
 
102
}
 
103
 
 
104
type attrs map[string]interface{}
 
105
 
 
106
var environmentWatchTests = []attrs{
 
107
        {
 
108
                "type":            "my-type",
 
109
                "name":            "my-name",
 
110
                "authorized-keys": "i-am-a-key",
 
111
        },
 
112
        {
 
113
                // Add an attribute.
 
114
                "type":            "my-type",
 
115
                "name":            "my-name",
 
116
                "default-series":  "my-series",
 
117
                "authorized-keys": "i-am-a-key",
 
118
        },
 
119
        {
 
120
                // Set a new attribute value.
 
121
                "type":            "my-type",
 
122
                "name":            "my-new-name",
 
123
                "default-series":  "my-series",
 
124
                "authorized-keys": "i-am-a-key",
 
125
        },
 
126
}
 
127
 
 
128
var initialEnvironment = `name: test,
 
129
type: test,
 
130
authorized-keys: i-am-a-key,
 
131
default-series: precise`
115
132
 
116
133
func (s *StateSuite) TestWatchEnvironment(c *C) {
117
 
        // Blank out the environment created by JujuConnSuite,
 
134
        // Re-init the environment originally created by JujuConnSuite,
118
135
        // so that we know what we have.
119
 
        _, err := s.zkConn.Set("/environment", "", -1)
 
136
        _, err := s.zkConn.Set("/environment", initialEnvironment, -1)
120
137
        c.Assert(err, IsNil)
121
 
        // fetch the /environment key as a *ConfigNode
122
138
        environConfigWatcher := s.State.WatchEnvironConfig()
123
139
        defer func() {
124
140
                c.Assert(environConfigWatcher.Stop(), IsNil)
125
141
        }()
126
142
 
127
 
        config, ok := <-environConfigWatcher.Changes()
 
143
        _, ok := <-environConfigWatcher.Changes()
128
144
        c.Assert(ok, Equals, true)
129
145
 
130
146
        for i, test := range environmentWatchTests {
131
147
                c.Logf("test %d", i)
132
 
                config.Set(test.key, test.value)
133
 
                _, err := config.Write()
 
148
                change, err := config.New(test)
 
149
                c.Assert(err, IsNil)
 
150
                err = s.State.SetEnvironConfig(change)
134
151
                c.Assert(err, IsNil)
135
152
                select {
136
153
                case got, ok := <-environConfigWatcher.Changes():
137
154
                        c.Assert(ok, Equals, true)
138
 
                        c.Assert(got.Map(), DeepEquals, test.want)
 
155
                        gotAttrs := got.AllAttrs()
 
156
                        for key, value := range test {
 
157
                                c.Assert(gotAttrs[key], Equals, value)
 
158
                        }
139
159
                case <-time.After(200 * time.Millisecond):
140
 
                        c.Fatalf("did not get change: %#v", test.want)
 
160
                        c.Fatalf("did not get change: %#v", test)
141
161
                }
142
162
        }
143
163