Skip to content

Commit 5217d9d

Browse files
committed
Fix upgrade tests to check if database is really ready.
Sometimes /usr/libexec/check-container does not work properly. Let's check it by accepting connections by simple command Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>
1 parent f7189e1 commit 5217d9d

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

test/test_container_upgrade.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def test_upgrade_functionality(self, upgrade_type, datadir):
6363
def create_database_in_prev_version(self):
6464
"""
6565
Create a database in the old version.
66+
Steps are:
67+
1. Create a container with the previous version and run pg_upgrade
68+
2. Test if the database connection works
69+
3. If datadir is empty, create a simple table and insert some data.
70+
If datadir is pagila, add the pagila example database
6671
"""
6772
cid_file_name = "create-db-test"
6873
assert ContainerTestLibUtils.commands_to_run(
@@ -87,13 +92,18 @@ def create_database_in_prev_version(self):
8792
assert self.db_api.wait_for_database(
8893
container_id=cid_create, command="/usr/libexec/check-container"
8994
)
95+
# Let's wait couple seconds
96+
time.sleep(5)
9097
is_db_ready = False
9198
for _ in range(5):
92-
output = PodmanCLIWrapper.podman_logs(container_id=cid_upg)
93-
for out in ["accepting connections", "Starting server"]:
94-
if out not in output:
95-
time.sleep(5)
96-
continue
99+
# Let's check if the database is ready by running a simple query.
100+
# Sometimes check-container can return before the database is fully ready to accept connections.
101+
if not self.db_api.wait_for_database(
102+
container_id=cid_create,
103+
command='psql -h localhost -tA -c \"select 1;\"',
104+
):
105+
time.sleep(5)
106+
continue
97107
is_db_ready = True
98108
break
99109
assert is_db_ready, "Database is not ready after waiting for 25 seconds"
@@ -122,6 +132,15 @@ def create_database_in_prev_version(self):
122132
def upgrade_image(self, upgrade_type: str, bool_test_upgrade: bool = True):
123133
"""
124134
Upgrade the image.
135+
Steps are:
136+
1. Create a container with the new version and run pg_upgrade
137+
2. Test if the database connection works
138+
3. If bool_test_upgrade is True, check the database output after upgrade.
139+
If False, just check the connection as the second upgrade
140+
can take more time and we have already tested the output after the first upgrade.
141+
4. If datadir is empty, create a simple table and insert some data.
142+
If datadir is pagila, add the pagila example database
143+
5. Stop and remove the container after the test.
125144
"""
126145
self.upgrade_db.image_name = VARS.IMAGE_NAME
127146
cid_file_name = f"upg-test-{upgrade_type}-{self.datadir}"
@@ -139,21 +158,26 @@ def upgrade_image(self, upgrade_type: str, bool_test_upgrade: bool = True):
139158
cip_upg, cid_upg = self.upgrade_db.get_cip_cid(cid_file_name=cid_file_name)
140159
assert cip_upg and cid_upg
141160
assert self.db_api.wait_for_database(
142-
container_id=cid_upg, command="/usr/libexec/check-container"
161+
container_id=cid_upg,
162+
command="/usr/libexec/check-container",
163+
not_shell=False,
143164
)
144165
# Let's wait couple seconds
145166
time.sleep(5)
146167
is_db_ready = False
147168
for _ in range(5):
148-
output = PodmanCLIWrapper.podman_logs(container_id=cid_upg)
149-
for out in ["accepting connections", "Starting server"]:
150-
if out not in output:
151-
time.sleep(5)
152-
continue
169+
# Let's check if the database is ready by running a simple query.
170+
# Sometimes check-container can return
171+
# before the database is fully ready to accept connections.
172+
if not self.db_api.wait_for_database(
173+
container_id=cid_upg,
174+
command='psql -h localhost -tA -c \"select 1;\"',
175+
):
176+
time.sleep(5)
177+
continue
153178
is_db_ready = True
154179
break
155180
assert is_db_ready, "Database is not ready after waiting for 25 seconds"
156-
assert output
157181
if self.datadir == "empty":
158182
self.check_db_output(cid=cid_upg)
159183
else:
@@ -165,7 +189,7 @@ def check_db_output(self, cid):
165189
"""
166190
output = PodmanCLIWrapper.podman_exec_shell_command(
167191
cid_file_name=cid,
168-
cmd="psql -h localhost -At -c \"SELECT * FROM blah ORDER BY id;\""
192+
cmd="psql -h localhost -At -c \"SELECT * FROM blah ORDER BY id;\"",
169193
)
170194
rows = [
171195
"1",

0 commit comments

Comments
 (0)