forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
106 lines (98 loc) · 2.39 KB
/
client_test.go
File metadata and controls
106 lines (98 loc) · 2.39 KB
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
package ssh
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetSSHCmdArgs(t *testing.T) {
cases := []struct {
binaryPath string
args []string
expectedArgs []string
}{
{
binaryPath: "/usr/local/bin/ssh",
args: []string{
"docker@localhost",
"apt-get install -y htop",
},
expectedArgs: []string{
"/usr/local/bin/ssh",
"docker@localhost",
"apt-get install -y htop",
},
},
{
binaryPath: "C:\\Program Files\\Git\\bin\\ssh.exe",
args: []string{
"docker@localhost",
"sudo /usr/bin/sethostname foobar && echo 'foobar' | sudo tee /var/lib/boot2docker/etc/hostname",
},
expectedArgs: []string{
"C:\\Program Files\\Git\\bin\\ssh.exe",
"docker@localhost",
"sudo /usr/bin/sethostname foobar && echo 'foobar' | sudo tee /var/lib/boot2docker/etc/hostname",
},
},
}
for _, c := range cases {
cmd := getSSHCmd(c.binaryPath, c.args...)
assert.Equal(t, cmd.Args, c.expectedArgs)
}
}
func TestNewExternalClient(t *testing.T) {
keyFile, err := ioutil.TempFile("", "docker-machine-tests-dummy-private-key")
if err != nil {
t.Fatal(err)
}
defer keyFile.Close()
keyFilename := keyFile.Name()
defer os.Remove(keyFilename)
cases := []struct {
sshBinaryPath string
user string
host string
port int
auth *Auth
perm os.FileMode
expectedError string
skipOS string
}{
{
auth: &Auth{Keys: []string{"/tmp/private-key-not-exist"}},
expectedError: "stat /tmp/private-key-not-exist: no such file or directory",
skipOS: "none",
},
{
auth: &Auth{Keys: []string{keyFilename}},
perm: 0400,
skipOS: "windows",
},
{
auth: &Auth{Keys: []string{keyFilename}},
perm: 0100,
expectedError: fmt.Sprintf("'%s' is not readable", keyFilename),
skipOS: "windows",
},
{
auth: &Auth{Keys: []string{keyFilename}},
perm: 0644,
expectedError: fmt.Sprintf("permissions 0644 for '%s' are too open", keyFilename),
skipOS: "windows",
},
}
for _, c := range cases {
if runtime.GOOS != c.skipOS {
keyFile.Chmod(c.perm)
_, err := NewExternalClient(c.sshBinaryPath, c.user, c.host, c.port, c.auth)
if c.expectedError != "" {
assert.EqualError(t, err, c.expectedError)
} else {
assert.Equal(t, err, nil)
}
}
}
}