forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscp_test.go
More file actions
136 lines (109 loc) · 3.05 KB
/
scp_test.go
File metadata and controls
136 lines (109 loc) · 3.05 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package commands
import (
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
type MockHostInfo struct {
name string
ip string
sshUsername string
sshKeyPath string
}
func (h *MockHostInfo) GetMachineName() string {
return h.name
}
func (h *MockHostInfo) GetIP() (string, error) {
return h.ip, nil
}
func (h *MockHostInfo) GetSSHUsername() string {
return h.sshUsername
}
func (h *MockHostInfo) GetSSHKeyPath() string {
return h.sshKeyPath
}
type MockHostInfoLoader struct {
hostInfo MockHostInfo
}
func (l *MockHostInfoLoader) load(name string) (HostInfo, error) {
info := l.hostInfo
info.name = name
return &info, nil
}
func TestGetInfoForLocalScpArg(t *testing.T) {
host, path, opts, err := getInfoForScpArg("/tmp/foo", nil)
assert.Nil(t, host)
assert.Equal(t, "/tmp/foo", path)
assert.Nil(t, opts)
assert.NoError(t, err)
host, path, opts, err = getInfoForScpArg("localhost:C:\\path", nil)
assert.Nil(t, host)
assert.Equal(t, "C:\\path", path)
assert.Nil(t, opts)
assert.NoError(t, err)
}
func TestGetInfoForRemoteScpArg(t *testing.T) {
hostInfoLoader := MockHostInfoLoader{MockHostInfo{
sshKeyPath: "/fake/keypath/id_rsa",
}}
host, path, opts, err := getInfoForScpArg("myfunhost:/home/docker/foo", &hostInfoLoader)
assert.Equal(t, "myfunhost", host.GetMachineName())
assert.Equal(t, "/home/docker/foo", path)
assert.Equal(t, []string{"-i", "/fake/keypath/id_rsa"}, opts)
assert.NoError(t, err)
host, path, opts, err = getInfoForScpArg("myfunhost:C:\\path", &hostInfoLoader)
assert.Equal(t, "myfunhost", host.GetMachineName())
assert.Equal(t, "C:\\path", path)
assert.NoError(t, err)
}
func TestHostLocation(t *testing.T) {
arg, err := generateLocationArg(nil, "/home/docker/foo")
assert.Equal(t, "/home/docker/foo", arg)
assert.NoError(t, err)
}
func TestRemoteLocation(t *testing.T) {
hostInfo := MockHostInfo{
ip: "12.34.56.78",
sshUsername: "root",
}
arg, err := generateLocationArg(&hostInfo, "/home/docker/foo")
assert.Equal(t, "root@12.34.56.78:/home/docker/foo", arg)
assert.NoError(t, err)
}
func TestGetScpCmd(t *testing.T) {
hostInfoLoader := MockHostInfoLoader{MockHostInfo{
ip: "12.34.56.78",
sshUsername: "root",
sshKeyPath: "/fake/keypath/id_rsa",
}}
cmd, err := getScpCmd("/tmp/foo", "myfunhost:/home/docker/foo", true, &hostInfoLoader)
expectedArgs := append(
baseSSHArgs,
"-3",
"-r",
"-i",
"/fake/keypath/id_rsa",
"/tmp/foo",
"root@12.34.56.78:/home/docker/foo",
)
expectedCmd := exec.Command("/usr/bin/scp", expectedArgs...)
assert.Equal(t, expectedCmd, cmd)
assert.NoError(t, err)
}
func TestGetScpCmdWithoutSshKey(t *testing.T) {
hostInfoLoader := MockHostInfoLoader{MockHostInfo{
ip: "1.2.3.4",
sshUsername: "user",
}}
cmd, err := getScpCmd("/tmp/foo", "myfunhost:/home/docker/foo", true, &hostInfoLoader)
expectedArgs := append(
baseSSHArgs,
"-3",
"-r",
"/tmp/foo",
"user@1.2.3.4:/home/docker/foo",
)
expectedCmd := exec.Command("/usr/bin/scp", expectedArgs...)
assert.Equal(t, expectedCmd, cmd)
assert.NoError(t, err)
}