-
Notifications
You must be signed in to change notification settings - Fork 1
/
scp_test.go
433 lines (400 loc) · 11.8 KB
/
scp_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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// SPDX-License-Identifier: Apache-2.0
package scp
import (
"errors"
"strings"
"testing"
"github.com/spf13/afero"
"github.com/go-vela/vela-openssh/internal/openssh"
"github.com/go-vela/vela-openssh/internal/testutils"
)
const (
mockTarget = "some-user@some-host:~"
)
var (
mockSource = []string{
"local-file",
"remote-user@remote-host:~/some/path",
"scp://another-user@another-host:1234/some/other/path",
}
)
func TestValidateSuccess(t *testing.T) {
tests := map[string]Config{
"returns no errors when properly configured": {
Source: mockSource,
Target: mockTarget,
},
"returns no errors when using an SSH Password": {
Source: mockSource,
Target: mockTarget,
SSHPassword: testutils.MockSSHPassword,
},
"returns no errors when using an SSH Passphrase": {
Source: mockSource,
Target: mockTarget,
SSHPassphrase: testutils.MockSSHPassphrase,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if err := test.Validate(); err != nil {
t.Errorf("Validate() should not have raised error %q", err)
}
})
}
}
func TestValidateErrors(t *testing.T) {
tests := map[string]struct {
config Config
wantErr error
}{
"with everything missing": {},
"with source missing": {
wantErr: ErrMissingSource,
},
"with target missing": {
config: Config{
Source: mockSource,
},
wantErr: ErrMissingTarget,
},
"with password and passphrase set": {
config: Config{
Source: mockSource,
Target: mockTarget,
SSHPassword: testutils.MockSSHPassword,
SSHPassphrase: testutils.MockSSHPassphrase,
},
wantErr: openssh.ErrAmbiguousAuth,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if err := test.config.Validate(); err == nil {
t.Errorf("Validate() should have raised an error")
} else if (test.wantErr != nil) && !errors.Is(err, test.wantErr) {
t.Errorf("Validate() returned wrong error\ngot: %s\nwanted: %s", err, test.wantErr)
}
})
}
}
func TestSetupSuccess(t *testing.T) {
tests := map[string]struct {
config Config
mockFS afero.Fs
}{
"sets default FS if not set": {
config: Config{},
},
"can find binaries in common locations": {
config: Config{},
mockFS: testutils.CreateMockFiles(t, "./scp", "/usr/local/bin/ssh", "/usr/bin/sshpass"),
},
"can find binaries in common locations pt2": {
config: Config{},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
},
"creates identity file from raw string and sets permissions and is default first identity file": {
config: Config{
IdentityFileContents: testutils.MockIdentityFileContents,
},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
},
"creates password file and saves temp location": {
config: Config{
SSHPassword: testutils.MockSSHPassword,
},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
},
"creates passphrase file and saves temp location": {
config: Config{
SSHPassphrase: testutils.MockSSHPassphrase,
},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.mockFS != nil {
test.config.fs = test.mockFS
}
if err := test.config.Setup(); err != nil && test.mockFS != nil {
t.Errorf("Setup() should not have raised error %q", err)
t.FailNow()
}
if test.config.fs == nil {
t.Error("Setup() should have created default file system")
t.FailNow()
}
if len(test.config.IdentityFileContents) > 0 {
if len(test.config.IdentityFilePath) == 0 || !strings.Contains(test.config.IdentityFilePath[0], openssh.TempFileDirectory) {
t.Error("Setup() did not add file first in the IdentityFile slice")
t.FailNow()
}
testutils.ValidateMockFile(t, test.mockFS, test.config.IdentityFilePath[0], test.config.IdentityFileContents)
}
if test.config.SSHPassword != "" {
if len(test.config.locationPasswordFile) == 0 {
t.Error("Setup() did not set password file location")
t.FailNow()
}
testutils.ValidateMockFile(t, test.mockFS, test.config.locationPasswordFile, test.config.SSHPassword)
}
if test.config.SSHPassphrase != "" {
if len(test.config.locationPassphraseFile) == 0 {
t.Error("Setup() did not set passphrase file location")
t.FailNow()
}
testutils.ValidateMockFile(t, test.mockFS, test.config.locationPassphraseFile, test.config.SSHPassphrase)
}
})
}
}
func TestSetupErrors(t *testing.T) {
tests := map[string]struct {
config Config
mockFS afero.Fs
wantErr error
}{
"when scp binary missing": {
mockFS: testutils.CreateMockFiles(t, testutils.MockSSHPath, testutils.MockSSHPassPath),
wantErr: openssh.ErrMissingSCP,
},
"when ssh binary missing": {
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPassPath),
wantErr: openssh.ErrMissingSSH,
},
"when sshpass binary missing": {
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath),
wantErr: openssh.ErrMissingSSHPASS,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.mockFS != nil {
test.config.fs = test.mockFS
}
if err := test.config.Setup(); err == nil {
t.Errorf("Setup() should have raised an error")
} else if test.wantErr != nil && err != nil && !errors.Is(err, test.wantErr) {
t.Errorf("Setup() returned wrong error\ngot: %s\nwanted: %s", err, test.wantErr)
}
})
}
}
func TestBinary(t *testing.T) {
tests := map[string]struct {
config Config
mockFS afero.Fs
wantSCP bool
}{
"uses scp by default": {
config: Config{},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
wantSCP: true,
},
"uses sshpass when sshpass flags set": {
config: Config{
SSHPASSFlags: []string{"-v"},
},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
wantSCP: false,
},
"uses sshpass when ssh password set": {
config: Config{
SSHPassword: testutils.MockSSHPassword,
},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
wantSCP: false,
},
"uses sshpass when ssh passphrase set": {
config: Config{
SSHPassphrase: testutils.MockSSHPassphrase,
},
mockFS: testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath),
wantSCP: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.mockFS != nil {
test.config.fs = test.mockFS
}
if err := test.config.Setup(); err != nil && test.mockFS != nil {
t.Errorf("Setup() should not have raised error %q", err)
t.FailNow()
}
if test.wantSCP && test.config.Binary() != testutils.MockSCPPath {
t.Errorf("Binary() should have return scp location")
} else if !test.wantSCP && test.config.Binary() != testutils.MockSSHPassPath {
t.Errorf("Binary() should have return sshpass location")
}
})
}
}
func TestArguments(t *testing.T) {
tests := map[string]struct {
config Config
wantCommand []string
}{
"basic scp usage": {
config: Config{
Source: mockSource,
Target: mockTarget,
},
wantCommand: testutils.FlattenArguments(
testutils.MockSCPPath,
openssh.DefaultSCPFlags,
mockSource,
mockTarget,
),
},
"basic sshpass usage": {
config: Config{
Source: mockSource,
Target: mockTarget,
SSHPASSFlags: []string{"-h"},
},
wantCommand: testutils.FlattenArguments(
testutils.MockSSHPassPath,
"-h",
testutils.MockSCPPath,
openssh.DefaultSCPFlags,
mockSource,
mockTarget,
),
},
"custom scp flags override defaults": {
config: Config{
Source: mockSource,
Target: mockTarget,
SCPFlags: []string{"-h"},
},
wantCommand: testutils.FlattenArguments(
testutils.MockSCPPath,
"-h",
mockSource,
mockTarget,
),
},
"custom sshpass flags override defaults": {
config: Config{
Source: mockSource,
Target: mockTarget,
SSHPASSFlags: []string{"-v"},
},
wantCommand: testutils.FlattenArguments(
testutils.MockSSHPassPath,
"-v",
testutils.MockSCPPath,
openssh.DefaultSCPFlags,
mockSource,
mockTarget,
),
},
"ssh password sets file path": {
config: Config{
Source: mockSource,
Target: mockTarget,
SSHPassword: testutils.MockSSHPassword,
},
wantCommand: testutils.FlattenArguments(
testutils.MockSSHPassPath,
openssh.DefaultSSHPassFlags,
"-f", "/tmp/vela-plugin-openssh-password-file-",
testutils.MockSCPPath,
openssh.DefaultSCPFlags,
mockSource,
mockTarget,
),
},
"ssh passphrase sets file path": {
config: Config{
Source: mockSource,
Target: mockTarget,
SSHPassphrase: testutils.MockSSHPassphrase,
},
wantCommand: testutils.FlattenArguments(
testutils.MockSSHPassPath,
openssh.DefaultSSHPassFlags,
"-Passphrase",
"-f", "/tmp/vela-plugin-openssh-passphrase-file-",
testutils.MockSCPPath,
openssh.DefaultSCPFlags,
mockSource,
mockTarget,
),
},
"multiple identity files set with identity contents and scp flags": {
config: Config{
Source: mockSource,
Target: mockTarget,
IdentityFilePath: []string{"~/.ssh/id_rsa", "$HOME/.ssh/id_dsa"},
IdentityFileContents: testutils.MockIdentityFileContents,
SCPFlags: []string{"-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null"},
},
wantCommand: testutils.FlattenArguments(
testutils.MockSCPPath,
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"-i", "/tmp/vela-plugin-openssh-identity-file-",
"-i", "~/.ssh/id_rsa",
"-i", "$HOME/.ssh/id_dsa",
mockSource,
mockTarget,
),
},
"everything all at once": {
config: Config{
Source: mockSource,
Target: mockTarget,
IdentityFilePath: []string{"~/.ssh/id_rsa", "$HOME/.ssh/id_dsa"},
IdentityFileContents: testutils.MockIdentityFileContents,
SCPFlags: []string{"-o", "StrictHostKeyChecking=yes"},
SSHPassphrase: testutils.MockSSHPassphrase,
SSHPASSFlags: []string{"-v"},
},
wantCommand: testutils.FlattenArguments(
testutils.MockSSHPassPath,
"-v",
"-Passphrase",
"-f", "/tmp/vela-plugin-openssh-passphrase-file-",
testutils.MockSCPPath,
"-o", "StrictHostKeyChecking=yes",
"-i", "/tmp/vela-plugin-openssh-identity-file-",
"-i", "~/.ssh/id_rsa",
"-i", "$HOME/.ssh/id_dsa",
mockSource,
mockTarget,
),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
test.config.fs = testutils.CreateMockFiles(t, testutils.MockSCPPath, testutils.MockSSHPath, testutils.MockSSHPassPath)
if err := test.config.Validate(); err != nil {
t.Errorf("Validate() should not have raised error %q", err)
t.FailNow()
}
if err := test.config.Setup(); err != nil {
t.Errorf("Setup() should not have raised error %q", err)
t.FailNow()
}
if !testutils.ArgCompare(test.wantCommand, test.config.Arguments()) {
t.Errorf("arguments mismatched\ngot: %s\nwanted: %s", test.config.Arguments(), test.wantCommand)
}
})
}
}
func TestEnvironment(t *testing.T) {
c := &Config{}
env := c.Environment()
if len(env) == 0 {
t.Errorf("Environment() should not be empty")
t.FailNow()
}
if len(env["VELA_SCP_PLUGIN_VERSION"]) == 0 {
t.Errorf("Environment() VELA_SCP_PLUGIN_VERSION should be set")
t.FailNow()
}
}