Debian Codegraph @

dirsrvtests/tests/suites/logging/error_json_logging_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 logging
10 import os
11 import time
12 import pytest
13 from lib389.properties import TASK_WAIT
14 from test389.topologies import topology_st as topo
15 from lib389.dirsrv_log import DirsrvErrorJSONLog
16
17 log = logging.getLogger(__name__)
18
19 MAIN_KEYS = [
20 "local_time",
21 "severity",
22 "subsystem",
23 "msg",
24 ]
25
26
27 @pytest.mark.parametrize("log_format", ["json", "json-pretty"])
28 def test_error_json_format(topo, log_format):
29 """Test error log is in JSON
30
31 :id: c9afb295-43de-4581-af8b-ec8f25a06d75
32 :parametrized: yes
33 :setup: Standalone
34 :steps:
35 1. Check error log has json and the expected data is present
36 :expectedresults:
37 1. Success
38 """
39
40 inst = topo.standalone
41 inst.config.replace('nsslapd-errorlog-logbuffering', 'off') # Just in case
42 inst.config.set("nsslapd-errorlog-log-format", log_format)
43 inst.stop()
44 inst.deleteErrorLogs()
45 inst.start()
46 time.sleep(1)
47
48 error_log = DirsrvErrorJSONLog(inst)
49 log_lines = error_log.parse_log()
50 for event in log_lines:
51 if event is None or 'header' in event:
52 # Skip non-json or header line
53 continue
54 for key in MAIN_KEYS:
55 assert key in event and event[key] != ""
56
57
58 if __name__ == '__main__':
59 # Run isolated
60 # -s for DEBUG mode
61 CURRENT_FILE = os.path.realpath(__file__)
62 pytest.main(["-s", CURRENT_FILE])