Files
homelab-gitops/tests/run-alert-recovery.py
T

39 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Run alert recovery regression tests with the deployed Prometheus version.
Requires PyYAML, kubectl and Podman (or a locally installed promtool).
"""
import pathlib
import shutil
import subprocess
import tempfile
import yaml
root = pathlib.Path(__file__).resolve().parents[1]
with tempfile.TemporaryDirectory(prefix="homelab-alert-tests-") as directory:
target = pathlib.Path(directory)
for name, source in (
("backup", "backup-alerts.yaml"),
("cpu", "cpu-capacity-alerts.yaml"),
):
resource = yaml.safe_load(
(root / "apps/prometheus/resources" / source).read_text()
)
(target / f"{name}.rules.yaml").write_text(yaml.safe_dump(resource["spec"]))
shutil.copyfile(root / "tests/alert-recovery.test.yaml", target / "tests.yaml")
if shutil.which("promtool"):
command = ["promtool", "test", "rules", "tests.yaml"]
else:
image = subprocess.check_output(
["kubectl", "-n", "prometheus", "get", "pod",
"prometheus-prometheus-kube-prometheus-prometheus-0",
"-o", "jsonpath={.spec.containers[?(@.name=='prometheus')].image}"],
text=True,
)
command = ["podman", "run", "--rm", "--network=none", "--user=0",
"-v", f"{target}:/tests:ro", "-w", "/tests",
"--entrypoint=/bin/promtool", image,
"test", "rules", "tests.yaml"]
subprocess.run(command, cwd=target, check=True)