X Tutup
Skip to content

Commit 3612c55

Browse files
committed
Add version to machine ls
Signed-off-by: David Gageot <david@gageot.net>
1 parent e26c485 commit 3612c55

File tree

19 files changed

+332
-50
lines changed

19 files changed

+332
-50
lines changed

commands/active.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99
)
1010

1111
var (
12-
errTooManyArguments = errors.New("Error: Too many arguments given")
13-
errNoActiveHost = errors.New("No active host found")
12+
errNoActiveHost = errors.New("No active host found")
1413
)
1514

1615
func cmdActive(c CommandLine, api libmachine.API) error {
1716
if len(c.Args()) > 0 {
18-
return errTooManyArguments
17+
return ErrTooManyArguments
1918
}
2019

2120
hosts, hostsInError, err := persist.LoadAllHosts(api)

commands/commands.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
ErrUnknownShell = errors.New("Error: Unknown shell")
2323
ErrNoMachineSpecified = errors.New("Error: Expected to get one or more machine names as arguments")
2424
ErrExpectedOneMachine = errors.New("Error: Expected one machine name as an argument")
25+
ErrTooManyArguments = errors.New("Error: Too many arguments given")
2526
)
2627

2728
// CommandLine contains all the information passed to the commands on the command line.
@@ -305,7 +306,7 @@ var Commands = []cli.Command{
305306
},
306307
{
307308
Name: "version",
308-
Usage: "Show the Docker Machine version information",
309+
Usage: "Show the Docker Machine version or a machine docker version",
309310
Action: fatalOnError(cmdVersion),
310311
},
311312
}
@@ -316,7 +317,9 @@ func printIP(h *host.Host) func() error {
316317
if err != nil {
317318
return fmt.Errorf("Error getting IP address: %s", err)
318319
}
320+
319321
fmt.Println(ip)
322+
320323
return nil
321324
}
322325
}

commands/commandstest/fake_command_line.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type FakeFlagger struct {
1010

1111
type FakeCommandLine struct {
1212
LocalFlags, GlobalFlags *FakeFlagger
13-
HelpShown bool
13+
HelpShown, VersionShown bool
1414
CliArgs []string
1515
}
1616

@@ -91,5 +91,5 @@ func (fcli *FakeCommandLine) Args() cli.Args {
9191
}
9292

9393
func (fcli *FakeCommandLine) ShowVersion() {
94-
return
94+
fcli.VersionShown = true
9595
}

commands/ls.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ type FilterOptions struct {
3333
}
3434

3535
type HostListItem struct {
36-
Name string
37-
Active bool
38-
DriverName string
39-
State state.State
40-
URL string
41-
SwarmOptions *swarm.Options
42-
Error string
36+
Name string
37+
Active bool
38+
DriverName string
39+
State state.State
40+
URL string
41+
SwarmOptions *swarm.Options
42+
Error string
43+
DockerVersion string
4344
}
4445

4546
func cmdLs(c CommandLine, api libmachine.API) error {
@@ -68,7 +69,7 @@ func cmdLs(c CommandLine, api libmachine.API) error {
6869
swarmInfo := make(map[string]string)
6970

7071
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
71-
fmt.Fprintln(w, "NAME\tACTIVE\tDRIVER\tSTATE\tURL\tSWARM\tERRORS")
72+
fmt.Fprintln(w, "NAME\tACTIVE\tDRIVER\tSTATE\tURL\tSWARM\tDOCKER\tERRORS")
7273

7374
for _, host := range hostList {
7475
swarmOptions := host.HostOptions.SwarmOptions
@@ -97,8 +98,8 @@ func cmdLs(c CommandLine, api libmachine.API) error {
9798
swarmInfo = fmt.Sprintf("%s (master)", swarmInfo)
9899
}
99100
}
100-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
101-
item.Name, activeString, item.DriverName, item.State, item.URL, swarmInfo, item.Error)
101+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
102+
item.Name, activeString, item.DriverName, item.State, item.URL, swarmInfo, item.DockerVersion, item.Error)
102103
}
103104

104105
w.Flush()
@@ -232,11 +233,21 @@ func matchesName(host *host.Host, names []string) bool {
232233
func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
233234
url := ""
234235
hostError := ""
236+
dockerVersion := "Unknown"
235237

236238
currentState, err := h.Driver.GetState()
237239
if err == nil {
238-
url, err = h.GetURL()
240+
url, err = h.URL()
239241
}
242+
if err == nil {
243+
dockerVersion, err = h.DockerVersion()
244+
if err != nil {
245+
dockerVersion = "Unknown"
246+
} else {
247+
dockerVersion = fmt.Sprintf("v%s", dockerVersion)
248+
}
249+
}
250+
240251
if err != nil {
241252
hostError = err.Error()
242253
}
@@ -250,13 +261,14 @@ func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
250261
}
251262

