git » chasquid » commit d78056a

test: Skip integration tests if $HOSTALIASES is not functional

author Alberto Bertogli
2021-07-14 23:20:21 UTC
committer Alberto Bertogli
2021-07-14 23:20:21 UTC
parent 4eaf5b38c89169584cb3910dca35cfab427194b5

test: Skip integration tests if $HOSTALIASES is not functional

Most integration tests depend on the $HOSTALIASES environment variable
being functional. That variable works on most systems, but not all. In
particular, systems with `systemd-resolved` can cause the variable to be
ignored.

This was reported by Alex Ellwein in
https://github.com/albertito/chasquid/issues/20.

This patch makes the affected tests to be skipped if $HOSTALIASES is not
working properly. It also removes unnecessary hosts files from tests
which don't need it, and documents this behaviour.

Thanks to Alex Ellwein and foxcpp@ for reporting and helping investigate
this issue!

test/README.md +7 -0
test/stress-01-load/hosts +0 -1
test/stress-02-connections/hosts +0 -1
test/t-01-simple_local/run.sh +1 -0
test/t-02-exim/run.sh +1 -0
test/t-03-queue_persistency/hosts +0 -1
test/t-04-aliases/run.sh +1 -0
test/t-05-null_address/run.sh +1 -0
test/t-06-idna/run.sh +1 -0
test/t-07-smtputf8/hosts +0 -1
test/t-09-loop/run.sh +1 -0
test/t-10-hooks/run.sh +1 -0
test/t-11-dovecot/run.sh +1 -0
test/t-12-minor_dialogs/hosts +0 -1
test/t-13-reload/run.sh +1 -0
test/t-14-tls_tracking/run.sh +1 -0
test/t-15-driusan_dkim/run.sh +1 -0
test/t-16-spf/run.sh +1 -0
test/t-17-maillog/run.sh +1 -0
test/t-18-haproxy/run.sh +1 -0
test/util/check-hostaliases +62 -0
test/util/lib.sh +6 -0

diff --git a/test/README.md b/test/README.md
index 74d9e7b..2bd2750 100644
--- a/test/README.md
+++ b/test/README.md
@@ -46,6 +46,12 @@ if the dependencies are not found:
 For some tests, python >= 3.5 is required; they will be skipped if it's not
 available.
 
