Skip to content

Commit 28dd17c

Browse files
committed
selective execution of e2e-tests
1 parent 54c1b06 commit 28dd17c

File tree

3 files changed

+110
-12
lines changed

3 files changed

+110
-12
lines changed

.tekton/pull-request.yaml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,12 @@ spec:
6969
value:
7070
- tasks := strings.any_prefix_match(input, ["task/", "hack/", ".tekton/"])
7171
- tasks_pipelines := strings.any_prefix_match(input, ["task/", "pipelines/", "hack/", ".tekton/"])
72-
- |
73-
e2e_tests if {
74-
some file in input
75-
strings.any_prefix_match(file, ["task/", "pipelines/", "hack/", ".tekton/"])
76-
not endswith(file, "/OWNERS")
77-
}
7872
- check_partner_tasks := strings.any_prefix_match(input, ["partners/", "hack/", ".tekton/"])
7973
runAfter:
8074
- build-appstudio-utils
75+
workspaces:
76+
- name: source
77+
workspace: workspace
8178
- name: task-lint-check
8279
when:
8380
- input: "tasks"
@@ -208,9 +205,9 @@ spec:
208205
- name: source
209206
- name: e2e-tests
210207
when:
211-
- input: "e2e_tests"
208+
- input: "execute_e2e"
212209
operator: "in"
213-
values: ["$(tasks.task-switchboard.results.bindings[*])"]
210+
values: ["$(tasks.task-switchboard.results.run-e2e)"]
214211
params:
215212
- name: e2e_test_namespace
216213
value: $(params.e2e_test_namespace)
@@ -286,9 +283,9 @@ spec:
286283
finally:
287284
- name: e2e-cleanup
288285
when:
289-
- input: "e2e_tests"
286+
- input: "execute_e2e"
290287
operator: "in"
291-
values: ["$(tasks.task-switchboard.results.bindings[*])"]
288+
values: ["$(tasks.task-switchboard.results.run-e2e)"]
292289
params:
293290
- name: e2e_test_namespace
294291
value: $(params.e2e_test_namespace)
@@ -306,9 +303,9 @@ spec:
306303
oc delete --ignore-not-found eventlisteners --all -n $(params.e2e_test_namespace)
307304
- name: pull-request-status-message
308305
when:
309-
- input: "tasks_pipelines"
306+
- input: "execute_e2e"
310307
operator: "in"
311-
values: ["$(tasks.task-switchboard.results.bindings[*])"]
308+
values: ["$(tasks.task-switchboard.results.run-e2e)"]
312309
taskRef:
313310
resolver: git
314311
params:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import sys
5+
import json
6+
import urllib.error
7+
import urllib.request
8+
import subprocess
9+
10+
# Pipelines currently covered by e2e tests
11+
pipelines_covered_by_e2e = ["docker-build", "docker-build-oci-ta", "docker-build-multi-platform-oci-ta", "fbc-builder"]
12+
13+
# Task list which are covered by e2e tests, generated dynamically
14+
tasks_covered_by_e2e = []
15+
16+
# Otherthan tasks and pipelines, related files for which e2e tests needs to be executed
17+
files_covered_by_e2e = [".tekton/pull-request.yaml", ".tekton/tasks/e2e-test.yaml", ".tekton/scripts/determine-if-e2e-execution-needed.py"]
18+
19+
def add_only_unique_task_names(task_list):
20+
for task_name in task_list:
21+
if task_name not in tasks_covered_by_e2e:
22+
tasks_covered_by_e2e.append(task_name)
23+
24+
def get_tasks_covered_by_e2e():
25+
for pipeline_name in pipelines_covered_by_e2e:
26+
pipeline_path = f"pipelines/{pipeline_name}/{pipeline_name}.yaml"
27+
# Get the task names from pipeline spec
28+
result = subprocess.run([f"yq -e '.spec.tasks[].taskRef.name' {pipeline_path}"], shell=True, capture_output=True, text=True)
29+
if result.stderr != "":
30+
print(f"[ERROR] failed to get tasks inside spec.tasks: {result.stderr}")
31+
sys.exit(1)
32+
output = result.stdout
33+
task_names = output.split()
34+
add_only_unique_task_names(task_names)
35+
# Get the task names from pipeline finally
36+
result = subprocess.run([f"yq -e '.spec.finally[].taskRef.name' {pipeline_path}"], shell=True, capture_output=True, text=True)
37+
if result.stderr != "":
38+
print(f"[ERROR] failed to get tasks inside .spec.finally: {result.stderr}")
39+
sys.exit(1)
40+
output = result.stdout
41+
task_names = output.split()
42+
add_only_unique_task_names(task_names)
43+
44+
def get_changed_files_from_pr(pull_number):
45+
updated_files = []
46+
token = os.environ["GITHUB_TOKEN"]
47+
headers = {"Authorization": "Bearer " + token}
48+
base_url = "https://api.github.com"
49+
repo = "konflux-ci/build-definitions"
50+
url = f"{base_url}/repos/{repo}/pulls/{pull_number}/files"
51+
req = urllib.request.Request(url=url, method="GET", headers=headers)
52+
try:
53+
with urllib.request.urlopen(req) as resp:
54+
if resp.status != 200:
55+
print(f"[ERROR] Unknown response status code: {resp.status}")
56+
sys.exit(1)
57+
response_in_json = json.loads(resp.read())
58+
for object in response_in_json:
59+
updated_files.append(object['filename'])
60+
except urllib.error.HTTPError as e:
61+
print(f"[ERROR] got error response: {e.read()} with status {e.code}")
62+
sys.exit(1)
63+
return updated_files
64+
65+
def does_updated_files_covered_by_e2e(updated_files):
66+
required_to_run_e2e = False
67+
for file_path in updated_files:
68+
if file_path.startswith("task/"):
69+
task_name = file_path.split("/")[1]
70+
if task_name in tasks_covered_by_e2e:
71+
required_to_run_e2e = True
72+
break
73+
elif file_path.startswith("pipelines/"):
74+
pipeline_name = file_path.split("/")[1]
75+
if pipeline_name in pipelines_covered_by_e2e:
76+
required_to_run_e2e = True
77+
break
78+
elif file_path in files_covered_by_e2e:
79+
required_to_run_e2e = True
80+
break
81+
else:
82+
print("No need to run e2e tests")
83+
return required_to_run_e2e
84+
85+
if __name__ == '__main__':
86+
if len(sys.argv) != 2:
87+
print("[ERROR] provide correct number of arguments")
88+
pr_number = sys.argv[1]
89+
get_tasks_covered_by_e2e()
90+
updated_files = get_changed_files_from_pr(pr_number)
91+
required_to_run_e2e = does_updated_files_covered_by_e2e(updated_files)
92+
if required_to_run_e2e:
93+
print("execute_e2e")
94+
else:
95+
print("dont_execute_e2e")

.tekton/tasks/task-switchboard.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ spec:
2121
results:
2222
- name: bindings
2323
type: array
24+
- name: run-e2e
25+
type: string
2426
steps:
2527
- name: evaluate
2628
image: $(params.utils_image)
@@ -32,6 +34,7 @@ spec:
3234
key: "git-provider-token"
3335
args:
3436
- "$(params.expressions[*])"
37+
workingDir: $(workspaces.source.path)/source
3538
script: |
3639
#!/bin/bash
3740
set -o errexit
@@ -53,3 +56,6 @@ spec:
5356
'data[_]' \
5457
| jq '[.result.[].expressions.[].value | to_entries | .[] | select(.value == true) | .key]' \
5558
| tee "$(results.bindings.path)"
59+
60+
# Output is "execute_e2e" or "dont_execute_e2e" based on files changed in pr
61+
.tekton/scripts/determine-if-e2e-execution-needed.py "${pr_number}" | tr -d '\n' | tee "$(results.run-e2e.path)"

0 commit comments

Comments
 (0)