Debian Codegraph @

dirsrvtests/tests/suites/logging/logging_compression_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2025 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8
9 import glob
10 import ldap
11 import logging
12 import pytest
13 import os
14 import time
15 from lib389._constants import DEFAULT_SUFFIX
16 from lib389.dseldif import DSEldif
17 from test389.topologies import topology_st as topo
18 from lib389.idm.domain import Domain
19 from lib389.idm.directorymanager import DirectoryManager
20
21 log = logging.getLogger(__name__)
22
23 pytestmark = pytest.mark.tier1
24
25
26 def log_rotated_count(log_type, log_dir, check_compressed=False):
27 """
28 Check if the log was rotated and has the correct permissions
29 """
30 log_file = f'{log_dir}/{log_type}.2*'
31 if check_compressed:
32 log_file += ".gz"
33 log_files = glob.glob(log_file)
34 for logf in log_files:
35 # Check permissions
36 st = os.stat(logf)
37 assert oct(st.st_mode) == '0o100600' # 0600
38
39 return len(log_files)
40
41
42 def update_and_sleep(inst, suffix, sleep=True):
43 for loop in range(2):
44 for count in range(10):
45 suffix.replace('description', str(count))
46 suffix.get_attr_val('description')
47 with pytest.raises(ldap.OBJECT_CLASS_VIOLATION):
48 suffix.add('doesNotExist', 'error')
49 # For security log we need binds to populate the log
50 DirectoryManager(inst).bind()
51
52 if sleep:
53 # log rotation smallest unit is 1 minute
54 time.sleep(61)
55 else:
56 # should still sleep for a little bit
57 time.sleep(1)
58
59
60 def test_logging_compression(topo):
61 """Test logging compression works, and log rotation/deletion is still
62 functional. This also tests a mix of non-compressed/compressed logs.
63
64 :id: 15b5ed0e-628c-48e5-a61e-43908590c9f1
65 :setup: Standalone Instance
66 :steps:
67 1. Enable all the logs (audit,and auditfail)
68 2. Set an aggressive rotation/deletion policy
69 3. Make sure all logs are rotated at least once
70 4. Enable log compression on all logs
71 5. Make sure all logs are rotated again and are compressed
72 6. Make sure log deletion is working
73 :expectedresults:
74 1. Success
75 2. Success
76 3. Success
77 4. Success
78 5. Success
79 6. Success
80 """
81
82 inst = topo.standalone
83 suffix = Domain(topo.standalone, DEFAULT_SUFFIX)
84 timeunit = "minute" # This is the smallest time unit available
85 log_dir = inst.get_log_dir()
86
87 # Enable all the logs (audit, and auditfail)
88 inst.stop()
89 dse_ldif = DSEldif(inst)
90 dse_ldif.replace('cn=config', 'nsslapd-auditfaillog', log_dir + "/auditfail")
91 inst.start()
92
93 inst.config.set('nsslapd-auditlog-logging-enabled', 'on')
94 inst.config.set('nsslapd-auditfaillog-logging-enabled', 'on')
95 inst.config.set('nsslapd-accesslog-logbuffering', 'off')
96 inst.config.set('nsslapd-securitylog-logbuffering', 'off')
97 inst.config.set('nsslapd-errorlog-level', '64')
98
99 # Set an aggressive rotation/deletion policy for all logs
100 for ds_log in ['accesslog', 'auditlog', 'auditfaillog', 'errorlog', 'securitylog']:
101 inst.config.set('nsslapd-' + ds_log + '-logrotationtime', '1')
102 inst.config.set('nsslapd-' + ds_log + '-logrotationtimeunit', timeunit)
103 inst.config.set('nsslapd-' + ds_log + '-maxlogsize', '1')
104 inst.config.set('nsslapd-' + ds_log + '-maxlogsperdir', '3')
105
106 # Perform ops that will write to each log
107 update_and_sleep(topo.standalone, suffix)
108
109 # Make sure logs are rotated
110 for log_type in ['access', 'audit', 'auditfail', 'errors', 'security']:
111 assert log_rotated_count(log_type, log_dir) > 0
112
113 # Enable log compression on all logs
114 for ds_log in ['accesslog', 'auditlog', 'auditfaillog', 'errorlog', 'securitylog']:
115 inst.config.set('nsslapd-' + ds_log + '-compress', 'on')
116
117 # Perform ops that will write to each log
118 update_and_sleep(topo.standalone, suffix)
119
120 # Make sure all logs were rotated again and are compressed
121 for log_type in ['access', 'audit', 'auditfail', 'errors', 'security']:
122 assert log_rotated_count(log_type, log_dir, check_compressed=True) > 0
123
124 # Make sure log deletion is working
125 update_and_sleep(topo.standalone, suffix, sleep=False)
126 for log_type in ['access', 'audit', 'auditfail', 'errors', 'security']:
127 assert log_rotated_count(log_type, log_dir) == 2
128
129
130 if __name__ == '__main__':
131 # Run isolated
132 # -s for DEBUG mode
133 CURRENT_FILE = os.path.realpath(__file__)
134 pytest.main(["-s", CURRENT_FILE])