From cc4ab8dd482d116df39ccaf404ad821ceed5961e Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Fri, 30 Jul 2021 01:00:00 +0200
Subject: [PATCH] Fix prometheus test

Fixes: #813
---
 .gitlab-ci.yml                 |  1 +
 openappstack/cluster.py        |  2 +-
 test/pytest/test_prometheus.py | 14 +++++++++++++-
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 2ba2d9905..8c2b44e86 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -827,6 +827,7 @@ kube-prometheus-stack-alerts:
   allow_failure: true
   script:
     - *debug_information
+    - export BASIC_AUTH_PW=$(python3 -m openappstack varac-test secrets | grep oas-prometheus-basic-auth | cut -d'=' -f2)
     - cd test/
     - pytest -s -m 'prometheus' --connection=ansible --ansible-inventory=${CLUSTER_DIR}/inventory.yml --hosts='ansible://*'
   extends:
diff --git a/openappstack/cluster.py b/openappstack/cluster.py
index 3902ffeba..f0eb71572 100644
--- a/openappstack/cluster.py
+++ b/openappstack/cluster.py
@@ -75,7 +75,7 @@ class Cluster:
         if not self.data_loaded:
             with open(self.inventory_file, 'r') as stream:
                 inventory = yaml.safe_load(stream)
-                print(inventory)
+                # print(inventory)
                 # Work with the master node from the inventory
                 self.hostname = inventory['all']['children']['master']['hosts']
                 self.domain = \
diff --git a/test/pytest/test_prometheus.py b/test/pytest/test_prometheus.py
index 14e64a61c..d46f6bea0 100755
--- a/test/pytest/test_prometheus.py
+++ b/test/pytest/test_prometheus.py
@@ -1,5 +1,6 @@
 import re
 import json
+import os
 import pytest
 
 
@@ -64,7 +65,18 @@ def test_prometheus_alerts(host):
 
     print("Starting prometheus test...")
 
-    url = 'http://kube-prometheus-stack-kube-prom-prometheus.oas.svc.cluster.local:9090/api/v1/alerts'
+    basic_auth_pw = os.environ.get("BASIC_AUTH_PW")
+    assert basic_auth_pw, "Please export the BASIC_AUTH_PW env var!"
+
+    domain = os.environ.get("FQDN")
+    if domain:
+        print(f"Using domain {domain} from FQDN environment variable.")
+    else:
+        ansible_vars = host.ansible.get_variables()
+        domain = ansible_vars["domain"]
+        print(f"Using domain {domain} from ansible inventory.")
+
+    url = f'https://admin:{basic_auth_pw}@prometheus.{domain}/api/v1/alerts'
 
     alert_json = json.loads(host.check_output('curl ' + url))
     status = alert_json["status"]
-- 
GitLab