|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (C) 2026 Checkmk GmbH - License: GNU General Public License v2 |
| 3 | +# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and |
| 4 | +# conditions defined in the file COPYING, which is part of this source code package. |
| 5 | +"""Unit tests for cmk.crash_reporting.cli. |
| 6 | +
|
| 7 | +Network calls are intercepted with `responses`; no real OMD site is needed. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | +import responses |
| 16 | + |
| 17 | +from cmk.ccc.site import omd_site |
| 18 | +from cmk.crash_reporting import cli |
| 19 | + |
| 20 | +_CRASH_URL = "https://crash.checkmk.com" |
| 21 | + |
| 22 | + |
| 23 | +def _write_global_mk(path: Path, **settings: object) -> None: |
| 24 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 25 | + path.write_text("\n".join(f"{key} = {value!r}" for key, value in settings.items())) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +def _fake_site(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 30 | + monkeypatch.setenv("OMD_ROOT", str(tmp_path)) |
| 31 | + monkeypatch.setenv("OMD_SITE", "mysite") |
| 32 | + monkeypatch.setattr("sys.argv", ["cmk-upload-crashes"]) |
| 33 | + omd_site.cache_clear() |
| 34 | + |
| 35 | + |
| 36 | +def _global_mk_path(tmp_path: Path) -> Path: |
| 37 | + return tmp_path / "etc/check_mk/multisite.d/wato/global.mk" |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "settings", |
| 42 | + [ |
| 43 | + pytest.param(None, id="no-settings-file"), |
| 44 | + pytest.param({"automatic_crash_report_upload": True}, id="enabled-but-no-email"), |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_gate_blocks_upload_is_silent_noop( |
| 48 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, settings: dict[str, object] | None |
| 49 | +) -> None: |
| 50 | + if settings is not None: |
| 51 | + _write_global_mk(_global_mk_path(tmp_path), **settings) |
| 52 | + called = False |
| 53 | + |
| 54 | + def _fake_run_batch(**_kwargs: object) -> None: |
| 55 | + nonlocal called |
| 56 | + called = True |
| 57 | + |
| 58 | + monkeypatch.setattr(cli, "run_batch", _fake_run_batch) |
| 59 | + assert cli.main() == 0 |
| 60 | + assert not called |
| 61 | + |
| 62 | + |
| 63 | +def test_toggle_on_with_email_calls_run_batch( |
| 64 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 65 | +) -> None: |
| 66 | + _write_global_mk( |
| 67 | + _global_mk_path(tmp_path), |
| 68 | + automatic_crash_report_upload=True, |
| 69 | + crash_report_contact_email="admin@example.com", |
| 70 | + ) |
| 71 | + captured: dict[str, object] = {} |
| 72 | + |
| 73 | + def _fake_run_batch(**kwargs: object) -> None: |
| 74 | + captured.update(kwargs) |
| 75 | + |
| 76 | + monkeypatch.setattr(cli, "run_batch", _fake_run_batch) |
| 77 | + assert cli.main() == 0 |
| 78 | + assert captured["mail"] == "admin@example.com" |
| 79 | + assert captured["name"] == "community mysite" |
| 80 | + assert captured["crash_report_url"] == _CRASH_URL |
| 81 | + assert captured["dry_run"] is False |
| 82 | + |
| 83 | + |
| 84 | +def test_crash_report_url_override_is_honored( |
| 85 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 86 | +) -> None: |
| 87 | + _write_global_mk( |
| 88 | + _global_mk_path(tmp_path), |
| 89 | + automatic_crash_report_upload=True, |
| 90 | + crash_report_contact_email="admin@example.com", |
| 91 | + crash_report_url="https://crash.example.com", |
| 92 | + ) |
| 93 | + captured: dict[str, object] = {} |
| 94 | + monkeypatch.setattr(cli, "run_batch", lambda **kwargs: captured.update(kwargs)) |
| 95 | + cli.main() |
| 96 | + assert captured["crash_report_url"] == "https://crash.example.com" |
| 97 | + |
| 98 | + |
| 99 | +def test_dry_run_flag_passed_through(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 100 | + _write_global_mk( |
| 101 | + _global_mk_path(tmp_path), |
| 102 | + automatic_crash_report_upload=True, |
| 103 | + crash_report_contact_email="admin@example.com", |
| 104 | + ) |
| 105 | + captured: dict[str, object] = {} |
| 106 | + monkeypatch.setattr(cli, "run_batch", lambda **kwargs: captured.update(kwargs)) |
| 107 | + monkeypatch.setattr("sys.argv", ["cmk-upload-crashes", "--dry-run"]) |
| 108 | + cli.main() |
| 109 | + assert captured["dry_run"] is True |
| 110 | + |
| 111 | + |
| 112 | +@responses.activate |
| 113 | +def test_end_to_end_uploads_via_real_run_batch(tmp_path: Path) -> None: |
| 114 | + _write_global_mk( |
| 115 | + _global_mk_path(tmp_path), |
| 116 | + automatic_crash_report_upload=True, |
| 117 | + crash_report_contact_email="admin@example.com", |
| 118 | + ) |
| 119 | + crash_dir = tmp_path / "var/check_mk/crashes/check/11111111-1111-1111-1111-111111111111" |
| 120 | + crash_dir.mkdir(parents=True) |
| 121 | + (crash_dir / "crash.info").write_bytes(b'{"id": "11111111-1111-1111-1111-111111111111"}') |
| 122 | + responses.add(responses.POST, _CRASH_URL, body=b"OK abc123", status=200) |
| 123 | + |
| 124 | + assert cli.main() == 0 |
| 125 | + assert len(responses.calls) == 1 |
| 126 | + assert (crash_dir / ".uploaded").exists() |
0 commit comments