1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package main
import (
"os"
"reflect"
. "launchpad.net/gocheck"
"launchpad.net/juju-core/cmd"
"launchpad.net/juju-core/environs/dummy"
"launchpad.net/juju-core/juju/testing"
coretesting "launchpad.net/juju-core/testing"
"launchpad.net/juju-core/testing/checkers"
)
type CmdSuite struct {
testing.JujuConnSuite
home *coretesting.FakeHome
}
var _ = Suite(&CmdSuite{})
const envConfig = `
default:
peckham
environments:
peckham:
type: dummy
state-server: false
admin-secret: arble
authorized-keys: i-am-a-key
default-series: defaultseries
walthamstow:
type: dummy
state-server: false
authorized-keys: i-am-a-key
brokenenv:
type: dummy
broken: Bootstrap Destroy
state-server: false
authorized-keys: i-am-a-key
`
func (s *CmdSuite) SetUpTest(c *C) {
s.JujuConnSuite.SetUpTest(c)
s.home = coretesting.MakeFakeHome(c, envConfig, "peckham", "walthamstow", "brokenenv")
}
func (s *CmdSuite) TearDownTest(c *C) {
s.home.Restore()
s.JujuConnSuite.TearDownTest(c)
}
// testInit checks that a command initialises correctly
// with the given set of arguments.
func testInit(c *C, com cmd.Command, args []string, errPat string) {
err := coretesting.InitCommand(com, args)
if errPat != "" {
c.Assert(err, ErrorMatches, errPat)
} else {
c.Assert(err, IsNil)
}
}
// assertConnName asserts that the Command is using
// the given environment name.
// Since every command has a different type,
// we use reflection to look at the value of the
// Conn field in the value.
func assertConnName(c *C, com cmd.Command, name string) {
v := reflect.ValueOf(com).Elem().FieldByName("EnvName")
c.Assert(v, checkers.Satisfies, reflect.Value.IsValid)
c.Assert(v.Interface(), Equals, name)
}
// All members of EnvironmentInitTests are tested for the -environment and -e
// flags, and that extra arguments will cause parsing to fail.
var EnvironmentInitTests = []func() (cmd.Command, []string){
func() (cmd.Command, []string) { return new(BootstrapCommand), nil },
func() (cmd.Command, []string) { return new(DestroyEnvironmentCommand), nil },
func() (cmd.Command, []string) {
return new(DeployCommand), []string{"charm-name", "service-name"}
},
func() (cmd.Command, []string) { return new(StatusCommand), nil },
}
// TestEnvironmentInit tests that all commands which accept
// the --environment variable initialise their
// environment name correctly.
func (*CmdSuite) TestEnvironmentInit(c *C) {
for i, cmdFunc := range EnvironmentInitTests {
c.Logf("test %d", i)
com, args := cmdFunc()
testInit(c, com, args, "")
assertConnName(c, com, "")
com, args = cmdFunc()
testInit(c, com, append(args, "-e", "walthamstow"), "")
assertConnName(c, com, "walthamstow")
com, args = cmdFunc()
testInit(c, com, append(args, "--environment", "walthamstow"), "")
assertConnName(c, com, "walthamstow")
// JUJU_ENV is the final place the environment can be overriden
com, args = cmdFunc()
oldenv := os.Getenv("JUJU_ENV")
os.Setenv("JUJU_ENV", "walthamstow")
testInit(c, com, args, "")
os.Setenv("JUJU_ENV", oldenv)
assertConnName(c, com, "walthamstow")
com, args = cmdFunc()
testInit(c, com, append(args, "hotdog"), "unrecognized args.*")
}
}
func runCommand(com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) {
errc = make(chan error, 1)
opc = make(chan dummy.Operation, 200)
dummy.Listen(opc)
go func() {
// signal that we're done with this ops channel.
defer dummy.Listen(nil)
err := coretesting.InitCommand(com, args)
if err != nil {
errc <- err
return
}
err = com.Run(cmd.DefaultContext())
errc <- err
}()
return
}
func (*CmdSuite) TestDestroyEnvironmentCommand(c *C) {
// normal destroy
opc, errc := runCommand(new(DestroyEnvironmentCommand))
c.Check(<-errc, IsNil)
c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham")
// destroy with broken environment
opc, errc = runCommand(new(DestroyEnvironmentCommand), "-e", "brokenenv")
c.Check(<-opc, IsNil)
c.Check(<-errc, ErrorMatches, "dummy.Destroy is broken")
c.Check(<-opc, IsNil)
}
var deployTests = []struct {
args []string
com *DeployCommand
}{
{
[]string{"charm-name"},
&DeployCommand{},
}, {
[]string{"charm-name", "service-name"},
&DeployCommand{ServiceName: "service-name"},
}, {
[]string{"--repository", "/path/to/another-repo", "charm-name"},
&DeployCommand{RepoPath: "/path/to/another-repo"},
}, {
[]string{"--upgrade", "charm-name"},
&DeployCommand{BumpRevision: true},
}, {
[]string{"-u", "charm-name"},
&DeployCommand{BumpRevision: true},
}, {
[]string{"--num-units", "33", "charm-name"},
&DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 33}},
}, {
[]string{"-n", "104", "charm-name"},
&DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 104}},
},
}
func initExpectations(com *DeployCommand) {
if com.CharmName == "" {
com.CharmName = "charm-name"
}
if com.NumUnits == 0 {
com.NumUnits = 1
}
if com.RepoPath == "" {
com.RepoPath = "/path/to/repo"
}
}
func initDeployCommand(args ...string) (*DeployCommand, error) {
com := &DeployCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestDeployCommandInit(c *C) {
defer os.Setenv("JUJU_REPOSITORY", os.Getenv("JUJU_REPOSITORY"))
os.Setenv("JUJU_REPOSITORY", "/path/to/repo")
for _, t := range deployTests {
initExpectations(t.com)
com, err := initDeployCommand(t.args...)
c.Assert(err, IsNil)
c.Assert(com, DeepEquals, t.com)
}
// test relative --config path
ctx := coretesting.Context(c)
expected := []byte("test: data")
path := ctx.AbsPath("testconfig.yaml")
file, err := os.Create(path)
c.Assert(err, IsNil)
_, err = file.Write(expected)
c.Assert(err, IsNil)
file.Close()
com, err := initDeployCommand("--config", "testconfig.yaml", "charm-name")
c.Assert(err, IsNil)
actual, err := com.Config.Read(ctx)
c.Assert(err, IsNil)
c.Assert(expected, DeepEquals, actual)
// missing args
_, err = initDeployCommand()
c.Assert(err, ErrorMatches, "no charm specified")
// environment tested elsewhere
}
func initAddUnitCommand(args ...string) (*AddUnitCommand, error) {
com := &AddUnitCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestAddUnitCommandInit(c *C) {
// missing args
_, err := initAddUnitCommand()
c.Assert(err, ErrorMatches, "no service specified")
// bad unit count
_, err = initDeployCommand("charm-name", "--num-units", "0")
c.Assert(err, ErrorMatches, "--num-units must be a positive integer")
_, err = initDeployCommand("charm-name", "-n", "0")
c.Assert(err, ErrorMatches, "--num-units must be a positive integer")
// environment tested elsewhere
}
func initExposeCommand(args ...string) (*ExposeCommand, error) {
com := &ExposeCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestExposeCommandInit(c *C) {
// missing args
_, err := initExposeCommand()
c.Assert(err, ErrorMatches, "no service name specified")
// environment tested elsewhere
}
func initUnexposeCommand(args ...string) (*UnexposeCommand, error) {
com := &UnexposeCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestUnexposeCommandInit(c *C) {
// missing args
_, err := initUnexposeCommand()
c.Assert(err, ErrorMatches, "no service name specified")
// environment tested elsewhere
}
func initSSHCommand(args ...string) (*SSHCommand, error) {
com := &SSHCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestSSHCommandInit(c *C) {
// missing args
_, err := initSSHCommand()
c.Assert(err, ErrorMatches, "no service name specified")
}
func initSCPCommand(args ...string) (*SCPCommand, error) {
com := &SCPCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestSCPCommandInit(c *C) {
// missing args
_, err := initSCPCommand()
c.Assert(err, ErrorMatches, "at least two arguments required")
// not enough args
_, err = initSCPCommand("mysql/0:foo")
c.Assert(err, ErrorMatches, "at least two arguments required")
}
func initGetCommand(args ...string) (*GetCommand, error) {
com := &GetCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestGetCommandInit(c *C) {
// missing args
_, err := initGetCommand()
c.Assert(err, ErrorMatches, "no service name specified")
}
func initSetCommand(args ...string) (*SetCommand, error) {
com := &SetCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestSetCommandInit(c *C) {
// missing args
_, err := initSetCommand()
c.Assert(err, ErrorMatches, "no service name specified")
// missing service name
_, err = initSetCommand("name=cow")
c.Assert(err, ErrorMatches, "no service name specified")
// test --config path
expected := []byte("this: is some test data")
ctx := coretesting.Context(c)
path := ctx.AbsPath("testconfig.yaml")
file, err := os.Create(path)
c.Assert(err, IsNil)
_, err = file.Write(expected)
c.Assert(err, IsNil)
file.Close()
com, err := initSetCommand("--config", "testconfig.yaml", "service")
c.Assert(err, IsNil)
c.Assert(com.SettingsYAML.Path, Equals, "testconfig.yaml")
actual, err := com.SettingsYAML.Read(ctx)
c.Assert(err, IsNil)
c.Assert(actual, DeepEquals, expected)
// --config path, but no service
com, err = initSetCommand("--config", "testconfig")
c.Assert(err, ErrorMatches, "no service name specified")
// --config and options specified
com, err = initSetCommand("service", "--config", "testconfig", "bees=")
c.Assert(err, ErrorMatches, "cannot specify --config when using key=value arguments")
}
func initDestroyUnitCommand(args ...string) (*DestroyUnitCommand, error) {
com := &DestroyUnitCommand{}
return com, coretesting.InitCommand(com, args)
}
func (*CmdSuite) TestDestroyUnitCommandInit(c *C) {
// missing args
_, err := initDestroyUnitCommand()
c.Assert(err, ErrorMatches, "no units specified")
// not a unit
_, err = initDestroyUnitCommand("seven/nine")
c.Assert(err, ErrorMatches, `invalid unit name "seven/nine"`)
}
|