Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit b9ef58f

Browse files
committed
commands/ssh: Add
1 parent 4bd0e5b commit b9ef58f

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

commands/ssh.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/docker/machine/libmachine"
7+
"github.com/docker/machine/libmachine/state"
8+
)
9+
10+
type errStateInvalidForSSH struct {
11+
HostName string
12+
}
13+
14+
func (e errStateInvalidForSSH) Error() string {
15+
return fmt.Sprintf("Error: Cannot run SSH command: Host %q is not running", e.HostName)
16+
}
17+
18+
func cmdSSH(c CommandLine, api libmachine.API) error {
19+
// Check for help flag -- Needed due to SkipFlagParsing
20+
firstArg := c.Args().First()
21+
if firstArg == "-help" || firstArg == "--help" || firstArg == "-h" {
22+
c.ShowHelp()
23+
return nil
24+
}
25+
26+
target, err := targetHost(c, api)
27+
if err != nil {
28+
return err
29+
}
30+
31+
host, err := api.Load(target)
32+
if err != nil {
33+
return err
34+
}
35+
36+
currentState, err := host.Driver.GetState()
37+
if err != nil {
38+
return err
39+
}
40+
41+
if currentState != state.Running {
42+
return errStateInvalidForSSH{host.Name}
43+
}
44+
45+
client, err := host.CreateSSHClient()
46+
if err != nil {
47+
return err
48+
}
49+
50+
return client.Shell(c.Args().Tail()...)
51+
}

commands/ssh_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)