|
1 | | -import logging |
| 1 | +import pathlib |
| 2 | + |
| 3 | +import pytest |
| 4 | +from saltfactories.utils import random_string |
2 | 5 |
|
3 | 6 | from salt._logging import DFLT_LOG_FMT_JID |
4 | | -from tests.support.helpers import PRE_PYTEST_SKIP |
5 | | - |
6 | | -# Using the PRE_PYTEST_SKIP decorator since this test still fails on some platforms. |
7 | | -# Will investigate later. |
8 | | - |
9 | | - |
10 | | -@PRE_PYTEST_SKIP |
11 | | -def test_jid_in_logs(caplog, salt_call_cli): |
12 | | - """ |
13 | | - Test JID in log_format |
14 | | - """ |
15 | | - jid_formatted_str = DFLT_LOG_FMT_JID.split("%", maxsplit=1)[0] |
16 | | - formatter = logging.Formatter(fmt="%(jid)s %(message)s") |
17 | | - with caplog.at_level(logging.DEBUG): |
18 | | - previous_formatter = caplog.handler.formatter |
19 | | - try: |
20 | | - caplog.handler.setFormatter(formatter) |
21 | | - ret = salt_call_cli.run("test.ping") |
22 | | - assert ret.returncode == 0 |
23 | | - assert ret.data is True |
24 | | - |
25 | | - assert_error_msg = ( |
26 | | - "'{}' not found in log messages:\n>>>>>>>>>{}\n<<<<<<<<<".format( |
27 | | - jid_formatted_str, caplog.text |
28 | | - ) |
29 | | - ) |
30 | | - assert jid_formatted_str in caplog.text, assert_error_msg |
31 | | - finally: |
32 | | - caplog.handler.setFormatter(previous_formatter) |
| 7 | +from salt._logging.impl import DFLT_LOG_FMT_MINION_ID |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope="module") |
| 11 | +def log_field_marker(): |
| 12 | + """ |
| 13 | + Marker to make identifying log fields possible without risk of matching |
| 14 | + other instances of jid or minion_id in the log messages |
| 15 | + """ |
| 16 | + return "EXTRA_LOG_FIELD:" |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(scope="module") |
| 20 | +def logging_master(salt_master_factory, log_field_marker): |
| 21 | + """ |
| 22 | + A logging master fixture with JID and minion_id in log format |
| 23 | + """ |
| 24 | + log_format = ( |
| 25 | + f"{log_field_marker}%(jid)s {log_field_marker}%(minion_id)s %(message)s" |
| 26 | + ) |
| 27 | + config_overrides = { |
| 28 | + "log_level_logfile": "debug", |
| 29 | + "log_fmt_logfile": log_format, |
| 30 | + } |
| 31 | + logging_master_factory = salt_master_factory.salt_master_daemon( |
| 32 | + random_string("master-logging-"), |
| 33 | + overrides=config_overrides, |
| 34 | + extra_cli_arguments_after_first_start_failure=["--log-level=info"], |
| 35 | + ) |
| 36 | + with logging_master_factory.started(): |
| 37 | + yield logging_master_factory |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope="module") |
| 41 | +def logging_master_logfile(logging_master): |
| 42 | + """ |
| 43 | + The logging master log file path |
| 44 | + """ |
| 45 | + assert logging_master.is_running() |
| 46 | + return pathlib.Path(logging_master.config["log_file"]) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(scope="module") |
| 50 | +def salt_cli(logging_master): |
| 51 | + """ |
| 52 | + A ``salt``` CLI fixture |
| 53 | + """ |
| 54 | + assert logging_master.is_running() |
| 55 | + return logging_master.salt_cli(timeout=30) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="module") |
| 59 | +def logging_minion_id(logging_master): |
| 60 | + """ |
| 61 | + Random minion id for a salt-minion fixture |
| 62 | + """ |
| 63 | + return random_string("minion-logging-") |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def logging_minion(logging_master, logging_minion_id): |
| 68 | + """ |
| 69 | + A running salt-minion fixture connected to the logging master |
| 70 | + """ |
| 71 | + assert logging_master.is_running() |
| 72 | + salt_minion_factory = logging_master.salt_minion_daemon( |
| 73 | + logging_minion_id, |
| 74 | + ) |
| 75 | + with salt_minion_factory.started(): |
| 76 | + yield salt_minion_factory |
| 77 | + |
| 78 | + |
| 79 | +def test_jid_minion_id_in_logs( |
| 80 | + logging_master_logfile, log_field_marker, salt_cli, logging_minion |
| 81 | +): |
| 82 | + """ |
| 83 | + Test JID and minion_id appear in master log file in the expected format |
| 84 | + """ |
| 85 | + ret = salt_cli.run("test.ping", "-v", minion_tgt=logging_minion.id) |
| 86 | + assert ret.returncode == 0 |
| 87 | + assert "Executing job with jid" in ret.stdout |
| 88 | + |
| 89 | + jid_str = DFLT_LOG_FMT_JID % {"jid": ret.stdout.splitlines()[0].split()[-1]} |
| 90 | + minion_id_str = DFLT_LOG_FMT_MINION_ID % {"minion_id": logging_minion.id} |
| 91 | + |
| 92 | + log_file_text = logging_master_logfile.read_text() |
| 93 | + |
| 94 | + assert f"{log_field_marker}{jid_str}" in log_file_text |
| 95 | + assert f"{log_field_marker}{minion_id_str}" in log_file_text |
0 commit comments