Skip to content

Commit a7a86fd

Browse files
committed
Some refactoring caugh by real testing.
Don not call the function twice Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>
1 parent 9c46f50 commit a7a86fd

4 files changed

Lines changed: 100 additions & 141 deletions

File tree

test/test_container_basics.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import shutil
2+
13
from pathlib import Path
24

35
from container_ci_suite.container_lib import ContainerTestLib
6+
from container_ci_suite.utils import ContainerTestLibUtils
47

58
from conftest import VARS, create_postgresql_temp_file
69

@@ -23,15 +26,21 @@ class TestPostgreSQLBasicsContainer:
2326
"""
2427

2528
def setup_method(self):
29+
"""
30+
Setup the test environment.
31+
"""
2632
self.app_image = build_s2i_app(app_path=VARS.TEST_DIR / "test-app")
2733
self.app_image.db_lib.db_type = "postgresql"
2834

2935
def teardown_method(self):
36+
"""
37+
Teardown the test environment.
38+
"""
3039
self.app_image.cleanup()
3140

3241
def test_s2i_usage(self):
3342
"""
34-
Test container creation fails with invalid combinations of arguments.
43+
Test container creation based on s2i technology.
3544
"""
3645
cid_config_build = "s2i_config_build"
3746
psql_password = "password"
@@ -70,14 +79,16 @@ def test_s2i_usage(self):
7079
password=psql_backup_password,
7180
database="backup",
7281
)
73-
tmp_file = create_postgresql_temp_file()
7482
backup_user_script = (
7583
VARS.TEST_DIR / "test-app" / "postgresql-init" / "backup_user.sh"
7684
)
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)
85+
tmp_file = create_postgresql_temp_file()
86+
shutil.copy(backup_user_script, tmp_file)
87+
ContainerTestLibUtils.commands_to_run(
88+
commands_to_run=[
89+
f"setfacl -m u:26:rw- {tmp_file}",
90+
]
91+
)
8192
cid_s2i_test_mount = "s2i_test_mount"
8293
mount_point = "/opt/app-root/src/postgresql-init/add_backup_user.sh"
8394
assert self.app_image.create_container(

test/test_container_configuration.py

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def setup_method(self):
1818
"""
1919
self.db = ContainerTestLib(image_name=VARS.IMAGE_NAME, db_type="postgresql")
2020
self.db_api = DatabaseWrapper(image_name=VARS.IMAGE_NAME, db_type="postgresql")
21+
self.volume_dir = create_postgresql_volume_dir()
2122

