-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_skaffold.py
More file actions
330 lines (273 loc) · 11.7 KB
/
test_skaffold.py
File metadata and controls
330 lines (273 loc) · 11.7 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
import shutil
import os
from ch_cli_tools.preprocessing import preprocess_build_overrides
from ch_cli_tools.helm import *
from ch_cli_tools.skaffold import *
HERE = os.path.dirname(os.path.realpath(__file__))
RESOURCES = os.path.join(HERE, 'resources')
RESOURCES_BUGGY = os.path.join(HERE, 'resources_buggy')
CLOUDHARNESS_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
CLOUDHARNESS_DIRNAME = os.path.basename(CLOUDHARNESS_ROOT)
def test_create_skaffold_configuration(tmp_path):
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=tmp_path,
include=['samples', 'myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
BUILD_DIR = "/tmp/build"
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_DIR
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path
)
assert os.path.exists(os.path.join(tmp_path, 'skaffold.yaml'))
exp_apps = ('accounts', 'samples', 'workflows', 'myapp', 'common')
assert len(sk['build']['artifacts']) == len(
exp_apps) + len(values[KEY_TASK_IMAGES])
assert 'reg' in sk['build']['artifacts'][0]['image']
assert 'cloudharness' in sk['build']['artifacts'][0]['image']
artifact_overrides = sk['deploy']['helm']['releases'][0]['artifactOverrides']
for app in exp_apps:
assert app in artifact_overrides[KEY_APPS]
for img in values[KEY_TASK_IMAGES]:
assert img in artifact_overrides[KEY_TASK_IMAGES]
assert f'reg/testprojectname/cloudharness-base' in (
a['image'] for a in sk['build']['artifacts'])
overrides = sk['deploy']['helm']['releases'][0]['overrides']
assert overrides[KEY_APPS]['samples'][KEY_HARNESS][KEY_DEPLOYMENT]['command'] == [
'python']
assert overrides[KEY_APPS]['samples'][KEY_HARNESS][KEY_DEPLOYMENT]['args']
assert 'reg' == artifact_overrides[KEY_APPS]['accounts'][KEY_HARNESS][KEY_DEPLOYMENT]['image'][0:3]
assert 'harness' not in artifact_overrides[KEY_APPS]['accounts'][KEY_HARNESS][KEY_DEPLOYMENT]['image']
cloudharness_base_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == f'reg/testprojectname/cloudharness-base')
assert cloudharness_base_artifact['context'] == BUILD_DIR
assert 'requires' not in cloudharness_base_artifact
cloudharness_flask_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == f'reg/testprojectname/cloudharness-flask')
assert os.path.samefile(cloudharness_flask_artifact['context'],
join(CLOUDHARNESS_ROOT, 'infrastructure/common-images/cloudharness-flask')
)
assert len(cloudharness_flask_artifact['requires']) == 1
expected_samples_image = values[KEY_APPS]['samples'][KEY_HARNESS][KEY_DEPLOYMENT]['image'].split(':')[0]
samples_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == expected_samples_image
)
assert os.path.samefile(samples_artifact['context'], join(CLOUDHARNESS_ROOT, 'applications/samples'))
assert 'TEST_ARGUMENT' in samples_artifact['docker']['buildArgs']
assert samples_artifact['docker']['buildArgs']['TEST_ARGUMENT'] == 'example value'
myapp_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == f'reg/testprojectname/myapp')
assert os.path.samefile(myapp_artifact['context'], join(
RESOURCES, 'applications/myapp'))
assert myapp_artifact['hooks']['before'], 'The hook for dependencies should be included'
assert len(myapp_artifact['hooks']['before']) == 2, 'The hook for dependencies should include 2 clone commands'
accounts_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == f'reg/testprojectname/accounts')
assert os.path.samefile(accounts_artifact['context'], '/tmp/build/applications/accounts')
# Custom unit tests
assert len(sk['test']) == 2, 'Unit tests should be included'
samples_test = sk['test'][0]
assert samples_test['image'] == expected_samples_image, 'Unit tests for samples should be included'
assert "samples/test" in samples_test['custom'][0]['command'], "The test command must come from values.yaml test/unit/commands"
assert len(sk['test'][1]['custom']) == 2
flags = sk['deploy']['helm']['flags']
assert '--timeout=10m' in flags['install']
assert '--install' in flags['upgrade']
shutil.rmtree(tmp_path)
shutil.rmtree(BUILD_DIR)
def test_create_skaffold_configuration_with_conflicting_dependencies(tmp_path):
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES_BUGGY],
output_path=tmp_path,
include=['myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES_BUGGY],
helm_values=values,
merge_build_path=str(tmp_path)
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path
)
releases = sk['deploy']['helm']['releases']
assert len(releases) == 1 # Ensure we only found 1 deployment (for myapp)
release = releases[0]
assert 'myapp' in release['overrides']['apps']
assert 'matplotlib' not in release['overrides']['apps']
myapp_config = release['overrides']['apps']['myapp']
assert myapp_config['harness']['deployment']['args'][0] == '/usr/src/app/myapp_code/__main__.py'
def test_create_skaffold_configuration_with_conflicting_dependencies_requirements_file(tmp_path):
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES_BUGGY],
output_path=tmp_path,
include=['myapp2'],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES_BUGGY],
helm_values=values,
merge_build_path=str(tmp_path)
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path
)
releases = sk['deploy']['helm']['releases']
assert len(releases) == 1 # Ensure we only found 1 deployment (for myapp)
release = releases[0]
assert 'myapp2' in release['overrides']['apps']
assert 'matplotlib' not in release['overrides']['apps']
myapp_config = release['overrides']['apps']['myapp2']
assert myapp_config['harness']['deployment']['args'][0] == '/usr/src/app/myapp_code/__main__.py'
def test_create_skaffold_configuration_nobuild(tmp_path):
values = create_helm_chart(
[RESOURCES],
output_path=tmp_path,
include=['myapp'],
domain="my.local",
namespace='test',
env='nobuild',
local=False,
tag=1,
registry='reg'
)
BUILD_DIR = "/tmp/build"
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_DIR
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path
)
releases = sk['deploy']['helm']['releases']
assert len(sk['build']['artifacts']) == 1
assert len(releases) == 1 # Ensure we only found 1 deployment (for myapp)
release = releases[0]
assert 'myapp' not in release['overrides']['apps']
def test_env_dockerfile(tmp_path):
"""When a [env].Dockerfile exists it should be used instead of Dockerfile."""
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=tmp_path,
include=['samples', 'myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
BUILD_DIR = "/tmp/build"
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_DIR
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path,
env=['dev']
)
myapp_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == f'reg/testprojectname/myapp')
# myapp has a dev.Dockerfile so it should be used
assert myapp_artifact['docker']['dockerfile'].endswith('dev.Dockerfile'), \
f"Expected dev.Dockerfile but got {myapp_artifact['docker']['dockerfile']}"
# samples has no dev.Dockerfile, so it should fall back to Dockerfile
expected_samples_image = values[KEY_APPS]['samples'][KEY_HARNESS][KEY_DEPLOYMENT]['image'].split(':')[0]
samples_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == expected_samples_image)
assert samples_artifact['docker']['dockerfile'].endswith('Dockerfile'), \
f"Expected Dockerfile but got {samples_artifact['docker']['dockerfile']}"
assert not samples_artifact['docker']['dockerfile'].endswith('dev.Dockerfile'), \
"samples should not use dev.Dockerfile"
shutil.rmtree(tmp_path)
shutil.rmtree(BUILD_DIR)
def test_env_dockerfile_fallback(tmp_path):
"""Without env, or when no env.Dockerfile exists, the regular Dockerfile should be used."""
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=tmp_path,
include=['myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env='',
local=False,
tag=1,
registry='reg'
)
BUILD_DIR = "/tmp/build2"
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_DIR
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path,
env=None
)
myapp_artifact = next(
a for a in sk['build']['artifacts'] if a['image'] == f'reg/testprojectname/myapp')
assert myapp_artifact['docker']['dockerfile'].endswith('Dockerfile'), \
f"Expected Dockerfile but got {myapp_artifact['docker']['dockerfile']}"
assert not myapp_artifact['docker']['dockerfile'].endswith('dev.Dockerfile'), \
"Should not use dev.Dockerfile when no env is specified"
shutil.rmtree(tmp_path)
shutil.rmtree(BUILD_DIR)
def test_app_depends_on_app(tmp_path):
out_folder = tmp_path / 'test_app_depends_on_app'
values = create_helm_chart([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_folder, domain="my.local",
env='', local=False, include=["dependantapp"], exclude=[])
BUILD_DIR = "/tmp/build"
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_DIR
)
sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=tmp_path
)
releases = sk['deploy']['helm']['releases']
assert len(sk['build']['artifacts']) == 6, "There should be 6 build artifacts (base+common, dependantapp plus its 2 tasks, myapp)"
assert len(releases) == 1 # Ensure we only found 1 deployment (for myapp)
release = releases[0]
assert 'myapp' not in release['overrides']['apps'], "myapp should not be included in the overrides because it's a build only dependency"