|
1 | 1 | # Copyright 2023 Canonical Ltd.
|
2 | 2 | # See LICENSE file for licensing details.
|
3 |
| -from unittest.mock import call, patch |
| 3 | +from unittest.mock import MagicMock, call, patch |
4 | 4 |
|
5 | 5 | import psycopg2
|
6 | 6 | import pytest
|
@@ -460,30 +460,26 @@ def test_build_postgresql_parameters(harness):
|
460 | 460 |
|
461 | 461 |
|
462 | 462 | def test_configure_pgaudit(harness):
|
463 |
| - with patch( |
464 |
| - "charms.postgresql_k8s.v0.postgresql.PostgreSQL._connect_to_database" |
465 |
| - ) as _connect_to_database: |
466 |
| - # Test when pgAudit is enabled. |
467 |
| - execute = ( |
468 |
| - _connect_to_database.return_value.cursor.return_value.__enter__.return_value.execute |
469 |
| - ) |
470 |
| - harness.charm.postgresql._configure_pgaudit(True) |
471 |
| - execute.assert_has_calls([ |
472 |
| - call("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';"), |
473 |
| - call("ALTER SYSTEM SET pgaudit.log_client TO off;"), |
474 |
| - call("ALTER SYSTEM SET pgaudit.log_parameter TO off;"), |
475 |
| - call("SELECT pg_reload_conf();"), |
476 |
| - ]) |
477 |
| - |
478 |
| - # Test when pgAudit is disabled. |
479 |
| - execute.reset_mock() |
480 |
| - harness.charm.postgresql._configure_pgaudit(False) |
481 |
| - execute.assert_has_calls([ |
482 |
| - call("ALTER SYSTEM RESET pgaudit.log;"), |
483 |
| - call("ALTER SYSTEM RESET pgaudit.log_client;"), |
484 |
| - call("ALTER SYSTEM RESET pgaudit.log_parameter;"), |
485 |
| - call("SELECT pg_reload_conf();"), |
486 |
| - ]) |
| 463 | + # Test when pgAudit is enabled. |
| 464 | + mock_conn = MagicMock() |
| 465 | + execute = mock_conn.cursor.return_value.__enter__.return_value.execute |
| 466 | + harness.charm.postgresql._configure_pgaudit(True, mock_conn) |
| 467 | + execute.assert_has_calls([ |
| 468 | + call("ALTER SYSTEM SET pgaudit.log = 'ROLE,DDL,MISC,MISC_SET';"), |
| 469 | + call("ALTER SYSTEM SET pgaudit.log_client TO off;"), |
| 470 | + call("ALTER SYSTEM SET pgaudit.log_parameter TO off;"), |
| 471 | + call("SELECT pg_reload_conf();"), |
| 472 | + ]) |
| 473 | + |
| 474 | + # Test when pgAudit is disabled. |
| 475 | + execute.reset_mock() |
| 476 | + harness.charm.postgresql._configure_pgaudit(False, mock_conn) |
| 477 | + execute.assert_has_calls([ |
| 478 | + call("ALTER SYSTEM RESET pgaudit.log;"), |
| 479 | + call("ALTER SYSTEM RESET pgaudit.log_client;"), |
| 480 | + call("ALTER SYSTEM RESET pgaudit.log_parameter;"), |
| 481 | + call("SELECT pg_reload_conf();"), |
| 482 | + ]) |
487 | 483 |
|
488 | 484 |
|
489 | 485 | def test_validate_group_map(harness):
|
|
0 commit comments