File size: 3,346 Bytes
f3b0a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""H8: the inhibition simulator agrees with a real Alertmanager process on the generated case shapes.

Marked `am`: needs the alertmanager binary (AF_ALERTMANAGER, PATH, or ../../vendor/bin/alertmanager).
"""

import json
import os
import shutil
import socket
import subprocess
import tempfile
import time
import urllib.request

import pytest
import yaml

from alertforge import routing
from alertforge.grader.amcheck import inhibited

from conftest import VENDOR_BIN

AM = os.environ.get("AF_ALERTMANAGER") or shutil.which("alertmanager") or os.path.join(VENDOR_BIN, "alertmanager")
pytestmark = [pytest.mark.am, pytest.mark.skipif(not os.path.exists(AM), reason="alertmanager binary not found")]

RULES = {
    "oracle": routing.inhibit_rule("SvcDown", ["BurnFast", "HighErrorRatio"]),
    "R05": {k: v for k, v in routing.inhibit_rule("SvcDown", ["BurnFast", "HighErrorRatio"]).items() if k != "equal"},
    "legacy": {"source_match": {"alertname": "SvcDown"}, "target_match_re": {"alertname": "Burn.*"}, "equal": ["service"]},
}


def _port() -> int:
    s = socket.socket()
    s.bind(("127.0.0.1", 0))
    p = s.getsockname()[1]
    s.close()
    return p


def _real(rule: dict, source: dict | None, target: dict) -> bool:
    d = tempfile.mkdtemp()
    cfg = {"route": {"receiver": "null"}, "receivers": [{"name": "null"}], "inhibit_rules": [rule]}
    with open(os.path.join(d, "am.yml"), "w") as fh:
        yaml.safe_dump(cfg, fh)
    port, cport = _port(), _port()
    proc = subprocess.Popen([AM, f"--config.file={d}/am.yml", f"--storage.path={d}/data",
                             f"--web.listen-address=127.0.0.1:{port}", f"--cluster.listen-address=",
                             f"--cluster.advertise-address=127.0.0.1:{cport}"],
                            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    base = f"http://127.0.0.1:{port}/api/v2"
    try:
        for _ in range(100):
            try:
                urllib.request.urlopen(f"{base}/status", timeout=1)
                break
            except OSError:
                time.sleep(0.1)
        alerts = [{"labels": a} for a in ([source] if source else []) + [target]]
        req = urllib.request.Request(f"{base}/alerts", json.dumps(alerts).encode(), {"Content-Type": "application/json"})
        urllib.request.urlopen(req, timeout=5)
        want = target["alertname"]
        for _ in range(30):
            time.sleep(0.2)
            got = json.load(urllib.request.urlopen(f"{base}/alerts", timeout=5))
            tgt = [a for a in got if a["labels"].get("alertname") == want and a["labels"] == target]
            if tgt and (tgt[0]["status"]["inhibitedBy"] or len(got) == len(alerts)):
                return bool(tgt[0]["status"]["inhibitedBy"])
        return False
    finally:
        proc.terminate()
        proc.wait(timeout=10)
        shutil.rmtree(d, ignore_errors=True)


CASES = routing.inhibit_cases("SvcDown", ["BurnFast", "HighErrorRatio"], "chat-gw", "orders-api", "payments", "PodMemoryHigh")


@pytest.mark.parametrize("rule_name", sorted(RULES))
@pytest.mark.parametrize("i", range(len(CASES)))
def test_simulator_matches_alertmanager(rule_name, i):
    c = CASES[i]
    rule = RULES[rule_name]
    sim = inhibited({"inhibit_rules": [rule]}, c["source"], c["target"])
    assert sim == _real(rule, c["source"], c["target"])