Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import os
import sys
import tempfile

from pathlib import Path
from collections import namedtuple

from container_ci_suite.utils import check_variables
from container_ci_suite.utils import ContainerTestLibUtils

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

TAGS = {
"rhel8": "-el8",
"rhel9": "-el9",
"rhel10": "-el10",
}
TEST_DIR = Path(__file__).parent.absolute()
Vars = namedtuple(
"Vars",
[
"OS",
"VERSION",
"IMAGE_NAME",
"TEST_DIR",
"TAG",
"TEST_APP",
"VERY_LONG_IDENTIFIER",
],
)
VERSION = os.getenv("VERSION")
OS = os.getenv("TARGET").lower()
TEST_APP = TEST_DIR / "test-app"
VERY_LONG_IDENTIFIER = (
"very_long_identifier_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
VARS = Vars(
OS=OS,
VERSION=VERSION,
IMAGE_NAME=os.getenv("IMAGE_NAME"),
TEST_DIR=Path(__file__).parent.absolute(),
TAG=TAGS.get(OS),
TEST_APP=TEST_APP,
VERY_LONG_IDENTIFIER=VERY_LONG_IDENTIFIER,
)


def get_previous_major_version():
version_dict = {
"13": "12",
"15": "13",
"16": "15",
}
return version_dict.get(VARS.VERSION)


def get_upgrade_path():
upgrade_path = {
"rhel8": "none 12 13 15 16 none",
"rhel9": "none 13 15 16 none",
"rhel10": "none 13 15 16 none",
"fedora": "none 12 13 14 15 16 none",
}
for version in upgrade_path.keys():
if version == VARS.VERSION:
break
prev = version
if prev == "none":
return None
return prev


def get_image_id(version):
ns = {
"rhel8": f"registry.redhat.io/rhel8/postgresql-{version}",
"rhel9": f"registry.redhat.io/rhel9/postgresql-{version}",
"rhel10": f"registry.redhat.io/rhel10/postgresql-{version}",
"c9s": f"quay.io/sclorg/postgresql-{version}-c9s",
"c10s": f"quay.io/sclorg/postgresql-{version}-c10s",
}
return ns[VARS.OS]


def create_postgresql_volume_dir():
"""
Create a PostgreSQL volume directory and set the permissions to 26:-wx.
"""
volume_dir = tempfile.mkdtemp(prefix="/tmp/psql-volume-dir")
ContainerTestLibUtils.commands_to_run(
commands_to_run=[
f"setfacl -m u:26:-wx {volume_dir}",
]
)
return volume_dir


def create_postgresql_temp_file():
"""
Create a PostgreSQL temporary file and set the permissions to 26:rw-.
"""
temp_file = tempfile.mktemp(prefix="/tmp/psql-temp-file")
ContainerTestLibUtils.commands_to_run(
commands_to_run=[
f"setfacl -m u:26:rw- {temp_file}",
]
)
return temp_file
5 changes: 0 additions & 5 deletions test/constants.py

This file was deleted.

17 changes: 17 additions & 0 deletions test/run-pytest
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/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 PostgreSQL in format of X.Y
# OS specifies RHEL version (e.g. OS=rhel10)
#

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

git show -s

PYTHON_VERSION="3.12"
if [[ ! -f "/usr/bin/python$PYTHON_VERSION" ]]; then
PYTHON_VERSION="3.13"
fi
cd "${THISDIR}" && "python${PYTHON_VERSION}" -m pytest -s -rA --showlocals -vv test_container_*.py
116 changes: 116 additions & 0 deletions test/test_container_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import shutil

from pathlib import Path

from container_ci_suite.container_lib import ContainerTestLib
from container_ci_suite.utils import ContainerTestLibUtils

from conftest import VARS, create_postgresql_temp_file


def build_s2i_app(app_path: Path) -> ContainerTestLib:
container_lib = ContainerTestLib(image_name=VARS.IMAGE_NAME, db_type="postgresql")
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 TestPostgreSQLBasicsContainer:
"""
Test PostgreSQL container configuration.
"""

def setup_method(self):
"""
Setup the test environment.
"""
self.app_image = build_s2i_app(app_path=VARS.TEST_DIR / "test-app")
self.app_image.db_lib.db_type = "postgresql"

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

def test_s2i_usage(self):
"""
Test container creation based on s2i technology.
"""
cid_config_build = "s2i_config_build"
psql_password = "password"
psql_database = "db"
psql_user = "user"
psql_admin_password = psql_password
psql_backup_user = "backuser"
psql_backup_password = "pass"
self.app_image.assert_container_creation_fails(
cid_file_name=cid_config_build,
command="",
container_args=[
"-e POSTGRESQL_PASSWORD=pass",
f"-e POSTGRESQL_DATABASE={psql_database}",
],
)
assert self.app_image.create_container(
cid_file_name=cid_config_build,
docker_args=[
f"-e POSTGRESQL_USER={psql_user}",
f"-e POSTGRESQL_PASSWORD={psql_password}",
f"-e POSTGRESQL_DATABASE={psql_database}",
f"-e POSTGRESQL_BACKUP_USER={psql_backup_user}",
f"-e POSTGRESQL_BACKUP_PASSWORD={psql_backup_password}",
f"-e POSTGRESQL_ADMIN_PASSWORD={psql_admin_password}",
],
)
cip = self.app_image.get_cip(cid_file_name=cid_config_build)
assert cip
assert self.app_image.test_db_connection(
container_ip=cip, username=psql_user, password=psql_password
)
assert self.app_image.test_db_connection(
container_ip=cip,
username=psql_backup_user,
password=psql_backup_password,
database="backup",
)
backup_user_script = (
VARS.TEST_DIR / "test-app" / "postgresql-init" / "backup_user.sh"
)
tmp_file = create_postgresql_temp_file()
shutil.copy(backup_user_script, tmp_file)
ContainerTestLibUtils.commands_to_run(
commands_to_run=[
f"setfacl -m u:26:rw- {tmp_file}",
]
)
cid_s2i_test_mount = "s2i_test_mount"
mount_point = "/opt/app-root/src/postgresql-init/add_backup_user.sh"
assert self.app_image.create_container(
cid_file_name=cid_s2i_test_mount,
docker_args=[
f"-e POSTGRESQL_USER={psql_user}",
f"-e POSTGRESQL_PASSWORD={psql_password}",
f"-e POSTGRESQL_DATABASE={psql_database}",
f"-e POSTGRESQL_BACKUP_USER={psql_backup_user}",
f"-e POSTGRESQL_BACKUP_PASSWORD={psql_backup_password}",
f"-e POSTGRESQL_ADMIN_PASSWORD={psql_admin_password}",
f"-v {tmp_file}:{mount_point}:z,ro",
],
)
cip = self.app_image.get_cip(cid_file_name=cid_s2i_test_mount)
assert cip
assert self.app_image.test_db_connection(
container_ip=cip, username=psql_user, password=psql_password
)
assert self.app_image.test_db_connection(
container_ip=cip,
username=psql_backup_user,
password=psql_backup_password,
database="backup",
)
Loading