forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_test.go
111 lines (102 loc) · 2.6 KB
/
ssh_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
package commands
import (
"testing"
"github.com/docker/machine/commands/commandstest"
"github.com/docker/machine/drivers/fakedriver"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/libmachinetest"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/ssh/sshtest"
"github.com/docker/machine/libmachine/state"
"github.com/stretchr/testify/assert"
)
type FakeSSHClientCreator struct {
client ssh.Client
}
func (fsc *FakeSSHClientCreator) CreateSSHClient(d drivers.Driver) (ssh.Client, error) {
if fsc.client == nil {
fsc.client = &sshtest.FakeClient{}
}
return fsc.client, nil
}
func TestCmdSSH(t *testing.T) {
testCases := []struct {
commandLine CommandLine
api libmachine.API
expectedErr error
helpShown bool
clientCreator host.SSHClientCreator
expectedShell []string
}{
{
commandLine: &commandstest.FakeCommandLine{
CliArgs: []string{"-h"},
},
api: &libmachinetest.FakeAPI{},
expectedErr: nil,
helpShown: true,
},
{
commandLine: &commandstest.FakeCommandLine{
CliArgs: []string{"--help"},
},
api: &libmachinetest.FakeAPI{},
expectedErr: nil,
helpShown: true,
},
{
commandLine: &commandstest.FakeCommandLine{
CliArgs: []string{},
},
api: &libmachinetest.FakeAPI{},
expectedErr: ErrNoDefault,
},
{
commandLine: &commandstest.FakeCommandLine{
CliArgs: []string{"default", "df", "-h"},
},
api: &libmachinetest.FakeAPI{
Hosts: []*host.Host{
{
Name: "default",
Driver: &fakedriver.Driver{
MockState: state.Running,
},
},
},
},
expectedErr: nil,
clientCreator: &FakeSSHClientCreator{},
expectedShell: []string{"df", "-h"},
},
{
commandLine: &commandstest.FakeCommandLine{
CliArgs: []string{"default"},
},
api: &libmachinetest.FakeAPI{
Hosts: []*host.Host{
{
Name: "default",
Driver: &fakedriver.Driver{
MockState: state.Stopped,
},
},
},
},
expectedErr: errStateInvalidForSSH{"default"},
},
}
for _, tc := range testCases {
host.SetSSHClientCreator(tc.clientCreator)
err := cmdSSH(tc.commandLine, tc.api)
assert.Equal(t, err, tc.expectedErr)
if fcl, ok := tc.commandLine.(*commandstest.FakeCommandLine); ok {
assert.Equal(t, tc.helpShown, fcl.HelpShown)
}
if fcc, ok := tc.clientCreator.(*FakeSSHClientCreator); ok {
assert.Equal(t, tc.expectedShell, fcc.client.(*sshtest.FakeClient).ActivatedShell)
}
}
}