|
| 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/drivers" |
| 10 | + "github.com/docker/machine/libmachine/host" |
| 11 | + "github.com/docker/machine/libmachine/libmachinetest" |
| 12 | + "github.com/docker/machine/libmachine/ssh" |
| 13 | + "github.com/docker/machine/libmachine/ssh/sshtest" |
| 14 | + "github.com/docker/machine/libmachine/state" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +type FakeSSHClientCreator struct { |
| 19 | + client ssh.Client |
| 20 | +} |
| 21 | + |
| 22 | +func (fsc *FakeSSHClientCreator) CreateSSHClient(d drivers.Driver) (ssh.Client, error) { |
| 23 | + if fsc.client == nil { |
| 24 | + fsc.client = &sshtest.FakeClient{} |
| 25 | + } |
| 26 | + return fsc.client, nil |
| 27 | +} |
| 28 | + |
| 29 | +func TestCmdSSH(t *testing.T) { |
| 30 | + testCases := []struct { |
| 31 | + commandLine CommandLine |
| 32 | + api libmachine.API |
| 33 | + expectedErr error |
| 34 | + helpShown bool |
| 35 | + clientCreator host.SSHClientCreator |
| 36 | + expectedShell []string |
| 37 | + }{ |
| 38 | + { |
| 39 | + commandLine: &commandstest.FakeCommandLine{ |
| 40 | + CliArgs: []string{"-h"}, |
| 41 | + }, |
| 42 | + api: &libmachinetest.FakeAPI{}, |
| 43 | + expectedErr: nil, |
| 44 | + helpShown: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + commandLine: &commandstest.FakeCommandLine{ |
| 48 | + CliArgs: []string{"--help"}, |
| 49 | + }, |
| 50 | + api: &libmachinetest.FakeAPI{}, |
| 51 | + expectedErr: nil, |
| 52 | + helpShown: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + commandLine: &commandstest.FakeCommandLine{ |
| 56 | + CliArgs: []string{}, |
| 57 | + }, |
| 58 | + api: &libmachinetest.FakeAPI{}, |
| 59 | + expectedErr: ErrNoDefault, |
| 60 | + }, |
| 61 | + { |
| 62 | + commandLine: &commandstest.FakeCommandLine{ |
| 63 | + CliArgs: []string{"default", "df", "-h"}, |
| 64 | + }, |
| 65 | + api: &libmachinetest.FakeAPI{ |
| 66 | + Hosts: []*host.Host{ |
| 67 | + { |
| 68 | + Name: "default", |
| 69 | + Driver: &fakedriver.Driver{ |
| 70 | + MockState: state.Running, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + expectedErr: nil, |
| 76 | + clientCreator: &FakeSSHClientCreator{}, |
| 77 | + expectedShell: []string{"df", "-h"}, |
| 78 | + }, |
| 79 | + { |
| 80 | + commandLine: &commandstest.FakeCommandLine{ |
| 81 | + CliArgs: []string{"default"}, |
| 82 | + }, |
| 83 | + api: &libmachinetest.FakeAPI{ |
| 84 | + Hosts: []*host.Host{ |
| 85 | + { |
| 86 | + Name: "default", |
| 87 | + Driver: &fakedriver.Driver{ |
| 88 | + MockState: state.Stopped, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + expectedErr: errStateInvalidForSSH{"default"}, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tc := range testCases { |
| 98 | + host.SetSSHClientCreator(tc.clientCreator) |
| 99 | + |
| 100 | + err := cmdSSH(tc.commandLine, tc.api) |
| 101 | + assert.Equal(t, err, tc.expectedErr) |
| 102 | + |
| 103 | + if fcl, ok := tc.commandLine.(*commandstest.FakeCommandLine); ok { |
| 104 | + assert.Equal(t, tc.helpShown, fcl.HelpShown) |
| 105 | + } |
| 106 | + |
| 107 | + if fcc, ok := tc.clientCreator.(*FakeSSHClientCreator); ok { |
| 108 | + assert.Equal(t, tc.expectedShell, fcc.client.(*sshtest.FakeClient).ActivatedShell) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments