From 11622de170ebaae246a561c30aaeef52923131a5 Mon Sep 17 00:00:00 2001 From: Michele Baldessari Date: Fri, 6 Dec 2024 15:23:34 +0100 Subject: [PATCH 1/2] Stop skipping the machine-sensor test With the new IE we need to push the git change directly to gitea. For this we need to fetch the credentials from the HUB first. Tested and got a passing test. Co-Authored-By: Akos Eros --- tests/interop/run_tests.sh | 4 ++ tests/interop/test_toggle_machine_sensor.py | 55 +++++++++++++++------ 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/tests/interop/run_tests.sh b/tests/interop/run_tests.sh index 52a759ee4..44ac727b0 100755 --- a/tests/interop/run_tests.sh +++ b/tests/interop/run_tests.sh @@ -37,6 +37,10 @@ pytest -lv --disable-warnings test_check_logging_hub.py --kubeconfig $KUBECONFIG KUBECONFIG=$KUBECONFIG_EDGE pytest -lv --disable-warnings test_check_logging_edge.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_check_logging_edge.xml +# We need a few things from the hub cluster before working on the edge cluster +export GITEA_PASS=$(oc --kubeconfig $KUBECONFIG extract -n vp-gitea secret/gitea-admin-secret --to=- --keys=password 2>/dev/null) +export GITEA_USER=$(oc --kubeconfig $KUBECONFIG extract -n vp-gitea secret/gitea-admin-secret --to=- --keys=username 2>/dev/null) +export GITEA_ROUTE=$(oc --kubeconfig $KUBECONFIG get routes -n vp-gitea gitea-route -o jsonpath='{.spec.host}') KUBECONFIG=$KUBECONFIG_EDGE pytest -lv --disable-warnings test_toggle_machine_sensor.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_toggle_machine_sensor.xml pytest -lv --disable-warnings test_pipeline_build_check.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_pipeline_build_check.xml diff --git a/tests/interop/test_toggle_machine_sensor.py b/tests/interop/test_toggle_machine_sensor.py index 321e202a3..49a8a2dba 100644 --- a/tests/interop/test_toggle_machine_sensor.py +++ b/tests/interop/test_toggle_machine_sensor.py @@ -16,10 +16,6 @@ oc = os.environ["HOME"] + "/oc_client/oc" -# FIXME(bandini): For now we skip this test, we need to rewrite it so that the change pushed in git -# is done in the in-cluster gitea and not on the upstream repo. Otherwise the change will never be -# propagated -@pytest.mark.skip(reason="Need to push the changes to the in-cluster gitea") @pytest.mark.toggle_machine_sensor def test_toggle_machine_sensor(openshift_dyn_client): logger.info("Testing machine-sensor config change") @@ -76,21 +72,50 @@ def test_toggle_machine_sensor(openshift_dyn_client): orig_content=orig_content, new_content=new_content, ) + gitea_pass = os.getenv("GITEA_PASS") + gitea_user = os.getenv("GITEA_USER") + gitea_route = os.getenv("GITEA_ROUTE") + gitea_url = ( + f"https://{gitea_user}:{gitea_pass}@{gitea_route}/{gitea_user}/industrial-edge" + ) + logger.info( + f"Using the gitea user {gitea_user} on https://{gitea_route}/{gitea_user}/industrial-edge" + ) + if gitea_pass == "" or gitea_user == "" or gitea_route == "": + err_msg = "gitea_pass or gitea_user or gitea_route were empty" + logger.error(f"FAIL: {err_msg}") + assert False, err_msg logger.info("Merge the change") if os.getenv("EXTERNAL_TEST") != "true": - subprocess.run(["git", "add", machine_sensor_file], cwd=patterns_repo) - subprocess.run( - ["git", "commit", "-m", "Toggling SENSOR_TEMPERATURE_ENABLED"], - cwd=patterns_repo, - ) - push = subprocess.run( - ["git", "push"], cwd=patterns_repo, capture_output=True, text=True - ) + cur_dir = patterns_repo else: - subprocess.run(["git", "add", machine_sensor_file]) - subprocess.run(["git", "commit", "-m", "Toggling SENSOR_TEMPERATURE_ENABLED"]) - push = subprocess.run(["git", "push"], capture_output=True, text=True) + cur_dir = os.getcwd() + + subprocess.run( + [ + "git", + "-c", + "http.sslVerify=false", + "remote", + "add", + "gitea-qe", + "-f", + gitea_url, + ], + cwd=cur_dir, + ) + subprocess.run(["git", "add", machine_sensor_file], cwd=cur_dir) + subprocess.run( + ["git", "commit", "-m", "Toggling SENSOR_TEMPERATURE_ENABLED"], + cwd=cur_dir, + ) + push = subprocess.run( + ["git", "-c", "http.sslVerify=false", "push", "gitea-qe"], + cwd=cur_dir, + capture_output=True, + text=True, + ) logger.info(push.stdout) logger.info(push.stderr) From 11ddfc38732a0bc346ac3fb1001863269e59cac2 Mon Sep 17 00:00:00 2001 From: Michele Baldessari Date: Mon, 9 Dec 2024 10:44:09 +0100 Subject: [PATCH 2/2] Fetch gitea info from inside the test --- tests/interop/run_tests.sh | 6 +-- tests/interop/test_toggle_machine_sensor.py | 42 +++++++++++++++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/tests/interop/run_tests.sh b/tests/interop/run_tests.sh index 44ac727b0..d0dafa4fb 100755 --- a/tests/interop/run_tests.sh +++ b/tests/interop/run_tests.sh @@ -37,11 +37,7 @@ pytest -lv --disable-warnings test_check_logging_hub.py --kubeconfig $KUBECONFIG KUBECONFIG=$KUBECONFIG_EDGE pytest -lv --disable-warnings test_check_logging_edge.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_check_logging_edge.xml -# We need a few things from the hub cluster before working on the edge cluster -export GITEA_PASS=$(oc --kubeconfig $KUBECONFIG extract -n vp-gitea secret/gitea-admin-secret --to=- --keys=password 2>/dev/null) -export GITEA_USER=$(oc --kubeconfig $KUBECONFIG extract -n vp-gitea secret/gitea-admin-secret --to=- --keys=username 2>/dev/null) -export GITEA_ROUTE=$(oc --kubeconfig $KUBECONFIG get routes -n vp-gitea gitea-route -o jsonpath='{.spec.host}') -KUBECONFIG=$KUBECONFIG_EDGE pytest -lv --disable-warnings test_toggle_machine_sensor.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_toggle_machine_sensor.xml +KUBECONFIG_HUB=$KUBECONFIG KUBECONFIG=$KUBECONFIG_EDGE pytest -lv --disable-warnings test_toggle_machine_sensor.py --kubeconfig $KUBECONFIG_EDGE --junit-xml $WORKSPACE/test_toggle_machine_sensor.xml pytest -lv --disable-warnings test_pipeline_build_check.py --kubeconfig $KUBECONFIG --junit-xml $WORKSPACE/test_pipeline_build_check.xml diff --git a/tests/interop/test_toggle_machine_sensor.py b/tests/interop/test_toggle_machine_sensor.py index 49a8a2dba..ef9f84350 100644 --- a/tests/interop/test_toggle_machine_sensor.py +++ b/tests/interop/test_toggle_machine_sensor.py @@ -4,8 +4,12 @@ import subprocess import time +import kubernetes import pytest from ocp_resources.config_map import ConfigMap +from ocp_resources.route import Route +from ocp_resources.secret import Secret +from openshift.dynamic import DynamicClient from openshift.dynamic.exceptions import NotFoundError from validatedpatterns_tests.interop.edge_util import modify_file_content @@ -16,6 +20,40 @@ oc = os.environ["HOME"] + "/oc_client/oc" +# returns (user, pass, route) tuple +def get_gitea_info(): + kubefile = os.getenv("KUBECONFIG_HUB") + kexp = os.path.expandvars(kubefile) + ocp_hub = DynamicClient( + client=kubernetes.config.new_client_from_config(config_file=kexp) + ) + logger.info("Getting HUB gitea info") + try: + gitea_secret_obj = Secret.get( + dyn_client=ocp_hub, namespace="vp-gitea", name="gitea-admin-secret" + ) + gitea_secret = next(gitea_secret_obj) + except NotFoundError: + err_msg = "The gitea-admin-secret was not found in ns vp-gitea" + logger.error(f"FAIL: {err_msg}") + assert False, err_msg + username = gitea_secret.instance.data.username + password = gitea_secret.instance.data.password + + try: + gitea_route_obj = Route.get( + dyn_client=ocp_hub, namespace="vp-gitea", name="gitea-route" + ) + gitea_route = next(gitea_route_obj) + except NotFoundError: + err_msg = "The gitea-route was not found in ns vp-gitea" + logger.error(f"FAIL: {err_msg}") + assert False, err_msg + route = gitea_route.instance.spec.host + + return (username, password, route) + + @pytest.mark.toggle_machine_sensor def test_toggle_machine_sensor(openshift_dyn_client): logger.info("Testing machine-sensor config change") @@ -72,9 +110,7 @@ def test_toggle_machine_sensor(openshift_dyn_client): orig_content=orig_content, new_content=new_content, ) - gitea_pass = os.getenv("GITEA_PASS") - gitea_user = os.getenv("GITEA_USER") - gitea_route = os.getenv("GITEA_ROUTE") + (gitea_user, gitea_pass, gitea_route) = get_gitea_info() gitea_url = ( f"https://{gitea_user}:{gitea_pass}@{gitea_route}/{gitea_user}/industrial-edge" )