forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands_test.go
243 lines (204 loc) · 5.51 KB
/
commands_test.go
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
package commands
import (
"errors"
"flag"
"testing"
"github.com/codegangsta/cli"
"github.com/docker/machine/commands/commandstest"
"github.com/docker/machine/drivers/fakedriver"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/crashreport"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/hosttest"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/state"
"github.com/stretchr/testify/assert"
)
func TestRunActionForeachMachine(t *testing.T) {
defer provision.SetDetector(&provision.StandardDetector{})
provision.SetDetector(&provision.FakeDetector{
Provisioner: provision.NewNetstatProvisioner(),
})
// Assume a bunch of machines in randomly started or
// stopped states.
machines := []*host.Host{
{
Name: "foo",
DriverName: "fakedriver",
Driver: &fakedriver.Driver{
MockState: state.Running,
},
},
{
Name: "bar",
DriverName: "fakedriver",
Driver: &fakedriver.Driver{
MockState: state.Stopped,
},
},
{
Name: "baz",
// Ssh, don't tell anyone but this
// driver only _thinks_ it's named
// virtualbox... (to test serial actions)
// It's actually FakeDriver!
DriverName: "virtualbox",
Driver: &fakedriver.Driver{
MockState: state.Stopped,
},
},
{
Name: "spam",
DriverName: "virtualbox",
Driver: &fakedriver.Driver{
MockState: state.Running,
},
},
{
Name: "eggs",
DriverName: "fakedriver",
Driver: &fakedriver.Driver{
MockState: state.Stopped,
},
},
{
Name: "ham",
DriverName: "fakedriver",
Driver: &fakedriver.Driver{
MockState: state.Running,
},
},
}
runActionForeachMachine("start", machines)
for _, machine := range machines {
machineState, _ := machine.Driver.GetState()
assert.Equal(t, state.Running, machineState)
}
runActionForeachMachine("stop", machines)
for _, machine := range machines {
machineState, _ := machine.Driver.GetState()
assert.Equal(t, state.Stopped, machineState)
}
}
func TestPrintIPEmptyGivenLocalEngine(t *testing.T) {
stdoutGetter := commandstest.NewStdoutGetter()
defer stdoutGetter.Stop()
host, _ := hosttest.GetDefaultTestHost()
err := printIP(host)()
assert.NoError(t, err)
assert.Equal(t, "\n", stdoutGetter.Output())
}
func TestPrintIPPrintsGivenRemoteEngine(t *testing.T) {
stdoutGetter := commandstest.NewStdoutGetter()
defer stdoutGetter.Stop()
host, _ := hosttest.GetDefaultTestHost()
host.Driver = &fakedriver.Driver{
MockState: state.Running,
MockIP: "1.2.3.4",
}
err := printIP(host)()
assert.NoError(t, err)
assert.Equal(t, "1.2.3.4\n", stdoutGetter.Output())
}
func TestConsolidateError(t *testing.T) {
cases := []struct {
inputErrs []error
expectedErr error
}{
{
inputErrs: []error{
errors.New("Couldn't remove host 'bar'"),
},
expectedErr: errors.New("Couldn't remove host 'bar'"),
},
{
inputErrs: []error{
errors.New("Couldn't remove host 'bar'"),
errors.New("Couldn't remove host 'foo'"),
},
expectedErr: errors.New("Couldn't remove host 'bar'\nCouldn't remove host 'foo'"),
},
}
for _, c := range cases {
assert.Equal(t, c.expectedErr, consolidateErrs(c.inputErrs))
}
}
type MockCrashReporter struct {
sent bool
}
func (m *MockCrashReporter) Send(err crashreport.CrashError) error {
m.sent = true
return nil
}
func TestSendCrashReport(t *testing.T) {
defer func(fnOsExit func(code int)) { osExit = fnOsExit }(osExit)
osExit = func(code int) {}
defer func(factory func(baseDir string, apiKey string) crashreport.CrashReporter) {
crashreport.NewCrashReporter = factory
}(crashreport.NewCrashReporter)
tests := []struct {
description string
err error
sent bool
}{
{
description: "Should send crash error",
err: crashreport.CrashError{
Cause: errors.New("BUG"),
Command: "command",
Context: "context",
DriverName: "virtualbox",
},
sent: true,
},
{
description: "Should not send standard error",
err: errors.New("BUG"),
sent: false,
},
}
for _, test := range tests {
mockCrashReporter := &MockCrashReporter{}
crashreport.NewCrashReporter = func(baseDir string, apiKey string) crashreport.CrashReporter {
return mockCrashReporter
}
command := func(commandLine CommandLine, api libmachine.API) error {
return test.err
}
context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
runCommand(command)(context)
assert.Equal(t, test.sent, mockCrashReporter.sent, test.description)
}
}
func TestReturnExitCode1onError(t *testing.T) {
command := func(commandLine CommandLine, api libmachine.API) error {
return errors.New("foo is not bar")
}
exitCode := checkErrorCodeForCommand(command)
assert.Equal(t, 1, exitCode)
}
func TestReturnExitCode3onErrorDuringPreCreate(t *testing.T) {
command := func(commandLine CommandLine, api libmachine.API) error {
return crashreport.CrashError{
Cause: mcnerror.ErrDuringPreCreate{
Cause: errors.New("foo is not bar"),
},
}
}
exitCode := checkErrorCodeForCommand(command)
assert.Equal(t, 3, exitCode)
}
func checkErrorCodeForCommand(command func(commandLine CommandLine, api libmachine.API) error) int {
var setExitCode int
originalOSExit := osExit
defer func() {
osExit = originalOSExit
}()
osExit = func(code int) {
setExitCode = code
}
context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)
runCommand(command)(context)
return setExitCode
}