Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 6c3ca24

Browse files
committed
commands/commands: Populate []cli.Command{} and add previously removed test back
* commands/commands_test.go Add `HostOptions: ...` to `[]*host.Host` in TestRunActionForeachMachine. This is needed because we now call Provision in Start action. Provision method requires this structure to be populated.
1 parent 82c20a7 commit 6c3ca24

File tree

2 files changed

+330
-2
lines changed

2 files changed

+330
-2
lines changed

commands/commands.go

Lines changed: 198 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,195 @@ func confirmInput(msg string) (bool, error) {
192192
return confirmed, nil
193193
}
194194

195-
var Commands = []cli.Command{}
195+
var Commands = []cli.Command{
196+
{
197+
Name: "active",
198+
Usage: "Print which machine is active",
199+
Action: runCommand(cmdActive),
200+
Flags: []cli.Flag{
201+
cli.IntFlag{
202+
Name: "timeout, t",
203+
Usage: fmt.Sprintf("Timeout in seconds, default to %ds", activeDefaultTimeout),
204+
Value: activeDefaultTimeout,
205+
},
206+
},
207+
},
208+
{
209+
Name: "config",
210+
Usage: "Print the connection config for machine",
211+
Description: "Argument is a machine name.",
212+
Action: runCommand(cmdConfig),
213+
},
214+
{
215+
Flags: SharedCreateFlags,
216+
Name: "create",
217+
Usage: "Create a machine",
218+
Description: fmt.Sprintf("Run '%s create --driver name' to include the create flags for that driver in the help text.", os.Args[0]),
219+
Action: runCommand(cmdCreateOuter),
220+
SkipFlagParsing: true,
221+
},
222+
{
223+
Name: "env",
224+
Usage: "Display the commands to set up the environment for the Docker client",
225+
Description: "Argument is a machine name.",
226+
Action: runCommand(cmdEnv),
227+
Flags: []cli.Flag{
228+
cli.StringFlag{
229+
Name: "shell",
230+
Usage: "Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh], default is auto-detect",
231+
},
232+
cli.BoolFlag{
233+
Name: "unset, u",
234+
Usage: "Unset variables instead of setting them",
235+
},
236+
cli.BoolFlag{
237+
Name: "no-proxy",
238+
Usage: "Add machine IP to NO_PROXY environment variable",
239+
},
240+
},
241+
},
242+
{
243+
Name: "inspect",
244+
Usage: "Inspect information about a machine",
245+
Description: "Argument is a machine name.",
246+
Action: runCommand(cmdInspect),
247+
Flags: []cli.Flag{
248+
cli.StringFlag{
249+
Name: "format, f",
250+
Usage: "Format the output using the given go template.",
251+
Value: "",
252+
},
253+
},
254+
},
255+
{
256+
Name: "ip",
257+
Usage: "Get the IP address of a machine",
258+
Description: "Argument(s) are one or more machine names.",
259+
Action: runCommand(cmdIP),
260+
},
261+
{
262+
Name: "kill",
263+
Usage: "Kill a machine",
264+
Description: "Argument(s) are one or more machine names.",
265+
Action: runCommand(cmdKill),
266+
},
267+
{
268+
Name: "ls",
269+
Usage: "List machines",
270+
Action: runCommand(cmdLs),
271+
Flags: []cli.Flag{
272+
cli.BoolFlag{
273+
Name: "quiet, q",
274+
Usage: "Enable quiet mode",
275+
},
276+
cli.StringSliceFlag{
277+
Name: "filter",
278+
Usage: "Filter output based on conditions provided",
279+
Value: &cli.StringSlice{},
280+
},
281+
cli.IntFlag{
282+
Name: "timeout, t",
283+
Usage: fmt.Sprintf("Timeout in seconds, default to %ds", lsDefaultTimeout),
284+
Value: lsDefaultTimeout,
285+
},
286+
cli.StringFlag{
287+
Name: "format, f",
288+
Usage: "Pretty-print machines using a Go template",
289+
},
290+
},
291+
},
292+
{
293+
Name: "provision",
294+
Usage: "Re-provision existing machines",
295+
Action: runCommand(cmdProvision),
296+
},
297+
{
298+
Name: "regenerate-certs",
299+
Usage: "Regenerate TLS Certificates for a machine",
300+
Description: "Argument(s) are one or more machine names.",
301+
Action: runCommand(cmdRegenerateCerts),
302+
Flags: []cli.Flag{
303+
cli.BoolFlag{
304+
Name: "force, f",
305+
Usage: "Force rebuild and do not prompt",
306+
},
307+
},
308+
},
309+
{
310+
Name: "restart",
311+
Usage: "Restart a machine",
312+
Description: "Argument(s) are one or more machine names.",
313+
Action: runCommand(cmdRestart),
314+
},
315+
{
316+
Flags: []cli.Flag{
317+
cli.BoolFlag{
318+
Name: "force, f",
319+
Usage: "Remove local configuration even if machine cannot be removed, also implies an automatic yes (`-y`)",
320+
},
321+
cli.BoolFlag{
322+
Name: "y",
323+
Usage: "Assumes automatic yes to proceed with remove, without prompting further user confirmation",
324+
},
325+
},
326+
Name: "rm",
327+
Usage: "Remove a machine",
328+
Description: "Argument(s) are one or more machine names.",
329+
Action: runCommand(cmdRm),
330+
},
331+
{
332+
Name: "ssh",
333+
Usage: "Log into or run a command on a machine with SSH.",
334+
Description: "Arguments are [machine-name] [command]",
335+
Action: runCommand(cmdSSH),
336+
SkipFlagParsing: true,
337+
},
338+
{
339+
Name: "scp",
340+
Usage: "Copy files between machines",
341+
Description: "Arguments are [machine:][path] [machine:][path].",
342+
Action: runCommand(cmdScp),
343+
Flags: []cli.Flag{
344+
cli.BoolFlag{
345+
Name: "recursive, r",
346+
Usage: "Copy files recursively (required to copy directories)",
347+
},
348+
cli.BoolFlag{
349+
Name: "delta, d",
350+
Usage: "Reduce amount of data sent over network by sending only the differences (uses rsync)",
351+
},
352+
},
353+
},
354+
{
355+
Name: "start",
356+
Usage: "Start a machine",
357+
Description: "Argument(s) are one or more machine names.",
358+
Action: runCommand(cmdStart),
359+
},
360+
{
361+
Name: "status",
362+
Usage: "Get the status of a machine",
363+
Description: "Argument is a machine name.",
364+
Action: runCommand(cmdStatus),
365+
},
366+
{
367+
Name: "stop",
368+
Usage: "Stop a machine",
369+
Description: "Argument(s) are one or more machine names.",
370+
Action: runCommand(cmdStop),
371+
},
372+
{
373+
Name: "url",
374+
Usage: "Get the URL of a machine",
375+
Description: "Argument is a machine name.",
376+
Action: runCommand(cmdURL),
377+
},
378+
{
379+
Name: "version",
380+
Usage: "Show the Lambda Machine Local version or a machine docker version",
381+
Action: runCommand(cmdVersion),
382+
},
383+
}
196384

