LEFT | RIGHT |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package main | 4 package main |
5 | 5 |
6 import ( | 6 import ( |
| 7 "bytes" |
7 "os" | 8 "os" |
8 "reflect" | 9 "reflect" |
9 | 10 |
10 . "launchpad.net/gocheck" | 11 . "launchpad.net/gocheck" |
11 "launchpad.net/juju-core/cmd" | 12 "launchpad.net/juju-core/cmd" |
12 "launchpad.net/juju-core/environs/dummy" | 13 "launchpad.net/juju-core/environs/dummy" |
13 "launchpad.net/juju-core/juju/osenv" | 14 "launchpad.net/juju-core/juju/osenv" |
14 "launchpad.net/juju-core/juju/testing" | 15 "launchpad.net/juju-core/juju/testing" |
15 coretesting "launchpad.net/juju-core/testing" | 16 coretesting "launchpad.net/juju-core/testing" |
16 "launchpad.net/juju-core/testing/checkers" | 17 "launchpad.net/juju-core/testing/checkers" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 os.Setenv(osenv.JujuEnv, oldenv) | 114 os.Setenv(osenv.JujuEnv, oldenv) |
114 assertConnName(c, com, "walthamstow") | 115 assertConnName(c, com, "walthamstow") |
115 | 116 |
116 com, args = cmdFunc() | 117 com, args = cmdFunc() |
117 if _, ok := com.(*StatusCommand); !ok { | 118 if _, ok := com.(*StatusCommand); !ok { |
118 testInit(c, com, append(args, "hotdog"), "unrecognized a
rgs.*") | 119 testInit(c, com, append(args, "hotdog"), "unrecognized a
rgs.*") |
119 } | 120 } |
120 } | 121 } |
121 } | 122 } |
122 | 123 |
123 func runCommand(com cmd.Command, args ...string) (opc chan dummy.Operation, errc
chan error) { | 124 func runCommand(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dum
my.Operation, errc chan error) { |
124 errc = make(chan error, 1) | 125 errc = make(chan error, 1) |
125 opc = make(chan dummy.Operation, 200) | 126 opc = make(chan dummy.Operation, 200) |
126 dummy.Listen(opc) | 127 dummy.Listen(opc) |
127 go func() { | 128 go func() { |
128 // signal that we're done with this ops channel. | 129 // signal that we're done with this ops channel. |
129 defer dummy.Listen(nil) | 130 defer dummy.Listen(nil) |
130 | 131 |
131 err := coretesting.InitCommand(com, args) | 132 err := coretesting.InitCommand(com, args) |
132 if err != nil { | 133 if err != nil { |
133 errc <- err | 134 errc <- err |
134 return | 135 return |
135 } | 136 } |
136 | 137 |
137 » » err = com.Run(cmd.DefaultContext()) | 138 » » if ctx == nil { |
| 139 » » » ctx = cmd.DefaultContext() |
| 140 » » } |
| 141 » » err = com.Run(ctx) |
138 errc <- err | 142 errc <- err |
139 }() | 143 }() |
140 return | 144 return |
141 } | 145 } |
142 | 146 |
143 func (*CmdSuite) TestDestroyEnvironmentCommand(c *C) { | 147 func (*CmdSuite) TestDestroyEnvironmentCommand(c *C) { |
144 // normal destroy | 148 // normal destroy |
145 » opc, errc := runCommand(new(DestroyEnvironmentCommand)) | 149 » opc, errc := runCommand(nil, new(DestroyEnvironmentCommand), "--yes") |
146 c.Check(<-errc, IsNil) | 150 c.Check(<-errc, IsNil) |
147 c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") | 151 c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") |
148 | 152 |
149 // destroy with broken environment | 153 // destroy with broken environment |
150 » opc, errc = runCommand(new(DestroyEnvironmentCommand), "-e", "brokenenv"
) | 154 » opc, errc = runCommand(nil, new(DestroyEnvironmentCommand), "--yes", "-e
", "brokenenv") |
151 c.Check(<-opc, IsNil) | 155 c.Check(<-opc, IsNil) |
152 c.Check(<-errc, ErrorMatches, "dummy.Destroy is broken") | 156 c.Check(<-errc, ErrorMatches, "dummy.Destroy is broken") |
153 c.Check(<-opc, IsNil) | 157 c.Check(<-opc, IsNil) |
| 158 } |
| 159 |
| 160 func (*CmdSuite) TestDestroyEnvironmentCommandConfirmation(c *C) { |
| 161 com := new(DestroyEnvironmentCommand) |
| 162 c.Check(coretesting.InitCommand(com, nil), IsNil) |
| 163 c.Check(com.assumeYes, Equals, false) |
| 164 |
| 165 com = new(DestroyEnvironmentCommand) |
| 166 c.Check(coretesting.InitCommand(com, []string{"-y"}), IsNil) |
| 167 c.Check(com.assumeYes, Equals, true) |
| 168 |
| 169 com = new(DestroyEnvironmentCommand) |
| 170 c.Check(coretesting.InitCommand(com, []string{"--yes"}), IsNil) |
| 171 c.Check(com.assumeYes, Equals, true) |
| 172 |
| 173 var stdin, stdout bytes.Buffer |
| 174 ctx := cmd.DefaultContext() |
| 175 ctx.Stdout = &stdout |
| 176 ctx.Stdin = &stdin |
| 177 |
| 178 // Ensure confirmation is requested if "-y" is not specified. |
| 179 stdin.WriteString("n") |
| 180 opc, errc := runCommand(ctx, new(DestroyEnvironmentCommand)) |
| 181 c.Check(<-errc, ErrorMatches, "Environment destruction aborted") |
| 182 c.Check(<-opc, IsNil) |
| 183 c.Check(stdout.String(), Matches, "WARNING:.*peckham.*\\(type: dummy\\)(
.|\n)*") |
| 184 |
| 185 // EOF on stdin: equivalent to answering no. |
| 186 stdin.Reset() |
| 187 stdout.Reset() |
| 188 opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand)) |
| 189 c.Check(<-opc, IsNil) |
| 190 c.Check(<-errc, ErrorMatches, "Environment destruction aborted") |
| 191 |
| 192 // "--yes" passed: no confirmation request. |
| 193 stdin.Reset() |
| 194 stdout.Reset() |
| 195 opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "--yes") |
| 196 c.Check(<-errc, IsNil) |
| 197 c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") |
| 198 c.Check(stdout.String(), Equals, "") |
| 199 |
| 200 // Any of casing of "y" and "yes" will confirm. |
| 201 for _, answer := range []string{"y", "Y", "yes", "YES"} { |
| 202 stdin.Reset() |
| 203 stdout.Reset() |
| 204 stdin.WriteString(answer) |
| 205 opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand)) |
| 206 c.Check(<-errc, IsNil) |
| 207 c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") |
| 208 c.Check(stdout.String(), Matches, "WARNING:.*peckham.*\\(type: d
ummy\\)(.|\n)*") |
| 209 } |
154 } | 210 } |
155 | 211 |
156 var deployTests = []struct { | 212 var deployTests = []struct { |
157 args []string | 213 args []string |
158 com *DeployCommand | 214 com *DeployCommand |
159 }{ | 215 }{ |
160 { | 216 { |
161 []string{"charm-name"}, | 217 []string{"charm-name"}, |
162 &DeployCommand{}, | 218 &DeployCommand{}, |
163 }, { | 219 }, { |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 } | 414 } |
359 | 415 |
360 func (*CmdSuite) TestDestroyUnitCommandInit(c *C) { | 416 func (*CmdSuite) TestDestroyUnitCommandInit(c *C) { |
361 // missing args | 417 // missing args |
362 _, err := initDestroyUnitCommand() | 418 _, err := initDestroyUnitCommand() |
363 c.Assert(err, ErrorMatches, "no units specified") | 419 c.Assert(err, ErrorMatches, "no units specified") |
364 // not a unit | 420 // not a unit |
365 _, err = initDestroyUnitCommand("seven/nine") | 421 _, err = initDestroyUnitCommand("seven/nine") |
366 c.Assert(err, ErrorMatches, `invalid unit name "seven/nine"`) | 422 c.Assert(err, ErrorMatches, `invalid unit name "seven/nine"`) |
367 } | 423 } |
LEFT | RIGHT |