From 386ce4ba604264055f6be396267dcb22df53b448 Mon Sep 17 00:00:00 2001 From: Mark LaBonte Date: Thu, 15 Aug 2024 20:20:00 -0400 Subject: [PATCH 1/4] Add CI badge creation --- tests/interop/create_ci_badge.py | 84 ++++++++++++++++++++++++++++++++ tests/interop/requirements.txt | 1 + tests/interop/run_tests.sh | 11 ++++- 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/interop/create_ci_badge.py diff --git a/tests/interop/create_ci_badge.py b/tests/interop/create_ci_badge.py new file mode 100644 index 00000000..aaf3429e --- /dev/null +++ b/tests/interop/create_ci_badge.py @@ -0,0 +1,84 @@ +import json +import os +import subprocess +from junitparser import JUnitXml +from datetime import datetime + +oc = os.environ["HOME"] + "/oc_client/oc" + +ci_badge = { + "schemaVersion": 1, + "label": "Community test", + "message": "", + "color": "red", + "openshiftVersion": "", + "infraProvider": os.environ.get("INFRA_PROVIDER"), + "patternName": os.environ.get("PATTERN_NAME"), + "patternRepo": "", + "patternBranch": "", + "date": datetime.today().strftime("%Y-%m-%d"), + "testSource": "Community", + "debugInfo": None, +} + + +def get_openshift_version(): + try: + version_ret = subprocess.run([oc, "version", "-o", "json"], capture_output=True) + version_out = version_ret.stdout.decode("utf-8") + openshift_version = json.loads(version_out)["openshiftVersion"] + major_minor = ".".join(openshift_version.split(".")[:-1]) + return openshift_version, major_minor + except KeyError as e: + print("KeyError:" + str(e)) + return None + + +if __name__ == "__main__": + versions = get_openshift_version() + ci_badge["openshiftVersion"] = versions[0] + + pattern_repo = subprocess.run( + ["git", "config", "--get", "remote.origin.url"], capture_output=True, text=True + ) + pattern_branch = subprocess.run( + ["git", "branch", "--show-current"], capture_output=True, text=True + ) + + ci_badge["patternRepo"] = pattern_repo.stdout.strip() + ci_badge["patternBranch"] = pattern_branch.stdout.strip() + + # Check each xml file for failures + results_dir = os.environ.get("WORKSPACE") + failures = 0 + + for file in os.listdir(results_dir): + if file.startswith("test_") and file.endswith(".xml"): + with open(os.path.join(results_dir, file), "r") as result_file: + xml = JUnitXml.fromfile(result_file) + for suite in xml: + for case in suite: + if case.result: + print(case.result) + failures += 1 + + # Determine badge color from results + if failures == 0: + ci_badge["color"] = "green" + + # For now we assume `message` is the same as patternBranch + ci_badge["message"] = ci_badge["patternBranch"] + + ci_badge_json_basename = ( + os.environ.get("PATTERN_SHORTNAME") + + "-" + + os.environ.get("INFRA_PROVIDER") + + "-" + + versions[1] + + "-stable-badge.json" + ) + ci_badge_json_filename = os.path.join(results_dir, ci_badge_json_basename) + print(f"Creating CI badge file at: {ci_badge_json_filename}") + + with open(ci_badge_json_filename, "w") as ci_badge_file: + json.dump(ci_badge, ci_badge_file) diff --git a/tests/interop/requirements.txt b/tests/interop/requirements.txt index 7898fa21..3b20852f 100644 --- a/tests/interop/requirements.txt +++ b/tests/interop/requirements.txt @@ -2,4 +2,5 @@ pytest kubernetes openshift openshift-python-wrapper +junitparser git+https://github.com/validatedpatterns/vp-qe-test-common.git@development#egg=vp-qe-test-common \ No newline at end of file diff --git a/tests/interop/run_tests.sh b/tests/interop/run_tests.sh index 92d5fd27..17c43fb3 100755 --- a/tests/interop/run_tests.sh +++ b/tests/interop/run_tests.sh @@ -1,6 +1,8 @@ #!/usr/bin/bash export EXTERNAL_TEST="true" +export PATTERN_NAME="Retail" +export PATTERN_SHORTNAME="retail" if [ -z "${KUBECONFIG}" ]; then echo "No kubeconfig file set for hub cluster" @@ -12,6 +14,11 @@ if [ -z "${KUBECONFIG_EDGE}" ]; then exit 1 fi +if [ -z "${INFRA_PROVIDER}" ]; then + echo "INFRA_PROVIDER is not defined" + exit 1 +fi + if [ -z "${WORKSPACE}" ]; then export WORKSPACE=/tmp fi @@ -24,4 +31,6 @@ pytest -lv --disable-warnings test_validate_hub_site_components.py --kubeconfig KUBECONFIG=$KUBECONFIG_EDGE pytest -lv --disable-warnings test_validate_edge_site_components.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_validate_edge_site_components.xml -pytest -lv --disable-warnings test_validate_pipelineruns.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_validate_pipelineruns.xml \ No newline at end of file +pytest -lv --disable-warnings test_validate_pipelineruns.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_validate_pipelineruns.xml + +python3 create_ci_badge.py From c0ca63873886071ed77fe941fdf0ec10620a1240 Mon Sep 17 00:00:00 2001 From: Mark LaBonte Date: Thu, 15 Aug 2024 20:36:00 -0400 Subject: [PATCH 2/4] Fix isort error, blow off mypy errors --- tests/interop/create_ci_badge.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/interop/create_ci_badge.py b/tests/interop/create_ci_badge.py index aaf3429e..1527642e 100644 --- a/tests/interop/create_ci_badge.py +++ b/tests/interop/create_ci_badge.py @@ -1,9 +1,10 @@ import json import os import subprocess -from junitparser import JUnitXml from datetime import datetime +from junitparser import JUnitXml + oc = os.environ["HOME"] + "/oc_client/oc" ci_badge = { @@ -54,8 +55,8 @@ def get_openshift_version(): for file in os.listdir(results_dir): if file.startswith("test_") and file.endswith(".xml"): - with open(os.path.join(results_dir, file), "r") as result_file: - xml = JUnitXml.fromfile(result_file) + with open(os.path.join(results_dir, file), "r") as result_file: # type: ignore + xml = JUnitXml.fromfile(result_file) # type: ignore for suite in xml: for case in suite: if case.result: @@ -70,14 +71,14 @@ def get_openshift_version(): ci_badge["message"] = ci_badge["patternBranch"] ci_badge_json_basename = ( - os.environ.get("PATTERN_SHORTNAME") + os.environ.get("PATTERN_SHORTNAME") # type: ignore + "-" + os.environ.get("INFRA_PROVIDER") + "-" + versions[1] + "-stable-badge.json" ) - ci_badge_json_filename = os.path.join(results_dir, ci_badge_json_basename) + ci_badge_json_filename = os.path.join(results_dir, ci_badge_json_basename) # type: ignore print(f"Creating CI badge file at: {ci_badge_json_filename}") with open(ci_badge_json_filename, "w") as ci_badge_file: From 948c0aad4a423f81118c782acb379e52fe423be4 Mon Sep 17 00:00:00 2001 From: Mark LaBonte Date: Thu, 15 Aug 2024 20:44:26 -0400 Subject: [PATCH 3/4] Fix more linting errors --- tests/interop/create_ci_badge.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/interop/create_ci_badge.py b/tests/interop/create_ci_badge.py index 1527642e..ad9d26a1 100644 --- a/tests/interop/create_ci_badge.py +++ b/tests/interop/create_ci_badge.py @@ -55,8 +55,8 @@ def get_openshift_version(): for file in os.listdir(results_dir): if file.startswith("test_") and file.endswith(".xml"): - with open(os.path.join(results_dir, file), "r") as result_file: # type: ignore - xml = JUnitXml.fromfile(result_file) # type: ignore + with open(os.path.join(results_dir, file), "r") as result_file: # type: ignore + xml = JUnitXml.fromfile(result_file) # type: ignore for suite in xml: for case in suite: if case.result: @@ -71,14 +71,14 @@ def get_openshift_version(): ci_badge["message"] = ci_badge["patternBranch"] ci_badge_json_basename = ( - os.environ.get("PATTERN_SHORTNAME") # type: ignore + os.environ.get("PATTERN_SHORTNAME") # type: ignore + "-" + os.environ.get("INFRA_PROVIDER") + "-" + versions[1] + "-stable-badge.json" ) - ci_badge_json_filename = os.path.join(results_dir, ci_badge_json_basename) # type: ignore + ci_badge_json_filename = os.path.join(results_dir, ci_badge_json_basename) # type: ignore print(f"Creating CI badge file at: {ci_badge_json_filename}") with open(ci_badge_json_filename, "w") as ci_badge_file: From e4654ea4b7ffaa01b2e804b448bbf19b4404b45e Mon Sep 17 00:00:00 2001 From: Mark LaBonte Date: Thu, 15 Aug 2024 21:02:30 -0400 Subject: [PATCH 4/4] Remove unneeded print statement --- tests/interop/create_ci_badge.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/interop/create_ci_badge.py b/tests/interop/create_ci_badge.py index ad9d26a1..8ed179a4 100644 --- a/tests/interop/create_ci_badge.py +++ b/tests/interop/create_ci_badge.py @@ -60,7 +60,6 @@ def get_openshift_version(): for suite in xml: for case in suite: if case.result: - print(case.result) failures += 1 # Determine badge color from results