Skip to content

Commit 2f86e86

Browse files
committed
Merge remote-tracking branch 'origin/16/edge' into add-invalid-database-name
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
2 parents d0c4ddc + c7b0383 commit 2f86e86

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

tests/unit/test_postgresql.py

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ def test_set_up_database_with_temp_tablespace_and_missing_owner_role(harness):
341341
patch("single_kernel_postgresql.utils.filesystem.os.stat") as _stat,
342342
patch("single_kernel_postgresql.utils.filesystem.pwd.getpwnam") as _getpwnam,
343343
):
344-
# Simulate a temp location owned by wrong user/permissions to trigger fixup
345-
stat_result = type("stat_result", (), {"st_uid": 0, "st_gid": 0, "st_mode": 0o755})
344+
# Simulate a temp location owned by wrong user/permissions to trigger fixup (33188 means 0o644)
345+
stat_result = type("stat_result", (), {"st_uid": 0, "st_gid": 0, "st_mode": 33188})
346346
_stat.return_value = stat_result
347347
_getpwnam.return_value.pw_name = "root"
348348
_getpwnam.return_value.pw_uid = 0
@@ -405,13 +405,9 @@ def test_set_up_database_owner_mismatch_triggers_rename_and_fix(harness):
405405
patch("single_kernel_postgresql.utils.filesystem.pwd.getpwnam") as _getpwnam,
406406
patch("single_kernel_postgresql.utils.postgresql.datetime") as _dt,
407407
):
408-
# Owner differs, permissions are correct
408+
# Owner differs, permissions are correct (16832 means 0o700)
409409
# Simulate directory owned by uid 1000 while expected owner has uid 0 to force mismatch
410-
stat_result = type(
411-
"stat_result",
412-
(),
413-
{"st_uid": 1000, "st_gid": 1000, "st_mode": POSTGRESQL_STORAGE_PERMISSIONS},
414-
)
410+
stat_result = type("stat_result", (), {"st_uid": 1000, "st_gid": 1000, "st_mode": 16832})
415411
_stat.return_value = stat_result
416412
# The expected owner (SNAP_USER) resolves to uid 0/gid 0 for the test
417413
_getpwnam.return_value.pw_name = "root"
@@ -449,8 +445,8 @@ def test_set_up_database_permissions_mismatch_triggers_rename_and_fix(harness):
449445
patch("single_kernel_postgresql.utils.filesystem.pwd.getpwnam") as _getpwnam,
450446
patch("single_kernel_postgresql.utils.postgresql.datetime") as _dt,
451447
):
452-
# Owner matches SNAP_USER, permissions differ
453-
stat_result = type("stat_result", (), {"st_uid": 0, "st_gid": 0, "st_mode": 0o755})
448+
# Owner matches SNAP_USER, permissions differ (33188 means 0o644)
449+
stat_result = type("stat_result", (), {"st_uid": 0, "st_gid": 0, "st_mode": 33188})
454450
_stat.return_value = stat_result
455451
_getpwnam.return_value.pw_name = SNAP_USER
456452
_getpwnam.return_value.pw_uid = 0
@@ -739,3 +735,44 @@ def test_create_user():
739735

740736
with pytest.raises(PostgreSQLCreateUserError):
741737
pg.create_user("username", "password")
738+
739+
740+
def test_set_up_database_owner_and_permissions_match_no_rename_or_fix(harness):
741+
with (
742+
patch(
743+
"single_kernel_postgresql.utils.postgresql.PostgreSQL._connect_to_database"
744+
) as _connect_to_database,
745+
patch("single_kernel_postgresql.utils.postgresql.PostgreSQL.set_up_login_hook_function"),
746+
patch(
747+
"single_kernel_postgresql.utils.postgresql.PostgreSQL.set_up_predefined_catalog_roles_function"
748+
),
749+
patch("single_kernel_postgresql.utils.postgresql.change_owner") as _change_owner,
750+
patch("single_kernel_postgresql.utils.postgresql.os.chmod") as _chmod,
751+
patch("single_kernel_postgresql.utils.postgresql.os.stat") as _stat,
752+
patch("single_kernel_postgresql.utils.filesystem.pwd.getpwnam") as _getpwnam,
753+
):
754+
# Owner matches SNAP_USER and permissions are correct (16832 means 0o700)
755+
stat_result = type("stat_result", (), {"st_uid": 0, "st_gid": 0, "st_mode": 16832})
756+
_stat.return_value = stat_result
757+
_getpwnam.return_value.pw_name = SNAP_USER
758+
_getpwnam.return_value.pw_uid = 0
759+
_getpwnam.return_value.pw_gid = 0
760+
761+
execute_direct = _connect_to_database.return_value.cursor.return_value.execute
762+
fetchone_direct = _connect_to_database.return_value.cursor.return_value.fetchone
763+
# No mismatch, so the existence check returns True and no creation/rename occurs
764+
fetchone_direct.return_value = True
765+
766+
harness.charm.postgresql.set_up_database(temp_location="/var/lib/postgresql/tmp")
767+
768+
# No permission/owner fix should be performed
769+
_change_owner.assert_not_called()
770+
_chmod.assert_not_called()
771+
772+
# It should check for temp tablespace existence
773+
execute_direct.assert_any_call("SELECT TRUE FROM pg_tablespace WHERE spcname='temp';")
774+
775+
# Ensure that no rename was attempted
776+
for c in execute_direct.call_args_list:
777+
if c.args:
778+
assert "ALTER TABLESPACE temp RENAME TO" not in c.args[0]

0 commit comments

Comments
 (0)