2223
def teardown_method(self):
2324
"""
@@ -142,16 +143,10 @@ def test_invalid_configuration_tests(
142143
"-e POSTGRESQL_ADMIN_PASSWORD=",
143144
],
144145
[
145-
"-e POSTGRESQL_USER=user",
146-
"-e POSTGRESQL_PASSWORD=pass",
147-
"-e POSTGRESQL_DATABASE=db",
148-
"-e POSTGRESQL_ADMIN_PASSWORD=",
149-
],
150-
[
151-
"-e POSTGRESQL_USER=",
152-
"-e POSTGRESQL_PASSWORD=",
153-
"-e POSTGRESQL_DATABASE=",
154-
'-e POSTGRESQL_ADMIN_PASSWORD="The @password"',
146+
"",
147+
"",
148+
"",
149+
'-e POSTGRESQL_ADMIN_PASSWORD="the @password"',
155150
],
156151
[
157152
'-e POSTGRESQL_USER="the user"',
@@ -185,13 +180,21 @@ def test_configuration_hook(self):
185180
"""
186181
Test container creation fails with no arguments.
187182
"""
188-
cid_file_name = "test_pg_hook"
189-
volume_dir = create_postgresql_volume_dir()
183+
self.shared_buffer_test("32MB")
184+
self.shared_buffer_test("113MB")
185+
self.shared_buffer_test("111MB")
186+
187+
def shared_buffer_test(self, shared_buffer_value):
188+
"""
189+
Test shared buffer configuration.
190+
"""
191+
cid_file_name = f"test_pg_hook_{shared_buffer_value}"
190192
docker_args = [
191193
"-e POSTGRESQL_ADMIN_PASSWORD=password",
192-
"-e POSTGRESQL_SHARED_BUFFERS=32MB",
193-
f"-v {volume_dir}:/opt/app-root/src:Z",
194+
f"-e POSTGRESQL_SHARED_BUFFERS={shared_buffer_value}",
195+
f"-v {self.volume_dir}:/opt/app-root/src:Z",
194196
]
197+
195198
assert self.db.create_container(
196199
cid_file_name=cid_file_name,
197200
docker_args=docker_args,
@@ -208,52 +211,6 @@ def test_configuration_hook(self):
208211
cid_file_name=cid,
209212
cmd='psql -tA -c "SHOW shared_buffers;"',
210213
)
211-
assert "32MB" in output, f"Shared buffers should be 32MB, but is {output}"
212-
# Check that POSTGRESQL_SHARED_BUFFERS has effect.
213-
docker_args = [
214-
"-e POSTGRESQL_ADMIN_PASSWORD=password",
215-
"-e POSTGRESQL_SHARED_BUFFERS=113MB",
216-
]
217-
cid_file_name2 = "test_pg_hook_2"
218-
assert self.db.create_container(
219-
cid_file_name=cid_file_name2,
220-
docker_args=docker_args,
221-
command="",
214+
assert shared_buffer_value in output, (
215+
f"Shared buffers should be {shared_buffer_value}, but is {output}"
222216
)
223-
cid = self.db.get_cid(cid_file_name=cid_file_name2)
224-
assert cid
225-
cip = self.db.get_cip(cid_file_name=cid_file_name2)
226-
assert cip
227-
assert self.db_api.wait_for_database(
228-
container_id=cid, command="/usr/libexec/check-container"
229-
)
230-
output = PodmanCLIWrapper.podman_exec_shell_command(
231-
cid_file_name=cid,
232-
cmd='psql -tA -c "SHOW shared_buffers;"',
233-
)
234-
assert "113MB" in output, f"Shared buffers should be 113MB, but is {output}"
235-
# Check that volume has priority over POSTGRESQL_SHARED_BUFFERS.
236-
cid_file_name3 = "test_pg_hook_3"
237-
docker_args = [
238-
"-e POSTGRESQL_ADMIN_PASSWORD=password",
239-
"-e POSTGRESQL_SHARED_BUFFERS=113MB",
240-
f"-v {volume_dir}:/opt/app-root/src:Z",
241-
]
242-
assert self.db.create_container(
243-
cid_file_name=cid_file_name3,
244-
docker_args=docker_args,
245-
command="",
246-
)
247-
cid = self.db.get_cid(cid_file_name=cid_file_name3)
248-
assert cid
249-
cip = self.db.get_cip(cid_file_name=cid_file_name3)
250-
assert cip
251-
assert self.db_api.wait_for_database(
252-
container_id=cid, command="/usr/libexec/check-container"
253-
)
254-
output = PodmanCLIWrapper.podman_exec_shell_command(
255-
cid_file_name=cid,
256-
cmd='psql -tA -c "SHOW shared_buffers;"',
257-
)
258-
259-
assert "113MB" in output, f"Shared buffers should be 113MB, but is {output}"

test/test_container_general.py

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def teardown_method(self):
3333
[
3434
("", "user", "pass", "", "", "no_admin"),
3535
("", "user1", "pass1", "", "r00t", "admin"),
36-
# ("", "", "", "postgres", "r00t", "only_admin"),
36+
("", "", "", "postgres", "r00t", "only_admin"),
3737
("-u 12345", "user2", "pass", "", "", "no_admin_altuid"),
3838
("-u 12345", "user3", "pass1", "", "r00t", "admin_altuid"),
39-
# ("-u 12345", "", "", "postgres", "r00t", "only_admin_altuid"),
39+
("-u 12345", "", "", "postgres", "r00t", "only_admin_altuid"),
4040
],
4141
)
4242
def test_run(
@@ -60,28 +60,24 @@ def test_run(
6060
POSTGRESQL_SHARED_BUFFERS = "64MB"
6161
expected_success = False
6262
expected_admin_success = False
63+
psql_user_arg = ""
64+
psql_pwd_arg = ""
65+
db_name_arg = ""
66+
admin_root_password_arg = ""
6367
if psql_user != "":
6468
psql_user_arg = f"-e POSTGRESQL_USER={psql_user}"
6569
expected_success = True
66-
else:
67-
psql_user_arg = ""
6870
if psql_password: # empty password is allowed
6971
psql_pwd_arg = f"-e POSTGRESQL_PASSWORD={psql_password}"
70-
else:
71-
psql_pwd_arg = ""
72-
if psql_database:
73-
db_name = psql_database
74-
else:
75-
db_name = "db"
72+
if psql_user and psql_password:
73+
db_name_arg = "-e POSTGRESQL_DATABASE=db"
7674
if root_password == "r00t":
7775
admin_root_password_arg = f"-e POSTGRESQL_ADMIN_PASSWORD={root_password}"
7876
expected_admin_success = True
79-
else:
80-
admin_root_password_arg = ""
8177
docker_all_args = [
8278
psql_user_arg,
8379
psql_pwd_arg,
84-
f"-e POSTGRESQL_DATABASE={db_name}",
80+
db_name_arg,
8581
admin_root_password_arg,
8682
f"-e POSTGRESQL_MAX_CONNECTIONS={POSTGRESQL_MAX_CONNECTIONS}",
8783
f"-e POSTGRESQL_MAX_PREPARED_TRANSACTIONS={POSTGRESQL_MAX_PREPARED_TRANSACTIONS}",
@@ -115,30 +111,29 @@ def test_run(
115111
cmd="psql --version",
116112
)
117113
assert VARS.VERSION in output
118-
self.db_image.db_lib.assert_login_access(
119-
container_ip=cip,
120-
username=psql_user,
121-
password=psql_password,
122-
expected_success=expected_success,
123-
)
124-
self.db_image.db_lib.assert_login_access(
125-
container_ip=cip,
126-
username=psql_user,
127-
password=f"{psql_password}_foo",
128-
expected_success=False,
129-
)
130-
self.db_image.db_lib.assert_login_access(
131-
container_ip=cip,
132-
username="postgres",
133-
password=root_password,
134-
expected_success=expected_admin_success,
135-
)
136-
self.db_image.db_lib.assert_login_access(
137-
container_ip=cip,
138-
username="postgres",
139-
password=f"{root_password}_foo",
140-
expected_success=False,
141-
)
114+
access_output = True
115+
if psql_database == "":
116+
psql_database = "db"
117+
for user, pwd, ret_value in [
118+
(psql_user, psql_password, expected_success),
119+
(psql_user, f"{psql_password}_foo", False),
120+
("postgres", root_password, expected_admin_success),
121+
("postgres", f"{root_password}_foo", False),
122+
]:
123+
test_assert = self.db_image.db_lib.assert_login_access(
124+
container_ip=cip,
125+
username=user,
126+
password=pwd,
127+
expected_success=ret_value,
128+
database=psql_database,
129+
)
130+
if not test_assert:
131+
print(
132+
f"Login access failed for {user}:{pwd} with expected success {ret_value}"
133+
)
134+
access_output = False
135+
break
136+
assert access_output, "Login access failed for above results"
142137
assert self.db_image.db_lib.assert_local_access(container_id=cid)
143138
output = PodmanCLIWrapper.podman_exec_shell_command(
144139
cid_file_name=cid,
@@ -154,24 +149,30 @@ def test_run(
154149
assert re.search(word, output), f"Word {word} not found in {output}"
155150
# test_postgresql
156151
if test_name == "admin":
157-
psql_user = "postgres"
158-
psql_password = root_password
159-
psql_database = "postgres"
160152
output = self.db_api.run_sql_command(
161153
container_ip=cip,
162-
username=psql_user,
163-
password=psql_password,
154+
username="postgres",
155+
password=root_password,
164156
container_id=VARS.IMAGE_NAME,
165-
database=db_name,
157+
database="postgres",
166158
sql_cmd="-At -c 'CREATE EXTENSION \"uuid-ossp\";'",
167159
expected_output="CREATE EXTENSION",
168160
)
161+
if psql_password == "":
162+
psql_password = root_password
163+
164+
self.database_test(cip, psql_user, psql_password, psql_database)
165+
166+
def database_test(self, cip, psql_user, psql_password, psql_database):
167+
"""
168+
Test PostgreSQL database creation.
169+
"""
169170
output = self.db_api.run_sql_command(
170171
container_ip=cip,
171172
username=psql_user,
172173
password=psql_password,
173174
container_id=VARS.IMAGE_NAME,
174-
database=db_name,
175+
database=psql_database,
175176
sql_cmd='-At -c "CREATE TABLE tbl (a integer, b integer);"',
176177
expected_output="CREATE TABLE",
177178
)
@@ -181,7 +182,7 @@ def test_run(
181182
username=psql_user,
182183
password=psql_password,
183184
container_id=VARS.IMAGE_NAME,
184-
database=db_name,
185+
database=psql_database,
185186
sql_cmd=[
186187
'-At -c "INSERT INTO tbl VALUES (1, 2);"',
187188
'-At -c "INSERT INTO tbl VALUES (3, 4);"',
@@ -194,7 +195,7 @@ def test_run(
194195
username=psql_user,
195196
password=psql_password,
196197
container_id=VARS.IMAGE_NAME,
197-
database=db_name,
198+
database=psql_database,
198199
sql_cmd='-At -c "SELECT * FROM tbl;"',
199200
)
200201
words = [
@@ -209,7 +210,7 @@ def test_run(
209210
username=psql_user,
210211
password=psql_password,
211212
container_id=VARS.IMAGE_NAME,
212-
database=db_name,
213+
database=psql_database,
213214
sql_cmd='-At -c "DROP TABLE tbl;"',
214215
expected_output="DROP TABLE",
215216
)

test/test_container_password.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ def test_password_change(self):
6464
)
6565
cid1 = self.pwd_change.get_cid(cid_file_name=cid_file_name1)
6666
assert cid1
67-
self.pwd_change.db_lib.assert_login_access(
67+
assert self.pwd_change.db_lib.assert_login_access(
6868
container_ip=cip1,
6969
username=username,
7070
password=password,
7171
expected_success=True,
7272
)
73-
self.pwd_change.db_lib.assert_login_access(
73+
assert self.pwd_change.db_lib.assert_login_access(
7474
container_ip=cip1,
7575
username="postgres",
7676
password=admin_password,
@@ -135,36 +135,26 @@ def test_password_change(self):
135135
)
136136
cip_new = self.pwd_change.get_cip(cid_file_name=cid_file_name_new)
137137
assert cip_new
138+
138139
assert self.pwd_change.test_db_connection(
139140
container_ip=cip_new,
140141
username=username,
141142
password=new_password,
142143
max_attempts=10,
143144
)
144-
self.pwd_change.db_lib.assert_login_access(
145-
container_ip=cip_new,
146-
username=username,
147-
password=new_password,
148-
expected_success=True,
149-
)
150-
self.pwd_change.db_lib.assert_login_access(
151-
container_ip=cip_new,
152-
username=username,
153-
password=password,
154-
expected_success=False,
155-
)
156-
self.pwd_change.db_lib.assert_login_access(
157-
container_ip=cip_new,
158-
username="postgres",
159-
password=new_admin_password,
160-
expected_success=True,
161-
)
162-
self.pwd_change.db_lib.assert_login_access(
163-
container_ip=cip_new,
164-
username="postgres",
165-
password=admin_password,
166-
expected_success=False,
167-
)
145+
for user, pwd, ret_value in [
146+
(username, new_password, True),
147+
(username, password, False),
148+
("postgres", new_admin_password, True),
149+
("postgres", "admin_password", False),
150+
]:
151+
assert self.pwd_change.db_lib.assert_login_access(
152+
container_ip=cip_new,
153+
username=user,
154+
password=pwd,
155+
expected_success=ret_value,
156+
)
157+
168158
output = self.dw_api.run_sql_command(
169159
container_ip=cip_new,
170160
username=username,

0 commit comments

Comments
 (0)