-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_codefresh.py
More file actions
388 lines (318 loc) · 16.6 KB
/
test_codefresh.py
File metadata and controls
388 lines (318 loc) · 16.6 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
from ch_cli_tools.preprocessing import preprocess_build_overrides
from ch_cli_tools.helm import *
from ch_cli_tools.configurationgenerator import *
from ch_cli_tools.codefresh import *
HERE = os.path.dirname(os.path.realpath(__file__))
RESOURCES = os.path.join(HERE, 'resources')
OUT = '/tmp/deployment'
CLOUDHARNESS_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
BUILD_MERGE_DIR = "./build/.overrides"
myapp_path = os.path.join(HERE, "resources/applications/myapp")
if not os.path.exists(os.path.join(myapp_path, "dependencies/a/.git")):
os.makedirs(os.path.join(myapp_path, "dependencies/a/.git"))
STEP_0 = "build_application_images_0"
STEP_1 = "build_application_images_1"
STEP_2 = "build_application_images_2"
STEP_3 = "build_application_images_3"
def test_create_codefresh_configuration():
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=OUT,
include=['samples', 'myapp', "workflows"],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
try:
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)
build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=["dev"],
base_image_name=values['name'],
helm_values=values, save=False)
assert "test_step" in cf
assert cf['steps']['main_clone']['title'] == 'Overridden', "Steps overriding is not working correctly"
assert cf['steps']['main_clone']['type'] == 'git-clone', "Steps overriding missing values from the parent"
l1_steps = cf['steps']
step_build_base = l1_steps[STEP_0]
assert step_build_base["type"] == "parallel"
steps = l1_steps[STEP_0]["steps"]
assert len(steps) == 7, "all images that do not depend on othe builds should be included in the first step"
assert "cloudharness-base" in steps, "cloudharness-base image should be included as dependency"
assert "testprojectname/" in steps["cloudharness-base"]['image_name'], "cloudharness-base image should be overridden and take the main name"
assert "cloudharness-base-debian" not in steps, "cloudharness-base image should not be included"
assert "cloudharness-frontend-build" in steps, "cloudharness-frontend-build image should be included as dependency"
assert "testprojectname/" in steps["cloudharness-frontend-build"]['image_name'], "cloudharness-frontend-build image is not overridden"
step = steps["cloudharness-frontend-build"]
assert os.path.samefile(step['working_directory'], CLOUDHARNESS_ROOT)
assert os.path.samefile(os.path.join(step['working_directory'], step['dockerfile']),
os.path.join(CLOUDHARNESS_ROOT, BASE_IMAGES_PATH, "cloudharness-frontend-build", "Dockerfile"))
step = steps["cloudharness-base"]
assert step['working_directory'] == BUILD_MERGE_DIR, "Overridden base images should build inside the merge directory"
assert os.path.samefile(
os.path.join(step['working_directory'], step['dockerfile']),
os.path.join(step['working_directory'],
BASE_IMAGES_PATH, "cloudharness-base", "Dockerfile")
), "Not overridden base images should be built from the base directory"
assert "my-common" in steps, "my-common image should be included as dependency"
step = steps["my-common"]
assert step['dockerfile'] == "Dockerfile"
assert os.path.samefile(step['working_directory'], os.path.join(
RESOURCES, STATIC_IMAGES_PATH, "my-common"))
step = steps["accounts"]
assert step['dockerfile'] == "Dockerfile"
assert os.path.samefile(step['working_directory'], os.path.join(
BUILD_MERGE_DIR, APPS_PATH, "accounts"))
steps = l1_steps[STEP_1]["steps"]
assert "cloudharness-flask" in steps, "cloudharness-flask image should be included as dependency"
assert "samples" not in steps, "samples depends on cloudharness-flask, so it should be included in the next step"
step = steps["cloudharness-flask"]
assert step['dockerfile'] == "Dockerfile"
assert os.path.samefile(step['working_directory'], os.path.join(
CLOUDHARNESS_ROOT, STATIC_IMAGES_PATH, "cloudharness-flask"))
step = steps["workflows-notify-queue"]
assert step['dockerfile'] == "Dockerfile"
assert os.path.samefile(step['working_directory'], os.path.join(
BUILD_MERGE_DIR, APPS_PATH, "workflows/tasks/notify-queue"))
steps = l1_steps[STEP_2]["steps"]
assert "myapp" in steps
assert "samples" in steps
assert "accounts" not in steps
assert "workflows" in steps
assert "events" not in steps
step = steps["samples"]
assert step['dockerfile'] == "Dockerfile"
assert os.path.samefile(
step['working_directory'], os.path.join(CLOUDHARNESS_ROOT, APPS_PATH, "samples"))
step = steps["myapp"]
assert step['dockerfile'] == "Dockerfile"
assert "testprojectname/" in step['image_name'], f"myapp image should have the project name coming from the chart in its path, is {step['image_name']}"
for build_argument in step['build_arguments']:
if build_argument.startswith("CLOUDHARNESS_FLASK="):
assert "testprojectname" in build_argument, "Cloudharness flask image should have cloud-harness in its path"
assert build_argument == "CLOUDHARNESS_FLASK=${{REGISTRY}}/testprojectname/cloudharness-flask:${{CLOUDHARNESS_FLASK_TAG}}", "Dependency is not properly set in the build arguments"
assert os.path.samefile(step['working_directory'], os.path.join(
RESOURCES, APPS_PATH, "myapp"))
assert CD_UNIT_TEST_STEP in l1_steps, "Unit tests run step should be specified"
assert CD_API_TEST_STEP in l1_steps, "Api steps are available in the dev env template"
assert CD_E2E_TEST_STEP in l1_steps, "E2E steps are included in the dev env template"
assert len(l1_steps[CD_UNIT_TEST_STEP]['steps']
) == 2, "Two unit test steps are expected"
assert 'myapp_ut' in l1_steps[CD_UNIT_TEST_STEP]['steps'], "Myapp test step is expected"
tstep = l1_steps[CD_UNIT_TEST_STEP]['steps']['myapp_ut']
assert tstep['image'] == r"${{REGISTRY}}/testprojectname/myapp:${{MYAPP_TAG}}", "The test image should be the one built for the current app"
assert len(
tstep['commands']) == 2, "Unit test commands are not properly loaded from the unit test configuration file"
assert tstep['commands'][0] == "tox", "Unit test commands are not properly loaded from the unit test configuration file"
assert len(l1_steps[CD_STEP_CLONE_DEPENDENCIES]['steps']) == 3, "3 clone steps should be included as we have 2 dependencies from myapp, plus cloudharness"
finally:
shutil.rmtree(BUILD_MERGE_DIR)
def test_create_codefresh_configuration_multienv():
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=OUT,
include=['samples', 'myapp', "workflows"],
exclude=['events'],
domain="my.local",
namespace='test',
env=['dev', 'test'],
local=False,
tag=1,
registry='reg'
)
try:
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)
build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=['dev', 'test'],
base_image_name=values['name'],
helm_values=values, save=False)
assert cf['test_step'] == 'test'
assert cf['test'] == True
assert cf['dev'] == True
for cmd in cf['steps']['prepare_deployment']['commands']:
if 'harness-deployment' in cmd:
assert '-e dev-test' in cmd
assert "test-${{NAMESPACE_BASENAME}}" in cmd
assert "-i samples" in cmd
finally:
shutil.rmtree(BUILD_MERGE_DIR)
def test_create_codefresh_configuration_tests():
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=OUT,
include=['samples', 'myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env=['dev', 'test'],
local=False,
tag=1,
registry='reg'
)
try:
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)
build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=['dev', 'test'],
base_image_name=values['name'],
helm_values=values, save=False)
# assert 'jest-puppeteer' in values['task-images']
l1_steps = cf['steps']
assert "test-e2e" in l1_steps[STEP_0]["steps"], "e2e tests image should be built"
e2e_steps = l1_steps[CD_E2E_TEST_STEP]['scale']
assert "samples_e2e_test" in e2e_steps, "samples e2e test step must be included"
test_step = e2e_steps["samples_e2e_test"]
assert "APP_URL=https://samples.${{DOMAIN}}" in test_step[
'environment'], "APP_URL must be provided as environment variable"
assert len(test_step['volumes']) == 1
assert "test-api" in l1_steps[STEP_1]["steps"], "api tests image should be built"
assert "test-api" in l1_steps[STEP_1]["steps"]["test-api"]["dockerfile"], "test-api image must be built from root context"
api_steps = l1_steps['tests_api']['scale']
test_step = api_steps["samples_api_test"]
assert "APP_URL=https://samples.${{DOMAIN}}/api" in test_step[
'environment'], "APP_URL must be provided as environment variable"
assert len(test_step['volumes']) == 2
assert any("allvalues.yaml" in v for v in test_step['volumes'])
assert len(test_step["commands"]) == 2, "Both default and custom api tests should be run"
st_cmd = test_step["commands"][0]
assert "--pre-run cloudharness_test.apitest_init" in st_cmd, "Prerun hook must be specified in schemathesis command"
assert "api/openapi.yaml" in st_cmd, "Openapi file must be passed to the schemathesis command"
assert "-c all" in st_cmd, "Default check loaded is `all` on schemathesis command"
assert "--hypothesis-deadline=" in st_cmd, "Custom parameters are loaded from values.yaml"
test_step = api_steps["common_api_test"]
for volume in test_step["volumes"]:
assert "server" not in volume
assert any("CLOUDHARNESS_BASE" in arg for arg in l1_steps[STEP_1]["steps"]["test-api"]
["build_arguments"]), "Missing build dependency on api test image"
finally:
shutil.rmtree(BUILD_MERGE_DIR)
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=OUT,
include=['myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env=['dev', 'test'],
local=False,
tag=1,
registry='reg'
)
try:
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)
build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=['dev', 'test'],
base_image_name=values['name'],
helm_values=values, save=False)
l1_steps = cf['steps']
assert CD_API_TEST_STEP not in l1_steps, "Api steps are not included in any app"
assert CD_E2E_TEST_STEP not in l1_steps, "E2E steps are not included in any app"
finally:
shutil.rmtree(BUILD_MERGE_DIR)
def test_create_codefresh_configuration_nobuild():
values = create_helm_chart(
[RESOURCES],
output_path=OUT,
include=['myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env=['dev', 'nobuild'],
local=False,
tag=1,
registry='reg'
)
root_paths = preprocess_build_overrides(
root_paths=[CLOUD_HARNESS_PATH, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)
build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=['dev', 'nobuild'],
base_image_name=values['name'],
helm_values=values, save=False)
l1_steps = cf['steps']
assert STEP_0 in l1_steps, "the task image should be included"
assert len(l1_steps[STEP_0]["steps"]) == 1
assert "myapp-mytask" in l1_steps[STEP_0]["steps"]
assert STEP_1 not in l1_steps, "no image other than the task image should be included, because the included app specifies a fixed image tag"
assert "publish_myapp" not in l1_steps["publish"]["steps"]
assert "publish_myapp-mytask" in l1_steps["publish"]["steps"]
def test_codefresh_secret_with_quotes():
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES],
output_path=OUT,
include=['myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
try:
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)
build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]
values.apps["myapp"].harness.secrets = {
"settings_secret": "SECRET_KEY='replace-with-strong-shared-secret'"
}
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=['dev'],
base_image_name=values['name'],
helm_values=values, save=False)
custom_values = cf['steps']['deployment']['arguments']['custom_values']
entry = next(
value for value in custom_values
if value.startswith("apps_myapp_harness_secrets_settings__secret=")
)
assert entry == 'apps_myapp_harness_secrets_settings__secret="${{SETTINGS__SECRET}}"'
rendered = entry.replace(
"${{SETTINGS__SECRET}}",
values.apps["myapp"].harness.secrets["settings_secret"]
)
assert rendered == 'apps_myapp_harness_secrets_settings__secret="SECRET_KEY=\'replace-with-strong-shared-secret\'"'
finally:
shutil.rmtree(BUILD_MERGE_DIR)
def test_app_depends_on_app():
root_paths = [CLOUDHARNESS_ROOT, RESOURCES]
build_included = ['dependantapp']
values = create_helm_chart(root_paths, output_path=OUT, domain="my.local",
env='', local=False, include=build_included, exclude=[])
cf = create_codefresh_deployment_scripts([CLOUD_HARNESS_PATH, RESOURCES], include=build_included,
envs=[],
base_image_name=values['name'],
helm_values=values, save=False)