OLD | NEW |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | 4 "bytes" |
5 "io/ioutil" | 5 "io/ioutil" |
6 . "launchpad.net/gocheck" | 6 . "launchpad.net/gocheck" |
7 "launchpad.net/juju-core/environs" | 7 "launchpad.net/juju-core/environs" |
8 "launchpad.net/juju-core/environs/config" | 8 "launchpad.net/juju-core/environs/config" |
9 "launchpad.net/juju-core/juju/testing" | 9 "launchpad.net/juju-core/juju/testing" |
10 "launchpad.net/juju-core/state" | 10 "launchpad.net/juju-core/state" |
11 coretesting "launchpad.net/juju-core/testing" | 11 coretesting "launchpad.net/juju-core/testing" |
12 "launchpad.net/juju-core/version" | 12 "launchpad.net/juju-core/version" |
13 "net/http" | 13 "net/http" |
14 "path/filepath" | 14 "path/filepath" |
15 "time" | 15 "time" |
16 ) | 16 ) |
17 | 17 |
18 var _ = Suite(&upgraderSuite{}) | 18 var _ = Suite(&upgraderSuite{}) |
19 | 19 |
20 type upgraderSuite struct { | 20 type upgraderSuite struct { |
21 testing.JujuConnSuite | 21 testing.JujuConnSuite |
22 oldVarDir string | |
23 } | |
24 | |
25 func (s *upgraderSuite) SetUpTest(c *C) { | |
26 s.JujuConnSuite.SetUpTest(c) | |
27 s.oldVarDir = environs.VarDir | |
28 environs.VarDir = c.MkDir() | |
29 } | |
30 | |
31 func (s *upgraderSuite) TearDownTest(c *C) { | |
32 environs.VarDir = s.oldVarDir | |
33 s.JujuConnSuite.TearDownTest(c) | |
34 } | 22 } |
35 | 23 |
36 func (s *upgraderSuite) TestUpgraderError(c *C) { | 24 func (s *upgraderSuite) TestUpgraderError(c *C) { |
37 st, err := state.Open(s.StateInfo(c)) | 25 st, err := state.Open(s.StateInfo(c)) |
38 c.Assert(err, IsNil) | 26 c.Assert(err, IsNil) |
39 // We have no installed tools, so the logic should set the agent | 27 // We have no installed tools, so the logic should set the agent |
40 // tools anyway, but with no URL. | 28 // tools anyway, but with no URL. |
41 » u := startUpgrader(c, st, &state.Tools{Binary: version.Current}) | 29 » u := startUpgrader(c, st, c.MkDir(), &state.Tools{Binary: version.Curren
t}) |
42 | 30 |
43 // Close the state under the watcher and check that the upgrader dies. | 31 // Close the state under the watcher and check that the upgrader dies. |
44 st.Close() | 32 st.Close() |
45 waitDeath(c, u, nil, "watcher: cannot get content of node.*") | 33 waitDeath(c, u, nil, "watcher: cannot get content of node.*") |
46 } | 34 } |
47 | 35 |
48 func (s *upgraderSuite) TestUpgraderStop(c *C) { | 36 func (s *upgraderSuite) TestUpgraderStop(c *C) { |
49 » u := startUpgrader(c, s.State, &state.Tools{Binary: version.Current}) | 37 » u := startUpgrader(c, s.State, c.MkDir(), &state.Tools{Binary: version.C
urrent}) |
50 err := u.Stop() | 38 err := u.Stop() |
51 c.Assert(err, IsNil) | 39 c.Assert(err, IsNil) |
52 } | 40 } |
53 | 41 |
54 func (s *upgraderSuite) proposeVersion(c *C, vers version.Number, development bo
ol) { | 42 func (s *upgraderSuite) proposeVersion(c *C, vers version.Number, development bo
ol) { |
55 cfg, err := s.State.EnvironConfig() | 43 cfg, err := s.State.EnvironConfig() |
56 c.Assert(err, IsNil) | 44 c.Assert(err, IsNil) |
57 attrs := cfg.AllAttrs() | 45 attrs := cfg.AllAttrs() |
58 attrs["agent-version"] = vers.String() | 46 attrs["agent-version"] = vers.String() |
59 attrs["development"] = development | 47 attrs["development"] = development |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 propose: "2.0.5", | 112 propose: "2.0.5", |
125 upgradeTo: "2.0.5", | 113 upgradeTo: "2.0.5", |
126 }, | 114 }, |
127 } | 115 } |
128 | 116 |
129 func (s *upgraderSuite) TestUpgrader(c *C) { | 117 func (s *upgraderSuite) TestUpgrader(c *C) { |
130 // Set up the current version and tools. | 118 // Set up the current version and tools. |
131 version.Current = version.MustParseBinary("2.0.0-foo-bar") | 119 version.Current = version.MustParseBinary("2.0.0-foo-bar") |
132 v0path, v0tools := s.uploadTools(c, version.Current) | 120 v0path, v0tools := s.uploadTools(c, version.Current) |
133 | 121 |
| 122 dataDir := c.MkDir() |
134 // Unpack the "current" version of the tools, and delete them from | 123 // Unpack the "current" version of the tools, and delete them from |
135 // the storage so that we're sure that the uploader isn't trying | 124 // the storage so that we're sure that the uploader isn't trying |
136 // to fetch them. | 125 // to fetch them. |
137 resp, err := http.Get(v0tools.URL) | 126 resp, err := http.Get(v0tools.URL) |
138 c.Assert(err, IsNil) | 127 c.Assert(err, IsNil) |
139 » err = environs.UnpackTools(v0tools, resp.Body) | 128 » err = environs.UnpackTools(dataDir, v0tools, resp.Body) |
140 c.Assert(err, IsNil) | 129 c.Assert(err, IsNil) |
141 err = s.Conn.Environ.Storage().Remove(v0path) | 130 err = s.Conn.Environ.Storage().Remove(v0path) |
142 c.Assert(err, IsNil) | 131 c.Assert(err, IsNil) |
143 | 132 |
144 var ( | 133 var ( |
145 u *Upgrader | 134 u *Upgrader |
146 upgraderDone <-chan error | 135 upgraderDone <-chan error |
147 currentTools = v0tools | 136 currentTools = v0tools |
148 ) | 137 ) |
149 | 138 |
150 defer func() { | 139 defer func() { |
151 if u != nil { | 140 if u != nil { |
152 c.Assert(u.Stop(), IsNil) | 141 c.Assert(u.Stop(), IsNil) |
153 } | 142 } |
154 }() | 143 }() |
155 | 144 |
156 uploaded := make(map[version.Number]*state.Tools) | 145 uploaded := make(map[version.Number]*state.Tools) |
157 for i, test := range upgraderTests { | 146 for i, test := range upgraderTests { |
158 c.Logf("%d. %s; current version: %v", i, test.about, version.Cur
rent) | 147 c.Logf("%d. %s; current version: %v", i, test.about, version.Cur
rent) |
159 for _, v := range test.upload { | 148 for _, v := range test.upload { |
160 vers := version.Current | 149 vers := version.Current |
161 vers.Number = version.MustParse(v) | 150 vers.Number = version.MustParse(v) |
162 _, tools := s.uploadTools(c, vers) | 151 _, tools := s.uploadTools(c, vers) |
163 uploaded[vers.Number] = tools | 152 uploaded[vers.Number] = tools |
164 } | 153 } |
165 if u == nil { | 154 if u == nil { |
166 » » » u = startUpgrader(c, s.State, currentTools) | 155 » » » u = startUpgrader(c, s.State, dataDir, currentTools) |
167 } | 156 } |
168 s.proposeVersion(c, version.MustParse(test.propose), test.devVer
sion) | 157 s.proposeVersion(c, version.MustParse(test.propose), test.devVer
sion) |
169 if test.upgradeTo == "" { | 158 if test.upgradeTo == "" { |
170 assertNothingHappens(c, upgraderDone) | 159 assertNothingHappens(c, upgraderDone) |
171 } else { | 160 } else { |
172 tools := uploaded[version.MustParse(test.upgradeTo)] | 161 tools := uploaded[version.MustParse(test.upgradeTo)] |
173 waitDeath(c, u, tools, "") | 162 waitDeath(c, u, tools, "") |
174 // Check that the upgraded version was really downloaded
. | 163 // Check that the upgraded version was really downloaded
. |
175 » » » data, err := ioutil.ReadFile(filepath.Join(environs.Tool
sDir(tools.Binary), "jujud")) | 164 » » » data, err := ioutil.ReadFile(filepath.Join(environs.Tool
sDir(dataDir, tools.Binary), "jujud")) |
176 c.Assert(err, IsNil) | 165 c.Assert(err, IsNil) |
177 c.Assert(string(data), Equals, "jujud contents "+tools.B
inary.String()) | 166 c.Assert(string(data), Equals, "jujud contents "+tools.B
inary.String()) |
178 | 167 |
179 u, upgraderDone = nil, nil | 168 u, upgraderDone = nil, nil |
180 currentTools = tools | 169 currentTools = tools |
181 version.Current = tools.Binary | 170 version.Current = tools.Binary |
182 } | 171 } |
183 } | 172 } |
184 } | 173 } |
185 | 174 |
186 func assertNothingHappens(c *C, upgraderDone <-chan error) { | 175 func assertNothingHappens(c *C, upgraderDone <-chan error) { |
187 select { | 176 select { |
188 case got := <-upgraderDone: | 177 case got := <-upgraderDone: |
189 c.Fatalf("expected nothing to happen, got %v", got) | 178 c.Fatalf("expected nothing to happen, got %v", got) |
190 case <-time.After(100 * time.Millisecond): | 179 case <-time.After(100 * time.Millisecond): |
191 } | 180 } |
192 } | 181 } |
193 | 182 |
194 func assertEvent(c *C, event <-chan string, want string) { | 183 func assertEvent(c *C, event <-chan string, want string) { |
195 select { | 184 select { |
196 case got := <-event: | 185 case got := <-event: |
197 c.Assert(got, Equals, want) | 186 c.Assert(got, Equals, want) |
198 case <-time.After(500 * time.Millisecond): | 187 case <-time.After(500 * time.Millisecond): |
199 c.Fatalf("no event received; expected %q", want) | 188 c.Fatalf("no event received; expected %q", want) |
200 } | 189 } |
201 } | 190 } |
202 | 191 |
203 // startUpgrader starts the upgrader using the given machine, | 192 // startUpgrader starts the upgrader using the given machine, |
204 // expecting to see it set the given agent tools. | 193 // expecting to see it set the given agent tools. |
205 func startUpgrader(c *C, st *state.State, expectTools *state.Tools) *Upgrader { | 194 func startUpgrader(c *C, st *state.State, dataDir string, expectTools *state.Too
ls) *Upgrader { |
206 as := testAgentState(make(chan *state.Tools)) | 195 as := testAgentState(make(chan *state.Tools)) |
207 » u := NewUpgrader(st, as) | 196 » u := NewUpgrader(st, as, dataDir) |
208 select { | 197 select { |
209 case tools := <-as: | 198 case tools := <-as: |
210 c.Assert(tools, DeepEquals, expectTools) | 199 c.Assert(tools, DeepEquals, expectTools) |
211 case <-time.After(500 * time.Millisecond): | 200 case <-time.After(500 * time.Millisecond): |
212 c.Fatalf("upgrader did not set agent tools") | 201 c.Fatalf("upgrader did not set agent tools") |
213 } | 202 } |
214 return u | 203 return u |
215 } | 204 } |
216 | 205 |
217 func waitDeath(c *C, u *Upgrader, upgradeTo *state.Tools, errPat string) { | 206 func waitDeath(c *C, u *Upgrader, upgradeTo *state.Tools, errPat string) { |
(...skipping 16 matching lines...) Expand all Loading... |
234 } | 223 } |
235 } | 224 } |
236 | 225 |
237 type testAgentState chan *state.Tools | 226 type testAgentState chan *state.Tools |
238 | 227 |
239 func (as testAgentState) SetAgentTools(tools *state.Tools) error { | 228 func (as testAgentState) SetAgentTools(tools *state.Tools) error { |
240 t := *tools | 229 t := *tools |
241 as <- &t | 230 as <- &t |
242 return nil | 231 return nil |
243 } | 232 } |
OLD | NEW |