git » chasquid » commit af7b1d8

test: Add a Prometheus integration test

author Alberto Bertogli
2026-02-02 19:20:08 UTC
committer Alberto Bertogli
2026-02-02 19:33:57 UTC
parent 1a4c672ac32330718797183ec4ef9f1bb2a75fa2

test: Add a Prometheus integration test

This patch adds a Prometheus integration test, that runs a Prometheus
instance which scrapes chasquid, and then does a basic query to ensure
the scraping is working correctly.

test/Dockerfile +2 -1
test/README.md +2 -0
test/t-23-prometheus/config/chasquid.conf +10 -0
test/t-23-prometheus/content +4 -0
test/t-23-prometheus/hosts +1 -0
test/t-23-prometheus/prometheus.yml +12 -0
test/t-23-prometheus/run.sh +53 -0
test/t-23-prometheus/smtpc.conf +4 -0

diff --git a/test/Dockerfile b/test/Dockerfile
index d0803f2..208f049 100644
--- a/test/Dockerfile
+++ b/test/Dockerfile
@@ -27,7 +27,8 @@ RUN apt-get install -y -q \
 	gettext-base dovecot-imapd \
 	exim4-daemon-light \
 	haproxy \
-	python3-dkim
+	python3-dkim \
+	prometheus promtool
 
 # Install sudo, needed for the docker entrypoint.
 RUN apt-get install -y -q sudo
diff --git a/test/README.md b/test/README.md
index 5635655..1dc9924 100644
--- a/test/README.md
+++ b/test/README.md
@@ -44,6 +44,8 @@ if the dependencies are not found:
     - `haproxy`
 - `t-19-dkimpy`: [dkimpy](https://pypi.org/project/dkimpy/) integration tests:
     - `python3-dkim`
+- `t-23-prometheus`: [Prometheus](https://prometheus.io/) integration tests:
+    - `prometheus promtool`
 
 For some tests, python >= 3.5 is required; they will be skipped if it's not
 available.
diff --git a/test/t-23-prometheus/config/chasquid.conf b/test/t-23-prometheus/config/chasquid.conf
new file mode 100644
index 0000000..cf76e8a
--- /dev/null
+++ b/test/t-23-prometheus/config/chasquid.conf
@@ -0,0 +1,10 @@
+smtp_address: ":1025"
+submission_address: ":1587"
+submission_over_tls_address: ":1465"
+monitoring_address: ":1099"
+
+mail_delivery_agent_bin: "test-mda"
+mail_delivery_agent_args: "%to%"
+
+data_dir: "../.data"
+mail_log_path: "../.logs/mail_log"
diff --git a/test/t-23-prometheus/content b/test/t-23-prometheus/content
new file mode 100644
index 0000000..76a8b16
--- /dev/null
+++ b/test/t-23-prometheus/content
@@ -0,0 +1,4 @@
+Subject: Prueba desde el test
+
+Crece desde el test el futuro
+Crece desde el test
diff --git a/test/t-23-prometheus/hosts b/test/t-23-prometheus/hosts
new file mode 100644
index 0000000..2b9b623
--- /dev/null
+++ b/test/t-23-prometheus/hosts
@@ -0,0 +1 @@
+testserver localhost
diff --git a/test/t-23-prometheus/prometheus.yml b/test/t-23-prometheus/prometheus.yml
new file mode 100644
index 0000000..d13d352
--- /dev/null
+++ b/test/t-23-prometheus/prometheus.yml
@@ -0,0 +1,12 @@
+# Prometheus configuration file for testing chasquid metrics.
+
+global:
+  # Reduce intervals to speed up the tests.
+  scrape_interval: 100ms
+  scrape_timeout: 100ms
+  evaluation_interval: 100ms
+
+scrape_configs:
+  - job_name: 'chasquid'
+    static_configs:
+      - targets: ['localhost:1099']
diff --git a/test/t-23-prometheus/run.sh b/test/t-23-prometheus/run.sh
new file mode 100755
index 0000000..bbd20e4
--- /dev/null
+++ b/test/t-23-prometheus/run.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+set -e
+. "$(dirname "$0")/../util/lib.sh"
+
+init
+check_hostaliases
+
+# This is an integration test with Prometheus, so skip if it is not present.
+if ! prometheus --version > /dev/null; then
+	skip "prometheus not installed"
+fi
+if ! promtool help > /dev/null; then
+	skip "promtool not installed"
+fi
+
+# Start chasquid and prometheus.
+rm -rf .logs/ .prometheus/
+mkdir -p .logs .prometheus
+
+generate_certs_for testserver
+chasquid-util-user-add user@testserver secretpassword
+chasquid -v=2 --logfile=.logs/chasquid.log --config_dir=config &
+
+prometheus \
+	--config.file=prometheus.yml \
+	--storage.tsdb.path=.prometheus/ \
+	> .prometheus/log 2>&1 &
+
+
+# Wait until they're both up and running.
+wait_until_ready 1025
+wait_until "promtool check ready 2>/dev/null"
+
+# Send an email.
+smtpc user@testserver < content
+wait_for_file .mail/user@testserver
+mail_diff content .mail/user@testserver
+
+
+# Query Prometheus and validate that it has scraped chasquid correctly.
+function expect_value() {
+	promtool query instant http://localhost:9090 "$1" > ".logs/value-$1"
+	grep -q "=> $2" ".logs/value-$1"
+}
+
+# Note that it takes Prometheus ~5s to start scraping targets on the first
+# run, for reasons currently unknown.
+# If we don't clear up .prometheus/, then subsequent runs are faster, but
+# starting with an empty data directory makes the test more hermetic.
+wait_until expect_value chasquid_queue_putCount 1
+
+success
diff --git a/test/t-23-prometheus/smtpc.conf b/test/t-23-prometheus/smtpc.conf
new file mode 100644
index 0000000..c912b55
--- /dev/null
+++ b/test/t-23-prometheus/smtpc.conf
@@ -0,0 +1,4 @@
+addr localhost:1465
+server_cert config/certs/testserver/fullchain.pem
+user user@testserver
+password secretpassword