Public Access
The v0.49.x tag line diverged from origin/master, leaving many features only accessible via tags but not on the master branch. New modules: - ci/cancel_superseded_runs.py — cancel superseded CI runs - ci/check_workflow_artifact_deps.py — validate artifact deps - ci/check_workflow_tofu_init.py — validate tofu init steps - tools/check_alert_rules.py — validate Prometheus alert rules - tools/check_ansible_set_fact_to_json.py — lint set_fact usage - tools/check_docker_init.py — validate Docker init scripts - utils/jinja.py — Jinja2 template utilities - utils/ui.py — UI/console utilities Modified modules: - distribute_molecule.py: add --include-roles/--exclude-roles - utils/api.py: add container.credentials for private registry auth - install_tools.py: retry ansible-galaxy on transient timeouts - setup_image.py: skip dep resolution with --no-deps - cli.py: register new commands - i18n.py: add new translation keys Also removes accidentally committed .vale/styles/Google/ files. Test results: 2195 passed, 100% coverage. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
104 lines
4.3 KiB
Python
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
|