Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "io/ioutil" | 4 "io/ioutil" |
5 "launchpad.net/gnuflag" | 5 "launchpad.net/gnuflag" |
6 . "launchpad.net/gocheck" | 6 . "launchpad.net/gocheck" |
7 » "launchpad.net/juju-core/juju/cmd" | 7 » "launchpad.net/juju-core/cmd" |
8 » "launchpad.net/juju-core/juju/environs" | 8 » "launchpad.net/juju-core/environs" |
9 » "launchpad.net/juju-core/juju/environs/dummy" | 9 » "launchpad.net/juju-core/environs/dummy" |
10 » "launchpad.net/juju-core/juju/testing" | 10 » "launchpad.net/juju-core/testing" |
11 "os" | 11 "os" |
12 "path/filepath" | 12 "path/filepath" |
13 "reflect" | 13 "reflect" |
14 ) | 14 ) |
15 | 15 |
16 type envFixture struct { | 16 type envSuite struct { |
niemeyer
2012/06/25 23:19:22
Let's please continue using the Suite suffix until
fwereade
2012/06/26 10:10:44
Done.
| |
17 home string | 17 home string |
18 } | 18 } |
19 | 19 |
20 func (f *envFixture) SetUp(c *C, config string) { | 20 func (f *envSuite) SetUpTest(c *C, config string) { |
21 // Arrange so that the "home" directory points | 21 // Arrange so that the "home" directory points |
22 // to a temporary directory containing the config file. | 22 // to a temporary directory containing the config file. |
23 f.home = os.Getenv("HOME") | 23 f.home = os.Getenv("HOME") |
24 dir := c.MkDir() | 24 dir := c.MkDir() |
25 os.Setenv("HOME", dir) | 25 os.Setenv("HOME", dir) |
26 err := os.Mkdir(filepath.Join(dir, ".juju"), 0777) | 26 err := os.Mkdir(filepath.Join(dir, ".juju"), 0777) |
27 c.Assert(err, IsNil) | 27 c.Assert(err, IsNil) |
28 err = ioutil.WriteFile(filepath.Join(dir, ".juju", "environments.yaml"), []byte(config), 0666) | 28 err = ioutil.WriteFile(filepath.Join(dir, ".juju", "environments.yaml"), []byte(config), 0666) |
29 c.Assert(err, IsNil) | 29 c.Assert(err, IsNil) |
30 } | 30 } |
31 | 31 |
32 func (f *envFixture) TearDown(c *C) { | 32 func (f *envSuite) TearDownTest(c *C) { |
33 os.Setenv("HOME", f.home) | 33 os.Setenv("HOME", f.home) |
34 dummy.Reset() | 34 dummy.Reset() |
35 } | 35 } |
36 | 36 |
37 type cmdSuite struct { | 37 type cmdSuite struct { |
38 testing.LoggingSuite | 38 testing.LoggingSuite |
39 » envFixture | 39 » envSuite |
40 } | 40 } |
41 | 41 |
42 var _ = Suite(&cmdSuite{}) | 42 var _ = Suite(&cmdSuite{}) |
43 | 43 |
44 // N.B. Barking is broken. | 44 // N.B. Barking is broken. |
45 var config = ` | 45 var config = ` |
46 default: | 46 default: |
47 peckham | 47 peckham |
48 environments: | 48 environments: |
49 peckham: | 49 peckham: |
50 type: dummy | 50 type: dummy |
51 zookeeper: false | 51 zookeeper: false |
52 walthamstow: | 52 walthamstow: |
53 type: dummy | 53 type: dummy |
54 zookeeper: false | 54 zookeeper: false |
55 barking: | 55 barking: |
56 type: dummy | 56 type: dummy |
57 broken: true | 57 broken: true |
58 zookeeper: false | 58 zookeeper: false |
59 ` | 59 ` |
60 | 60 |
61 func (s *cmdSuite) SetUpTest(c *C) { | 61 func (s *cmdSuite) SetUpTest(c *C) { |
62 s.LoggingSuite.SetUpTest(c) | 62 s.LoggingSuite.SetUpTest(c) |
niemeyer
2012/06/25 23:19:22
And here is the proof. :-)
| |
63 » s.envFixture.SetUp(c, config) | 63 » s.envSuite.SetUpTest(c, config) |
64 } | 64 } |
65 | 65 |
66 func (s *cmdSuite) TearDownTest(c *C) { | 66 func (s *cmdSuite) TearDownTest(c *C) { |
67 » s.envFixture.TearDown(c) | 67 » s.envSuite.TearDownTest(c) |
68 s.LoggingSuite.TearDownTest(c) | 68 s.LoggingSuite.TearDownTest(c) |
69 } | 69 } |
70 | 70 |
71 func newFlagSet() *gnuflag.FlagSet { | 71 func newFlagSet() *gnuflag.FlagSet { |
72 return gnuflag.NewFlagSet("", gnuflag.ContinueOnError) | 72 return gnuflag.NewFlagSet("", gnuflag.ContinueOnError) |
73 } | 73 } |
74 | 74 |
75 // testInit checks that a command initialises correctly | 75 // testInit checks that a command initialises correctly |
76 // with the given set of arguments. | 76 // with the given set of arguments. |
77 func testInit(c *C, com cmd.Command, args []string, errPat string) { | 77 func testInit(c *C, com cmd.Command, args []string, errPat string) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 errc <- err | 141 errc <- err |
142 return | 142 return |
143 } | 143 } |
144 | 144 |
145 err = com.Run(cmd.DefaultContext()) | 145 err = com.Run(cmd.DefaultContext()) |
146 errc <- err | 146 errc <- err |
147 }() | 147 }() |
148 return | 148 return |
149 } | 149 } |
150 | 150 |
151 func op(kind dummy.OperationKind, name string) dummy.Operation { | |
152 return dummy.Operation{ | |
153 Env: name, | |
154 Kind: kind, | |
155 } | |
156 } | |
157 | |
158 func (*cmdSuite) TestBootstrapCommand(c *C) { | 151 func (*cmdSuite) TestBootstrapCommand(c *C) { |
159 // normal bootstrap | 152 // normal bootstrap |
160 opc, errc := runCommand(new(BootstrapCommand)) | 153 opc, errc := runCommand(new(BootstrapCommand)) |
161 » c.Check(<-opc, Equals, op(dummy.OpBootstrap, "peckham")) | 154 » c.Check((<-opc).(dummy.OpBootstrap).Env, Equals, "peckham") |
162 c.Check(<-errc, IsNil) | 155 c.Check(<-errc, IsNil) |
163 | 156 |
164 // bootstrap with tool uploading - checking that a file | 157 // bootstrap with tool uploading - checking that a file |
165 // is uploaded should be sufficient, as the detailed semantics | 158 // is uploaded should be sufficient, as the detailed semantics |
166 // of UploadTools are tested in environs. | 159 // of UploadTools are tested in environs. |
167 opc, errc = runCommand(new(BootstrapCommand), "--upload-tools") | 160 opc, errc = runCommand(new(BootstrapCommand), "--upload-tools") |
168 » c.Check(<-opc, Equals, op(dummy.OpPutFile, "peckham")) | 161 » c.Check((<-opc).(dummy.OpPutFile).Env, Equals, "peckham") |
169 » c.Check(<-opc, Equals, op(dummy.OpBootstrap, "peckham")) | 162 » c.Check((<-opc).(dummy.OpBootstrap).Env, Equals, "peckham") |
170 c.Check(<-errc, IsNil) | 163 c.Check(<-errc, IsNil) |
171 | 164 |
172 envs, err := environs.ReadEnvirons("") | 165 envs, err := environs.ReadEnvirons("") |
173 c.Assert(err, IsNil) | 166 c.Assert(err, IsNil) |
174 env, err := envs.Open("peckham") | 167 env, err := envs.Open("peckham") |
175 c.Assert(err, IsNil) | 168 c.Assert(err, IsNil) |
176 dir := c.MkDir() | 169 dir := c.MkDir() |
177 err = environs.GetTools(env, dir) | 170 err = environs.GetTools(env, dir) |
178 c.Assert(err, IsNil) | 171 c.Assert(err, IsNil) |
179 | 172 |
180 // bootstrap with broken environment | 173 // bootstrap with broken environment |
181 opc, errc = runCommand(new(BootstrapCommand), "-e", "barking") | 174 opc, errc = runCommand(new(BootstrapCommand), "-e", "barking") |
182 » c.Check((<-opc).Kind, Equals, dummy.OpNone) | 175 » c.Check(<-opc, IsNil) |
183 c.Check(<-errc, ErrorMatches, `broken environment`) | 176 c.Check(<-errc, ErrorMatches, `broken environment`) |
184 } | 177 } |
185 | 178 |
186 func (*cmdSuite) TestDestroyCommand(c *C) { | 179 func (*cmdSuite) TestDestroyCommand(c *C) { |
187 // normal destroy | 180 // normal destroy |
188 opc, errc := runCommand(new(DestroyCommand)) | 181 opc, errc := runCommand(new(DestroyCommand)) |
189 » c.Check(<-opc, Equals, op(dummy.OpDestroy, "peckham")) | 182 » c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") |
190 c.Check(<-errc, IsNil) | 183 c.Check(<-errc, IsNil) |
191 | 184 |
192 // destroy with broken environment | 185 // destroy with broken environment |
193 opc, errc = runCommand(new(DestroyCommand), "-e", "barking") | 186 opc, errc = runCommand(new(DestroyCommand), "-e", "barking") |
194 » c.Check((<-opc).Kind, Equals, dummy.OpNone) | 187 » c.Check(<-opc, IsNil) |
195 c.Check(<-errc, ErrorMatches, `broken environment`) | 188 c.Check(<-errc, ErrorMatches, `broken environment`) |
196 } | 189 } |
197 | 190 |
198 var deployTests = []struct { | 191 var deployTests = []struct { |
199 args []string | 192 args []string |
200 com *DeployCommand | 193 com *DeployCommand |
201 }{ | 194 }{ |
202 { | 195 { |
203 []string{"charm-name"}, | 196 []string{"charm-name"}, |
204 &DeployCommand{}, | 197 &DeployCommand{}, |
205 }, { | 198 }, { |
206 []string{"charm-name", "service-name"}, | 199 []string{"charm-name", "service-name"}, |
207 &DeployCommand{ServiceName: "service-name"}, | 200 &DeployCommand{ServiceName: "service-name"}, |
208 }, { | 201 }, { |
209 []string{"--config", "/path/to/config.yaml", "charm-name"}, | 202 []string{"--config", "/path/to/config.yaml", "charm-name"}, |
210 &DeployCommand{ConfPath: "/path/to/config.yaml"}, | 203 &DeployCommand{ConfPath: "/path/to/config.yaml"}, |
211 }, { | 204 }, { |
212 []string{"--repository", "/path/to/another-repo", "charm-name"}, | 205 []string{"--repository", "/path/to/another-repo", "charm-name"}, |
213 &DeployCommand{RepoPath: "/path/to/another-repo"}, | 206 &DeployCommand{RepoPath: "/path/to/another-repo"}, |
214 }, { | 207 }, { |
215 []string{"--upgrade", "charm-name"}, | 208 []string{"--upgrade", "charm-name"}, |
216 » » &DeployCommand{Upgrade: true}, | 209 » » &DeployCommand{BumpRevision: true}, |
217 }, { | 210 }, { |
218 []string{"-u", "charm-name"}, | 211 []string{"-u", "charm-name"}, |
219 » » &DeployCommand{Upgrade: true}, | 212 » » &DeployCommand{BumpRevision: true}, |
220 }, { | 213 }, { |
221 []string{"--num-units", "33", "charm-name"}, | 214 []string{"--num-units", "33", "charm-name"}, |
222 &DeployCommand{NumUnits: 33}, | 215 &DeployCommand{NumUnits: 33}, |
223 }, { | 216 }, { |
224 []string{"-n", "104", "charm-name"}, | 217 []string{"-n", "104", "charm-name"}, |
225 &DeployCommand{NumUnits: 104}, | 218 &DeployCommand{NumUnits: 104}, |
226 }, | 219 }, |
227 } | 220 } |
228 | 221 |
229 func initExpectations(com *DeployCommand) { | 222 func initExpectations(com *DeployCommand) { |
(...skipping 27 matching lines...) Expand all Loading... | |
257 // missing args | 250 // missing args |
258 _, err := initDeployCommand() | 251 _, err := initDeployCommand() |
259 c.Assert(err, ErrorMatches, "no charm specified") | 252 c.Assert(err, ErrorMatches, "no charm specified") |
260 | 253 |
261 // bad unit count | 254 // bad unit count |
262 _, err = initDeployCommand("charm-name", "--num-units", "0") | 255 _, err = initDeployCommand("charm-name", "--num-units", "0") |
263 c.Assert(err, ErrorMatches, "must deploy at least one unit") | 256 c.Assert(err, ErrorMatches, "must deploy at least one unit") |
264 | 257 |
265 // environment tested elsewhere | 258 // environment tested elsewhere |
266 } | 259 } |
LEFT | RIGHT |