| 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 os |
| 11 |
import logging |
| 12 |
import time |
| 13 |
import pytest |
| 14 |
from lib389._constants import DEFAULT_SUFFIX, PW_DM |
| 15 |
from lib389.tasks import ImportTask |
| 16 |
from lib389.idm.user import UserAccounts |
| 17 |
from lib389.idm.domain import Domain |
| 18 |
from lib389.idm.directorymanager import DirectoryManager |
| 19 |
from test389.topologies import topology_st as topo |
| 20 |
|
| 21 |
|
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
|
| 25 |
def remove_rotated_access_logs(inst): |
| 26 |
""" |
| 27 |
Remove all rotated access log files to start fresh for each test. |
| 28 |
This prevents log files from previous tests affecting current test results. |
| 29 |
""" |
| 30 |
log_dir = inst.get_log_dir() |
| 31 |
patterns = [ |
| 32 |
f'{log_dir}/access.2*', # Uncompressed rotated logs |
| 33 |
f'{log_dir}/access.*.gz', # Compressed rotated logs |
| 34 |
] |
| 35 |
for pattern in patterns: |
| 36 |
for log_file in glob.glob(pattern): |
| 37 |
try: |
| 38 |
os.remove(log_file) |
| 39 |
log.info(f"Removed old log file: {log_file}") |
| 40 |
except OSError as e: |
| 41 |
log.warning(f"Could not remove {log_file}: {e}") |
| 42 |
|
| 43 |
|
| 44 |
def reset_access_log_config(inst): |
| 45 |
""" |
| 46 |
Reset access log configuration to default values. |
| 47 |
""" |
| 48 |
inst.config.set('nsslapd-accesslog-compress', 'off') |
| 49 |
inst.config.set('nsslapd-accesslog-maxlogsize', '100') |
| 50 |
inst.config.set('nsslapd-accesslog-maxlogsperdir', '10') |
| 51 |
inst.config.set('nsslapd-accesslog-logrotationsync-enabled', 'off') |
| 52 |
inst.config.set('nsslapd-accesslog-logbuffering', 'on') |
| 53 |
inst.config.set('nsslapd-accesslog-logexpirationtime', '-1') |
| 54 |
inst.config.set('nsslapd-accesslog-logminfreediskspace', '5') |
| 55 |
|
| 56 |
|
| 57 |
def generate_heavy_load(inst, suffix, iterations=50): |
| 58 |
""" |
| 59 |
Generate heavy LDAP load to fill access log quickly. |
| 60 |
Performs multiple operations: searches, modifies, binds to populate logs. |
| 61 |
""" |
| 62 |
for i in range(iterations): |
| 63 |
suffix.replace('description', f'iteration_{i}') |
| 64 |
suffix.get_attr_val('description') |
| 65 |
|
| 66 |
|
| 67 |
def count_access_logs(log_dir, compressed_only=False): |
| 68 |
""" |
| 69 |
Count access log files in the log directory. |
| 70 |
Returns count of rotated access logs (not including the active 'access' file). |
| 71 |
""" |
| 72 |
if compressed_only: |
| 73 |
pattern = f'{log_dir}/access.*.gz' |
| 74 |
else: |
| 75 |
pattern = f'{log_dir}/access.2*' |
| 76 |
log_files = glob.glob(pattern) |
| 77 |
return len(log_files) |
| 78 |
|
| 79 |
|
| 80 |
def test_log_pileup_with_compression(topo): |
| 81 |
"""Test that log rotation properly deletes old logs when compression is enabled. |
| 82 |
|
| 83 |
:id: fa1bfce8-b6d3-4520-a0a8-bead14fa5838 |
| 84 |
:setup: Standalone Instance |
| 85 |
:steps: |
| 86 |
1. Clean up existing rotated logs and reset configuration |
| 87 |
2. Enable access log compression |
| 88 |
3. Set strict log limits (small maxlogsperdir) |
| 89 |
4. Disable log expiration to test count-based deletion |
| 90 |
5. Generate heavy load to create many log rotations |
| 91 |
6. Verify log count does not exceed maxlogsperdir limit |
| 92 |
:expectedresults: |
| 93 |
1. Success |
| 94 |
2. Success |
| 95 |
3. Success |
| 96 |
4. Success |
| 97 |
5. Success |
| 98 |
6. Log count should be at or below maxlogsperdir + small buffer |
| 99 |
""" |
| 100 |
|
| 101 |
inst = topo.standalone |
| 102 |
suffix = Domain(inst, DEFAULT_SUFFIX) |
| 103 |
log_dir = inst.get_log_dir() |
| 104 |
|
| 105 |
# Clean up before test |
| 106 |
remove_rotated_access_logs(inst) |
| 107 |
reset_access_log_config(inst) |
| 108 |
inst.restart() |
| 109 |
|
| 110 |
max_logs = 5 |
| 111 |
inst.config.set('nsslapd-accesslog-compress', 'on') |
| 112 |
inst.config.set('nsslapd-accesslog-maxlogsperdir', str(max_logs)) |
| 113 |
inst.config.set('nsslapd-accesslog-maxlogsize', '1') # 1MB to trigger rotation |
| 114 |
inst.config.set('nsslapd-accesslog-logrotationsync-enabled', 'off') |
| 115 |
inst.config.set('nsslapd-accesslog-logbuffering', 'off') |
| 116 |
|
| 117 |
inst.config.set('nsslapd-accesslog-logexpirationtime', '-1') |
| 118 |
|
| 119 |
inst.config.set('nsslapd-accesslog-logminfreediskspace', '5') |
| 120 |
|
| 121 |
inst.restart() |
| 122 |
time.sleep(2) |
| 123 |
|
| 124 |
target_logs = max_logs * 3 |
| 125 |
for i in range(target_logs): |
| 126 |
log.info(f"Generating load for log rotation {i+1}/{target_logs}") |
| 127 |
generate_heavy_load(inst, suffix, iterations=150) |
| 128 |
time.sleep(1) # Wait for rotation |
| 129 |
|
| 130 |
time.sleep(3) |
| 131 |
|
| 132 |
logs_on_disk = count_access_logs(log_dir) |
| 133 |
log.info(f"Configured maxlogsperdir: {max_logs}") |
| 134 |
log.info(f"Actual rotated logs on disk: {logs_on_disk}") |
| 135 |
|
| 136 |
all_access_logs = glob.glob(f'{log_dir}/access*') |
| 137 |
log.info(f"All access log files: {all_access_logs}") |
| 138 |
|
| 139 |
max_allowed = max_logs + 2 |
| 140 |
assert logs_on_disk <= max_allowed, ( |
| 141 |
f"Log rotation failed to delete old files! " |
| 142 |
f"Expected at most {max_allowed} rotated logs (maxlogsperdir={max_logs} + 2 buffer), " |
| 143 |
f"but found {logs_on_disk}. The server has lost track of the file list." |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.parametrize("compress_enabled", ["on", "off"]) |
| 148 |
def test_accesslog_list_mismatch(topo, compress_enabled): |
| 149 |
"""Test that nsslapd-accesslog-list stays synchronized with actual log files. |
| 150 |
|
| 151 |
:id: 0a8a46a6-cae7-43bd-8b64-5e3481480cd3 |
| 152 |
:parametrized: yes |
| 153 |
:setup: Standalone Instance |
| 154 |
:steps: |
| 155 |
1. Clean up existing rotated logs and reset configuration |
| 156 |
2. Configure log rotation with compression enabled/disabled |
| 157 |
3. Generate activity to trigger multiple rotations |
| 158 |
4. Get the nsslapd-accesslog-list attribute |
| 159 |
5. Compare with actual files on disk |
| 160 |
6. Verify they match (accounting for .gz extension when enabled) |
| 161 |
:expectedresults: |
| 162 |
1. Success |
| 163 |
2. Success |
| 164 |
3. Success |
| 165 |
4. Success |
| 166 |
5. Success |
| 167 |
6. The list attribute should match actual files on disk |
| 168 |
""" |
| 169 |
|
| 170 |
inst = topo.standalone |
| 171 |
suffix = Domain(inst, DEFAULT_SUFFIX) |
| 172 |
log_dir = inst.get_log_dir() |
| 173 |
compression_on = compress_enabled == "on" |
| 174 |
|
| 175 |
# Clean up before test |
| 176 |
remove_rotated_access_logs(inst) |
| 177 |
reset_access_log_config(inst) |
| 178 |
inst.restart() |
| 179 |
|
| 180 |
inst.config.set('nsslapd-accesslog-compress', compress_enabled) |
| 181 |
inst.config.set('nsslapd-accesslog-maxlogsize', '1') |
| 182 |
inst.config.set('nsslapd-accesslog-maxlogsperdir', '10') |
| 183 |
inst.config.set('nsslapd-accesslog-logrotationsync-enabled', 'off') |
| 184 |
inst.config.set('nsslapd-accesslog-logbuffering', 'off') |
| 185 |
inst.config.set('nsslapd-accesslog-logexpirationtime', '-1') |
| 186 |
|
| 187 |
inst.restart() |
| 188 |
time.sleep(2) |
| 189 |
|
| 190 |
for i in range(15): |
| 191 |
suffix_note = "(no compression)" if not compression_on else "" |
| 192 |
log.info(f"Generating load for rotation {i+1}/15 {suffix_note}") |
| 193 |
generate_heavy_load(inst, suffix, iterations=150) |
| 194 |
time.sleep(1) |
| 195 |
|
| 196 |
time.sleep(3) |
| 197 |
|
| 198 |
accesslog_list = inst.config.get_attr_vals_utf8('nsslapd-accesslog-list') |
| 199 |
log.info(f"nsslapd-accesslog-list entries (compress={compress_enabled}): {len(accesslog_list)}") |
| 200 |
log.info(f"nsslapd-accesslog-list (compress={compress_enabled}): {accesslog_list}") |
| 201 |
|
| 202 |
disk_files = glob.glob(f'{log_dir}/access.2*') |
| 203 |
log.info(f"Actual files on disk (compress={compress_enabled}): {len(disk_files)}") |
| 204 |
log.info(f"Disk files (compress={compress_enabled}): {disk_files}") |
| 205 |
|
| 206 |
disk_files_for_compare = set() |
| 207 |
for fpath in disk_files: |
| 208 |
if compression_on and fpath.endswith('.gz'): |
| 209 |
disk_files_for_compare.add(fpath[:-3]) |
| 210 |
else: |
| 211 |
disk_files_for_compare.add(fpath) |
| 212 |
|
| 213 |
list_files_set = set(accesslog_list) |
| 214 |
missing_from_disk = list_files_set - disk_files_for_compare |
| 215 |
extra_on_disk = disk_files_for_compare - list_files_set |
| 216 |
|
| 217 |
if missing_from_disk: |
| 218 |
log.error( |
| 219 |
f"[compress={compress_enabled}] Files in list but NOT on disk: {missing_from_disk}" |
| 220 |
) |
| 221 |
if extra_on_disk: |
| 222 |
log.warning( |
| 223 |
f"[compress={compress_enabled}] Files on disk but NOT in list: {extra_on_disk}" |
| 224 |
) |
| 225 |
|
| 226 |
assert not missing_from_disk, ( |
| 227 |
f"nsslapd-accesslog-list mismatch (compress={compress_enabled})! " |
| 228 |
f"Files listed but missing from disk: {missing_from_disk}. " |
| 229 |
f"This indicates the server's internal list is out of sync with actual files." |
| 230 |
) |
| 231 |
|
| 232 |
if len(extra_on_disk) > 2: |
| 233 |
log.warning( |
| 234 |
f"Potential log tracking issue (compress={compress_enabled}): " |
| 235 |
f"{len(extra_on_disk)} files on disk are not tracked in the accesslog-list: " |
| 236 |
f"{extra_on_disk}" |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
def test_accesslog_list_mixed_compression(topo): |
| 241 |
"""Test that nsslapd-accesslog-list correctly tracks both compressed and uncompressed logs. |
| 242 |
|
| 243 |
:id: 11b088cd-23be-407d-ad16-4ce2e12da09e |
| 244 |
:setup: Standalone Instance |
| 245 |
:steps: |
| 246 |
1. Clean up existing rotated logs and reset configuration |
| 247 |
2. Create rotated logs with compression OFF |
| 248 |
3. Enable compression and create more rotated logs |
| 249 |
4. Get the nsslapd-accesslog-list attribute |
| 250 |
5. Compare with actual files on disk |
| 251 |
6. Verify all files are correctly tracked (uncompressed and compressed) |
| 252 |
:expectedresults: |
| 253 |
1. Success |
| 254 |
2. Success - uncompressed rotated logs created |
| 255 |
3. Success - compressed rotated logs created |
| 256 |
4. Success |
| 257 |
5. Success |
| 258 |
6. The list should contain base filenames (without .gz) that |
| 259 |
correspond to files on disk (either as-is or with .gz suffix) |
| 260 |
""" |
| 261 |
|
| 262 |
inst = topo.standalone |
| 263 |
suffix = Domain(inst, DEFAULT_SUFFIX) |
| 264 |
log_dir = inst.get_log_dir() |
| 265 |
|
| 266 |
# Clean up before test |
| 267 |
remove_rotated_access_logs(inst) |
| 268 |
reset_access_log_config(inst) |
| 269 |
inst.restart() |
| 270 |
|
| 271 |
inst.config.set('nsslapd-accesslog-compress', 'off') |
| 272 |
inst.config.set('nsslapd-accesslog-maxlogsize', '1') |
| 273 |
inst.config.set('nsslapd-accesslog-maxlogsperdir', '20') |
| 274 |
inst.config.set('nsslapd-accesslog-logrotationsync-enabled', 'off') |
| 275 |
inst.config.set('nsslapd-accesslog-logbuffering', 'off') |
| 276 |
inst.config.set('nsslapd-accesslog-logexpirationtime', '-1') |
| 277 |
|
| 278 |
inst.restart() |
| 279 |
time.sleep(2) |
| 280 |
|
| 281 |
for i in range(15): |
| 282 |
log.info(f"Generating load for uncompressed rotation {i+1}/15") |
| 283 |
generate_heavy_load(inst, suffix, iterations=150) |
| 284 |
time.sleep(1) |
| 285 |
|
| 286 |
time.sleep(2) |
| 287 |
|
| 288 |
# Check what we have so far |
| 289 |
uncompressed_files = glob.glob(f'{log_dir}/access.2*') |
| 290 |
log.info(f"Files on disk after uncompressed phase: {uncompressed_files}") |
| 291 |
|
| 292 |
inst.config.set('nsslapd-accesslog-compress', 'on') |
| 293 |
inst.restart() |
| 294 |
time.sleep(2) |
| 295 |
|
| 296 |
for i in range(15): |
| 297 |
log.info(f"Generating load for compressed rotation {i+1}/15") |
| 298 |
generate_heavy_load(inst, suffix, iterations=150) |
| 299 |
time.sleep(1) |
| 300 |
|
| 301 |
time.sleep(3) |
| 302 |
|
| 303 |
accesslog_list = inst.config.get_attr_vals_utf8('nsslapd-accesslog-list') |
| 304 |
|
| 305 |
disk_files = glob.glob(f'{log_dir}/access.2*') |
| 306 |
|
| 307 |
log.info(f"nsslapd-accesslog-list entries: {len(accesslog_list)}") |
| 308 |
log.info(f"nsslapd-accesslog-list: {sorted(accesslog_list)}") |
| 309 |
log.info(f"Actual files on disk: {len(disk_files)}") |
| 310 |
log.info(f"Disk files: {sorted(disk_files)}") |
| 311 |
|
| 312 |
compressed_on_disk = [f for f in disk_files if f.endswith('.gz')] |
| 313 |
uncompressed_on_disk = [f for f in disk_files if not f.endswith('.gz')] |
| 314 |
log.info(f"Compressed files on disk: {compressed_on_disk}") |
| 315 |
log.info(f"Uncompressed files on disk: {uncompressed_on_disk}") |
| 316 |
|
| 317 |
list_files_set = set(accesslog_list) |
| 318 |
|
| 319 |
disk_files_base = set() |
| 320 |
for fpath in disk_files: |
| 321 |
if fpath.endswith('.gz'): |
| 322 |
disk_files_base.add(fpath[:-3]) # Strip .gz |
| 323 |
else: |
| 324 |
disk_files_base.add(fpath) |
| 325 |
|
| 326 |
missing_from_disk = list_files_set - disk_files_base |
| 327 |
|
| 328 |
extra_on_disk = disk_files_base - list_files_set |
| 329 |
|
| 330 |
if missing_from_disk: |
| 331 |
log.error(f"Files in list but NOT on disk: {missing_from_disk}") |
| 332 |
if extra_on_disk: |
| 333 |
log.warning(f"Files on disk but NOT in list: {extra_on_disk}") |
| 334 |
|
| 335 |
assert not missing_from_disk, ( |
| 336 |
f"nsslapd-accesslog-list contains stale entries! " |
| 337 |
f"Files in list but not on disk (as base or .gz): {missing_from_disk}" |
| 338 |
) |
| 339 |
|
| 340 |
for list_file in accesslog_list: |
| 341 |
exists_uncompressed = os.path.exists(list_file) |
| 342 |
exists_compressed = os.path.exists(list_file + '.gz') |
| 343 |
assert exists_uncompressed or exists_compressed, ( |
| 344 |
f"File in accesslog-list does not exist on disk: {list_file} " |
| 345 |
f"(checked both {list_file} and {list_file}.gz)" |
| 346 |
) |
| 347 |
if exists_compressed and not exists_uncompressed: |
| 348 |
log.info(f" {list_file} -> exists as .gz (compressed)") |
| 349 |
elif exists_uncompressed: |
| 350 |
log.info(f" {list_file} -> exists (uncompressed)") |
| 351 |
|
| 352 |
if len(extra_on_disk) > 1: |
| 353 |
log.warning( |
| 354 |
f"Some files on disk are not tracked in accesslog-list: {extra_on_disk}" |
| 355 |
) |
| 356 |
|
| 357 |
log.info("Mixed compression test completed successfully") |
| 358 |
|
| 359 |
|
| 360 |
def test_log_flush_and_rotation_crash(topo): |
| 361 |
"""Make sure server does not crash when flushing a buffer and rotating |
| 362 |
the log at the same time |
| 363 |
|
| 364 |
:id: d4b0af2f-48b2-45f5-ae8b-f06f692c3133 |
| 365 |
:setup: Standalone Instance |
| 366 |
:steps: |
| 367 |
1. Enable all logs |
| 368 |
2. Enable log buffering for all logs |
| 369 |
3. Set rotation time unit to 1 minute |
| 370 |
4. Make sure server is still running after 1 minute |
| 371 |
:expectedresults: |
| 372 |
1. Success |
| 373 |
2. Success |
| 374 |
3. Success |
| 375 |
4. Success |
| 376 |
""" |
| 377 |
# NOTE: This test is placed last as it may affect the suffix state. |
| 378 |
|
| 379 |
inst = topo.standalone |
| 380 |
|
| 381 |
# Enable logging and buffering |
| 382 |
inst.config.set("nsslapd-auditlog-logging-enabled", "on") |
| 383 |
inst.config.set("nsslapd-accesslog-logbuffering", "on") |
| 384 |
inst.config.set("nsslapd-auditlog-logbuffering", "on") |
| 385 |
inst.config.set("nsslapd-errorlog-logbuffering", "on") |
| 386 |
inst.config.set("nsslapd-securitylog-logbuffering", "on") |
| 387 |
|
| 388 |
# Set rotation policy to trigger rotation asap |
| 389 |
inst.config.set("nsslapd-accesslog-logrotationtimeunit", "minute") |
| 390 |
inst.config.set("nsslapd-auditlog-logrotationtimeunit", "minute") |
| 391 |
inst.config.set("nsslapd-errorlog-logrotationtimeunit", "minute") |
| 392 |
inst.config.set("nsslapd-securitylog-logrotationtimeunit", "minute") |
| 393 |
|
| 394 |
# |
| 395 |
# Performs ops to populate all the logs |
| 396 |
# |
| 397 |
# Access & audit log |
| 398 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX) |
| 399 |
user = users.create_test_user() |
| 400 |
user.set("userPassword", PW_DM) |
| 401 |
# Security log |
| 402 |
user.bind(PW_DM) |
| 403 |
# Error log |
| 404 |
import_task = ImportTask(inst) |
| 405 |
import_task.import_suffix_from_ldif(ldiffile="/not/here", |
| 406 |
suffix=DEFAULT_SUFFIX) |
| 407 |
|
| 408 |
# Wait a minute and make sure the server did not crash |
| 409 |
log.info("Sleep until logs are flushed and rotated") |
| 410 |
time.sleep(61) |
| 411 |
|
| 412 |
assert inst.status() |
| 413 |
|
| 414 |
|
| 415 |
if __name__ == '__main__': |
| 416 |
# Run isolated |
| 417 |
# -s for DEBUG mode |
| 418 |
CURRENT_FILE = os.path.realpath(__file__) |
| 419 |
pytest.main(["-s", CURRENT_FILE]) |
| 420 |
|