Skip to content

Commit 3ddea9f

Browse files
Merge pull request docker-archive-public#2409 from dgageot/more-commands-tests
Add more command tests
2 parents 20f789a + 742cdb9 commit 3ddea9f

File tree

16 files changed

+447
-68
lines changed

16 files changed

+447
-68
lines changed

commands/commands_test.go

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,16 @@ package commands
22

33
import (
44
"errors"
5-
"os"
6-
"strings"
75
"testing"
86

7+
"github.com/docker/machine/commands/commandstest"
98
"github.com/docker/machine/drivers/fakedriver"
109
"github.com/docker/machine/libmachine/host"
1110
"github.com/docker/machine/libmachine/hosttest"
1211
"github.com/docker/machine/libmachine/state"
1312
"github.com/stretchr/testify/assert"
1413
)
1514

16-
var (
17-
stdout *os.File
18-
)
19-
20-
func init() {
21-
stdout = os.Stdout
22-
}
23-
24-
func cleanup() {
25-
os.Stdout = stdout
26-
}
27-
2815
func TestRunActionForeachMachine(t *testing.T) {
2916
// Assume a bunch of machines in randomly started or
3017
// stopped states.
@@ -95,29 +82,29 @@ func TestRunActionForeachMachine(t *testing.T) {
9582
}
9683

9784
func TestPrintIPEmptyGivenLocalEngine(t *testing.T) {
98-
defer cleanup()
99-
host, _ := hosttest.GetDefaultTestHost()
85+
stdoutGetter := commandstest.NewStdoutGetter()
86+
defer stdoutGetter.Stop()
10087

101-
out, w := captureStdout()
102-
103-
assert.Nil(t, printIP(host)())
104-
w.Close()
88+
host, _ := hosttest.GetDefaultTestHost()
89+
err := printIP(host)()
10590

106-
assert.Equal(t, "", strings.TrimSpace(<-out))
91+
assert.NoError(t, err)
92+
assert.Equal(t, "\n", stdoutGetter.Output())
10793
}
10894

10995
func TestPrintIPPrintsGivenRemoteEngine(t *testing.T) {
110-
defer cleanup()
111-
host, _ := hosttest.GetDefaultTestHost()
112-
host.Driver = &fakedriver.Driver{}
113-
114-
out, w := captureStdout()
96+
stdoutGetter := commandstest.NewStdoutGetter()
97+
defer stdoutGetter.Stop()
11598

116-
assert.Nil(t, printIP(host)())
117-
118-
w.Close()
99+
host, _ := hosttest.GetDefaultTestHost()
100+
host.Driver = &fakedriver.Driver{
101+
MockState: state.Running,
102+
MockIP: "1.2.3.4",
103+
}
104+
err := printIP(host)()
119105

120-
assert.Equal(t, "1.2.3.4", strings.TrimSpace(<-out))
106+
assert.NoError(t, err)
107+
assert.Equal(t, "1.2.3.4\n", stdoutGetter.Output())
121108
}
122109

123110
func TestConsolidateError(t *testing.T) {

commands/commandstest/fake_command_line.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func (fcli *FakeCommandLine) Int(key string) int {
5555
}
5656

5757
func (fcli *FakeCommandLine) Bool(key string) bool {
58+
if fcli.LocalFlags == nil {
59+
return false
60+
}
5861
return fcli.LocalFlags.Bool(key)
5962
}
6063

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package commandstest
2+
3+
import (
4+
"github.com/docker/machine/libmachine"
5+
"github.com/docker/machine/libmachine/drivers"
6+
"github.com/docker/machine/libmachine/host"
7+
"github.com/docker/machine/libmachine/mcnerror"
8+
"github.com/docker/machine/libmachine/state"
9+
)
10+
11+
type FakeLibmachineAPI struct {
12+
Hosts []*host.Host
13+
}
14+
15+
func (api *FakeLibmachineAPI) NewPluginDriver(string, []byte) (drivers.Driver, error) {
16+
return nil, nil
17+
}
18+
19+
func (api *FakeLibmachineAPI) NewHost(driver drivers.Driver) (*host.Host, error) {
20+
return nil, nil
21+
}
22+
23+
func (api *FakeLibmachineAPI) Create(h *host.Host) error {
24+
return nil
25+
}
26+
27+
func (api *FakeLibmachineAPI) Exists(name string) (bool, error) {
28+
for _, host := range api.Hosts {
29+
if name == host.Name {
30+
return true, nil
31+
}
32+
}
33+
34+
return false, nil
35+
}
36+
37+
func (api *FakeLibmachineAPI) List() ([]string, error) {
38+
return []string{}, nil
39+
}
40+
41+
func (api *FakeLibmachineAPI) Load(name string) (*host.Host, error) {
42+
for _, host := range api.Hosts {
43+
if name == host.Name {
44+
return host, nil
45+
}
46+
}
47+
48+
return nil, mcnerror.ErrHostDoesNotExist{
49+
Name: name,
50+
}
51+
}
52+
53+
func (api *FakeLibmachineAPI) Remove(name string) error {
54+
newHosts := []*host.Host{}
55+
56+
for _, host := range api.Hosts {
57+
if name != host.Name {
58+
newHosts = append(newHosts, host)
59+
}
60+
}
61+
62+
api.Hosts = newHosts
63+
64+
return nil
65+
}
66+
67+
func (api *FakeLibmachineAPI) Save(host *host.Host) error {
68+
return nil
69+
}
70+
71+
func State(api libmachine.API, name string) state.State {
72+
host, _ := api.Load(name)
73+
machineState, _ := host.Driver.GetState()
74+
return machineState
75+
}
76+
77+
func Exists(api libmachine.API, name string) bool {
78+
exists, _ := api.Exists(name)
79+
return exists
80+
}

commands/commandstest/fake_machine_cli_client.go

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package commandstest
2+
3+
import (
4+
"bytes"
5+
"io"
6+
7+
"os"
8+
)
9+
10+
var (
11+
stdout *os.File
12+
)
13+
14+
func init() {
15+
stdout = os.Stdout
16+
}
17+
18+
type StdoutGetter interface {
19+
Output() string
20+
Stop()
21+
}
22+
23+
type stdoutCapturer struct {
24+
stdout *os.File
25+
output chan string
26+
}
27+
28+
func NewStdoutGetter() StdoutGetter {
29+
r, w, _ := os.Pipe()
30+
os.Stdout = w
31+
32+
output := make(chan string)
33+
go func() {
34+
var testOutput bytes.Buffer
35+
io.Copy(&testOutput, r)
36+
output <- testOutput.String()
37+
}()
38+
39+
return &stdoutCapturer{
40+
stdout: w,
41+
output: output,
42+
}
43+
}
44+
45+
func (c *stdoutCapturer) Output() string {
46+
c.stdout.Close()
47+
text := <-c.output
48+
close(c.output)
49+
return text
50+
}
51+
52+
func (c *stdoutCapturer) Stop() {
53+
os.Stdout = stdout
54+
}

commands/env_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/machine/libmachine/host"
1515
"github.com/docker/machine/libmachine/libmachinetest"
1616
"github.com/docker/machine/libmachine/persist/persisttest"
17+
"github.com/docker/machine/libmachine/state"
1718
"github.com/stretchr/testify/assert"
1819
)
1920

@@ -272,8 +273,11 @@ func TestShellCfgSet(t *testing.T) {
272273
FakeStore: &persisttest.FakeStore{
273274
Hosts: []*host.Host{
274275
{
275-
Name: "quux",
276-
Driver: &fakedriver.Driver{},
276+
Name: "quux",
277+
Driver: &fakedriver.Driver{
278+
MockState: state.Running,
279+
MockIP: "1.2.3.4",
280+
},
277281
},
278282
},
279283
},
@@ -315,8 +319,11 @@ func TestShellCfgSet(t *testing.T) {
315319
FakeStore: &persisttest.FakeStore{
316320
Hosts: []*host.Host{
317321
{
318-
Name: "quux",
319-
Driver: &fakedriver.Driver{},
322+
Name: "quux",
323+
Driver: &fakedriver.Driver{
324+
MockState: state.Running,
325+
MockIP: "1.2.3.4",
326+
},
320327
},
321328
},
322329
},

commands/ip_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/machine/commands/commandstest"
7+
"github.com/docker/machine/drivers/fakedriver"
8+
"github.com/docker/machine/libmachine/host"
9+
"github.com/docker/machine/libmachine/state"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestCmdIPMissingMachineName(t *testing.T) {
14+
commandLine := &commandstest.FakeCommandLine{}
15+
api := &commandstest.FakeLibmachineAPI{}
16+
17+
err := cmdURL(commandLine, api)
18+
19+
assert.EqualError(t, err, "Error: Expected one machine name as an argument")
20+
}
21+
22+
func TestCmdIP(t *testing.T) {
23+
commandLine := &commandstest.FakeCommandLine{
24+
CliArgs: []string{"machine"},
25+
}
26+
api := &commandstest.FakeLibmachineAPI{
27+
Hosts: []*host.Host{
28+
{
29+
Name: "machine",
30+
Driver: &fakedriver.Driver{
31+
MockState: state.Running,
32+
MockIP: "1.2.3.4",
33+
},
34+
},
35+
},
36+
}
37+
38+
stdoutGetter := commandstest.NewStdoutGetter()
39+
defer stdoutGetter.Stop()
40+
41+
err := cmdIP(commandLine, api)
42+
43+
assert.NoError(t, err)
44+
assert.Equal(t, "1.2.3.4\n", stdoutGetter.Output())
45+
}

commands/kill_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/machine/commands/commandstest"
7+
"github.com/docker/machine/drivers/fakedriver"
8+
"github.com/docker/machine/libmachine/host"
9+
"github.com/docker/machine/libmachine/state"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestCmdKillMissingMachineName(t *testing.T) {
14+
commandLine := &commandstest.FakeCommandLine{}
15+
api := &commandstest.FakeLibmachineAPI{}
16+
17+
err := cmdKill(commandLine, api)
18+
19+
assert.EqualError(t, err, "Error: Expected to get one or more machine names as arguments")
20+
}
21+
22+
func TestCmdKill(t *testing.T) {
23+
commandLine := &commandstest.FakeCommandLine{
24+
CliArgs: []string{"machineToKill1", "machineToKill2"},
25+
}
26+
api := &commandstest.FakeLibmachineAPI{
27+
Hosts: []*host.Host{
28+
{
29+
Name: "machineToKill1",
30+
Driver: &fakedriver.Driver{
31+
MockState: state.Running,
32+
},
33+
},
34+
{
35+
Name: "machineToKill2",
36+
Driver: &fakedriver.Driver{
37+
MockState: state.Running,
38+
},
39+
},
40+
{
41+
Name: "machine",
42+
Driver: &fakedriver.Driver{
43+
MockState: state.Running,
44+
},
45+
},
46+
},
47+
}
48+
49+
err := cmdKill(commandLine, api)
50+
assert.NoError(t, err)
51+
52+
assert.Equal(t, state.Stopped, commandstest.State(api, "machineToKill1"))
53+
assert.Equal(t, state.Stopped, commandstest.State(api, "machineToKill2"))
54+
assert.Equal(t, state.Running, commandstest.State(api, "machine"))
55+
}

0 commit comments

Comments
 (0)