Skip to content
45 changes: 45 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import sys

from pathlib import Path
from collections import namedtuple

from container_ci_suite.utils import check_variables

if not check_variables():
sys.exit(1)

TAGS = {
"rhel8": "-el8",
"rhel9": "-el9",
"rhel10": "-el10",
}
Comment on lines +12 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: Turn this into a function containing just tag.replace("rh", "-", 1) - that way, you'll never have to add anything here manually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a part of container-ci-suite directly. Good point.

TEST_DIR = Path(__file__).parent.absolute()
Vars = namedtuple(
"Vars",
[
"OS",
"VERSION",
"IMAGE_NAME",
"TEST_DIR",
"TAG",
"TEST_APP",
"VERY_LONG_DB_NAME",
"VERY_LONG_USER_NAME",
],
)
VERSION = os.getenv("VERSION")
OS = os.getenv("TARGET").lower()
TEST_APP = TEST_DIR / "test-app"
VERY_LONG_DB_NAME = "very_long_database_name_" + "x" * 40
VERY_LONG_USER_NAME = "very_long_user_name_" + "x" * 40
VARS = Vars(
OS=OS,
VERSION=VERSION,
IMAGE_NAME=os.getenv("IMAGE_NAME"),
TEST_DIR=TEST_DIR,
TAG=TAGS.get(OS),
TEST_APP=TEST_APP,
VERY_LONG_DB_NAME=VERY_LONG_DB_NAME,
VERY_LONG_USER_NAME=VERY_LONG_USER_NAME,
)
5 changes: 0 additions & 5 deletions test/constants.py

This file was deleted.

1 change: 0 additions & 1 deletion test/run
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

set -o nounset
shopt -s nullglob

THISDIR=$(dirname ${BASH_SOURCE[0]})
source ${THISDIR}/test-lib.sh

Expand Down
8 changes: 6 additions & 2 deletions test/run-openshift-pytest
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
THISDIR=$(dirname ${BASH_SOURCE[0]})

git show -s

cd "${THISDIR}" && python3.12 -m pytest -s -rA --showlocals -vv test_ocp_*.py
if python3 -c 'import sys; sys.exit(0 if sys.version_info < (3,13) else 1)'; then
PYTHON_VERSION="3.12"
else
PYTHON_VERSION="3"
fi
cd "${THISDIR}" && "python${PYTHON_VERSION}" -m pytest -s -rA --showlocals -vv test_ocp_*.py
18 changes: 18 additions & 0 deletions test/run-pytest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/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=rhel10)
#

THISDIR=$(dirname ${BASH_SOURCE[0]})

git show -s

if python3 -c 'import sys; sys.exit(0 if sys.version_info < (3,13) else 1)'; then
PYTHON_VERSION="3.12"
else
PYTHON_VERSION="3"
fi
cd "${THISDIR}" && "python${PYTHON_VERSION}" -m pytest -s -rA --showlocals -vv test_container_*.py
118 changes: 118 additions & 0 deletions test/test_container_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import shutil
import tempfile

from container_ci_suite.container_lib import ContainerTestLib
from container_ci_suite.container_lib import ContainerTestLibUtils
from container_ci_suite.engines.podman_wrapper import PodmanCLIWrapper
from pathlib import Path

from conftest import VARS


def build_s2i_app(app_path: Path) -> ContainerTestLib:
container_lib = ContainerTestLib(VARS.IMAGE_NAME)
app_name = app_path.name
s2i_app = container_lib.build_as_df(
app_path=app_path,
s2i_args="--pull-policy=never",
src_image=VARS.IMAGE_NAME,
dst_image=f"{VARS.IMAGE_NAME}-{app_name}",
)
return s2i_app


class TestMySqlBasicsContainer:
"""
Test MySQL container configuration.
"""

def setup_method(self):
"""
Setup the test environment.
"""
self.app_image = build_s2i_app(app_path=VARS.TEST_APP)
self.app_image.set_new_db_type(db_type="mysql")

def teardown_method(self):
"""
Teardown the test environment.
"""
self.app_image.cleanup()

def test_connection_without_mount(self):
"""
Test if MySQL container failed in case of invalid combinations.
Steps are:
1. Test if the container creation fails with invalid combinations of arguments
2. Test if the container creation succeeds with valid combinations of arguments
3. Test if the database connection works
"""
cid_mount = "connection_without_mount"
self.app_image.assert_container_creation_fails(
cid_file_name=cid_mount,
command="",
container_args=[
"-e MYSQL_USER=root",
"-e MYSQL_PASSWORD=pass",
"-e MYSQL_DATABASE=db",
"-e MYSQL_ROOT_PASSWORD=pass",
],
)
operation_user = "operations_user"
operation_pass = "operations_pass"
assert self.app_image.create_container(
cid_file_name=cid_mount,
container_args=[
"-e MYSQL_USER=config_test_user",
"-e MYSQL_PASSWORD=config_test_user",
"-e MYSQL_DATABASE=db",
f"-e MYSQL_OPERATIONS_USER={operation_user}",
f"-e MYSQL_OPERATIONS_PASSWORD={operation_pass}",
],
)
cip, cid = self.app_image.get_cip_cid(cid_file_name=cid_mount)
assert cip and cid
assert self.app_image.test_db_connection(
container_ip=cip, username=operation_user, password=operation_pass
)
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")

def test_connection_with_mount(self):
"""
Test if the MySQL container works properly with mounted application directory.
Steps are:
1. Copy the test-app directory to a temporary directory and set proper permissions
2. Create a container with the mounted directory
3. Test if the database connection works with the operations user
"""
with tempfile.TemporaryDirectory(prefix="/tmp/mysql-test_data") as data_dir:
shutil.copytree(VARS.TEST_APP, f"{data_dir}/test-app")
assert ContainerTestLibUtils.commands_to_run(
commands_to_run=[
f"chown -R 27:27 {data_dir}/test-app",
]
)
cid_with_mount = "connection_with_mount"
operation_user = "operations_user"
operation_pass = "operations_pass"

self.app_image.create_container(
cid_file_name=cid_with_mount,
container_args=[
"-e MYSQL_USER=config_test_user",
"-e MYSQL_PASSWORD=config_test_user",
"-e MYSQL_DATABASE=db",
f"-e MYSQL_OPERATIONS_USER={operation_user}",
f"-e MYSQL_OPERATIONS_PASSWORD={operation_pass}",
f"-v {data_dir}/test-app:/opt/app-root/src/:z",
],
)
cip, cid = self.app_image.get_cip_cid(cid_file_name=cid_with_mount)
assert cip and cid
assert self.app_image.test_db_connection(
container_ip=cip,
username=operation_user,
password=operation_pass,
max_attempts=10,
)
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")
Loading
Loading