Extract common logic in OpenSSL-specific code
[tinc] / .ci / test / run.sh
1 #!/bin/sh
2
3 set -eu
4
5 bail() {
6   echo >&2 "@"
7   exit 1
8 }
9
10 header() {
11   echo '################################################################################'
12   echo "# $*"
13   echo '################################################################################'
14 }
15
16 run_tests() {
17   flavor="$1"
18   shift
19
20   header "Cleaning up leftovers from previous runs"
21
22   for name in tinc tincd; do
23     sudo pkill -TERM -x "$name" || true
24     sudo pkill -KILL -x "$name" || true
25   done
26
27   sudo git clean -dfx
28   sudo chown -R "${USER:-$(whoami)}" .
29
30   mkdir -p sanitizer /tmp/logs
31
32   header "Running test flavor $flavor"
33
34   autoreconf -fsi
35
36   DISTCHECK_CONFIGURE_FLAGS=$(sh .ci/conf.sh "$@")
37   export DISTCHECK_CONFIGURE_FLAGS
38
39   # shellcheck disable=SC2086
40   ./configure $DISTCHECK_CONFIGURE_FLAGS
41
42   make -j"$(nproc)" all extra
43
44   if [ "$(uname -s)" = Linux ]; then
45     cmd=distcheck
46   else
47     cmd=check
48   fi
49
50   code=0
51   make $cmd -j2 VERBOSE=1 || code=$?
52
53   sudo tar -c -z -f "/tmp/logs/tests.$flavor.tar.gz" test/ sanitizer/
54
55   return $code
56 }
57
58 case "$(uname -s)" in
59 Linux)
60   if [ -n "${HOST:-}" ]; then
61     # Needed for cross-compilation for 32-bit targets.
62     export CPPFLAGS="${CPPFLAGS:-} -D_FILE_OFFSET_BITS=64"
63   fi
64   ;;
65
66 MINGW*)
67   # No-op.
68   sudo() { "$@"; }
69   ;;
70
71 Darwin)
72   nproc() { sysctl -n hw.ncpu; }
73   gcrypt=$(brew --prefix libgcrypt)
74   openssl=$(brew --prefix openssl)
75   export CPPFLAGS="${CPPFLAGS:-} -I/usr/local/include -I$gcrypt/include -I$openssl/include -I$gcrypt/include"
76   ;;
77 esac
78
79 case "$1" in
80 default)
81   run_tests default
82   ;;
83 nolegacy)
84   run_tests nolegacy --disable-legacy-protocol
85   ;;
86 gcrypt)
87   run_tests gcrypt --with-libgcrypt
88   ;;
89 openssl3)
90   if [ -d /opt/ssl3 ]; then
91     run_tests openssl3 --with-openssl=/opt/ssl3 --with-openssl-include=/opt/ssl3/include --with-openssl-lib=/opt/ssl3/lib64
92   else
93     echo >&2 "OpenSSL 3 not installed, skipping test"
94   fi
95   ;;
96 *)
97   bail "unknown test flavor $1"
98   ;;
99 esac