|
| 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" |
| 9 | + "github.com/docker/machine/libmachine/host" |
| 10 | + "github.com/docker/machine/libmachine/libmachinetest" |
| 11 | + "github.com/docker/machine/libmachine/state" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCmdStop(t *testing.T) { |
| 16 | + testCases := []struct { |
| 17 | + commandLine CommandLine |
| 18 | + api libmachine.API |
| 19 | + expectedErr error |
| 20 | + expectedStates map[string]state.State |
| 21 | + }{ |
| 22 | + { |
| 23 | + commandLine: &commandstest.FakeCommandLine{ |
| 24 | + CliArgs: []string{}, |
| 25 | + }, |
| 26 | + api: &libmachinetest.FakeAPI{ |
| 27 | + Hosts: []*host.Host{ |
| 28 | + { |
| 29 | + Name: "default", |
| 30 | + Driver: &fakedriver.Driver{ |
| 31 | + MockState: state.Running, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + expectedErr: nil, |
| 37 | + expectedStates: map[string]state.State{ |
| 38 | + "default": state.Stopped, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + commandLine: &commandstest.FakeCommandLine{ |
| 43 | + CliArgs: []string{}, |
| 44 | + }, |
| 45 | + api: &libmachinetest.FakeAPI{ |
| 46 | + Hosts: []*host.Host{ |
| 47 | + { |
| 48 | + Name: "foobar", |
| 49 | + Driver: &fakedriver.Driver{ |
| 50 | + MockState: state.Running, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + expectedErr: ErrNoDefault, |
| 56 | + expectedStates: map[string]state.State{ |
| 57 | + "foobar": state.Running, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + commandLine: &commandstest.FakeCommandLine{ |
| 62 | + CliArgs: []string{"machineToStop1", "machineToStop2"}, |
| 63 | + }, |
| 64 | + api: &libmachinetest.FakeAPI{ |
| 65 | + Hosts: []*host.Host{ |
| 66 | + { |
| 67 | + Name: "machineToStop1", |
| 68 | + Driver: &fakedriver.Driver{ |
| 69 | + MockState: state.Running, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + Name: "machineToStop2", |
| 74 | + Driver: &fakedriver.Driver{ |
| 75 | + MockState: state.Running, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + Name: "machine", |
| 80 | + Driver: &fakedriver.Driver{ |
| 81 | + MockState: state.Running, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + expectedErr: nil, |
| 87 | + expectedStates: map[string]state.State{ |
| 88 | + "machineToStop1": state.Stopped, |
| 89 | + "machineToStop2": state.Stopped, |
| 90 | + "machine": state.Running, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tc := range testCases { |
| 96 | + err := cmdStop(tc.commandLine, tc.api) |
| 97 | + assert.Equal(t, tc.expectedErr, err) |
| 98 | + |
| 99 | + for hostName, expectedState := range tc.expectedStates { |
| 100 | + assert.Equal(t, expectedState, libmachinetest.State(tc.api, hostName)) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments