-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_task_test.py
More file actions
477 lines (410 loc) · 17.8 KB
/
Copy pathdocker_task_test.py
File metadata and controls
477 lines (410 loc) · 17.8 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
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
from tiltfile_runner import run_tiltfile_func
from unittest.mock import Mock
import unittest
import pytest
import yaml
class DockerLocalTest(unittest.TestCase):
def test_delegates_to_local_resource_for_build(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_local",
mocks={'local_resource': local_resource},
ref="my_image",
context="././path/to/build/context")
local_resource.assert_any_call(
"my_image_build", "docker build -t my_image -f Dockerfile ././path/to/build/context")
def test_delegates_to_local_resource_for_run(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_local",
mocks={'local_resource': local_resource},
ref="my_image",
context="././path/to/build/context")
local_resource.assert_called_with("my_image",
"docker run --rm my_image",
resource_deps=["my_image_build"])
def test_adds_optional_recource_deps_to_run(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_local",
mocks={'local_resource': local_resource},
ref="my_image",
context="././path/to/build/context",
runtime_deps=["something", "else"])
local_resource.assert_called_with(
"my_image",
"docker run --rm my_image",
resource_deps=["something", "else", "my_image_build"])
def test_adds_optional_env_vars_to_run(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_local",
mocks={'local_resource': local_resource},
ref="my_image",
context="././path/to/build/context",
env_vars={
"DOG": 1,
"CAT": "two"
})
local_resource.assert_called_with(
"my_image",
'docker run --rm -e DOG="1" -e CAT="two" my_image',
resource_deps=["my_image_build"])
def test_adds_optional_run_command_array(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_local",
mocks={'local_resource': local_resource},
ref="my_image",
context="././path/to/build/context",
run_cmd=["sh", "echo", "hi"])
local_resource.assert_called_with(
"my_image",
'docker run --rm my_image sh echo hi',
resource_deps=["my_image_build"])
def test_overrides_dockerfile_for_build(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_local",
mocks={'local_resource': local_resource},
ref="my_image",
dockerfile="another.Dockerfile",
context="././path/to/build/context")
local_resource.assert_any_call(
"my_image_build", "docker build -t my_image -f another.Dockerfile ././path/to/build/context")
class DockerRemoteTest(unittest.TestCase):
def test_delegates_to_docker_build_for_build(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="./path/to/build/context",
readiness_probe=None)
docker_build.assert_called_with("my-image", "./path/to/build/context", dockerfile="Dockerfile")
def test_overrides_dockerfile_for_build(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
dockerfile="another.Dockerfile",
build_context="./path/to/build/context",
readiness_probe=None)
docker_build.assert_called_with("my-image", "./path/to/build/context", dockerfile="another.Dockerfile")
def test_uses_repository_instead_if_provided(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
docker_repo="my.aws/repo",
build_context="././path/to/build/context",
readiness_probe=None)
docker_build.assert_called_with("my.aws/repo", "././path/to/build/context", dockerfile="Dockerfile")
def test_generates_k8_yaml_job_with_defaults_for_image(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context")
expected_spec = yaml.safe_load("""
apiVersion: batch/v1
kind: Job
metadata:
name: my-image
spec:
parallelism: 1
completions: 1
backoffLimit: 0
template:
metadata:
annotations:
sidecar.istio.io/inject: "false"
spec:
containers:
- name: main
image: my-image
readinessProbe:
exec:
command:
- 'false'
initialDelaySeconds: 120
periodSeconds: 120
resources:
requests:
cpu: 1
memory: 2056Mi
limits:
cpu: 1
memory: 2056Mi
restartPolicy: Never
""")
assert k8s_yaml.call_count == 1
print(k8s_yaml.call_args[0][0])
assert yaml.safe_load(k8s_yaml.call_args[0][0]) == expected_spec
def test_can_overwrite_resource_requirements(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
docker_repo="my.aws/repo",
build_context="././path/to/build/context",
cpu="2000m",
memory="4Gi",
readiness_probe=None)
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["spec"]["template"]["spec"]["containers"][0][
"resources"] == yaml.safe_load("""
requests:
cpu: 2000m
memory: 4Gi
limits:
cpu: 2000m
memory: 4Gi
""")
def test_includes_image_repo_if_provided(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
docker_repo="my.aws/repo",
build_context="././path/to/build/context",
readiness_probe=None)
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["spec"]["template"]["spec"]["containers"][0][
"image"] == "my.aws/repo"
def test_defines_k8_job_namespace_if_provided(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context",
namespace="somewhere",
readiness_probe=None)
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["metadata"]["namespace"] == "somewhere"
def test_creates_dependent_k8s_resource_for_yaml(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context",
namespace="somewhere",
readiness_probe=None,
runtime_deps=["a", "b"])
assert k8s_resource.call_count == 1
k8s_resource.assert_called_with("my-image", resource_deps=["a", "b"])
def test_passes_env_vars_to_container_spec(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context",
namespace="somewhere",
readiness_probe=None,
env_vars={
"DOG": 1,
"CAT": "two"
})
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["spec"]["template"]["spec"]["containers"][0][
"env"] == yaml.safe_load("""
- name: DOG
value: 1
- name: CAT
value: two
""")
def test_creates_specified_readiness_probe(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context",
namespace="somewhere",
readiness_probe={"httpGet": {
"path": "/health"
}})
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["spec"]["template"]["spec"]["containers"][0][
"readinessProbe"] == yaml.safe_load("""
httpGet:
path: /health
""")
def test_passes_command_array_to_container_spec(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context",
namespace="somewhere",
readiness_probe=None,
run_cmd=["bloop", "--something", "--another-thing"])
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["spec"]["template"]["spec"]["containers"][0]["args"] == [
"bloop", "--something", "--another-thing"
]
def test_adds_custom_pod_annotations(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
build_context="././path/to/build/context",
namespace="somewhere",
annotations={
'custom': 'annotation'
})
assert k8s_yaml.call_count == 1
job = yaml.safe_load(k8s_yaml.call_args[0][0])
assert job["spec"]["template"]["metadata"]["annotations"] == {
"sidecar.istio.io/inject": "false",
'custom': 'annotation'
}
def test_errors_if_resource_name_contains_invalid_char(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
with pytest.raises(Exception):
run_tiltfile_func(
"docker_task/Tiltfile",
"docker_remote",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my_image",
build_context="././path/to/build/context",
)
class DockerTaskTest(unittest.TestCase):
def test_delegates_to_local_resource_for_build(self):
local_resource = Mock()
k8s_yaml = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_task",
mocks={
'local_resource': local_resource,
'k8s_yaml': k8s_yaml
},
ref="my-image",
build_context="././path/to/build/context",
run_remote=False)
local_resource.assert_any_call(
"my-image_build", "docker build -t my-image -f Dockerfile ././path/to/build/context")
k8s_yaml.call_count == 0
def test_strips_out_non_local_args(self):
local_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_task",
mocks={'local_resource': local_resource},
ref="my-image",
build_context="././path/to/build/context",
run_remote=False,
namespace="dave",
docker_repo="test",
readiness_probe="1234")
local_resource.assert_any_call(
"my-image_build", "docker build -t my-image -f Dockerfile ././path/to/build/context")
def test_runs_on_remote(self):
docker_build = Mock()
k8s_yaml = Mock()
k8s_resource = Mock()
run_tiltfile_func("docker_task/Tiltfile",
"docker_task",
mocks={
'docker_build': docker_build,
"k8s_yaml": k8s_yaml,
"k8s_resource": k8s_resource
},
ref="my-image",
run_remote=True,
build_context="././path/to/build/context",
readiness_probe=None)
assert k8s_yaml.call_count == 1