@@ -35,39 +35,11 @@ async def test_audit_plugin(ops_test: OpsTest, charm) -> None:
35
35
apps = [APPLICATION_NAME , DATABASE_APP_NAME ], status = "active"
36
36
)
37
37
38
- logger .info ("Checking that the audit plugin is disabled " )
38
+ logger .info ("Checking that the audit plugin is enabled " )
39
39
connection_string = await build_connection_string (
40
40
ops_test , APPLICATION_NAME , RELATION_ENDPOINT
41
41
)
42
42
connection = None
43
- try :
44
- connection = psycopg2 .connect (connection_string )
45
- with connection .cursor () as cursor :
46
- cursor .execute ("CREATE TABLE test1(value TEXT);" )
47
- cursor .execute ("GRANT SELECT ON test1 TO PUBLIC;" )
48
- cursor .execute ("SET TIME ZONE 'Europe/Rome';" )
49
- finally :
50
- if connection is not None :
51
- connection .close ()
52
- try :
53
- logs = await run_command_on_unit (
54
- ops_test ,
55
- "postgresql/0" ,
56
- "sudo grep AUDIT /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log" ,
57
- )
58
- except Exception :
59
- pass
60
- else :
61
- logger .info (f"Logs: { logs } " )
62
- assert False , "Audit logs were found when the plugin is disabled."
63
-
64
- logger .info ("Enabling the audit plugin" )
65
- await ops_test .model .applications [DATABASE_APP_NAME ].set_config ({
66
- "plugin_audit_enable" : "True"
67
- })
68
- await ops_test .model .wait_for_idle (apps = [DATABASE_APP_NAME ], status = "active" )
69
-
70
- logger .info ("Checking that the audit plugin is enabled" )
71
43
try :
72
44
connection = psycopg2 .connect (connection_string )
73
45
with connection .cursor () as cursor :
@@ -77,12 +49,13 @@ async def test_audit_plugin(ops_test: OpsTest, charm) -> None:
77
49
finally :
78
50
if connection is not None :
79
51
connection .close ()
52
+ unit_name = f"{ DATABASE_APP_NAME } /0"
80
53
for attempt in Retrying (stop = stop_after_delay (90 ), wait = wait_fixed (10 ), reraise = True ):
81
54
with attempt :
82
55
try :
83
56
logs = await run_command_on_unit (
84
57
ops_test ,
85
- "postgresql/0" ,
58
+ unit_name ,
86
59
"sudo grep AUDIT /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log" ,
87
60
)
88
61
assert "MISC,BEGIN,,,BEGIN" in logs
@@ -93,3 +66,38 @@ async def test_audit_plugin(ops_test: OpsTest, charm) -> None:
93
66
assert "MISC,SET,,,SET TIME ZONE 'Europe/Rome';" in logs
94
67
except Exception :
95
68
assert False , "Audit logs were not found when the plugin is enabled."
69
+
70
+ logger .info ("Disabling the audit plugin" )
71
+ await ops_test .model .applications [DATABASE_APP_NAME ].set_config ({
72
+ "plugin_audit_enable" : "False"
73
+ })
74
+ await ops_test .model .wait_for_idle (apps = [DATABASE_APP_NAME ], status = "active" )
75
+
76
+ logger .info ("Removing the previous logs" )
77
+ await run_command_on_unit (
78
+ ops_test ,
79
+ unit_name ,
80
+ "rm /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log" ,
81
+ )
82
+
83
+ logger .info ("Checking that the audit plugin is disabled" )
84
+ try :
85
+ connection = psycopg2 .connect (connection_string )
86
+ with connection .cursor () as cursor :
87
+ cursor .execute ("CREATE TABLE test1(value TEXT);" )
88
+ cursor .execute ("GRANT SELECT ON test1 TO PUBLIC;" )
89
+ cursor .execute ("SET TIME ZONE 'Europe/Rome';" )
90
+ finally :
91
+ if connection is not None :
92
+ connection .close ()
93
+ try :
94
+ logs = await run_command_on_unit (
95
+ ops_test ,
96
+ unit_name ,
97
+ "sudo grep AUDIT /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log" ,
98
+ )
99
+ except Exception :
100
+ pass
101
+ else :
102
+ logger .info (f"Logs: { logs } " )
103
+ assert False , "Audit logs were found when the plugin is disabled."
0 commit comments