Files
devx/tests/unit/test_tools_check_alert_rules.py
T
emilandemo 3d4b4940ff
Post-merge / detect-and-configure (push) Successful in 16s
Post-merge / release-and-maintain (push) Successful in 1m2s
DEVX-153: feat: sync missing features from v0.49.x line to master
Co-authored-by: emil User <emil.simeonov@tutanota.com>
2026-08-09 01:09:20 +00:00

104 lines
4.3 KiB
Python

"""Unit tests for devx.tools.check_alert_rules."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock, patch
from click.testing import CliRunner
from devx.tools.check_alert_rules import main
class TestMain:
def test_skip_when_promtool_not_found(self, tmp_path: Path):
"""Should exit 0 and print skip message when promtool is not on PATH."""
with patch("shutil.which", return_value=None):
runner = CliRunner()
result = runner.invoke(main, ["--template-path", str(tmp_path)])
assert result.exit_code == 0
assert "promtool not found" in result.output
def test_validates_rules_successfully(self, tmp_path: Path):
"""Should exit 0 when promtool reports SUCCESS."""
(tmp_path / "alert-rules.yml.j2").write_text("groups: []")
mock_result = MagicMock()
mock_result.returncode = 0
mock_result.stdout = "Checking /tmp/test.yml\n SUCCESS: 60 rules found\n"
mock_result.stderr = ""
with patch("shutil.which", return_value="/usr/bin/promtool"):
with patch("subprocess.run", return_value=mock_result):
runner = CliRunner()
result = runner.invoke(main, ["--template-path", str(tmp_path)])
assert result.exit_code == 0
def test_fails_on_promtool_error(self, tmp_path: Path):
"""Should exit non-zero when promtool reports an error."""
(tmp_path / "alert-rules.yml.j2").write_text("groups: []")
mock_result = MagicMock()
mock_result.returncode = 1
mock_result.stdout = ""
mock_result.stderr = "Error: invalid template function 'default'\n"
with patch("shutil.which", return_value="/usr/bin/promtool"):
with patch("subprocess.run", return_value=mock_result):
runner = CliRunner()
result = runner.invoke(main, ["--template-path", str(tmp_path)])
assert result.exit_code != 0
def test_uses_correct_template_path(self, tmp_path: Path):
"""Should render the specified template from the given path."""
(tmp_path / "alert-rules.yml.j2").write_text("groups: []")
captured_args = []
def fake_run(args, **kwargs):
captured_args.append(args)
mock = MagicMock()
mock.returncode = 0
mock.stdout = "SUCCESS"
mock.stderr = ""
return mock
with patch("shutil.which", return_value="/usr/bin/promtool"):
with patch("subprocess.run", side_effect=fake_run):
runner = CliRunner()
runner.invoke(main, ["--template-path", str(tmp_path)])
assert captured_args[0][0] == "promtool"
assert captured_args[0][1] == "check"
assert captured_args[0][2] == "rules"
def test_custom_template_name(self, tmp_path: Path):
"""Should render a custom template name."""
(tmp_path / "custom-rules.yml.j2").write_text("groups: []")
mock_result = MagicMock()
mock_result.returncode = 0
mock_result.stdout = "SUCCESS"
mock_result.stderr = ""
with patch("shutil.which", return_value="/usr/bin/promtool"):
with patch("subprocess.run", return_value=mock_result):
runner = CliRunner()
result = runner.invoke(
main, ["--template-path", str(tmp_path), "--template-name", "custom-rules.yml.j2"]
)
assert result.exit_code == 0
def test_template_vars_passed(self, tmp_path: Path):
"""Should pass template variables to the render call."""
(tmp_path / "alert-rules.yml.j2").write_text("grafana: {{ grafana_base_url }}\ngroups: []")
mock_result = MagicMock()
mock_result.returncode = 0
mock_result.stdout = "SUCCESS"
mock_result.stderr = ""
with patch("shutil.which", return_value="/usr/bin/promtool"):
with patch("subprocess.run", return_value=mock_result):
runner = CliRunner()
result = runner.invoke(
main,
[
"--template-path",
str(tmp_path),
"--var",
"grafana_base_url=https://grafana.test.example.com",
],
)
assert result.exit_code == 0