From 526ed040cf81a9e62c2dedf60238f96569a2fdea Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Mon, 17 Jun 2024 10:27:35 +0200 Subject: [PATCH 1/5] Add GitHub Action for Pytest Signed-off-by: Petr "Stone" Hracek --- .github/workflows/openshift-pytests.yml | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/openshift-pytests.yml diff --git a/.github/workflows/openshift-pytests.yml b/.github/workflows/openshift-pytests.yml new file mode 100644 index 0000000..46b11cd --- /dev/null +++ b/.github/workflows/openshift-pytests.yml @@ -0,0 +1,41 @@ +on: + issue_comment: + types: + - created +jobs: + check-imagestreams: + runs-on: ubuntu-latest + permissions: + contents: read + statuses: write + if: | + github.event.issue.pull_request + && (contains(github.event.comment.body, '[test-openshift-pytest]') || contains(github.event.comment.body, '[test-all]')) + && contains(fromJson('["OWNER", "MEMBER"]'), github.event.comment.author_association) + steps: + - uses: sclorg/ci-scripts/ocp-stream-generator@master + with: + ref: "refs/pull/${{ github.event.issue.number }}/head" + + openshift-pytests: + name: "${{ matrix.test_case }} PyTests: ${{ matrix.version }} - ${{ matrix.os_test }}" + runs-on: ubuntu-latest + needs: check-imagestreams + concurrency: + group: ocp-pytest-${{ github.event.issue.number }}-${{ matrix.version }}-${{ matrix.os_test }} + cancel-in-progress: true + strategy: + fail-fast: false + matrix: + version: [ "10.3", "10.5", "10.11" ] + os_test: [ "rhel8", "rhel9"] + test_case: [ "openshift-pytest" ] + + steps: + - uses: sclorg/tfaga-wrapper@main + with: + os_test: ${{ matrix.os_test }} + version: ${{ matrix.version }} + test_case: ${{ matrix.test_case }} + public_api_key: ${{ secrets.TF_PUBLIC_API_KEY }} + private_api_key: ${{ secrets.TF_INTERNAL_API_KEY }} From ece26f9eb627295ff4840fe0e0cae1b7ad6a2d29 Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Mon, 17 Jun 2024 15:16:02 +0200 Subject: [PATCH 2/5] Add suite for testing mariadb-container in OpenSHift4 by pytest Signed-off-by: Petr "Stone" Hracek --- test/run-openshift-pytest | 13 ++++++ test/test_mariadb_imagestream.py | 50 +++++++++++++++++++++ test/test_mariadb_imagestream_template.py | 46 +++++++++++++++++++ test/test_mariadb_latest_imagestreams.py | 25 +++++++++++ test/test_mariadb_template.py | 55 +++++++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100755 test/run-openshift-pytest create mode 100644 test/test_mariadb_imagestream.py create mode 100644 test/test_mariadb_imagestream_template.py create mode 100644 test/test_mariadb_latest_imagestreams.py create mode 100644 test/test_mariadb_template.py diff --git a/test/run-openshift-pytest b/test/run-openshift-pytest new file mode 100755 index 0000000..e677e4e --- /dev/null +++ b/test/run-openshift-pytest @@ -0,0 +1,13 @@ +#!/bin/bash +# +# IMAGE_NAME specifies a name of the candidate image used for testing. +# The image has to be available before this script is executed. +# VERSION specifies the major version of the MariaDB in format of X.Y +# OS specifies RHEL version (e.g. OS=rhel7) +# + +THISDIR=$(dirname ${BASH_SOURCE[0]}) + +git show -s + +cd "${THISDIR}" && python3 -m pytest --showlocals -vv test_mariadb_*.py diff --git a/test/test_mariadb_imagestream.py b/test/test_mariadb_imagestream.py new file mode 100644 index 0000000..e309aee --- /dev/null +++ b/test/test_mariadb_imagestream.py @@ -0,0 +1,50 @@ +import os +import sys + +import pytest + +from container_ci_suite.openshift import OpenShiftAPI +from container_ci_suite.utils import check_variables + +if not check_variables(): + print("At least one variable from IMAGE_NAME, OS, SINGLE_VERSION is missing.") + sys.exit(1) + + +VERSION = os.getenv("SINGLE_VERSION") +IMAGE_NAME = os.getenv("IMAGE_NAME") +OS = os.getenv("TARGET") +TAGS = { + "rhel8": "-el8", + "rhel9": "-el9" +} +TAG = TAGS.get(OS, None) + + +class TestMariaDBImagestreamTemplate: + + def setup_method(self): + self.oc_api = OpenShiftAPI(pod_name_prefix="mariadb", version=VERSION) + + def teardown_method(self): + self.oc_api.delete_project() + + @pytest.mark.parametrize( + "template", + [ + "mariadb-ephemeral-template.json", + "mariadb-persistent-template.json" + ] + ) + def test_mariadb_imagestream_template(self, template): + os_name = ''.join(i for i in OS if not i.isdigit()) + print(os.getcwd()) + assert self.oc_api.deploy_image_stream_template( + imagestream_file=f"imagestreams/mariadb-{os_name}.json", + template_file=f"examples/{template}", + app_name=self.oc_api.pod_name_prefix, + openshift_args=[ + f"MARIADB_VERSION={VERSION}{TAG}" + ] + ) + assert self.oc_api.is_pod_running(pod_name_prefix=self.oc_api.pod_name_prefix) diff --git a/test/test_mariadb_imagestream_template.py b/test/test_mariadb_imagestream_template.py new file mode 100644 index 0000000..d5515e0 --- /dev/null +++ b/test/test_mariadb_imagestream_template.py @@ -0,0 +1,46 @@ +import os +import sys + +import pytest + +from container_ci_suite.openshift import OpenShiftAPI +from container_ci_suite.utils import check_variables + +if not check_variables(): + print("At least one variable from IMAGE_NAME, OS, SINGLE_VERSION is missing.") + sys.exit(1) + + +VERSION = os.getenv("SINGLE_VERSION") +IMAGE_NAME = os.getenv("IMAGE_NAME") +OS = os.getenv("TARGET") +TAGS = { + "rhel8": "-el8", + "rhel9": "-el9" +} +TAG = TAGS.get(OS, None) + + +class TestMariaDBImagestreamTemplate: + + def setup_method(self): + self.oc_api = OpenShiftAPI(pod_name_prefix="mariadb", version=VERSION) + + def teardown_method(self): + self.oc_api.delete_project() + + @pytest.mark.parametrize( + "template", + [ + "mariadb-ephemeral-template.json", + "mariadb-persistent-template.json" + ] + ) + def test_mariadb_imagestream_template(self, template): + os_name = ''.join(i for i in OS if not i.isdigit()) + assert self.oc_api.deploy_image_stream_template( + imagestream_file=f"imagestreams/mariadb-{os_name}.json", + template_file=f"examples/{template}", + app_name=self.oc_api.pod_name_prefix + ) + assert self.oc_api.is_pod_running(pod_name_prefix=self.oc_api.pod_name_prefix) diff --git a/test/test_mariadb_latest_imagestreams.py b/test/test_mariadb_latest_imagestreams.py new file mode 100644 index 0000000..919c73c --- /dev/null +++ b/test/test_mariadb_latest_imagestreams.py @@ -0,0 +1,25 @@ +import os +import sys +import pytest + +from pathlib import Path + +from container_ci_suite.imagestreams import ImageStreamChecker +from container_ci_suite.utils import check_variables + +TEST_DIR = Path(os.path.abspath(os.path.dirname(__file__))) + +if not check_variables(): + print("At least one variable from IMAGE_NAME, OS, SINGLE_VERSION is missing.") + sys.exit(1) + + +class TestLatestImagestreams: + + def setup_method(self): + self.isc = ImageStreamChecker(working_dir=TEST_DIR.parent) + + def test_latest_imagestream(self): + self.latest_version = self.isc.get_latest_version() + assert self.latest_version != "" + self.isc.check_imagestreams(self.latest_version) diff --git a/test/test_mariadb_template.py b/test/test_mariadb_template.py new file mode 100644 index 0000000..40c6478 --- /dev/null +++ b/test/test_mariadb_template.py @@ -0,0 +1,55 @@ +import os +import sys + +import pytest + +from container_ci_suite.openshift import OpenShiftAPI +from container_ci_suite.utils import check_variables + +if not check_variables(): + print("At least one variable from IMAGE_NAME, OS, SINGLE_VERSION is missing.") + sys.exit(1) + + +VERSION = os.getenv("SINGLE_VERSION") +IMAGE_NAME = os.getenv("IMAGE_NAME") +OS = os.getenv("TARGET") + + +class TestMariaDBDeployTemplate: + + def setup_method(self): + self.oc_api = OpenShiftAPI(pod_name_prefix="mariadb-testing", version=VERSION) + + def teardown_method(self): + self.oc_api.delete_project() + + @pytest.mark.parametrize( + "template", + [ + "mariadb-ephemeral-template.json", + "mariadb-persistent-template.json" + ] + ) + def test_python_template_inside_cluster(self, template): + short_version = VERSION.replace(".", "") + assert self.oc_api.deploy_template_with_image( + image_name=IMAGE_NAME, + template=template, + name_in_template="mariadb", + openshift_args=[ + f"MARIADB_VERSION={VERSION}", + f"DATABASE_SERVICE_NAME={self.oc_api.pod_name_prefix}", + f"MYSQL_USER=testu", + f"MYSQL_PASSWORD=testp", + f"MYSQL_DATABASE=testdb" + ] + ) + + assert self.oc_api.is_pod_running(pod_name_prefix=self.oc_api.pod_name_prefix) + assert self.oc_api.check_command_internal( + image_name=f"registry.redhat.io/{OS}/mariadb-{short_version}", + service_name=self.oc_api.pod_name_prefix, + cmd="echo 'SELECT 42 as testval\\g' | mysql --connect-timeout=15 -h testdb -utestu -ptestp", + expected_output="42" + ) From 94d022c0fa187926ef71e049e48b88f1eb779f54 Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Wed, 26 Jun 2024 11:53:27 +0200 Subject: [PATCH 3/5] Add more outputs to PyTest module Signed-off-by: Petr "Stone" Hracek --- test/run-openshift-pytest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/run-openshift-pytest b/test/run-openshift-pytest index e677e4e..e9e16b9 100755 --- a/test/run-openshift-pytest +++ b/test/run-openshift-pytest @@ -10,4 +10,4 @@ THISDIR=$(dirname ${BASH_SOURCE[0]}) git show -s -cd "${THISDIR}" && python3 -m pytest --showlocals -vv test_mariadb_*.py +cd "${THISDIR}" && python3 -m pytest -s -rxfapP --showlocals -vv test_mariadb_*.py From 1538fb8cd27dee9e53f03ab8c627f4893d445b0b Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Wed, 26 Jun 2024 14:09:02 +0200 Subject: [PATCH 4/5] Add tags into template Signed-off-by: Petr "Stone" Hracek --- test/test_mariadb_template.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/test_mariadb_template.py b/test/test_mariadb_template.py index 40c6478..efc87a0 100644 --- a/test/test_mariadb_template.py +++ b/test/test_mariadb_template.py @@ -15,6 +15,12 @@ IMAGE_NAME = os.getenv("IMAGE_NAME") OS = os.getenv("TARGET") +TAGS = { + "rhel8": "-el8", + "rhel9": "-el9" +} +TAG = TAGS.get(OS, None) + class TestMariaDBDeployTemplate: @@ -38,7 +44,7 @@ def test_python_template_inside_cluster(self, template): template=template, name_in_template="mariadb", openshift_args=[ - f"MARIADB_VERSION={VERSION}", + f"MARIADB_VERSION={VERSION}{TAG}", f"DATABASE_SERVICE_NAME={self.oc_api.pod_name_prefix}", f"MYSQL_USER=testu", f"MYSQL_PASSWORD=testp", From 59aa762ec92ef940a6b3c1036bd8b8f8ea491a6c Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Wed, 26 Jun 2024 14:42:48 +0200 Subject: [PATCH 5/5] Import mariadb imagestreams for mariadb template Signed-off-by: Petr "Stone" Hracek --- test/test_mariadb_template.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_mariadb_template.py b/test/test_mariadb_template.py index efc87a0..753044d 100644 --- a/test/test_mariadb_template.py +++ b/test/test_mariadb_template.py @@ -26,6 +26,7 @@ class TestMariaDBDeployTemplate: def setup_method(self): self.oc_api = OpenShiftAPI(pod_name_prefix="mariadb-testing", version=VERSION) + self.oc_api.import_is("imagestreams/mariadb-rhel.json", "", skip_check=True) def teardown_method(self): self.oc_api.delete_project()