|
18 | 18 | import json |
19 | 19 | import multiprocessing |
20 | 20 | import os |
| 21 | +import re |
21 | 22 | import shutil |
22 | 23 | import threading |
23 | 24 | import traceback |
24 | 25 | import time |
| 26 | +from textwrap import dedent |
| 27 | + |
25 | 28 | import pytest |
26 | 29 | from unittest import mock |
27 | 30 |
|
@@ -240,6 +243,42 @@ def test_events_ingested_invalid_input(): |
240 | 243 | assert exp_msg == str(excinfo.value) |
241 | 244 |
|
242 | 245 |
|
| 246 | +def test_events_ingested_custom_license_usage(): |
| 247 | + with mock.patch("logging.Logger") as mock_logger: |
| 248 | + log.events_ingested( |
| 249 | + mock_logger, |
| 250 | + "input_type://input_name", |
| 251 | + "sourcetype", |
| 252 | + 5, |
| 253 | + "default", |
| 254 | + license_usage_source="custom:license:source", |
| 255 | + ) |
| 256 | + |
| 257 | + mock_logger.log.assert_called_once_with( |
| 258 | + logging.INFO, |
| 259 | + "action=events_ingested modular_input_name=custom:license:source sourcetype_ingested=sourcetype " |
| 260 | + "n_events=5 event_input=input_name event_index=default", |
| 261 | + ) |
| 262 | + |
| 263 | + with mock.patch("logging.Logger") as mock_logger: |
| 264 | + log.events_ingested( |
| 265 | + mock_logger, |
| 266 | + "demo://modular_input_name", |
| 267 | + "sourcetype", |
| 268 | + 5, |
| 269 | + "default", |
| 270 | + host="abcd", |
| 271 | + account="test_acc", |
| 272 | + license_usage_source="custom:license:source:123", |
| 273 | + ) |
| 274 | + |
| 275 | + mock_logger.log.assert_called_once_with( |
| 276 | + logging.INFO, |
| 277 | + "action=events_ingested modular_input_name=custom:license:source:123 sourcetype_ingested=sourcetype n_" |
| 278 | + "events=5 event_input=modular_input_name event_index=default event_account=test_acc event_host=abcd", |
| 279 | + ) |
| 280 | + |
| 281 | + |
243 | 282 | def test_log_exceptions_full_msg(): |
244 | 283 | start_msg = "some msg before exception" |
245 | 284 | with mock.patch("logging.Logger") as mock_logger: |
@@ -300,3 +339,33 @@ class AddonComplexError(Exception): |
300 | 339 | mock_logger.log.assert_called_with( |
301 | 340 | logging.ERROR, f"exc_l={result} \n{traceback.format_exc()}\n" |
302 | 341 | ) |
| 342 | + |
| 343 | + |
| 344 | +def test_log_format(monkeypatch, tmp_path): |
| 345 | + log_file = tmp_path / "logging_levels.log" |
| 346 | + |
| 347 | + monkeypatch.setattr(log.Logs, "_get_log_file", lambda _, name: str(log_file)) |
| 348 | + |
| 349 | + logger = log.Logs().get_logger("logging_levels") |
| 350 | + |
| 351 | + logger.warning("log 2") |
| 352 | + logger.error("log 3") |
| 353 | + |
| 354 | + log_content = transform_log(log_file.read_text()) |
| 355 | + |
| 356 | + assert ( |
| 357 | + log_content |
| 358 | + == dedent( |
| 359 | + """ |
| 360 | + 2024-03-23 10:15:20,555 log_level=WARNING pid=1234 tid=MainThread file=test_file.py:test_func:123 | log 2 |
| 361 | + 2024-03-23 10:15:20,555 log_level=ERROR pid=1234 tid=MainThread file=test_file.py:test_func:123 | log 3 |
| 362 | + """, |
| 363 | + ).lstrip() |
| 364 | + ) |
| 365 | + |
| 366 | + |
| 367 | +def transform_log(log: str) -> str: |
| 368 | + log = re.sub(r"pid=\d+", "pid=1234", log) |
| 369 | + log = re.sub(r"file=[^ ]+", "file=test_file.py:test_func:123", log) |
| 370 | + log = re.sub(r"\d{4}-\d\d-\d\d \d\d[^ ]+", "2024-03-23 10:15:20,555", log) |
| 371 | + return log |
0 commit comments