252263
stateQueryChan <- HostListItem{
253-
Name: h.Name,
254-
Active: isActive(currentState, url),
255-
DriverName: h.Driver.DriverName(),
256-
State: currentState,
257-
URL: url,
258-
SwarmOptions: swarmOptions,
259-
Error: hostError,
264+
Name: h.Name,
265+
Active: isActive(currentState, url),
266+
DriverName: h.Driver.DriverName(),
267+
State: currentState,
268+
URL: url,
269+
SwarmOptions: swarmOptions,
270+
DockerVersion: dockerVersion,
271+
Error: hostError,
260272
}
261273
}
262274

commands/ls_test.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/docker/machine/drivers/fakedriver"
1212
"github.com/docker/machine/libmachine/host"
13+
"github.com/docker/machine/libmachine/mcndockerclient"
1314
"github.com/docker/machine/libmachine/state"
1415
"github.com/docker/machine/libmachine/swarm"
1516
"github.com/stretchr/testify/assert"
@@ -296,6 +297,9 @@ func TestFilterHostsDifferentFlagsProduceAND(t *testing.T) {
296297
}
297298

298299
func TestGetHostListItems(t *testing.T) {
300+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{Version: "1.9"}
301+
defer mcndockerclient.CleanupDockerVersioner()
302+
299303
// TODO: Ideally this would mockable via interface instead.
300304
os.Setenv("DOCKER_HOST", "tcp://active.host.com:2376")
301305

@@ -331,14 +335,15 @@ func TestGetHostListItems(t *testing.T) {
331335
}
332336

333337
expected := []struct {
334-
name string
335-
state state.State
336-
active bool
337-
error string
338+
name string
339+
state state.State
340+
active bool
341+
version string
342+
error string
338343
}{
339-
{"bar10", state.Error, false, "Unable to get ip"},
340-
{"bar100", state.Stopped, false, ""},
341-
{"foo", state.Running, true, ""},
344+
{"bar10", state.Error, false, "Unknown", "Unable to get ip"},
345+
{"bar100", state.Stopped, false, "Unknown", ""},
346+
{"foo", state.Running, true, "v1.9", ""},
342347
}
343348

344349
items := getHostListItems(hosts, map[string]error{})
@@ -347,6 +352,7 @@ func TestGetHostListItems(t *testing.T) {
347352
assert.Equal(t, expected[i].name, items[i].Name)
348353
assert.Equal(t, expected[i].state, items[i].State)
349354
assert.Equal(t, expected[i].active, items[i].Active)
355+
assert.Equal(t, expected[i].version, items[i].DockerVersion)
350356
assert.Equal(t, expected[i].error, items[i].Error)
351357
}
352358

@@ -355,6 +361,9 @@ func TestGetHostListItems(t *testing.T) {
355361

356362
// issue #1908
357363
func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
364+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{Version: "1.9"}
365+
defer mcndockerclient.CleanupDockerVersioner()
366+
358367
orgDockerHost := os.Getenv("DOCKER_HOST")
359368
defer func() {
360369
// revert DOCKER_HOST
@@ -408,12 +417,13 @@ func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
408417
}
409418

410419
expected := map[string]struct {
411-
state state.State
412-
active bool
420+
state state.State
421+
active bool
422+
version string
413423
}{
414-
"foo": {state.Running, false},
415-
"bar": {state.Stopped, false},
416-
"baz": {state.Saved, false},
424+
"foo": {state.Running, false, "v1.9"},
425+
"bar": {state.Stopped, false, "Unknown"},
426+
"baz": {state.Saved, false, "Unknown"},
417427
}
418428

419429
items := getHostListItems(hosts, map[string]error{})
@@ -423,6 +433,7 @@ func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
423433

424434
assert.Equal(t, expected.state, item.State)
425435
assert.Equal(t, expected.active, item.Active)
436+
assert.Equal(t, expected.version, item.DockerVersion)
426437
}
427438
}
428439

@@ -463,7 +474,7 @@ func TestGetHostStateTimeout(t *testing.T) {
463474
},
464475
}
465476

466-
stateTimeoutDuration = 1 * time.Second
477+
stateTimeoutDuration = 1 * time.Millisecond
467478
hostItems := getHostListItems(hosts, map[string]error{})
468479
hostItem := hostItems[0]
469480

