forked from cyverse-de/interapps-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcompose_test.go
401 lines (387 loc) · 11 KB
/
dcompose_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
package main
import (
"reflect"
"testing"
"github.com/spf13/viper"
"gopkg.in/cyverse-de/model.v3"
yaml "gopkg.in/yaml.v2"
)
var testJob = &model.Job{
ID: "test-job-id",
InvocationID: "test-invocation-id",
Submitter: "test-submitter",
Steps: []model.Step{
{
Type: "condor",
StdinPath: "/stdin/path",
StdoutPath: "/stdout/path",
StderrPath: "/stderr/path",
LogFile: "/logfile/path",
Environment: map[string]string{
"FOO": "BAR",
"BAZ": "1",
},
Input: []model.StepInput{
{
ID: "step-input-1",
Multiplicity: "wut",
Name: "step-input-name-1",
Property: "step-input-property-1",
Retain: false,
Type: "step-input-type-1",
Value: "step-input-value-1",
},
{
ID: "step-input-2",
Multiplicity: "wut2",
Name: "step-input-name-2",
Property: "step-input-property-2",
Retain: false,
Type: "step-input-type-2",
Value: "step-input-value-2",
},
},
Config: model.StepConfig{
Params: []model.StepParam{
{
ID: "step-param-1",
Name: "step-param-name-1",
Value: "step-param-value-1",
Order: 0,
},
{
ID: "step-param-2",
Name: "step-param-name-2",
Value: "step-param-value-2",
Order: 1,
},
},
},
Component: model.StepComponent{
Container: model.Container{
ID: "container-id-1",
Name: "container-name-1",
CPUShares: 0,
PIDsLimit: 64,
Image: model.ContainerImage{
ID: "container-image-1",
Name: "container-image-name-1",
Tag: "container-image-tag-1",
},
Ports: []model.Ports{
{
ContainerPort: 80,
},
},
VolumesFrom: []model.VolumesFrom{
{
Tag: "tag1",
Name: "name1",
HostPath: "/host/path1",
ContainerPath: "/container/path1",
},
{
Tag: "tag2",
Name: "name2",
HostPath: "/host/path2",
ContainerPath: "/container/path2",
},
},
},
},
},
},
}
func TestJobCompose(t *testing.T) {
expected := `version: '2'
volumes:
test0:
driver: local
driver_opts:
opt0: value0
opt1: value1
test1:
driver: fake
driver_opts:
opt2: value2
opt3: value3
networks:
local:
driver: bridge
remote:
driver: bridge
services:
service-test-1:
image: hello-world
command: [echo, hi]
container_name: this-is-a-test
dns:
- "8.8.8.8"
- 8.8.4.4
dns_search:
- "notreal0.example.com"
- notreal1.example.com
tmpfs:
- /tmp
- /tmp1
entrypoint: /bin/echo
environment:
testing: value1
TESTING: value2
expose:
- "8080"
- 8081
labels:
foo: bar
logging:
driver: syslog
options:
option1: value1
option2: value2
network_mode: bridge
networks:
local:
remote:
aliases:
- a1
- a2
ports:
- "8080:8081"
- 9000
volumes:
- "~/test:/container/test"
- test0:/test0
working_dir: /working_dir
`
jc := &JobComposition{}
err := yaml.Unmarshal([]byte(expected), &jc)
if err != nil {
t.Error(err)
}
if jc.Version != "2" {
t.Errorf("version was %s instead of '2'", jc.Version)
}
if len(jc.Networks) != 2 {
t.Errorf("number of networks was %d instead of 1", len(jc.Networks))
}
if _, ok := jc.Networks["local"]; !ok {
t.Errorf("could not find the 'local' network")
}
if jc.Networks["local"].Driver != "bridge" {
t.Errorf("local network driver was %s instead of 'bridge'", jc.Networks["local"].Driver)
}
// if !jc.Networks["local"].EnableIPv6 {
// t.Error("enable_ipv6 was false")
// }
if _, ok := jc.Networks["remote"]; !ok {
t.Errorf("could not find the 'remote' network")
}
if jc.Networks["remote"].Driver != "bridge" {
t.Errorf("local network driver was %s instead of 'bridge'", jc.Networks["local"].Driver)
}
// if jc.Networks["remote"].EnableIPv6 {
// t.Error("enable_ipv6 was true")
// }
if len(jc.Volumes) != 2 {
t.Errorf("number of volumes was %d instead of 2", len(jc.Volumes))
}
if jc.Volumes["test0"].Driver != "local" {
t.Errorf("test0 volume driver was %s instead of 'local'", jc.Volumes["test0"].Driver)
}
if _, ok := jc.Volumes["test0"].Options["opt0"]; !ok {
t.Error("opt0 volume driver option not found")
}
if _, ok := jc.Volumes["test0"].Options["opt1"]; !ok {
t.Error("opt1 volume driver option not found")
}
if jc.Volumes["test1"].Driver != "fake" {
t.Errorf("test1 volume driver was %s instead of 'fake'", jc.Volumes["test1"].Driver)
}
if _, ok := jc.Volumes["test1"].Options["opt2"]; !ok {
t.Error("opt2 volume driver option not found")
}
if _, ok := jc.Volumes["test1"].Options["opt3"]; !ok {
t.Error("opt3 volume driver option not found")
}
if len(jc.Services) != 1 {
t.Errorf("number of services was %d instead of 1", len(jc.Services))
}
if _, ok := jc.Services["service-test-1"]; !ok {
t.Errorf("service-test-1 was not found")
}
svc := jc.Services["service-test-1"]
if svc.Image != "hello-world" {
t.Errorf("image was %s", svc.Image)
}
if !reflect.DeepEqual(svc.Command, []string{"echo", "hi"}) {
t.Errorf("command was '%s'", svc.Command)
}
if svc.ContainerName != "this-is-a-test" {
t.Errorf("container name was %s", svc.ContainerName)
}
if len(svc.DNS) != 2 {
t.Errorf("length of dns server list was %d instead of 2", len(svc.DNS))
}
if svc.DNS[0] != "8.8.8.8" {
t.Errorf("first dns server was %s and not 8.8.8.8", svc.DNS[0])
}
if svc.DNS[1] != "8.8.4.4" {
t.Errorf("second dns server was %s and not 8.8.4.4", svc.DNS[1])
}
if len(svc.DNSSearch) != 2 {
t.Errorf("number of dns search domains was %d and not 2", len(svc.DNSSearch))
}
if svc.DNSSearch[0] != "notreal0.example.com" {
t.Errorf("first dns search domain was %s", svc.DNSSearch[0])
}
if svc.DNSSearch[1] != "notreal1.example.com" {
t.Errorf("second dns search domain was %s", svc.DNSSearch[1])
}
if len(svc.TMPFS) != 2 {
t.Errorf("number if tmpfs'es was %d instead of 2", len(svc.TMPFS))
}
if svc.TMPFS[0] != "/tmp" {
t.Errorf("first tmpfs was %s", svc.TMPFS[0])
}
if svc.TMPFS[1] != "/tmp1" {
t.Errorf("second tmpfs was %s", svc.TMPFS[1])
}
if svc.EntryPoint != "/bin/echo" {
t.Errorf("entrypoint was %s", svc.EntryPoint)
}
if len(svc.Environment) != 2 {
t.Errorf("length of environment was %d instead of 2", len(svc.Environment))
}
if svc.Environment["testing"] != "value1" {
t.Errorf("testing environment var was %s", svc.Environment["testing"])
}
if svc.Environment["TESTING"] != "value2" {
t.Errorf("TESTING environment var was %s", svc.Environment["TESTING"])
}
if len(svc.Expose) != 2 {
t.Errorf("number of exposed ports was %d instead of 2", len(svc.Expose))
}
if svc.Expose[0] != "8080" {
t.Errorf("first exposed port was %s", svc.Expose[0])
}
if len(svc.Labels) != 1 {
t.Errorf("number of labels was %d instead of 1", len(svc.Labels))
}
if _, ok := svc.Labels["foo"]; !ok {
t.Error("label 'foo' not found")
}
if svc.Labels["foo"] != "bar" {
t.Errorf("label 'foo' was %s instead of 'bar'", svc.Labels["foo"])
}
if svc.Logging.Driver != "syslog" {
t.Errorf("logging driver was %s instead of 'syslog'", svc.Logging.Driver)
}
if len(svc.Logging.Options) != 2 {
t.Errorf("number of logging driver options was %d instead of 2", len(svc.Logging.Options))
}
if _, ok := svc.Logging.Options["option1"]; !ok {
t.Error("logging option option1 was not found")
}
if svc.Logging.Options["option1"] != "value1" {
t.Errorf("logging option option1 was %s instead of 'value1'", svc.Logging.Options["option1"])
}
if svc.NetworkMode != "bridge" {
t.Errorf("network mode is %s instead of 'bridge'", svc.NetworkMode)
}
if len(svc.Networks) != 2 {
t.Errorf("number of service networks was %d instead of 2", len(svc.Networks))
}
if _, ok := svc.Networks["local"]; !ok {
t.Error("local service network not found")
}
if _, ok := svc.Networks["remote"]; !ok {
t.Errorf("remote service network not found")
}
if len(svc.Networks["remote"].Aliases) != 2 {
t.Errorf("number of remote network aliases was %d instead of 2", len(svc.Networks["remote"].Aliases))
}
if svc.Networks["remote"].Aliases[0] != "a1" {
t.Errorf("first remote network alias was %s instead of 'a1'", svc.Networks["remote"].Aliases[0])
}
if svc.Networks["remote"].Aliases[1] != "a2" {
t.Errorf("second remote network alias was %s instead of 'a2'", svc.Networks["remote"].Aliases[1])
}
if len(svc.Ports) != 2 {
t.Errorf("number of service ports was %d instead of 2", len(svc.Ports))
}
if svc.Ports[0] != "8080:8081" {
t.Errorf("first service port was %s instead of '8080:8081'", svc.Ports[0])
}
if svc.Ports[1] != "9000" {
t.Errorf("second service port was %s instead of '9000'", svc.Ports[1])
}
if len(svc.Volumes) != 2 {
t.Errorf("number of service volumes as %d instead of 2", len(svc.Volumes))
}
if svc.Volumes[0] != "~/test:/container/test" {
t.Errorf("first volume was %s instead of '~/test:/container/test'", svc.Volumes[0])
}
if svc.Volumes[1] != "test0:/test0" {
t.Errorf("second volume was %s instead of 'test0:/test0'", svc.Volumes[1])
}
if svc.WorkingDir != "/working_dir" {
t.Errorf("working directory was %s instead of /working_dir", svc.WorkingDir)
}
}
func TestNewJobComposition(t *testing.T) {
jc, err := NewJobComposition("", "")
if err != nil {
t.Error(err)
}
if jc == nil {
t.Error("NewJobComposition() returned nil")
}
if jc.Version != "2.2" {
t.Errorf("version was %s", jc.Version)
}
}
func TestConvertStep(t *testing.T) {
cfg := viper.New()
cfg.Set("k8s.frontend.base", "http://cyverse.run/")
jc, err := NewComposer(testJob, cfg, "", "")
if err != nil {
t.Error(err)
}
stepcfg := &ConvertStepParams{
Step: &testJob.Steps[0],
Index: 0,
User: testJob.Submitter,
InvID: testJob.InvocationID,
WorkingDirHostPath: "",
AvailablePort: 0,
}
jc.ConvertStep(stepcfg)
if len(jc.composition.Services) != 2 {
t.Errorf("number of services was %d and not 1", len(jc.composition.Services))
}
if _, ok := jc.composition.Services["step_0"]; !ok {
t.Error("step_0 not found")
}
svc := jc.composition.Services["step_0"]
if _, ok := svc.Environment["FOO"]; !ok {
t.Error("environment var FOO not found")
}
if svc.Environment["FOO"] != "BAR" {
t.Errorf("FOO value was %s instead of 'BAR'", svc.Environment["FOO"])
}
if _, ok := svc.Environment["BAZ"]; !ok {
t.Error("environment var BAZ not found")
}
if svc.Environment["BAZ"] != "1" {
t.Errorf("BAZ value was %s instead of '1'", svc.Environment["BAZ"])
}
if svc.Image != "container-image-name-1:container-image-tag-1" {
t.Errorf("image was %s", svc.Image)
}
if svc.PIDsLimit != 64 {
t.Errorf("PIDsLimit was %d, expecting 64", svc.PIDsLimit)
}
if !reflect.DeepEqual(svc.Command, []string{"step-param-name-1", "step-param-value-1", "step-param-name-2", "step-param-value-2"}) {
t.Errorf("command was %#v", svc.Command)
}
}