|
11 | 11 | from single_kernel_postgresql.utils.postgresql import ( |
12 | 12 | ACCESS_GROUP_INTERNAL, |
13 | 13 | ACCESS_GROUPS, |
| 14 | + ROLE_DATABASES_OWNER, |
14 | 15 | PostgreSQL, |
15 | 16 | PostgreSQLCreateDatabaseError, |
16 | 17 | PostgreSQLCreateUserError, |
| 18 | + PostgreSQLDatabasesSetupError, |
17 | 19 | PostgreSQLGetLastArchivedWALError, |
18 | 20 | PostgreSQLUndefinedHostError, |
19 | 21 | PostgreSQLUndefinedPasswordError, |
@@ -315,6 +317,105 @@ def test_validate_group_map(harness): |
315 | 317 | assert harness.charm.postgresql.validate_group_map("ldap_group ldap_test_group") is False |
316 | 318 |
|
317 | 319 |
|
| 320 | +def test_set_up_database_with_temp_tablespace_and_missing_owner_role(harness): |
| 321 | + with ( |
| 322 | + patch( |
| 323 | + "single_kernel_postgresql.utils.postgresql.PostgreSQL._connect_to_database" |
| 324 | + ) as _connect_to_database, |
| 325 | + patch("single_kernel_postgresql.utils.postgresql.PostgreSQL.set_up_login_hook_function"), |
| 326 | + patch( |
| 327 | + "single_kernel_postgresql.utils.postgresql.PostgreSQL.set_up_predefined_catalog_roles_function" |
| 328 | + ), |
| 329 | + patch("single_kernel_postgresql.utils.postgresql.PostgreSQL.create_user") as _create_user, |
| 330 | + patch("single_kernel_postgresql.utils.postgresql.change_owner") as _change_owner, |
| 331 | + patch("single_kernel_postgresql.utils.postgresql.os.chmod") as _chmod, |
| 332 | + ): |
| 333 | + # First connection (non-context) for temp tablespace |
| 334 | + execute_direct = _connect_to_database.return_value.cursor.return_value.execute |
| 335 | + fetchone_direct = _connect_to_database.return_value.cursor.return_value.fetchone |
| 336 | + fetchone_direct.return_value = None |
| 337 | + |
| 338 | + # Second and third connections are context-managed |
| 339 | + execute_cm = _connect_to_database.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value.execute |
| 340 | + fetchone_cm = _connect_to_database.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value.fetchone |
| 341 | + fetchone_cm.return_value = None # owner role missing |
| 342 | + |
| 343 | + harness.charm.postgresql.set_up_database(temp_location="/var/lib/postgresql/tmp") |
| 344 | + |
| 345 | + # Ensure permission fixes applied |
| 346 | + _change_owner.assert_called_once_with("/var/lib/postgresql/tmp") |
| 347 | + _chmod.assert_called_once_with("/var/lib/postgresql/tmp", 0o700) |
| 348 | + |
| 349 | + # Validate temp tablespace operations |
| 350 | + execute_direct.assert_has_calls([ |
| 351 | + call("SELECT TRUE FROM pg_tablespace WHERE spcname='temp';"), |
| 352 | + call("CREATE TABLESPACE temp LOCATION '/var/lib/postgresql/tmp';"), |
| 353 | + call("GRANT CREATE ON TABLESPACE temp TO public;"), |
| 354 | + ]) |
| 355 | + |
| 356 | + # create_user called for missing owner role |
| 357 | + _create_user.assert_called_once_with( |
| 358 | + ROLE_DATABASES_OWNER, can_create_database=True, extra_user_roles=["charmed_dml"] |
| 359 | + ) |
| 360 | + |
| 361 | + # Final revokes and grants |
| 362 | + system_users = harness.charm.postgresql.system_users |
| 363 | + expected = [ |
| 364 | + call("REVOKE ALL PRIVILEGES ON DATABASE postgres FROM PUBLIC;"), |
| 365 | + call("REVOKE CREATE ON SCHEMA public FROM PUBLIC;"), |
| 366 | + *[ |
| 367 | + call(SQL("GRANT ALL PRIVILEGES ON DATABASE postgres TO {};").format(Identifier(u))) |
| 368 | + for u in system_users |
| 369 | + ], |
| 370 | + ] |
| 371 | + execute_cm.assert_has_calls(expected, any_order=False) |
| 372 | + |
| 373 | + |
| 374 | +def test_set_up_database_no_temp_and_existing_owner_role(harness): |
| 375 | + with ( |
| 376 | + patch( |
| 377 | + "single_kernel_postgresql.utils.postgresql.PostgreSQL._connect_to_database" |
| 378 | + ) as _connect_to_database, |
| 379 | + patch("single_kernel_postgresql.utils.postgresql.PostgreSQL.set_up_login_hook_function"), |
| 380 | + patch( |
| 381 | + "single_kernel_postgresql.utils.postgresql.PostgreSQL.set_up_predefined_catalog_roles_function" |
| 382 | + ), |
| 383 | + patch("single_kernel_postgresql.utils.postgresql.PostgreSQL.create_user") as _create_user, |
| 384 | + ): |
| 385 | + # owner role exists |
| 386 | + fetchone = _connect_to_database.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value.fetchone |
| 387 | + fetchone.return_value = True |
| 388 | + |
| 389 | + harness.charm.postgresql.set_up_database() |
| 390 | + |
| 391 | + _create_user.assert_not_called() |
| 392 | + |
| 393 | + execute = _connect_to_database.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value.execute |
| 394 | + system_users = harness.charm.postgresql.system_users |
| 395 | + execute.assert_has_calls([ |
| 396 | + call("REVOKE ALL PRIVILEGES ON DATABASE postgres FROM PUBLIC;"), |
| 397 | + call("REVOKE CREATE ON SCHEMA public FROM PUBLIC;"), |
| 398 | + *[ |
| 399 | + call(SQL("GRANT ALL PRIVILEGES ON DATABASE postgres TO {};").format(Identifier(u))) |
| 400 | + for u in system_users |
| 401 | + ], |
| 402 | + ]) |
| 403 | + |
| 404 | + |
| 405 | +def test_set_up_database_raises_wrapped_error(harness): |
| 406 | + with ( |
| 407 | + patch( |
| 408 | + "single_kernel_postgresql.utils.postgresql.PostgreSQL._connect_to_database" |
| 409 | + ) as _connect_to_database, |
| 410 | + patch("single_kernel_postgresql.utils.postgresql.change_owner"), |
| 411 | + patch("single_kernel_postgresql.utils.postgresql.os.chmod"), |
| 412 | + ): |
| 413 | + execute_direct = _connect_to_database.return_value.cursor.return_value.execute |
| 414 | + execute_direct.side_effect = psycopg2.Error |
| 415 | + with pytest.raises(PostgreSQLDatabasesSetupError): |
| 416 | + harness.charm.postgresql.set_up_database(temp_location="/tmp") |
| 417 | + |
| 418 | + |
318 | 419 | def test_connect_to_database(): |
319 | 420 | # Error on no host |
320 | 421 | pg = PostgreSQL(None, None, "operator", None, "postgres", None) |
|
0 commit comments