197385
func printIP(h *host.Host) func() error {
198386
return func() error {
@@ -211,7 +399,15 @@ func printIP(h *host.Host) func() error {
211399
// We run commands concurrently and communicate back an error if there was one.
212400
func machineCommand(actionName string, host *host.Host, errorChan chan<- error) {
213401
// TODO: These actions should have their own type.
214-
commands := map[string](func() error){}
402+
commands := map[string](func() error){
403+
"configureAuth": host.ConfigureAuth,
404+
"start": host.Start,
405+
"stop": host.Stop,
406+
"restart": host.Restart,
407+
"kill": host.Kill,
408+
"ip": printIP(host),
409+
"provision": host.Provision,
410+
}
215411

216412
log.Debugf("command=%s machine=%s", actionName, host.Name)
217413

commands/commands_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,143 @@ import (
88

99
"github.com/codegangsta/cli"
1010
"github.com/docker/machine/libmachine"
11+
"github.com/docker/machine/libmachine/auth"
1112
"github.com/docker/machine/libmachine/crashreport"
13+
"github.com/docker/machine/libmachine/engine"
14+
"github.com/docker/machine/libmachine/host"
15+
"github.com/docker/machine/libmachine/hosttest"
1216
"github.com/docker/machine/libmachine/mcnerror"
17+
"github.com/docker/machine/libmachine/provision"
18+
"github.com/docker/machine/libmachine/state"
19+
"github.com/lambda-linux/lambda-machine-local/commands/commandstest"
20+
"github.com/lambda-linux/lambda-machine-local/drivers/fakedriver"
1321
"github.com/stretchr/testify/assert"
1422
)
1523

24+
func TestRunActionForeachMachine(t *testing.T) {
25+
defer provision.SetDetector(&provision.StandardDetector{})
26+
provision.SetDetector(&provision.FakeDetector{
27+
Provisioner: provision.NewNetstatProvisioner(),
28+
})
29+
30+
// Assume a bunch of machines in randomly started or
31+
// stopped states.
32+
machines := []*host.Host{
33+
{
34+
Name: "foo",
35+
DriverName: "fakedriver",
36+
Driver: &fakedriver.Driver{
37+
MockState: state.Running,
38+
},
39+
HostOptions: &host.Options{
40+
AuthOptions: &auth.Options{},
41+
EngineOptions: &engine.Options{},
42+
},
43+
},
44+
{
45+
Name: "bar",
46+
DriverName: "fakedriver",
47+
Driver: &fakedriver.Driver{
48+
MockState: state.Stopped,
49+
},
50+
HostOptions: &host.Options{
51+
AuthOptions: &auth.Options{},
52+
EngineOptions: &engine.Options{},
53+
},
54+
},
55+
{
56+
Name: "baz",
57+
// Ssh, don't tell anyone but this
58+
// driver only _thinks_ it's named
59+
// virtualbox... (to test serial actions)
60+
// It's actually FakeDriver!
61+
DriverName: "virtualbox",
62+
Driver: &fakedriver.Driver{
63+
MockState: state.Stopped,
64+
},
65+
HostOptions: &host.Options{
66+
AuthOptions: &auth.Options{},
67+
EngineOptions: &engine.Options{},
68+
},
69+
},
70+
{
71+
Name: "spam",
72+
DriverName: "virtualbox",
73+
Driver: &fakedriver.Driver{
74+
MockState: state.Running,
75+
},
76+
HostOptions: &host.Options{
77+
AuthOptions: &auth.Options{},
78+
EngineOptions: &engine.Options{},
79+
},
80+
},
81+
{
82+
Name: "eggs",
83+
DriverName: "fakedriver",
84+
Driver: &fakedriver.Driver{
85+
MockState: state.Stopped,
86+
},
87+
HostOptions: &host.Options{
88+
AuthOptions: &auth.Options{},
89+
EngineOptions: &engine.Options{},
90+
},
91+
},
92+
{
93+
Name: "ham",
94+
DriverName: "fakedriver",
95+
Driver: &fakedriver.Driver{
96+
MockState: state.Running,
97+
},
98+
HostOptions: &host.Options{
99+
AuthOptions: &auth.Options{},
100+
EngineOptions: &engine.Options{},
101+
},
102+
},
103+
}
104+
105+
runActionForeachMachine("start", machines)
106+
107+
for _, machine := range machines {
108+
machineState, _ := machine.Driver.GetState()
109+
110+
assert.Equal(t, state.Running, machineState)
111+
}
112+
113+
runActionForeachMachine("stop", machines)
114+
115+
for _, machine := range machines {
116+
machineState, _ := machine.Driver.GetState()
117+
118+
assert.Equal(t, state.Stopped, machineState)
119+
}
120+
}
121+
122+
func TestPrintIPEmptyGivenLocalEngine(t *testing.T) {
123+
stdoutGetter := commandstest.NewStdoutGetter()
124+
defer stdoutGetter.Stop()
125+
126+
host, _ := hosttest.GetDefaultTestHost()
127+
err := printIP(host)()
128+
129+
assert.NoError(t, err)
130+
assert.Equal(t, "\n", stdoutGetter.Output())
131+
}
132+
133+
func TestPrintIPPrintsGivenRemoteEngine(t *testing.T) {
134+
stdoutGetter := commandstest.NewStdoutGetter()
135+
defer stdoutGetter.Stop()
136+
137+
host, _ := hosttest.GetDefaultTestHost()
138+
host.Driver = &fakedriver.Driver{
139+
MockState: state.Running,
140+
MockIP: "1.2.3.4",
141+
}
142+
err := printIP(host)()
143+
144+
assert.NoError(t, err)
145+
assert.Equal(t, "1.2.3.4\n", stdoutGetter.Output())
146+
}
147+
16148
func TestConsolidateError(t *testing.T) {
17149
cases := []struct {
18150
inputErrs []error

0 commit comments

Comments
 (0)