Skip to content

Commit 9c46f50

Browse files
committed
Add PyTest container test suite migrated from run_tests script
The migration matrix is following: run_container_creation_tests -> test_container_configuration.py run_general_tests -> test_container_general.py run_change_password_test -> test_container_password.py run_replication_test -> test_container_replication.py run_s2i_test -> test_container_basics.py run_test_cfg_hook -> test_container_configuration.py run_s2i_bake_data_test -> test_container_ssl.py run_s2i_enable_ssl_test -> test_container_ssl.py run_pgaudit_test -> test_container_extensions.py run_pgvector_test -> test_container_extensions.py run_env_extension_load_test -> test_container_extensions.py run_logging_test -> test_container_extensions.py The following tests are NOT migrated yet. run_migration_test and run_upgrade_tests. Signed-off-by: Petr "Stone" Hracek <[email protected]>
1 parent c5cda73 commit 9c46f50

10 files changed

Lines changed: 1393 additions & 0 deletions

test/conftest.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import os
2+
import sys
3+
import tempfile
4+
5+
from pathlib import Path
6+
from collections import namedtuple
7+
8+
from container_ci_suite.utils import check_variables
9+
from container_ci_suite.utils import ContainerTestLibUtils
10+
11+
if not check_variables():
12+
sys.exit(1)
13+
14+
TAGS = {
15+
"rhel8": "-el8",
16+
"rhel9": "-el9",
17+
"rhel10": "-el10",
18+
}
19+
TEST_DIR = Path(__file__).parent.absolute()
20+
Vars = namedtuple(
21+
"Vars",
22+
[
23+
"OS",
24+
"VERSION",
25+
"IMAGE_NAME",
26+
"TEST_DIR",
27+
"TAG",
28+
"TEST_APP",
29+
"VERY_LONG_IDENTIFIER",
30+
],
31+
)
32+
VERSION = os.getenv("VERSION")
33+
OS = os.getenv("TARGET").lower()
34+
TEST_APP = TEST_DIR / "test-app"
35+
VERY_LONG_IDENTIFIER = (
36+
"very_long_identifier_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
37+
)
38+
VARS = Vars(
39+
OS=OS,
40+
VERSION=VERSION,
41+
IMAGE_NAME=os.getenv("IMAGE_NAME"),
42+
TEST_DIR=Path(__file__).parent.absolute(),
43+
TAG=TAGS.get(OS),
44+
TEST_APP=TEST_APP,
45+
VERY_LONG_IDENTIFIER=VERY_LONG_IDENTIFIER,
46+
)
47+
48+
49+
def get_previous_major_version():
50+
version_dict = {
51+
"13": "12",
52+
"15": "13",
53+
"16": "15",
54+
}
55+
return version_dict.get(VARS.VERSION)
56+
57+
58+
def get_upgrade_path():
59+
upgrade_path = {
60+
"rhel8": "none 12 13 15 16 none",
61+
"rhel9": "none 13 15 16 none",
62+
"rhel10": "none 13 15 16 none",
63+
"fedora": "none 12 13 14 15 16 none",
64+
}
65+
for version in upgrade_path.keys():
66+
if version == VARS.VERSION:
67+
break
68+
prev = version
69+
if prev == "none":
70+
return None
71+
return prev
72+
73+
74+
def get_image_id(version):
75+
ns = {
76+
"rhel8": f"registry.redhat.io/rhel8/postgresql-{version}",
77+
"rhel9": f"registry.redhat.io/rhel9/postgresql-{version}",
78+
"rhel10": f"registry.redhat.io/rhel10/postgresql-{version}",
79+
"c9s": f"quay.io/sclorg/postgresql-{version}-c9s",
80+
"c10s": f"quay.io/sclorg/postgresql-{version}-c10s",
81+
}
82+
return ns[VARS.OS]
83+
84+
85+
def create_postgresql_volume_dir():
86+
"""
87+
Create a PostgreSQL volume directory and set the permissions to 26:-wx.
88+
"""
89+
volume_dir = tempfile.mkdtemp(prefix="/tmp/psql-volume-dir")
90+
ContainerTestLibUtils.commands_to_run(
91+
commands_to_run=[
92+
f"setfacl -m u:26:-wx {volume_dir}",
93+
]
94+
)
95+
return volume_dir
96+
97+
98+
def create_postgresql_temp_file():
99+
"""
100+
Create a PostgreSQL temporary file and set the permissions to 26:rw-.
101+
"""
102+
temp_file = tempfile.mktemp(prefix="/tmp/psql-temp-file")
103+
ContainerTestLibUtils.commands_to_run(
104+
commands_to_run=[
105+
f"setfacl -m u:26:rw- {temp_file}",
106+
]
107+
)
108+
return temp_file

