File size: 3,177 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
#!/usr/bin/env python3
"""af-check: syntax checks + example tests for this repo (the same tools CI runs).

  - promtool check rules on every rules/*.yml|yaml (PrometheusRule manifests are unwrapped first)
  - promtool test rules on tests/visible/*.test.yml against the extracted rules
  - amtool check-config alertmanager/alertmanager.yml
Exit code 0 when everything passes.
"""
import glob
import os
import shutil
import subprocess
import sys
import tempfile

import yaml

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def extract(path):
    with open(path) as fh:
        doc = yaml.safe_load(fh)
    if isinstance(doc, dict) and doc.get("kind") == "PrometheusRule":
        return {"groups": (doc.get("spec") or {}).get("groups") or []}
    return doc


def main():
    tmp = tempfile.mkdtemp(prefix="af-check-")
    ok = True
    rule_paths = []
    try:
        files = sorted(glob.glob(os.path.join(ROOT, "rules", "*.yml")) + glob.glob(os.path.join(ROOT, "rules", "*.yaml")))
        for f in files:
            rel = os.path.relpath(f, ROOT)
            try:
                doc = extract(f)
            except yaml.YAMLError as e:
                print(f"FAIL {rel}: YAML error: {e}")
                ok = False
                continue
            out = os.path.join(tmp, os.path.basename(f).rsplit(".", 1)[0] + ".yml")
            with open(out, "w") as fh:
                yaml.safe_dump(doc, fh, sort_keys=False)
            r = subprocess.run(["promtool", "check", "rules", out], capture_output=True, text=True)
            if r.returncode == 0:
                print(f"ok   {rel}")
                rule_paths.append(out)
            else:
                ok = False
                print(f"FAIL {rel}:\n{(r.stdout + r.stderr).replace(out, rel)}")
        for t in sorted(glob.glob(os.path.join(ROOT, "tests", "visible", "*.test.yml"))):
            with open(t) as fh:
                doc = yaml.safe_load(fh)
            doc["rule_files"] = rule_paths or [os.path.join(tmp, "none.yml")]
            if not rule_paths:
                with open(doc["rule_files"][0], "w") as fh:
                    fh.write("groups: []\n")
            tp = os.path.join(tmp, os.path.basename(t))
            with open(tp, "w") as fh:
                yaml.safe_dump(doc, fh, sort_keys=False, width=10**9)
            r = subprocess.run(["promtool", "test", "rules", tp], capture_output=True, text=True)
            print(f"{'ok  ' if r.returncode == 0 else 'FAIL'} tests/visible/{os.path.basename(t)}")
            if r.returncode != 0:
                ok = False
                print((r.stdout + r.stderr)[-4000:])
        am = os.path.join(ROOT, "alertmanager", "alertmanager.yml")
        r = subprocess.run(["amtool", "check-config", am], capture_output=True, text=True)
        print(f"{'ok  ' if r.returncode == 0 else 'FAIL'} alertmanager/alertmanager.yml")
        if r.returncode != 0:
            ok = False
            print(r.stdout + r.stderr)
    finally:
        shutil.rmtree(tmp, ignore_errors=True)
    print("ALL CHECKS PASSED" if ok else "SOME CHECKS FAILED")
    return 0 if ok else 1


if __name__ == "__main__":
    sys.exit(main())