Skip to content

Commit 14edff9

Browse files
committed
(ci-cd) fix regression with test images not being built as needed
1 parent 8db27c9 commit 14edff9

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

tools/deployment-cli-tools/ch_cli_tools/codefresh.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,21 @@ def add_unit_test_step(app_config: ApplicationHarnessConfig):
409409
codefresh_steps_from_base_path(join(root_path, APPS_PATH), include=helm_values[KEY_TASK_IMAGES].keys())
410410
codefresh_steps_from_base_path(join(root_path, APPS_PATH), include=build_included)
411411

412-
if CD_E2E_TEST_STEP in steps and steps[CD_E2E_TEST_STEP].get("scale"):
413-
name = "test-e2e"
412+
# Search all root_paths for test images after scale is fully populated from all apps.
413+
# Test images (test-e2e, test-api) may live in a different root_path than the apps that use them.
414+
if CD_E2E_TEST_STEP in steps and steps[CD_E2E_TEST_STEP].get("scale"):
415+
name = "test-e2e"
416+
for root_path in root_paths:
414417
if codefresh_steps_from_base_path(join(root_path, TEST_IMAGES_PATH), include=(name,), publish=False):
415-
steps[CD_E2E_TEST_STEP]["image"] = image_tag_with_variables(f"{base_name}/{name}", helm_values.registry.name, app_specific_tag_variable(name))
418+
steps[CD_E2E_TEST_STEP]["image"] = image_tag_with_variables(
419+
f"{base_image_name}/{name}", helm_values.registry.name, app_specific_tag_variable(name))
416420

417-
if CD_API_TEST_STEP in steps and steps[CD_API_TEST_STEP].get("scale"):
418-
name = "test-api"
421+
if CD_API_TEST_STEP in steps and steps[CD_API_TEST_STEP].get("scale"):
422+
name = "test-api"
423+
for root_path in root_paths:
419424
if codefresh_steps_from_base_path(join(root_path, TEST_IMAGES_PATH), include=(name,), fixed_context=relpath(root_path, os.getcwd()), publish=False):
420-
steps[CD_API_TEST_STEP]["image"] = image_tag_with_variables(f"{base_name}/{name}", helm_values.registry.name, app_specific_tag_variable(name))
425+
steps[CD_API_TEST_STEP]["image"] = image_tag_with_variables(
426+
f"{base_image_name}/{name}", helm_values.registry.name, app_specific_tag_variable(name))
421427

422428
if build_steps:
423429

tools/deployment-cli-tools/tests/test_codefresh.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,60 @@ def test_create_codefresh_configuration_multienv():
188188
shutil.rmtree(BUILD_MERGE_DIR)
189189

190190

191+
def test_test_images_built_only_when_tests_exist_in_project_apps():
192+
"""Test images are built iff the corresponding test type is enabled on at least one app.
193+
194+
Positive case: app in RESOURCES (not CH) with e2e enabled → test-e2e must be built even
195+
though the test image lives in cloud-harness (root_paths[0]).
196+
197+
Negative case: same app without e2e enabled → test-e2e must NOT be built.
198+
199+
Regression: the test image search was done per root_path, so if the apps with tests are in
200+
root_path[1] (project) and the test images are in root_path[0] (cloud-harness), the images
201+
were never found because scale was empty when root_path[0] was scanned.
202+
"""
203+
def _build_steps(e2e_enabled):
204+
values = create_helm_chart(
205+
[CLOUDHARNESS_ROOT, RESOURCES],
206+
output_path=OUT,
207+
include=['myapp'],
208+
exclude=['events'],
209+
domain="my.local",
210+
namespace='test',
211+
env=['dev', 'test'],
212+
local=False,
213+
tag=1,
214+
registry='reg'
215+
)
216+
root_paths = preprocess_build_overrides(
217+
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
218+
helm_values=values,
219+
merge_build_path=BUILD_MERGE_DIR
220+
)
221+
build_included = [app['harness']['name']
222+
for app in values['apps'].values() if 'harness' in app]
223+
values.apps["myapp"].harness.test.e2e.enabled = e2e_enabled
224+
cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
225+
envs=['dev', 'test'],
226+
base_image_name=values['name'],
227+
helm_values=values, save=False)
228+
l1_steps = cf['steps']
229+
return {name: step for build_step in [STEP_0, STEP_1, STEP_2, STEP_3]
230+
if build_step in l1_steps
231+
for name, step in l1_steps[build_step]['steps'].items()}
232+
233+
try:
234+
with_tests = _build_steps(e2e_enabled=True)
235+
assert "test-e2e" in with_tests, \
236+
"test-e2e image must be built when a project app has e2e tests, even if the image lives in cloud-harness"
237+
238+
without_tests = _build_steps(e2e_enabled=False)
239+
assert "test-e2e" not in without_tests, \
240+
"test-e2e image must NOT be built when no app has e2e tests enabled"
241+
finally:
242+
shutil.rmtree(BUILD_MERGE_DIR, ignore_errors=True)
243+
244+
191245
def test_create_codefresh_configuration_tests():
192246
values = create_helm_chart(
193247
[CLOUDHARNESS_ROOT, RESOURCES],

0 commit comments

Comments
 (0)