test/run-pytest

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
#
3+
# IMAGE_NAME specifies a name of the candidate image used for testing.
4+
# The image has to be available before this script is executed.
5+
# VERSION specifies the major version of the PostgreSQL in format of X.Y
6+
# OS specifies RHEL version (e.g. OS=rhel10)
7+
#
8+
9+
THISDIR=$(dirname ${BASH_SOURCE[0]})
10+
11+
git show -s
12+
13+
PYTHON_VERSION="3.12"
14+
if [[ ! -f "/usr/bin/python$PYTHON_VERSION" ]]; then
15+
PYTHON_VERSION="3.13"
16+
fi
17+
cd "${THISDIR}" && "python${PYTHON_VERSION}" -m pytest -s -rA --showlocals -vv test_container_*.py

test/test_container_basics.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from pathlib import Path
2+
3+
from container_ci_suite.container_lib import ContainerTestLib
4+
5+
from conftest import VARS, create_postgresql_temp_file
6+
7+
8+
def build_s2i_app(app_path: Path) -> ContainerTestLib:
9+
container_lib = ContainerTestLib(image_name=VARS.IMAGE_NAME, db_type="postgresql")
10+
app_name = app_path.name
11+
s2i_app = container_lib.build_as_df(
12+
app_path=app_path,
13+
s2i_args="--pull-policy=never",
14+
src_image=VARS.IMAGE_NAME,
15+
dst_image=f"{VARS.IMAGE_NAME}-{app_name}",
16+
)
17+
return s2i_app
18+
19+
20+
class TestPostgreSQLBasicsContainer:
21+
"""
22+
Test PostgreSQL container configuration.
23+
"""
24+
25+
def setup_method(self):
26+
self.app_image = build_s2i_app(app_path=VARS.TEST_DIR / "test-app")
27+
self.app_image.db_lib.db_type = "postgresql"
28+
29+
def teardown_method(self):
30+
self.app_image.cleanup()
31+
32+
def test_s2i_usage(self):
33+
"""
34+
Test container creation fails with invalid combinations of arguments.
35+
"""
36+
cid_config_build = "s2i_config_build"
37+
psql_password = "password"
38+
psql_database = "db"
39+
psql_user = "user"
40+
psql_admin_password = psql_password
41+
psql_backup_user = "backuser"
42+
psql_backup_password = "pass"
43+
self.app_image.assert_container_creation_fails(
44+
cid_file_name=cid_config_build,
45+
command="",
46+
container_args=[
47+
"-e POSTGRESQL_PASSWORD=pass",
48+
f"-e POSTGRESQL_DATABASE={psql_database}",
49+
],
50+
)
51+
assert self.app_image.create_container(
52+
cid_file_name=cid_config_build,
53+
docker_args=[
54+
f"-e POSTGRESQL_USER={psql_user}",
55+
f"-e POSTGRESQL_PASSWORD={psql_password}",
56+
f"-e POSTGRESQL_DATABASE={psql_database}",
57+
f"-e POSTGRESQL_BACKUP_USER={psql_backup_user}",
58+
f"-e POSTGRESQL_BACKUP_PASSWORD={psql_backup_password}",
59+
f"-e POSTGRESQL_ADMIN_PASSWORD={psql_admin_password}",
60+
],
61+
)
62+
cip = self.app_image.get_cip(cid_file_name=cid_config_build)
63+
assert cip
64+
assert self.app_image.test_db_connection(
65+
container_ip=cip, username=psql_user, password=psql_password
66+
)
67+
assert self.app_image.test_db_connection(
68+
container_ip=cip,
69+
username=psql_backup_user,
70+
password=psql_backup_password,
71+
database="backup",
72+
)
73+
tmp_file = create_postgresql_temp_file()
74+
backup_user_script = (
75+
VARS.TEST_DIR / "test-app" / "postgresql-init" / "backup_user.sh"
76+
)
77+
with open(backup_user_script, "r") as f:
78+
backup_user_script_content = f.read()
79+
with open(tmp_file, "w") as f:
80+
f.write(backup_user_script_content)
81+
cid_s2i_test_mount = "s2i_test_mount"
82+
mount_point = "/opt/app-root/src/postgresql-init/add_backup_user.sh"
83+
assert self.app_image.create_container(
84+
cid_file_name=cid_s2i_test_mount,
85+
docker_args=[
86+
f"-e POSTGRESQL_USER={psql_user}",
87+
f"-e POSTGRESQL_PASSWORD={psql_password}",
88+
f"-e POSTGRESQL_DATABASE={psql_database}",
89+
f"-e POSTGRESQL_BACKUP_USER={psql_backup_user}",
90+
f"-e POSTGRESQL_BACKUP_PASSWORD={psql_backup_password}",
91+
f"-e POSTGRESQL_ADMIN_PASSWORD={psql_admin_password}",
92+
f"-v {tmp_file}:{mount_point}:z,ro",
93+
],
94+
)
95+
cip = self.app_image.get_cip(cid_file_name=cid_s2i_test_mount)
96+
assert cip
97+
assert self.app_image.test_db_connection(
98+
container_ip=cip, username=psql_user, password=psql_password
99+
)
100+
assert self.app_image.test_db_connection(
101+
container_ip=cip,
102+
username=psql_backup_user,
103+
password=psql_backup_password,
104+
database="backup",
105+
)

0 commit comments

Comments
 (0)