From 81e09f871807fce85a0977082ef5e281675771df Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Thu, 17 Aug 2023 17:59:00 +0100
Subject: [PATCH] Add an integration test with a simple HTTP probe

---
 .gitlab-ci.yml             | 10 ++++++++++
 cmd/service-prober/main.go | 13 ++++++++++---
 test/http/config.json      | 20 ++++++++++++++++++++
 test/test.sh               | 19 +++++++++++++++++++
 4 files changed, 59 insertions(+), 3 deletions(-)
 create mode 100644 test/http/config.json
 create mode 100755 test/test.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ac1eb2f..05bbe2f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,3 +1,13 @@
 include:
   - "https://git.autistici.org/pipelines/containers/raw/master/common.yml"
   - "https://git.autistici.org/ai3/docker/test/golang/raw/master/ci.yml"
+
+ctest-http:
+  image: registry.git.autistici.org/ai3/docker/test/float-like-podman:master
+  stage: container-test
+  tags: [podman]
+  variables:
+    URL: https://www.incal.net/
+  script:
+    - sed -e "s,@URL@,$URL,g" < test/http/config.json > config.json
+    - with-container -v $PWD/config.json:/etc/service-prober.json --env SERVICE_PROBER_CONFIG=/etc/service-prober.json $IMAGE_TAG ./test/test.sh
diff --git a/cmd/service-prober/main.go b/cmd/service-prober/main.go
index c8243a8..86eda1c 100644
--- a/cmd/service-prober/main.go
+++ b/cmd/service-prober/main.go
@@ -21,12 +21,19 @@ import (
 	_ "git.autistici.org/ai3/tools/service-prober/probes/openvpn"
 )
 
+func getenvDef(key, def string) string {
+	if s := os.Getenv(key); s != "" {
+		return s
+	}
+	return def
+}
+
 var (
-	config      = flag.String("config", "", "configuration file `path`")
+	config      = flag.String("config", os.Getenv("SERVICE_PROBER_CONFIG"), "configuration file `path`")
 	oneshot     = flag.Bool("oneshot", false, "run just one shot and exit")
 	validate    = flag.Bool("validate", false, "just validate the configuration syntax")
-	only        = flag.String("only", "", "only run probes with names matching this `regex`")
-	httpAddr    = flag.String("http-addr", ":5522", "`address` for the HTTP server")
+	only        = flag.String("only", os.Getenv("SERVICE_PROBER_ONLY"), "only run probes with names matching this `regex`")
+	httpAddr    = flag.String("http-addr", getenvDef("SERVICE_PROBER_HTTP_ADDR", ":5522"), "`address` for the HTTP server")
 	keepResults = flag.Int("keep-results", 100, "keep `N` results of past probes in memory")
 )
 
diff --git a/test/http/config.json b/test/http/config.json
new file mode 100644
index 0000000..4a13b71
--- /dev/null
+++ b/test/http/config.json
@@ -0,0 +1,20 @@
+{
+    "vars": {},
+    "probes": [
+        {
+            "type": "http",
+            "name": "test",
+            "interval": "1m",
+            "timeout": "1m",
+            "params": {
+                "script": [
+                    {
+                        "type": "open",
+                        "url": "@URL@",
+                        "expected_url": "@URL@"
+                    }
+                ]
+            }
+        }
+    ]
+}
diff --git a/test/test.sh b/test/test.sh
new file mode 100755
index 0000000..43966a8
--- /dev/null
+++ b/test/test.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+count=0
+while [ $count -lt 120 ]; do
+    count=$(expr $count + 1)
+
+    value=$(curl -sf http://localhost:5521/metrics | awk '/^probe_success/ {print $2}')
+    if [ "$value" = "1" ]; then
+        echo "success!" >&2
+        exit 0
+    fi
+
+    echo "probe_success=${value}"
+
+    sleep 1
+done
+
+echo "failed" >&2
+exit 1
-- 
GitLab