From 7204690d332f931aebe3a65e3ba928fd3d077321 Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Wed, 3 Jul 2019 13:27:07 +0200
Subject: [PATCH] Test if prometheus can reach all targets

Closes #217
---
 test/pytest.ini                |  1 +
 test/pytest/test_prometheus.py | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100755 test/pytest/test_prometheus.py

diff --git a/test/pytest.ini b/test/pytest.ini
index 0bf407eab..281b543c4 100644
--- a/test/pytest.ini
+++ b/test/pytest.ini
@@ -3,3 +3,4 @@
 markers =
     certs: Run tests related to TLS certificates
     testinfra: Run testinfra tests (test OS/package versions etc)
+    prometheus: Test prometheus
diff --git a/test/pytest/test_prometheus.py b/test/pytest/test_prometheus.py
new file mode 100755
index 000000000..416a97a52
--- /dev/null
+++ b/test/pytest/test_prometheus.py
@@ -0,0 +1,31 @@
+import json
+import pytest
+import requests
+
+@pytest.mark.prometheus
+def test_prometheus_alerts(host):
+
+    print("Starting prometheus test...")
+    #cmd = host.run("hostname")
+    #print("Hostname: " + cmd.stdout)
+
+    url = 'http://127.0.0.1:30090/api/v1/alerts'
+    alert_json = json.loads(host.check_output('curl ' + url))
+    print(type(alert_json))
+    status = alert_json["status"]
+    alerts = alert_json["data"]["alerts"]
+
+    # Filter out the ever firing "Dead mans switch"
+    real_alerts = [alert for alert in alerts
+                   if alert["labels"]["severity"] != "none"]
+
+    count = len(real_alerts)
+
+    print(json.dumps(real_alerts))
+
+    assert status == "success", "Failure queriying the prometheus api at" + url
+    assert count == 0, "There are prometheus alerts firing !"
+
+
+if __name__ == "__main__":
+    test_prometheus_alerts('')
-- 
GitLab