-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_dockercompose.py
More file actions
383 lines (293 loc) · 18.5 KB
/
test_dockercompose.py
File metadata and controls
383 lines (293 loc) · 18.5 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
from ch_cli_tools.dockercompose import *
from ch_cli_tools.configurationgenerator import *
import pytest
import shutil
HERE = os.path.dirname(os.path.realpath(__file__))
RESOURCES = os.path.join(HERE, 'resources')
CLOUDHARNESS_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
COMPOSE_PATH = COMPOSE
HELM_IS_INSTALLED = shutil.which("helm") is not None
def exists(path):
return path.exists()
def test_collect_compose_values(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, include=['samples', 'myapp'],
exclude=['events'], domain="my.local",
namespace='test', env='dev', local=False, tag=1, registry='reg')
# Auto values
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image'] == 'reg/testprojectname/myapp:1'
assert values.apps['myapp'].harness.deployment.image == 'reg/testprojectname/myapp:1'
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['name'] == 'myapp'
assert values[KEY_APPS]['legacy'][KEY_HARNESS]['name'] == 'legacy'
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['image'] == 'reg/testprojectname/accounts:1'
# First level include apps
assert 'samples' in values[KEY_APPS]
assert 'myapp' in values[KEY_APPS]
# Not included
assert 'jupyterhub' not in values[KEY_APPS]
# Dependency include first level
assert 'accounts' in values[KEY_APPS]
assert 'legacy' in values[KEY_APPS]
# Dependency include second level
assert 'argo' in values[KEY_APPS]
# Explicit exclude overrides include
assert 'events' not in values[KEY_APPS]
# Base values kept
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['subdomain'] == 'accounts'
# Defaults
assert 'service' in values[KEY_APPS]['legacy'][KEY_HARNESS]
assert 'common' in values[KEY_APPS]['legacy']
assert 'common' in values[KEY_APPS]['accounts']
# Values overriding
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['port'] == 'overridden'
# Environment specific overriding
assert values[KEY_APPS]['accounts']['a'] == 'dev'
assert values['a'] == 'dev'
assert values['database']['auto'] is False
# legacy reading
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['auto'] is True
assert values[KEY_APPS]['legacy'][KEY_HARNESS]['deployment']['auto'] is False
compose_path = out_folder / COMPOSE_PATH
# Check files
assert exists(out_folder.parent / 'docker-compose.yaml')
assert exists(compose_path)
assert exists(compose_path / 'values.yaml')
assert exists(compose_path / 'allvalues.yaml')
assert exists(compose_path / 'resources' / 'accounts' / 'realm.json')
assert exists(compose_path / 'resources' / 'accounts' / 'aresource.txt')
assert exists(compose_path / 'resources' / 'myapp' / 'aresource.txt')
assert exists(compose_path / 'templates' / 'myapp' / 'mytemplate.yaml')
if HELM_IS_INSTALLED:
assert exists(compose_path / 'resources' / 'generated' / 'test.yaml')
assert exists(compose_path / 'resources' / 'generated' / 'test2.yaml')
content = (compose_path / 'resources' / 'generated' / 'test.yaml').read_text()
assert content == 'mykey: myvalue'
content = (compose_path / 'resources' / 'generated' / 'test2.yaml').read_text()
assert content == 'mykey2: myvalue2'
# Checl base and task images
assert values[KEY_TASK_IMAGES]
assert 'cloudharness-base' in values[KEY_TASK_IMAGES]
assert values[KEY_TASK_IMAGES]['cloudharness-base'] == 'reg/testprojectname/cloudharness-base:1', "cloudharness-base image is overridden"
assert values[KEY_TASK_IMAGES]['myapp-mytask'] == 'reg/testprojectname/myapp-mytask:1'
# Not indicated as a build dependency
assert 'cloudharness-base-debian' not in values[KEY_TASK_IMAGES]
def test_collect_compose_values_noreg_noinclude(tmp_path):
out_path = tmp_path / 'test_collect_compose_values_noreg_noinclude'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_path, domain="my.local",
namespace='test', env='dev', local=False, tag=1)
# Auto values
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image'] == 'testprojectname/myapp:1'
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['name'] == 'myapp'
assert values[KEY_APPS]['legacy'][KEY_HARNESS]['name'] == 'legacy'
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['image'] == 'testprojectname/accounts:1'
# First level include apps
assert 'samples' in values[KEY_APPS]
assert 'myapp' in values[KEY_APPS]
assert 'jupyterhub' in values[KEY_APPS]
assert 'accounts' in values[KEY_APPS]
assert 'legacy' in values[KEY_APPS]
assert 'argo' in values[KEY_APPS]
assert 'events' in values[KEY_APPS]
# Base values kept
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['subdomain'] == 'accounts'
# Defaults
assert 'service' in values[KEY_APPS]['legacy'][KEY_HARNESS]
assert 'common' in values[KEY_APPS]['legacy']
assert 'common' in values[KEY_APPS]['accounts']
# Values overriding
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['port'] == 'overridden'
assert values[KEY_APPS]['events']['kafka']['resources']['limits']['memory'] == 'overridden'
# Environment specific overriding
assert values[KEY_APPS]['accounts']['a'] == 'dev'
assert values['a'] == 'dev'
assert values['database']['auto'] is False
# legacy reading
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['auto'] is True
assert values[KEY_APPS]['legacy'][KEY_HARNESS]['deployment']['auto'] is False
compose_path = out_path / COMPOSE_PATH
# Check files
assert exists(out_path.parent / 'docker-compose.yaml')
assert exists(compose_path)
assert exists(compose_path / 'values.yaml')
assert exists(compose_path / 'allvalues.yaml')
assert exists(compose_path / 'resources' / 'accounts' / 'realm.json')
assert exists(compose_path / 'resources' / 'accounts' / 'aresource.txt')
assert exists(compose_path / 'resources' / 'myapp' / 'aresource.txt')
assert exists(compose_path / 'templates' / 'myapp' / 'mytemplate.yaml')
if HELM_IS_INSTALLED:
assert exists(compose_path / 'resources' / 'generated' / 'test.yaml')
assert exists(compose_path / 'resources' / 'generated' / 'test2.yaml')
content = (compose_path / 'resources' / 'generated' / 'test.yaml').read_text()
assert content == 'mykey: myvalue'
content = (compose_path / 'resources' / 'generated' / 'test2.yaml').read_text()
assert content == 'mykey2: myvalue2'
else:
assert False
assert values[KEY_TASK_IMAGES]
assert 'cloudharness-base' in values[KEY_TASK_IMAGES]
assert values[KEY_TASK_IMAGES]['cloudharness-base'] == 'testprojectname/cloudharness-base:1'
assert values[KEY_TASK_IMAGES]['myapp-mytask'] == 'testprojectname/myapp-mytask:1'
def test_collect_compose_values_precedence(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_precedence'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["events"])
# Values.yaml from current app must override values-prod.yaml from cloudharness
assert values[KEY_APPS]['events']['kafka']['resources']['limits']['memory'] == 'overridden'
assert values[KEY_APPS]['events']['kafka']['resources']['limits']['cpu'] == 'overridden-prod'
def test_collect_compose_values_multiple_envs(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_multiple_envs'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
namespace='test', env=['dev', 'test'], local=False, tag=1, include=["myapp"])
assert values[KEY_APPS]['myapp']['test'] is True, 'values-test not loaded'
assert values[KEY_APPS]['myapp']['dev'] is True, 'values-dev not loaded'
assert values[KEY_APPS]['myapp']['a'] == 'test', 'values-test not overriding'
def test_collect_compose_values_wrong_dependencies_validate(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_wrong_dependencies_validate'
with pytest.raises(ValuesValidationException):
create_docker_compose_configuration([CLOUDHARNESS_ROOT, f"{RESOURCES}/wrong-dependencies"], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["wrong-hard"])
try:
create_docker_compose_configuration([CLOUDHARNESS_ROOT, f"{RESOURCES}/wrong-dependencies"], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["wrong-soft"])
except ValuesValidationException as e:
pytest.fail("Should not error because of wrong soft dependency")
with pytest.raises(ValuesValidationException):
create_docker_compose_configuration([CLOUDHARNESS_ROOT, f"{RESOURCES}/wrong-dependencies"], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["wrong-build"])
with pytest.raises(ValuesValidationException):
create_docker_compose_configuration([CLOUDHARNESS_ROOT, f"{RESOURCES}/wrong-dependencies"], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["wrong-services"])
def test_collect_compose_values_build_dependencies(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_build_dependencies'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["myapp"])
assert 'cloudharness-flask' in values[KEY_TASK_IMAGES], "Cloudharness-flask is included in the build dependencies"
assert 'cloudharness-base' in values[KEY_TASK_IMAGES], "Cloudharness-base is included in cloudharness-flask Dockerfile and it should be guessed"
assert 'cloudharness-frontend-build' not in values[KEY_TASK_IMAGES], "cloudharness-frontend-build is not included in any dependency"
def test_collect_compose_values_build_dependencies_nodeps(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_build_dependencies_nodeps'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["events"])
assert 'cloudharness-flask' not in values[KEY_TASK_IMAGES], "Cloudharness-flask is not included in the build dependencies"
assert 'cloudharness-base' not in values[KEY_TASK_IMAGES], "Cloudharness-base is not included in the build dependencies"
assert 'cloudharness-frontend-build' not in values[KEY_TASK_IMAGES], "cloudharness-frontend-build is not included in any dependency"
def test_collect_compose_values_build_dependencies_exclude(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_build_dependencies_exclude'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
namespace='test', env='prod', local=False, tag=1, include=["workflows"], exclude=["workflows-extract-download"])
assert 'cloudharness-flask' in values[KEY_TASK_IMAGES], "Cloudharness-flask is included in the build dependencies"
assert 'cloudharness-base' in values[KEY_TASK_IMAGES], "Cloudharness-base is included in cloudharness-flask Dockerfile and it should be guessed"
assert 'workflows-extract-download' not in values[KEY_TASK_IMAGES], "workflows-extract-download has been explicitly excluded"
def test_clear_unused_dbconfig(tmp_path):
out_folder = tmp_path / 'test_clear_unused_dbconfig'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
env='withpostgres', local=False, include=["myapp"], exclude=["legacy"])
# There is a DB config
assert KEY_DATABASE in values[KEY_APPS]['myapp'][KEY_HARNESS]
db_config = values[KEY_APPS]['myapp'][KEY_HARNESS][KEY_DATABASE]
# postgres is set, but other entries are not.
assert db_config['postgres'] is not None
assert db_config['postgres']['image'].startswith('postgres:')
# However, it seems that even after removing unused entries,
# the finale instance of the HarnessMainConfig class that is created
# adds back those entries and set them to None.
assert db_config['mongo'] is None
assert db_config['neo4j'] is None
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
env='withmongo', local=False, include=["myapp"], exclude=["legacy"])
assert KEY_DATABASE in values[KEY_APPS]['myapp'][KEY_HARNESS]
db_config = values[KEY_APPS]['myapp'][KEY_HARNESS][KEY_DATABASE]
# mongo is set, but other entries are not.
assert db_config['mongo'] is not None
assert db_config['mongo']['image'].startswith('mongo:')
assert db_config['neo4j'] is None
assert db_config['postgres'] is None
def test_clear_all_dbconfig_if_nodb(tmp_path):
out_folder = tmp_path / 'test_clear_all_dbconfig_if_nodb'
values = create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
env='withoutdb', local=False, include=["myapp"], exclude=["legacy"])
# There is a DB config
assert KEY_DATABASE in values[KEY_APPS]['myapp'][KEY_HARNESS]
# But it is None
db_config = values[KEY_APPS]['myapp'][KEY_HARNESS][KEY_DATABASE]
assert db_config is None
def test_tag_hash_generation():
v1 = generate_tag_from_content(RESOURCES)
v2 = generate_tag_from_content(RESOURCES, ignore=['myapp'])
assert v1 != v2
v3 = generate_tag_from_content(RESOURCES, ignore=['*/myapp/*'])
assert v3 != v1
v4 = generate_tag_from_content(RESOURCES, ignore=['applications/myapp/*'])
assert v4 == v3
v5 = generate_tag_from_content(RESOURCES, ignore=['/applications/myapp/*'])
assert v5 == v4
fname = Path(RESOURCES) / 'applications' / 'myapp' / 'afile.txt'
try:
fname.write_text('a')
v6 = generate_tag_from_content(RESOURCES, ignore=['/applications/myapp/*'])
assert v6 == v5
v7 = generate_tag_from_content(RESOURCES)
assert v7 != v1
finally:
fname.unlink()
def test_collect_compose_values_auto_tag(tmp_path):
out_folder = tmp_path / 'test_collect_compose_values_auto_tag'
def create():
return create_docker_compose_configuration([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, include=['samples', 'myapp'],
exclude=['events'], domain="my.local",
namespace='test', env='dev', local=False, tag=None, registry='reg')
BASE_KEY = "cloudharness-base"
values = create()
# Auto values are set by using the directory hash
assert 'reg/testprojectname/myapp:' in values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image']
assert 'reg/testprojectname/myapp:' in values.apps['myapp'].harness.deployment.image
assert 'testprojectname/myapp-mytask' in values[KEY_TASK_IMAGES]['myapp-mytask']
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image'] == values.apps['myapp'].harness.deployment.image
v1 = values.apps['myapp'].harness.deployment.image
c1 = values["task-images"]["my-common"]
b1 = values["task-images"][BASE_KEY]
d1 = values["task-images"]["cloudharness-flask"]
values = create()
assert v1 == values.apps['myapp'].harness.deployment.image, "Nothing changed the hash value"
assert values["task-images"][BASE_KEY] == b1, "Base image should not change following the root .dockerignore"
fname = Path(RESOURCES) / 'applications' / 'myapp' / 'afile.txt'
try:
fname.write_text('a')
values = create()
assert v1 != values.apps['myapp'].harness.deployment.image, "Adding the file changed the hash value"
v2 = values.apps['myapp'].harness.deployment.image
assert values["task-images"][BASE_KEY] == b1, "Application files should be ignored for base image following the root .dockerignore"
finally:
fname.unlink()
try:
fname.write_text('a')
values = create()
assert v2 == values.apps['myapp'].harness.deployment.image, "Recreated an identical file, the hash value should be the same"
finally:
fname.unlink()
fname = Path(RESOURCES) / 'applications' / 'myapp' / 'afile.ignored'
try:
fname.write_text('a')
values = create()
assert values["task-images"][BASE_KEY] == b1, "2: Application files should be ignored for base image following the root .dockerignore"
assert v1 == values.apps['myapp'].harness.deployment.image, "Nothing should change the hash value as the file is ignored in the .dockerignore"
finally:
fname.unlink()
# Dependencies test: if a dependency is changed, the hash should change
fname = Path(RESOURCES) / 'infrastructure' / 'common-images' / 'my-common' / 'afile'
try:
fname.write_text('a')
values = create()
assert c1 != values["task-images"]["my-common"], "If content of a static image is changed, the hash should change"
assert v1 != values.apps['myapp'].harness.deployment.image, "If a static image dependency is changed, the hash should change"
finally:
fname.unlink()
fname = Path(RESOURCES) / 'atestfile'
try:
fname.write_text('a')
values = create()
assert b1 != values["task-images"][BASE_KEY], "Content for base image is changed, the hash should change"
assert d1 != values["task-images"]["cloudharness-flask"], "Content for base image is changed, the static image should change"
assert v1 != values.apps['myapp'].harness.deployment.image, "2 levels dependency: If a base image dependency is changed, the hash should change"
finally:
fname.unlink()