-
Notifications
You must be signed in to change notification settings - Fork 3
/
lope_test.go
563 lines (518 loc) · 9.99 KB
/
lope_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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package main
import (
"fmt"
"net/url"
"reflect"
"strings"
"testing"
)
var c = &config{
cmd: []string{"ls", "-lhatr"},
dockerSocket: "/var/run/docker.sock",
entrypoint: "/bin/sh",
blacklist: []string{""},
whitelist: []string{""},
home: "/home/lope",
image: "lopeImage",
instructions: []string{""},
workDir: "/lope",
paths: []string{
path(".vault-token"),
path(".aws/"),
path(".kube/"),
path(".ssh/"),
},
}
var l = lope{
cfg: c,
dockerfile: "imageName",
envs: []string{
"VAULT_ADDR=http://localhost:8200",
"VAULT_TOKEN=123456",
},
params: make([]string, 0),
}
func TestRunParams(t *testing.T) {
var tests = []struct {
description string
cmd []string
extra []string
image string
want []string
}{
{
"Run command with image and cmd",
[]string{"command"},
[]string{},
"imageName",
[]string{"imageName", "-c", "command"},
},
{
"Run command with image and multiple cmd args",
[]string{"command", "-arg"},
[]string{},
"imageName",
[]string{"imageName", "-c", "command -arg"},
},
{
"Run command with an extra arg",
[]string{"command", "-arg"},
[]string{"--ulimit 10"},
"imageName",
[]string{"--ulimit", "10", "imageName", "-c", "command -arg"},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.params = make([]string, 0)
l.cfg.cmd = test.cmd
l.cfg.image = test.image
extraArgs = test.extra
l.runParams()
got := l.params
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %q want %q", got, want)
}
})
}
}
func TestAddVolumes(t *testing.T) {
var tests = []struct {
description string
paths []string
home string
mount bool
docker bool
ssh bool
dir string
want string
}{
{
"Add the aws directory",
[]string{".aws"},
path("./test/"),
false,
false,
false,
"",
fmt.Sprintf("-v %v.aws:/root/.aws", path("./test/")),
},
{
"Don't add any directories if they don't exist",
[]string{".aws", ".not-exist"},
path("./test/"),
false,
false,
false,
"",
fmt.Sprintf("-v %v.aws:/root/.aws", path("./test/")),
},
{
"Don't add any directories if none are defined",
[]string{},
path("./test/"),
false,
false,
false,
"",
"",
},
{
"Mount the specified directory if -mount is set",
[]string{},
path("./test/"),
true,
false,
false,
"/home/user/pro/lope/",
"-v /home/user/pro/lope/:/lope",
},
{
"Add multiple directories",
[]string{".aws", ".kube"},
path("./test/"),
false,
false,
false,
"",
fmt.Sprintf("-v %v.aws:/root/.aws -v %v.kube:/root/.kube", path("./test/"), path("./test/")),
},
{
"Mount the docker socket",
[]string{},
path("./test/"),
false,
true,
false,
"",
"-v /var/run/docker.sock:/var/run/docker.sock",
},
{
"Mount ssh volumes if ssh is enabled",
[]string{},
path("./test/"),
false,
false,
true,
"",
"-v lope-ssh-agent:/ssh-agent",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.params = make([]string, 0)
l.cfg.home = test.home
l.cfg.paths = test.paths
l.cfg.mount = test.mount
l.cfg.dir = test.dir
l.cfg.docker = test.docker
l.cfg.ssh = test.ssh
l.addVolumes()
got := strings.Join(l.params, " ")
want := test.want
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}
}
func TestAddEnvVars(t *testing.T) {
var tests = []struct {
description string
envs []string
blacklist []string
whitelist []string
ssh bool
want string
}{
{
"Add an env var",
[]string{"ENV1=hello1"},
[]string{},
[]string{},
false,
"-e ENV1",
},
{
"Add multiple env vars",
[]string{"ENV1=hello1", "ENV2=hello2"},
[]string{},
[]string{},
false,
"-e ENV1 -e ENV2",
},
{
"Blacklist an env var",
[]string{"ENV1=hello1"},
[]string{"ENV1"},
[]string{},
false,
"",
},
{
"Whitelist an env var",
[]string{"ENV1=hello1", "ENV2=hello2", "NO=no"},
[]string{},
[]string{"ENV"},
false,
"-e ENV1 -e ENV2",
},
{
"Blacklist and whitelisting env vars",
[]string{"ENV1=hello1", "ENV2=hello2", "NO=no"},
[]string{"ENV1"},
[]string{"ENV"},
false,
"-e ENV2",
},
{
"Add the SSH auth socket if ssh is enabled",
[]string{},
[]string{},
[]string{},
true,
"-e SSH_AUTH_SOCK=/ssh-agent/ssh-agent.sock",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.params = make([]string, 0)
l.envs = test.envs
l.cfg.blacklist = test.blacklist
l.cfg.whitelist = test.whitelist
l.cfg.ssh = test.ssh
l.addEnvVars()
got := strings.Join(l.params, " ")
want := test.want
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}
}
func TestDefaultParams(t *testing.T) {
var tests = []struct {
description string
entrypoint string
tty bool
want string
}{
{
"Override the entrypoint",
"/bin/ohyeah",
false,
"docker run --rm --interactive --entrypoint /bin/ohyeah --workdir /lope --net host",
},
{
"Allocate a pseudo-TTY",
"/bin/ohyeah",
true,
"docker run --rm --interactive --entrypoint /bin/ohyeah --workdir /lope --net host --tty",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.params = make([]string, 0)
l.cfg.entrypoint = test.entrypoint
l.cfg.tty = test.tty
l.defaultParams()
got := strings.Join(l.params, " ")
want := test.want
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}
}
func TestCreateDockerfile(t *testing.T) {
var tests = []struct {
description string
image string
mount bool
addMount bool
addDocker bool
instructions []string
want []string
}{
{
"Create a basic dockerfile",
"imageName",
false,
true,
false,
[]string{""},
[]string{
"FROM imageName",
"ADD . /lope",
"",
},
},
{
"Dont ADD the files if mount is set",
"imageName",
true,
false,
false,
[]string{""},
[]string{
"FROM imageName",
"",
},
},
{
"Dont ADD the files if addMount is not set",
"imageName",
false,
false,
false,
[]string{""},
[]string{
"FROM imageName",
"",
},
},
{
"Create a dockerfile with extra instructions",
"imageName",
false,
false,
false,
[]string{
"RUN echo hello",
"RUN hello world",
},
[]string{
"FROM imageName",
"RUN echo hello",
"RUN hello world",
},
},
{
"Add docker binary to image",
"imageName",
false,
false,
true,
[]string{},
[]string{
"FROM imageName",
`RUN wget -q https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz && \`,
`tar xfv docker* && \`,
`mv docker/docker /usr/local/bin && \`,
`rm -rf docker/`,
},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.cfg.sourceImage = test.image
l.cfg.instructions = test.instructions
l.cfg.mount = test.mount
l.cfg.addMount = test.addMount
l.cfg.addDocker = test.addDocker
l.createDockerfile()
got := l.dockerfile
want := strings.Join(test.want, "\n")
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}
}
func TestUseSourceImage(t *testing.T) {
l.cfg.sourceImage = "canttouchthis"
l.cfg.addMount = false
l.cfg.instructions = []string{}
l.cfg.addDocker = false
l.createDockerfile()
got := l.cfg.image
want := l.cfg.sourceImage
if l.cfg.image != l.cfg.sourceImage {
t.Errorf("got %q want %q", got, want)
}
}
func TestUserAndGroupParams(t *testing.T) {
var tests = []struct {
description string
mount bool
os string
want string
}{
{
"--users is NOT set if mount is false",
false,
"",
"",
},
{
"--users is NOT set if mount is true but os isn't linux",
true,
"windows",
"",
},
{
"--users IS set if mount is true",
true,
"linux",
fmt.Sprintf("--user="),
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.params = make([]string, 0)
l.cfg.mount = test.mount
l.cfg.os = test.os
l.addUserAndGroup()
got := strings.Join(l.params, " ")
want := test.want
if !strings.HasPrefix(got, want) {
t.Errorf("got %q wanted prefix: %q", got, want)
}
})
}
}
func TestCleanEnvVars(t *testing.T) {
var tests = []struct {
description string
envs []string
want string
}{
{
"All env vars are valid",
[]string{
"TEST=hello",
},
"TEST=hello",
},
{
"Invalid env vars are stripped",
[]string{
"TEST=hello",
"T:EST=hello",
},
"TEST=hello",
},
{
"Only the key name is checked for invalid characters",
[]string{
"TEST=he:llo",
},
"TEST=he:llo",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.envs = test.envs
l.cleanEnvVars()
got := strings.Join(l.envs, ",")
want := test.want
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}
}
func TestCommandProxy(t *testing.T) {
var tests = []struct {
description string
enabled bool
port string
want string
}{
{
"No lope server env is added if it isn't enabled",
false,
"",
"",
},
{
"When lope server is enabled env var is exported",
true,
"8000",
"8000",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l.cfg.cmdProxy = test.enabled
l.cfg.cmdProxyPort = test.port
l.commandProxy()
got := ""
want := test.want
for _, e := range l.params {
split := strings.Split(e, "=")
if split[0] == "LOPE_PROXY_ADDR" {
addr, err := url.Parse(split[1])
if err != nil {
t.Errorf("got %q want %q", err, want)
}
got = addr.Port()
}
}
if got != want {
t.Errorf("got %q want %q", got, want)
}
})
}
}