@@ -496,6 +507,9 @@ func TestGetHostStateError(t *testing.T) {
496507
}
497508

498509
func TestGetSomeHostInEror(t *testing.T) {
510+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{Version: "1.9"}
511+
defer mcndockerclient.CleanupDockerVersioner()
512+
499513
hosts := []*host.Host{
500514
{
501515
Name: "foo",

commands/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func cmdURL(c CommandLine, api libmachine.API) error {
1616
return err
1717
}
1818

19-
url, err := host.GetURL()
19+
url, err := host.URL()
2020
if err != nil {
2121
return err
2222
}

commands/version.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
package commands
22

3-
import "github.com/docker/machine/libmachine"
3+
import (
4+
"fmt"
5+
6+
"io"
7+
"os"
8+
9+
"github.com/docker/machine/libmachine"
10+
)
411

512
func cmdVersion(c CommandLine, api libmachine.API) error {
6-
c.ShowVersion()
13+
return printVersion(c, api, os.Stdout)
14+
}
15+
16+
func printVersion(c CommandLine, api libmachine.API, out io.Writer) error {
17+
if len(c.Args()) == 0 {
18+
c.ShowVersion()
19+
return nil
20+
}
21+
22+
if len(c.Args()) != 1 {
23+
return ErrExpectedOneMachine
24+
}
25+
26+
host, err := api.Load(c.Args().First())
27+
if err != nil {
28+
return err
29+
}
30+
31+
version, err := host.DockerVersion()
32+
if err != nil {
33+
return err
34+
}
35+
36+
fmt.Fprintln(out, version)
37+
738
return nil
839
}

commands/version_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"bytes"
7+
8+
"github.com/docker/machine/commands/commandstest"
9+
"github.com/docker/machine/drivers/vmwarevsphere/errors"
10+
"github.com/docker/machine/libmachine/host"
11+
"github.com/docker/machine/libmachine/libmachinetest"
12+
"github.com/docker/machine/libmachine/mcndockerclient"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestCmdVersion(t *testing.T) {
17+
commandLine := &commandstest.FakeCommandLine{}
18+
api := &libmachinetest.FakeAPI{}
19+
20+
err := cmdVersion(commandLine, api)
21+
22+
assert.True(t, commandLine.VersionShown)
23+
assert.NoError(t, err)
24+
}
25+
26+
func TestCmdVersionTooManyNames(t *testing.T) {
27+
commandLine := &commandstest.FakeCommandLine{
28+
CliArgs: []string{"machine1", "machine2"},
29+
}
30+
api := &libmachinetest.FakeAPI{}
31+
32+
err := cmdVersion(commandLine, api)
33+
34+
assert.EqualError(t, err, "Error: Expected one machine name as an argument")
35+
}
36+
37+
func TestCmdVersionNotFound(t *testing.T) {
38+
commandLine := &commandstest.FakeCommandLine{
39+
CliArgs: []string{"unknown"},
40+
}
41+
api := &libmachinetest.FakeAPI{}
42+
43+
err := cmdVersion(commandLine, api)
44+
45+
assert.EqualError(t, err, `Host does not exist: "unknown"`)
46+
}
47+
48+
func TestCmdVersionOnHost(t *testing.T) {
49+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{Version: "1.9.1"}
50+
defer mcndockerclient.CleanupDockerVersioner()
51+
52+
commandLine := &commandstest.FakeCommandLine{
53+
CliArgs: []string{"machine"},
54+
}
55+
api := &libmachinetest.FakeAPI{
56+
Hosts: []*host.Host{
57+
{
58+
Name: "machine",
59+
},
60+
},
61+
}
62+
63+
out := &bytes.Buffer{}
64+
err := printVersion(commandLine, api, out)
65+
66+
assert.NoError(t, err)
67+
assert.Equal(t, "1.9.1\n", out.String())
68+
}
69+
70+
func TestCmdVersionFailure(t *testing.T) {
71+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{Err: errors.New("connection failure")}
72+
defer mcndockerclient.CleanupDockerVersioner()
73+
74+
commandLine := &commandstest.FakeCommandLine{
75+
CliArgs: []string{"machine"},
76+
}
77+
api := &libmachinetest.FakeAPI{
78+
Hosts: []*host.Host{
79+
{
80+
Name: "machine",
81+
},
82+
},
83+
}
84+
85+
out := &bytes.Buffer{}
86+
err := printVersion(commandLine, api, out)
87+
88+
assert.EqualError(t, err, "connection failure")
89+
}

0 commit comments

Comments
 (0)
X Tutup