+Most tests depend on the
+[`$HOSTALIASES`](https://man7.org/linux/man-pages/man7/hostname.7.html)
+environment variable being functional, and will be skipped if it isn't. This
+works by default in most Linux systems, but note that the use of
+`systemd-resolved` can prevent it from working properly.
+
 
 ## Stress tests
 
@@ -55,6 +61,7 @@ load against chasquid to measure performance and resource consumption.
 While they are not exhaustive, they are useful to catch regressions and track
 improvements on the main code paths.
 
+
 ## Fuzz tests
 
 Some Go packages also have instrumentation to run fuzz testing against them,
diff --git a/test/stress-01-load/hosts b/test/stress-01-load/hosts
deleted file mode 100644
index 2b9b623..0000000
--- a/test/stress-01-load/hosts
+++ /dev/null
@@ -1 +0,0 @@
-testserver localhost
diff --git a/test/stress-02-connections/hosts b/test/stress-02-connections/hosts
deleted file mode 100644
index 2b9b623..0000000
--- a/test/stress-02-connections/hosts
+++ /dev/null
@@ -1 +0,0 @@
-testserver localhost
diff --git a/test/t-01-simple_local/run.sh b/test/t-01-simple_local/run.sh
index aa47e64..905cd3a 100755
--- a/test/t-01-simple_local/run.sh
+++ b/test/t-01-simple_local/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 mkdir -p .logs
 
diff --git a/test/t-02-exim/run.sh b/test/t-02-exim/run.sh
index f0b2b87..78b3006 100755
--- a/test/t-02-exim/run.sh
+++ b/test/t-02-exim/run.sh
@@ -26,6 +26,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 if ! .exim4/exim4 --version > /dev/null; then
 	skip "exim4 binary at .exim4/exim4 is not functional"
diff --git a/test/t-03-queue_persistency/hosts b/test/t-03-queue_persistency/hosts
deleted file mode 100644
index 2b9b623..0000000
--- a/test/t-03-queue_persistency/hosts
+++ /dev/null
@@ -1 +0,0 @@
-testserver localhost
diff --git a/test/t-04-aliases/run.sh b/test/t-04-aliases/run.sh
index 52c5aba..2656dc3 100755
--- a/test/t-04-aliases/run.sh
+++ b/test/t-04-aliases/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 generate_certs_for testserver
 add_user user@testserver secretpassword
diff --git a/test/t-05-null_address/run.sh b/test/t-05-null_address/run.sh
index 26bae14..92a3abc 100755
--- a/test/t-05-null_address/run.sh
+++ b/test/t-05-null_address/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 generate_certs_for testserver
 add_user user@testserver secretpassword
diff --git a/test/t-06-idna/run.sh b/test/t-06-idna/run.sh
index 57b08a2..427fef6 100755
--- a/test/t-06-idna/run.sh
+++ b/test/t-06-idna/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 rm -rf .data-A .data-B .mail
 
diff --git a/test/t-07-smtputf8/hosts b/test/t-07-smtputf8/hosts
deleted file mode 100644
index d276cfd..0000000
--- a/test/t-07-smtputf8/hosts
+++ /dev/null
@@ -1 +0,0 @@
-ñoños localhost
diff --git a/test/t-09-loop/run.sh b/test/t-09-loop/run.sh
index ef051b5..9411e26 100755
--- a/test/t-09-loop/run.sh
+++ b/test/t-09-loop/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 rm -rf .data-A .data-B .mail
 
diff --git a/test/t-10-hooks/run.sh b/test/t-10-hooks/run.sh
index 490b225..918fed4 100755
--- a/test/t-10-hooks/run.sh
+++ b/test/t-10-hooks/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 generate_certs_for testserver
 add_user user@testserver secretpassword
diff --git a/test/t-11-dovecot/run.sh b/test/t-11-dovecot/run.sh
index 0be1339..c20f279 100755
--- a/test/t-11-dovecot/run.sh
+++ b/test/t-11-dovecot/run.sh
@@ -10,6 +10,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 if ! dovecot --version > /dev/null; then
 	skip "dovecot not installed"
diff --git a/test/t-12-minor_dialogs/hosts b/test/t-12-minor_dialogs/hosts
deleted file mode 100644
index 2b9b623..0000000
--- a/test/t-12-minor_dialogs/hosts
+++ /dev/null
@@ -1 +0,0 @@
-testserver localhost
diff --git a/test/t-13-reload/run.sh b/test/t-13-reload/run.sh
index b952497..326a8cf 100755
--- a/test/t-13-reload/run.sh
+++ b/test/t-13-reload/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 generate_certs_for testserver
 
diff --git a/test/t-14-tls_tracking/run.sh b/test/t-14-tls_tracking/run.sh
index ce181e0..de801d4 100755
--- a/test/t-14-tls_tracking/run.sh
+++ b/test/t-14-tls_tracking/run.sh
@@ -6,6 +6,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 # Build with the DNS override, so we can fake DNS records.
 export GOTAGS="dnsoverride"
diff --git a/test/t-15-driusan_dkim/run.sh b/test/t-15-driusan_dkim/run.sh
index c0825f1..85ac5d1 100755
--- a/test/t-15-driusan_dkim/run.sh
+++ b/test/t-15-driusan_dkim/run.sh
@@ -7,6 +7,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 for binary in dkimsign dkimverify dkimkeygen; do
 	if ! which $binary > /dev/null; then
diff --git a/test/t-16-spf/run.sh b/test/t-16-spf/run.sh
index 9ecbf49..db71b5d 100755
--- a/test/t-16-spf/run.sh
+++ b/test/t-16-spf/run.sh
@@ -8,6 +8,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 # Build with the DNS override, so we can fake DNS records.
 export GOTAGS="dnsoverride"
diff --git a/test/t-17-maillog/run.sh b/test/t-17-maillog/run.sh
index 37d0410..e3eb360 100755
--- a/test/t-17-maillog/run.sh
+++ b/test/t-17-maillog/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 mkdir -p .logs
 
diff --git a/test/t-18-haproxy/run.sh b/test/t-18-haproxy/run.sh
index e2f0e9d..98d0185 100755
--- a/test/t-18-haproxy/run.sh
+++ b/test/t-18-haproxy/run.sh
@@ -4,6 +4,7 @@ set -e
 . $(dirname ${0})/../util/lib.sh
 
 init
+check_hostaliases
 
 mkdir -p .logs
 
diff --git a/test/util/check-hostaliases b/test/util/check-hostaliases
new file mode 100755
index 0000000..ac1023b
--- /dev/null
+++ b/test/util/check-hostaliases
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+
+import argparse
+import tempfile
+import os
+import socket
+import subprocess
+import sys
+
+parser = argparse.ArgumentParser(
+    description="Check that $HOSTALIASES is working"
+)
+parser.add_argument(
+    "--child",
+    action="store_true",
+    help="run in child mode, for internal use only",
+)
+parser.add_argument(
+    "-v",
+    action="store_true",
+    help="verbose mode",
+)
+args = parser.parse_args()
+
+
+def dprint(*a):
+    if args.v:
+        print(*a)
+
+
+if args.child:
+    dprint("child mode, getting hosts")
+    lo = socket.gethostbyname("localhost")
+    ts = socket.gethostbyname("testserver")
+    dprint(ts, lo, ts == lo)
+    if ts != lo:
+        sys.exit(1)
+else:
+    dprint("## parent mode")
+    # Create the hostaliases file.
+    fd = tempfile.NamedTemporaryFile(mode="w+")
+    fd.write("testserver localhost\n")
+    fd.flush()
+
+    # Re-execute ourselves with --child, to do the check.
+    env = dict(os.environ)
+    env["HOSTALIASES"] = fd.name
+    cargs = [sys.argv[0], "--child"]
+    if args.v:
+        cargs.append("-v")
+    cmd = subprocess.run(
+        cargs,
+        env=env,
+        text=True,
+        encoding="ascii",
+        stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT,
+    )
+    dprint("## child output:")
+    dprint(cmd.stdout)
+    dprint("## child returned", cmd.returncode)
+    sys.exit(cmd.returncode)
diff --git a/test/util/lib.sh b/test/util/lib.sh
index b9d343c..25a8592 100644
--- a/test/util/lib.sh
+++ b/test/util/lib.sh
@@ -146,6 +146,12 @@ function fail() {
 	exit 1
 }
 
+function check_hostaliases() {
+	if ! "${UTILDIR}/check-hostaliases"; then
+		skip '$HOSTALIASES not working (probably systemd-resolved)'
+	fi
+}
+
 # Wait until there's something listening on the given port.
 function wait_until_ready() {
 	PORT=$1