From 7d268a4b7e12408a7057daf22c28cfce0fe52158 Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Tue, 26 Mar 2024 09:03:06 +0100 Subject: [PATCH 1/2] Add tests for CakePHP App with MySQL database Signed-off-by: Petr "Stone" Hracek --- tests/test_cakephp_mysql.py | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/test_cakephp_mysql.py diff --git a/tests/test_cakephp_mysql.py b/tests/test_cakephp_mysql.py new file mode 100644 index 000000000..40503da22 --- /dev/null +++ b/tests/test_cakephp_mysql.py @@ -0,0 +1,83 @@ +import os + +import pytest +from pathlib import Path + +from container_ci_suite.openshift import OpenShiftAPI + +test_dir = Path(os.path.abspath(os.path.dirname(__file__))) + +VERSION = os.getenv("SINGLE_VERSION") +if not VERSION: + VERSION = "8.1-ubi8" + + +class TestCakePHPAppExTemplate: + + def setup_method(self): + self.oc_api = OpenShiftAPI(pod_name_prefix="cakephp-example") + json_raw_file = self.oc_api.get_raw_url_for_json(container="s2i-php-container", dir="imagestreams", + filename="php-rhel.json") + self.oc_api.import_is(path=json_raw_file, name="php") + json_raw_file = self.oc_api.get_raw_url_for_json( + container="mysql-container", dir="imagestreams", filename="mysql-rhel.json" + ) + self.oc_api.import_is(path=json_raw_file, name="mysql") + + def teardown_method(self): + self.oc_api.delete_project() + + def test_template_inside_cluster(self): + branch_to_test = "master" + expected_output = "Welcome to PHP" + if VERSION.startswith("7.4") or VERSION.startswith("8.0"): + branch_to_test = "4.X" + expected_output = "Welcome to CakePHP 4" + if VERSION.startswith("8.1") or VERSION.startswith("8.2"): + branch_to_test = "5.X" + expected_output = "Welcome to CakePHP 5" + + template_json = self.oc_api.get_raw_url_for_json( + container="cakephp-ex", branch=branch_to_test, dir="openshift/templates", filename="cakephp-mysql-persistent.json" + ) + assert self.oc_api.deploy_template( + template=template_json, name_in_template="cakephp-example", expected_output=expected_output, + openshift_args=[ + f"SOURCE_REPOSITORY_REF={branch_to_test}", + f"PHP_VERSION={VERSION}", + "NAME=cakephp-example", + "MYSQL_VERSION=8.0-el8" + ] + ) + assert self.oc_api.template_deployed(name_in_template="cakephp-example") + assert self.oc_api.check_response_inside_cluster( + name_in_template="cakephp-example", expected_output=expected_output + ) + + def test_template_by_request(self): + branch_to_test = "master" + expected_output = "Welcome to PHP" + if VERSION.startswith("7.4") or VERSION.startswith("8.0"): + branch_to_test = "4.X" + expected_output = "Welcome to CakePHP 4" + elif VERSION.startswith("8.1") or VERSION.startswith("8.2"): + branch_to_test = "5.X" + expected_output = "Welcome to CakePHP 5" + + template_json = self.oc_api.get_raw_url_for_json( + container="cakephp-ex", branch=branch_to_test, dir="openshift/templates", filename="cakephp-mysql-persistent.json" + ) + assert self.oc_api.deploy_template( + template=template_json, name_in_template="cakephp-example", expected_output=expected_output, + openshift_args=[ + f"SOURCE_REPOSITORY_REF={branch_to_test}", + f"PHP_VERSION={VERSION}", + "NAME=cakephp-example", + "MYSQL_VERSION=8.0-el8" + ] + ) + assert self.oc_api.template_deployed(name_in_template="cakephp-example") + assert self.oc_api.check_response_outside_cluster( + protocol="https", + name_in_template="cakephp-example", expected_output=expected_output + ) From 64d61cc03cc336ea4db0acf7d6d396a5d8b9961e Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Tue, 26 Mar 2024 14:16:11 +0100 Subject: [PATCH 2/2] Update pull request with local templates Signed-off-by: Petr "Stone" Hracek --- .../templates/cakephp-mysql-persistent.json | 88 +++++++++++++++---- tests/test_cakephp.py | 28 +++++- tests/test_cakephp_mysql.py | 30 ++++++- 3 files changed, 124 insertions(+), 22 deletions(-) diff --git a/openshift/templates/cakephp-mysql-persistent.json b/openshift/templates/cakephp-mysql-persistent.json index 0c5137389..489ef2975 100644 --- a/openshift/templates/cakephp-mysql-persistent.json +++ b/openshift/templates/cakephp-mysql-persistent.json @@ -11,8 +11,7 @@ "openshift.io/long-description": "This template defines resources needed to develop a CakePHP application, including a build configuration, application deployment configuration, and database deployment configuration.", "openshift.io/provider-display-name": "Red Hat, Inc.", "openshift.io/documentation-url": "https://github.com/sclorg/cakephp-ex", - "openshift.io/support-url": "https://access.redhat.com", - "template.openshift.io/bindable": "false" + "openshift.io/support-url": "https://access.redhat.com" } }, "message": "The following service(s) have been created in your project: ${NAME}, ${DATABASE_SERVICE_NAME}.\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/sclorg/cakephp-ex/blob/master/README.md.", @@ -61,7 +60,10 @@ "kind": "Route", "apiVersion": "route.openshift.io/v1", "metadata": { - "name": "${NAME}" + "name": "${NAME}", + "annotations": { + "template.openshift.io/expose-uri": "http://{.spec.host}{.spec.path}" + } }, "spec": { "host": "${APPLICATION_DOMAIN}", @@ -149,23 +151,12 @@ "annotations": { "description": "Defines how to deploy the application server", "template.alpha.openshift.io/wait-for-ready": "true", - "image.openshift.io/triggers": "[{\"from\":{\"kind\":\"ImageStreamTag\",\"name\":\"${NAME}:latest\"},\"fieldPath\": \"spec.template.spec.containers[0].image\"}]" + "image.openshift.io/triggers": "[{\"from\":{\"kind\":\"ImageStreamTag\",\"name\":\"${NAME}:latest\"},\"fieldPath\": \"spec.template.spec.containers[0].image\"},{\"from\":{\"kind\":\"ImageStreamTag\",\"name\":\"${NAME}:latest\"},\"fieldPath\": \"spec.template.spec.initContainers[0].image\"}]" } }, "spec": { "strategy": { - "type": "Recreate", - "recreateParams": { - "pre": { - "failurePolicy": "Retry", - "execNewPod": { - "command": [ - "./migrate-database.sh" - ], - "containerName": "cakephp-mysql-persistent" - } - } - } + "type": "Recreate" }, "replicas": 1, "selector": { @@ -181,9 +172,72 @@ } }, "spec": { + "initContainers": [ + { + "name": "cakephp-init-container", + "image": " ", + "command": [ + "./migrate-database.sh" + ], + "env": [ + { + "name": "DATABASE_SERVICE_NAME", + "value": "${DATABASE_SERVICE_NAME}" + }, + { + "name": "DATABASE_USER", + "valueFrom": { + "secretKeyRef" : { + "name" : "${NAME}", + "key" : "database-user" + } + } + }, + { + "name": "DATABASE_PASSWORD", + "valueFrom": { + "secretKeyRef" : { + "name" : "${NAME}", + "key" : "database-password" + } + } + }, + { + "name": "CAKEPHP_SECRET_TOKEN", + "valueFrom": { + "secretKeyRef" : { + "name" : "${NAME}", + "key" : "cakephp-secret-token" + } + } + }, + { + "name": "CAKEPHP_SECURITY_SALT", + "valueFrom": { + "secretKeyRef" : { + "name" : "${NAME}", + "key" : "cakephp-security-salt" + } + } + }, + { + "name": "DATABASE_NAME", + "value": "${DATABASE_NAME}" + }, + { + "name": "DATABASE_ENGINE", + "value": "${DATABASE_NAME}" + }, + { + "name": "APPLICATION_DOMAIN", + "value": "${APPLICATION_DOMAIN}" + } + ] + } + ], "containers": [ { - "name": "cakephp-mysql-persistent", + "name": "${NAME}", "image": " ", "ports": [ { diff --git a/tests/test_cakephp.py b/tests/test_cakephp.py index 74c0a9cf4..bb5a3d8f4 100644 --- a/tests/test_cakephp.py +++ b/tests/test_cakephp.py @@ -23,7 +23,31 @@ def setup_method(self): def teardown_method(self): self.oc_api.delete_project() - def test_template_inside_cluster(self): + def test_local_template_inside_cluster(self): + branch_to_test = "master" + expected_output = "Welcome to PHP" + if VERSION.startswith("7.4") or VERSION.startswith("8.0"): + branch_to_test = "4.X" + expected_output = "Welcome to CakePHP 4" + if VERSION.startswith("8.1") or VERSION.startswith("8.2"): + branch_to_test = "5.X" + expected_output = "Welcome to CakePHP 5" + + template_json = "../openshift/templates/cakephp.json" + assert self.oc_api.deploy_template( + template=template_json, name_in_template="cakephp-example", expected_output=expected_output, + openshift_args=[ + f"SOURCE_REPOSITORY_REF={branch_to_test}", + f"PHP_VERSION={VERSION}", + "NAME=cakephp-example" + ] + ) + assert self.oc_api.template_deployed(name_in_template="cakephp-example") + assert self.oc_api.check_response_inside_cluster( + name_in_template="cakephp-example", expected_output=expected_output + ) + + def test_remote_template_inside_cluster(self): branch_to_test = "master" expected_output = "Welcome to PHP" if VERSION.startswith("7.4") or VERSION.startswith("8.0"): @@ -45,7 +69,7 @@ def test_template_inside_cluster(self): name_in_template="cakephp-example", expected_output=expected_output ) - def test_template_by_request(self): + def test_remote_template_by_request(self): branch_to_test = "master" expected_output = "Welcome to PHP" if VERSION.startswith("7.4") or VERSION.startswith("8.0"): diff --git a/tests/test_cakephp_mysql.py b/tests/test_cakephp_mysql.py index 40503da22..f68f9a623 100644 --- a/tests/test_cakephp_mysql.py +++ b/tests/test_cakephp_mysql.py @@ -12,7 +12,7 @@ VERSION = "8.1-ubi8" -class TestCakePHPAppExTemplate: +class TestCakePHPAppMySQLExTemplate: def setup_method(self): self.oc_api = OpenShiftAPI(pod_name_prefix="cakephp-example") @@ -27,7 +27,31 @@ def setup_method(self): def teardown_method(self): self.oc_api.delete_project() - def test_template_inside_cluster(self): + def test_local_template_inside_cluster(self): + branch_to_test = "master" + expected_output = "Welcome to PHP" + if VERSION.startswith("7.4") or VERSION.startswith("8.0"): + branch_to_test = "4.X" + expected_output = "Welcome to CakePHP 4" + if VERSION.startswith("8.1") or VERSION.startswith("8.2"): + branch_to_test = "5.X" + expected_output = "Welcome to CakePHP 5" + template_json = "../openshift/templates/cakephp-mysql-persistent.json" + assert self.oc_api.deploy_template( + template=template_json, name_in_template="cakephp-example", expected_output=expected_output, + openshift_args=[ + f"SOURCE_REPOSITORY_REF={branch_to_test}", + f"PHP_VERSION={VERSION}", + "NAME=cakephp-example", + "MYSQL_VERSION=8.0-el8" + ] + ) + assert self.oc_api.template_deployed(name_in_template="cakephp-example") + assert self.oc_api.check_response_inside_cluster( + name_in_template="cakephp-example", expected_output=expected_output + ) + + def test_remote_template_inside_cluster(self): branch_to_test = "master" expected_output = "Welcome to PHP" if VERSION.startswith("7.4") or VERSION.startswith("8.0"): @@ -54,7 +78,7 @@ def test_template_inside_cluster(self): name_in_template="cakephp-example", expected_output=expected_output ) - def test_template_by_request(self): + def test_remote_template_by_request(self): branch_to_test = "master" expected_output = "Welcome to PHP" if VERSION.startswith("7.4") or VERSION.startswith("8.0"):