|
3 | 3 | # This file is part of Checkmk (https://checkmk.com). It is subject to the terms and |
4 | 4 | # conditions defined in the file COPYING, which is part of this source code package. |
5 | 5 |
|
| 6 | +import datetime |
6 | 7 | from collections.abc import Mapping |
7 | 8 | from typing import Final |
| 9 | +from zoneinfo import ZoneInfo |
8 | 10 |
|
9 | 11 | import pytest |
| 12 | +import time_machine |
10 | 13 | from pytest import MonkeyPatch |
11 | 14 |
|
12 | 15 | import cmk.base.events |
13 | 16 | from cmk.base.events import ( |
14 | 17 | _update_enriched_context_from_notify_host_file, |
15 | 18 | add_to_event_context, |
16 | 19 | apply_matchers, |
| 20 | + complete_raw_context, |
17 | 21 | event_match_hosttags, |
18 | 22 | raw_context_from_string, |
19 | 23 | ) |
20 | | -from cmk.events.event_context import EnrichedEventContext, EventContext |
| 24 | +from cmk.events.event_context import EnrichedEventContext, EventContext, HostName |
21 | 25 | from cmk.utils.http_proxy_config import ( |
22 | 26 | EnvironmentProxyConfig, |
23 | 27 | HTTPProxySpec, |
@@ -60,6 +64,58 @@ def test_raw_context_from_string(context: str, expected: EventContext) -> None: |
60 | 64 | assert raw_context_from_string(context) == expected |
61 | 65 |
|
62 | 66 |
|
| 67 | +@pytest.mark.parametrize( |
| 68 | + "tz, expected_host, expected_service", |
| 69 | + [ |
| 70 | + ("Australia/Brisbane", "2025-05-12 18:11:34 AEST", "2025-05-12 18:15:00 AEST"), |
| 71 | + ("UTC", "2025-05-12 08:11:34 UTC", "2025-05-12 08:15:00 UTC"), |
| 72 | + ], |
| 73 | +) |
| 74 | +def test_complete_raw_context_pre_formats_state_change_with_local_tz( |
| 75 | + tz: str, expected_host: str, expected_service: str |
| 76 | +) -> None: |
| 77 | + raw_context = EventContext( |
| 78 | + HOSTNAME=HostName("heute"), |
| 79 | + CONTACTS="cmkadmin", |
| 80 | + MICROTIME="1747037494000000", |
| 81 | + LASTHOSTSTATECHANGE="1747037494", |
| 82 | + LASTSERVICESTATECHANGE="1747037700", |
| 83 | + ) |
| 84 | + |
| 85 | + with time_machine.travel(datetime.datetime.now(tz=ZoneInfo(tz)), tick=False): |
| 86 | + enriched = complete_raw_context( |
| 87 | + raw_context, |
| 88 | + ensure_nagios=lambda _msg: None, |
| 89 | + with_dump=False, |
| 90 | + contacts_needed=False, |
| 91 | + analyse=False, |
| 92 | + ) |
| 93 | + |
| 94 | + assert enriched["LASTHOSTSTATECHANGE_LOCAL"] == expected_host |
| 95 | + assert enriched["LASTSERVICESTATECHANGE_LOCAL"] == expected_service |
| 96 | + |
| 97 | + |
| 98 | +def test_complete_raw_context_skips_local_field_for_host_notification() -> None: |
| 99 | + raw_context = EventContext( |
| 100 | + HOSTNAME=HostName("heute"), |
| 101 | + CONTACTS="cmkadmin", |
| 102 | + MICROTIME="1747037494000000", |
| 103 | + LASTHOSTSTATECHANGE="1747037494", |
| 104 | + ) |
| 105 | + |
| 106 | + with time_machine.travel(datetime.datetime.now(tz=ZoneInfo("UTC")), tick=False): |
| 107 | + enriched = complete_raw_context( |
| 108 | + raw_context, |
| 109 | + ensure_nagios=lambda _msg: None, |
| 110 | + with_dump=False, |
| 111 | + contacts_needed=False, |
| 112 | + analyse=False, |
| 113 | + ) |
| 114 | + |
| 115 | + assert enriched["LASTHOSTSTATECHANGE_LOCAL"] == "2025-05-12 08:11:34 UTC" |
| 116 | + assert "LASTSERVICESTATECHANGE_LOCAL" not in enriched |
| 117 | + |
| 118 | + |
63 | 119 | def test_add_to_event_context_param_overrides_context() -> None: |
64 | 120 | context = {"FOO": "bar", "BAZ": "old"} |
65 | 121 | add_to_event_context(context, "BAZ", "new", lambda *args, **kw: HTTP_PROXY) |
|
0 commit comments