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

Commit 76fc89d

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 852c5d0 commit 76fc89d

File tree

2 files changed

+269
-2
lines changed

2 files changed

+269
-2
lines changed

commands/commands.go

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

195-
var Commands = []cli.Command{}
195+
var Commands = []cli.Command{
196+
{
197+
Flags: SharedCreateFlags,
198+
Name: "create",
199+
Usage: "Create a machine",
200+
Description: fmt.Sprintf("Run '%s create --driver name' to include the create flags for that driver in the help text.", os.Args[0]),
201+
Action: runCommand(cmdCreateOuter),
202+
SkipFlagParsing: true,
203+
},
204+
{
205+
Name: "inspect",
206+
Usage: "Inspect information about a machine",
207+
Description: "Argument is a machine name.",
208+
Action: runCommand(cmdInspect),
209+
Flags: []cli.Flag{
210+
cli.StringFlag{
211+
Name: "format, f",
212+
Usage: "Format the output using the given go template.",
213+
Value: "",
214+
},
215+
},
216+
},
217+
{
218+
Name: "ip",
219+
Usage: "Get the IP address of a machine",
220+
Description: "Argument(s) are one or more machine names.",
221+
Action: runCommand(cmdIP),
222+
},
223+
{
224+
Name: "kill",
225+
Usage: "Kill a machine",
226+
Description: "Argument(s) are one or more machine names.",
227+
Action: runCommand(cmdKill),
228+
},
229+
{
230+
Name: "ls",
231+
Usage: "List machines",
232+
Action: runCommand(cmdLs),
233+
Flags: []cli.Flag{
234+
cli.BoolFlag{
235+
Name: "quiet, q",
236+
Usage: "Enable quiet mode",
237+
},
238+
cli.StringSliceFlag{
239+
Name: "filter",
240+
Usage: "Filter output based on conditions provided",
241+
Value: &cli.StringSlice{},
242+
},
243+
cli.IntFlag{
244+
Name: "timeout, t",
245+
Usage: fmt.Sprintf("Timeout in seconds, default to %ds", lsDefaultTimeout),
246+
Value: lsDefaultTimeout,
247+
},
248+
cli.StringFlag{
249+
Name: "format, f",
250+
Usage: "Pretty-print machines using a Go template",
251+
},
252+
},
253+
},
254+
{
255+
Name: "restart",
256+
Usage: "Restart a machine",
257+
Description: "Argument(s) are one or more machine names.",
258+
Action: runCommand(cmdRestart),
259+
},
260+
{
261+
Flags: []cli.Flag{
262+
cli.BoolFlag{
263+
Name: "force, f",
264+
Usage: "Remove local configuration even if machine cannot be removed, also implies an automatic yes (`-y`)",
265+
},
266+
cli.BoolFlag{
267+
Name: "y",
268+
Usage: "Assumes automatic yes to proceed with remove, without prompting further user confirmation",
269+
},
270+
},
271+
Name: "rm",
272+
Usage: "Remove a machine",
273+
Description: "Argument(s) are one or more machine names.",
274+
Action: runCommand(cmdRm),
275+
},
276+
{
277+
Name: "ssh",
278+
Usage: "Log into or run a command on a machine with SSH.",
279+
Description: "Arguments are [machine-name] [command]",
280+
Action: runCommand(cmdSSH),
281+
SkipFlagParsing: true,
282+
},
283+
{
284+
Name: "scp",
285+
Usage: "Copy files between machines",
286+
Description: "Arguments are [machine:][path] [machine:][path].",
287+
Action: runCommand(cmdScp),
288+
Flags: []cli.Flag{
289+
cli.BoolFlag{
290+
Name: "recursive, r",
291+
Usage: "Copy files recursively (required to copy directories)",
292+
},
293+
cli.BoolFlag{
294+
Name: "delta, d",
295+
Usage: "Reduce amount of data sent over network by sending only the differences (uses rsync)",
296+
},
297+
},
298+
},
299+
{
300+
Name: "start",
301+
Usage: "Start a machine",
302+
Description: "Argument(s) are one or more machine names.",
303+
Action: runCommand(cmdStart),
304+
},
305+
{
306+
Name: "status",
307+
Usage: "Get the status of a machine",
308+
Description: "Argument is a machine name.",
309+
Action: runCommand(cmdStatus),
310+
},
311+
{
312+
Name: "stop",
313+
Usage: "Stop a machine",
314+
Description: "Argument(s) are one or more machine names.",
315+
Action: runCommand(cmdStop),
316+
},
317+
{
318+
Name: "version",
319+
Usage: "Show the Lambda Machine Local version",
320+
Action: runCommand(cmdVersion),
321+
},
322+
}
196323

197324
func printIP(h *host.Host) func() error {
198325
return func() error {
@@ -211,7 +338,15 @@ func printIP(h *host.Host) func() error {
211338
// We run commands concurrently and communicate back an error if there was one.
212339
func machineCommand(actionName string, host *host.Host, errorChan chan<- error) {
213340
// TODO: These actions should have their own type.
214-
commands := map[string](func() error){}
341+
commands := map[string](func() error){
342+
"configureAuth": host.ConfigureAuth,
343+
"start": host.Start,
344+
"stop": host.Stop,
345+
"restart": host.Restart,
346+
"kill": host.Kill,
347+
"ip": printIP(host),
348+
"provision": host.Provision,
349+
}
215350

216351
log.Debugf("command=%s machine=%s", actionName, host.Name)
217352

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)