1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-17 14:37:02 +00:00

Improve bash quoting, and other similar best practices

This patch updates the shell scripts with some of the common best
practices, which should make them more resilient to unusual failures and
unexpected environments (in particular, directories with spaces).

Most of these were identified by shellcheck.
This commit is contained in:
Alberto Bertogli
2022-11-13 00:37:28 +00:00
parent e2481b07a9
commit 948cee1ce1
32 changed files with 133 additions and 126 deletions

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../../test/util/lib.sh . "$(dirname "$0")/../../test/util/lib.sh"
init init

View File

@@ -1,13 +1,14 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../../test/util/lib.sh . "$(dirname "$0")/../../test/util/lib.sh"
init init
# Build the binary once, so we can use it and launch it in chamuyero scripts. # Build the binary once, so we can use it and launch it in chamuyero scripts.
# Otherwise, we not only spend time rebuilding it over and over, but also "go # Otherwise, we not only spend time rebuilding it over and over, but also "go
# run" masks the exit code, which is something we care about. # run" masks the exit code, which is something we care about.
# shellcheck disable=SC2086
if [ "${COVER_DIR}" != "" ]; then if [ "${COVER_DIR}" != "" ]; then
go test -covermode=count -coverpkg=../../... -c \ go test -covermode=count -coverpkg=../../... -c \
$GOFLAGS -tags="coveragebin $GOTAGS" $GOFLAGS -tags="coveragebin $GOTAGS"
@@ -22,9 +23,9 @@ if ! ./dovecot-auth-cli lalala 2>&1 | grep -q "invalid arguments"; then
fi fi
for i in *.cmy; do for i in *.cmy; do
if ! chamuyero $i > $i.log 2>&1 ; then if ! chamuyero "$i" > "$i.log" 2>&1 ; then
echo "# Test $i failed, log follows" echo "# Test $i failed, log follows"
cat $i.log cat "$i.log"
exit 1 exit 1
fi fi
done done

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../../test/util/lib.sh . "$(dirname "$0")/../../test/util/lib.sh"
init init
@@ -11,9 +11,9 @@ init
go build go build
for i in *.cmy; do for i in *.cmy; do
if ! chamuyero $i > $i.log 2>&1 ; then if ! chamuyero "$i" > "$i.log" 2>&1 ; then
echo "# Test $i failed, log follows" echo "# Test $i failed, log follows"
cat $i.log cat "$i.log"
exit 1 exit 1
fi fi
done done

View File

@@ -7,7 +7,7 @@
set -e set -e
read -p "Email (full user@domain format): " EMAIL read -r -p "Email (full user@domain format): " EMAIL
if ! echo "${EMAIL}" | grep -q @; then if ! echo "${EMAIL}" | grep -q @; then
echo "Error: email should have '@'." echo "Error: email should have '@'."
@@ -15,7 +15,7 @@ if ! echo "${EMAIL}" | grep -q @; then
fi fi
read -p "Password: " -s PASSWORD read -r -p "Password: " -s PASSWORD
echo echo
DOMAIN=$(echo echo "${EMAIL}" | cut -d '@' -f 2) DOMAIN=$(echo echo "${EMAIL}" | cut -d '@' -f 2)
@@ -33,7 +33,7 @@ mkdir -p /data/dovecot
touch /data/dovecot/users touch /data/dovecot/users
if grep -q "^${EMAIL}:" /data/dovecot/users; then if grep -q "^${EMAIL}:" /data/dovecot/users; then
cp /data/dovecot/users /data/dovecot/users.old cp /data/dovecot/users /data/dovecot/users.old
cat /data/dovecot/users.old | grep -v "^${EMAIL}:" \ grep -v "^${EMAIL}:" /data/dovecot/users.old \
> /data/dovecot/users > /data/dovecot/users
fi fi

View File

@@ -28,15 +28,16 @@ if [ "$AUTO_CERTS" != "" ]; then
MAIL_OPTS="-m $CERTS_MAIL" MAIL_OPTS="-m $CERTS_MAIL"
fi fi
for DOMAIN in $(echo $AUTO_CERTS); do for DOMAIN in $AUTO_CERTS; do
# If it has never been set up, then do so. # If it has never been set up, then do so.
if ! [ -e /etc/letsencrypt/live/$DOMAIN/fullchain.pem ]; then if ! [ -e "/etc/letsencrypt/live/$DOMAIN/fullchain.pem" ]; then
# shellcheck disable=SC2086
certbot certonly \ certbot certonly \
--non-interactive \ --non-interactive \
--standalone \ --standalone \
--agree-tos \ --agree-tos \
$MAIL_OPTS \ $MAIL_OPTS \
-d $DOMAIN -d "$DOMAIN"
else else
echo "$DOMAIN certificate already set up." echo "$DOMAIN certificate already set up."
fi fi
@@ -53,9 +54,10 @@ if [ "$AUTO_CERTS" != "" ]; then
fi fi
CERT_DOMAINS="" CERT_DOMAINS=""
for i in $(ls /etc/letsencrypt/live/); do for i in /etc/letsencrypt/live/*; do
if [ -e "/etc/letsencrypt/live/$i/fullchain.pem" ]; then D="${i##*/}" # Extract the last component of the path.
CERT_DOMAINS="$CERT_DOMAINS $i" if [ -e "/etc/letsencrypt/live/$D/fullchain.pem" ]; then
CERT_DOMAINS="$CERT_DOMAINS $D"
fi fi
done done
@@ -104,4 +106,5 @@ echo "hostname: '$ONE_DOMAIN'" >> /etc/chasquid/chasquid.conf
start-stop-daemon --start --quiet --pidfile /run/dovecot.pid \ start-stop-daemon --start --quiet --pidfile /run/dovecot.pid \
--exec /usr/sbin/dovecot -- -c /etc/dovecot/dovecot.conf --exec /usr/sbin/dovecot -- -c /etc/dovecot/dovecot.conf
# shellcheck disable=SC2086
sudo -u chasquid -g chasquid /usr/bin/chasquid $CHASQUID_FLAGS sudo -u chasquid -g chasquid /usr/bin/chasquid $CHASQUID_FLAGS

View File

@@ -8,20 +8,20 @@
set -e set -e
for IN in *.pod; do for IN in *.pod; do
OUT=$( basename $IN .pod ) OUT=$(basename "$IN" .pod)
SECTION=${OUT##*.} SECTION=${OUT##*.}
NAME=${OUT%.*} NAME=${OUT%.*}
# If it has not changed in git, set the mtime to the last commit that # If it has not changed in git, set the mtime to the last commit that
# touched the file. # touched the file.
CHANGED=$( git status --porcelain -- "$IN" | wc -l ) CHANGED=$( git status --porcelain -- "$IN" | wc -l )
if [ $CHANGED -eq 0 ]; then if [ "$CHANGED" -eq 0 ]; then
GIT_MTIME=$( git log --pretty=%at -n1 -- "$IN" ) GIT_MTIME=$( git log --pretty=%at -n1 -- "$IN" )
touch -d "@$GIT_MTIME" "$IN" touch -d "@$GIT_MTIME" "$IN"
fi fi
podchecker $IN podchecker "$IN"
pod2man --section=$SECTION --name=$NAME \ pod2man --section="$SECTION" --name="$NAME" \
--release "" --center "" \ --release "" --center "" \
$IN $OUT "$IN" "$OUT"
done done

View File

@@ -10,7 +10,7 @@
# coverage_test.go), but works for now. # coverage_test.go), but works for now.
set -e set -e
. $(dirname ${0})/util/lib.sh . "$(dirname "$0")/util/lib.sh"
init init
@@ -28,11 +28,11 @@ export COVER_DIR="$PWD/.coverage"
# tests, which don't expect any expvars to exists besides the one registered # tests, which don't expect any expvars to exists besides the one registered
# in the tests themselves. # in the tests themselves.
for pkg in $(go list ./... | grep -v -E 'chasquid/cmd/|chasquid/test'); do for pkg in $(go list ./... | grep -v -E 'chasquid/cmd/|chasquid/test'); do
OUT_FILE="$COVER_DIR/pkg-`echo $pkg | sed s+/+_+g`.out" OUT_FILE="$COVER_DIR/pkg-${pkg//\//_}.out"
go test -tags coverage \ go test -tags coverage \
-covermode=count \ -covermode=count \
-coverprofile="$OUT_FILE" \ -coverprofile="$OUT_FILE" \
-coverpkg=./... $pkg -coverpkg=./... "$pkg"
done done
# Integration tests. # Integration tests.
@@ -61,5 +61,5 @@ go run "${UTILDIR}/coverhtml/coverhtml.go" \
echo echo
echo echo
echo "Coverage report can be found in:" echo "Coverage report can be found in:"
echo file://$COVER_DIR/coverage.html echo "file://$COVER_DIR/coverage.html"

View File

@@ -1,16 +1,16 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/util/lib.sh . "$(dirname "$0")/util/lib.sh"
init init
FAILED=0 FAILED=0
for i in t-*; do for i in t-*; do
echo $i ... echo "$i" ...
setsid -w $i/run.sh setsid -w "$i/run.sh"
FAILED=$(( $FAILED + $? )) FAILED=$(( FAILED + $? ))
echo echo
done done

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
@@ -14,18 +14,18 @@ mkdir -p .logs
chasquid -v=-1 --logfile=.logs/chasquid.log --config_dir=config & chasquid -v=-1 --logfile=.logs/chasquid.log --config_dir=config &
wait_until_ready 1025 wait_until_ready 1025
echo Peak RAM: `chasquid_ram_peak` echo "Peak RAM: $(chasquid_ram_peak)"
if ! loadgen -logtime -addr=localhost:1025 -run_for=3s -noop; then if ! loadgen -logtime -addr=localhost:1025 -run_for=3s -noop; then
fail fail "loadgen -noop error"
fi fi
echo Peak RAM: `chasquid_ram_peak` echo "Peak RAM: $(chasquid_ram_peak)"
if ! loadgen -logtime -addr=localhost:1025 -run_for=3s; then if ! loadgen -logtime -addr=localhost:1025 -run_for=3s; then
fail fail "loadgen error"
fi fi
echo Peak RAM: `chasquid_ram_peak` echo "Peak RAM: $(chasquid_ram_peak)"
success success

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
@@ -14,22 +14,22 @@ mkdir -p .logs
chasquid -v=-1 --logfile=.logs/chasquid.log --config_dir=config & chasquid -v=-1 --logfile=.logs/chasquid.log --config_dir=config &
wait_until_ready 1025 wait_until_ready 1025
echo Peak RAM: `chasquid_ram_peak` echo "Peak RAM: $(chasquid_ram_peak)"
# Set connection count to (max open files) - (leeway). # Set connection count to (max open files) - (leeway).
# We set the leeway to account for file descriptors opened by the runtime and # We set the leeway to account for file descriptors opened by the runtime and
# listeners; 20 should be enough for now. # listeners; 20 should be enough for now.
# Cap it to 2000, as otherwise it can be problematic due to port availability. # Cap it to 2000, as otherwise it can be problematic due to port availability.
COUNT=$(( `ulimit -n` - 20 )) COUNT=$(( $(ulimit -n) - 20 ))
if [ $COUNT -gt 2000 ]; then if [ $COUNT -gt 2000 ]; then
COUNT=2000 COUNT=2000
fi fi
if ! conngen -logtime -addr=localhost:1025 -count=$COUNT; then if ! conngen -logtime -addr=localhost:1025 -count=$COUNT; then
tail -n 1 .logs/chasquid.log tail -n 1 .logs/chasquid.log
fail fail "conngen error"
fi fi
echo Peak RAM: `chasquid_ram_peak` echo "Peak RAM: $(chasquid_ram_peak)"
success success

View File

@@ -1,16 +1,16 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/util/lib.sh . "$(dirname "$0")/util/lib.sh"
init init
FAILED=0 FAILED=0
for i in stress-*; do for i in stress-*; do
echo $i ... echo "$i ..."
setsid -w $i/run.sh setsid -w "$i/run.sh"
FAILED=$(( $FAILED + $? )) FAILED=$(( FAILED + $? ))
echo echo
done done

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -5,19 +5,19 @@
# given the nature of these tests that's acceptable for now. # given the nature of these tests that's acceptable for now.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
# Download and extract the package in .exim-bin # Download and extract the package in .exim-bin
apt download exim4-daemon-light apt download exim4-daemon-light
dpkg -x exim4-daemon-light_*.deb $PWD/.exim-bin/ dpkg -x exim4-daemon-light_*.deb "$PWD/.exim-bin/"
# Create a symlink to .exim4, which is the directory we will use to store # Create a symlink to .exim4, which is the directory we will use to store
# configuration, spool, etc. # configuration, spool, etc.
# The configuration template will look for it here. # The configuration template will look for it here.
mkdir -p .exim4 mkdir -p .exim4
ln -sf $PWD/.exim-bin/usr/sbin/exim4 .exim4/ ln -sf "$PWD/.exim-bin/usr/sbin/exim4" .exim4/
# Remove the setuid bit, if there is one - we don't need it and may cause # Remove the setuid bit, if there is one - we don't need it and may cause
# confusion and/or security troubles. # confusion and/or security troubles.

View File

@@ -23,7 +23,7 @@
# chasquid will receive the email from exim, and deliver it locally. # chasquid will receive the email from exim, and deliver it locally.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases
@@ -14,12 +14,12 @@ chasquid -v=2 --logfile=.logs/chasquid.log --config_dir=config &
wait_until_ready 1025 wait_until_ready 1025
function send_and_check() { function send_and_check() {
run_msmtp $1@testserver < content run_msmtp "$1@testserver" < content
shift shift
for i in $@; do for i in "$@"; do
wait_for_file .mail/$i@testserver wait_for_file ".mail/$i@testserver"
mail_diff content .mail/$i@testserver mail_diff content ".mail/$i@testserver"
rm -f .mail/$i@testserver rm -f ".mail/$i@testserver"
done done
} }

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -5,7 +5,7 @@
# capitalizations. # capitalizations.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases
@@ -45,8 +45,8 @@ run_msmtp aliasB@srv-B < content
# Get some of the debugging pages, for troubleshooting, and to make sure they # Get some of the debugging pages, for troubleshooting, and to make sure they
# work reasonably well. # work reasonably well.
function fexp_gt10() { function fexp_gt10() {
fexp $1 -save $2 && \ fexp "$1" -save "$2" && \
[ $( cat $2 | wc -l ) -gt 10 ] [ "$( wc -l < "$2" )" -gt 10 ]
} }
fexp_gt10 http://localhost:1099/ .data-A/dbg-root \ fexp_gt10 http://localhost:1099/ .data-A/dbg-root \

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases
@@ -70,7 +70,7 @@ fi
# Check that the bad hooks don't prevent delivery. # Check that the bad hooks don't prevent delivery.
for i in config/hooks/post-data.bad*; do for i in config/hooks/post-data.bad*; do
cp $i config/hooks/post-data cp "$i" config/hooks/post-data
run_msmtp someone@testserver < content run_msmtp someone@testserver < content
wait_for_file .mail/someone@testserver wait_for_file .mail/someone@testserver

View File

@@ -7,7 +7,7 @@
# - dovecot listening on unix sockets in .dovecot/ # - dovecot listening on unix sockets in .dovecot/
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases
@@ -26,8 +26,8 @@ export ROOT="/tmp/chasquid-dovecot-test"
mkdir -p $ROOT $ROOT/run $ROOT/lib mkdir -p $ROOT $ROOT/run $ROOT/lib
rm -f $ROOT/dovecot.log rm -f $ROOT/dovecot.log
export GROUP=$(id -g -n) GROUP=$(id -g -n) envsubst \
envsubst < config/dovecot.conf.in > $ROOT/dovecot.conf < config/dovecot.conf.in > $ROOT/dovecot.conf
cp -f config/passwd $ROOT/passwd cp -f config/passwd $ROOT/passwd
dovecot -F -c $ROOT/dovecot.conf & dovecot -F -c $ROOT/dovecot.conf &

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
@@ -14,13 +14,13 @@ wait_until_ready 1025
FAILED=0 FAILED=0
for i in *.cmy; do for i in *.cmy; do
if ! chamuyero $i > .logs/$i.log 2>&1 ; then if ! chamuyero "$i" > ".logs/$i.log" 2>&1 ; then
echo "test $i failed, see .logs/$i.log" echo "test $i failed, see .logs/$i.log"
FAILED=1 FAILED=1
fi fi
done done
if [ $FAILED == 1 ]; then if [ $FAILED == 1 ]; then
fail fail "got at least one error"
fi fi
success success

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases
@@ -72,6 +72,6 @@ fexp http://localhost:1099/exit -status 405
# eventually exit. # eventually exit.
CHASQUID_PID=$(pgrep -s 0 chasquid) CHASQUID_PID=$(pgrep -s 0 chasquid)
fexp http://localhost:1099/exit -method POST -bodyre "OK" fexp http://localhost:1099/exit -method POST -bodyre "OK"
wait_until ! kill -s 0 $CHASQUID_PID 2> /dev/null wait_until ! kill -s 0 "$CHASQUID_PID" 2> /dev/null
success success

View File

@@ -3,7 +3,7 @@
# Test TLS tracking features, which require faking SPF. # Test TLS tracking features, which require faking SPF.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -4,7 +4,7 @@
# https://github.com/driusan/dkim # https://github.com/driusan/dkim
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -5,7 +5,7 @@
# main gaps. # main gaps.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases
@@ -47,7 +47,7 @@ function launch_minidns() {
kill $MINIDNS kill $MINIDNS
wait $MINIDNS || true wait $MINIDNS || true
fi fi
cp $1 .zones cp "$1" .zones
minidns_bg --addr=":9053" -zones=.zones >> .minidns.log 2>&1 minidns_bg --addr=":9053" -zones=.zones >> .minidns.log 2>&1
wait_until_ready 9053 wait_until_ready 9053
} }

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -3,7 +3,7 @@
# Test integration with dkimpy. # Test integration with dkimpy.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
check_hostaliases check_hostaliases

View File

@@ -8,7 +8,7 @@
# This is used for more hermetic Docker test environments. # This is used for more hermetic Docker test environments.
set -e set -e
. $(dirname ${0})/../util/lib.sh . "$(dirname "$0")/../util/lib.sh"
init init
@@ -45,7 +45,7 @@ export GOPROXY=off
# Launch arguments, which come from docker CMD, as "chasquid" user. # Launch arguments, which come from docker CMD, as "chasquid" user.
# Running tests as root makes some integration tests more difficult, as for # Running tests as root makes some integration tests more difficult, as for
# example Exim has hard-coded protections against running as root. # example Exim has hard-coded protections against running as root.
sudo -u chasquid -g chasquid \ sudo -u "chasquid" -g "chasquid" \
--set-home \ --set-home \
--preserve-env PATH=${PATH} \ --preserve-env PATH="$PATH" \
-- "$@" -- "$@"

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# Library to write the shell scripts in the tests. # Library to write the shell scripts in the tests.
function init() { function init() {
@@ -5,10 +6,11 @@ function init() {
set -v set -v
fi fi
export UTILDIR="$( realpath `dirname "${BASH_SOURCE[0]}"` )" UTILDIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")" )
export UTILDIR
export TBASE="$(realpath `dirname ${0}`)" TBASE=$(realpath "$(dirname "$0")" )
cd ${TBASE} cd "${TBASE}" || exit 1
if [ "${RACE}" == "1" ]; then if [ "${RACE}" == "1" ]; then
GOFLAGS="$GOFLAGS -race" GOFLAGS="$GOFLAGS -race"
@@ -30,7 +32,8 @@ function chasquid() {
return return
fi fi
( cd ${TBASE}/../../; go build $GOFLAGS -tags="$GOTAGS" . ) # shellcheck disable=SC2086
( cd "${TBASE}/../../" || exit 1; go build $GOFLAGS -tags="$GOTAGS" . )
# HOSTALIASES: so we "fake" hostnames. # HOSTALIASES: so we "fake" hostnames.
# PATH: so chasquid can call test-mda without path issues. # PATH: so chasquid can call test-mda without path issues.
@@ -38,13 +41,14 @@ function chasquid() {
HOSTALIASES=${TBASE}/hosts \ HOSTALIASES=${TBASE}/hosts \
PATH=${UTILDIR}:${PATH} \ PATH=${UTILDIR}:${PATH} \
MDA_DIR=${TBASE}/.mail \ MDA_DIR=${TBASE}/.mail \
${TBASE}/../../chasquid "$@" "${TBASE}/../../chasquid" "$@"
} }
function chasquid_cover() { function chasquid_cover() {
# Build the coverage-enabled binary. # Build the coverage-enabled binary.
# See coverage_test.go for more details. # See coverage_test.go for more details.
( cd ${TBASE}/../../; # shellcheck disable=SC2086
( cd "${TBASE}/../../" || exit 1;
go test -covermode=count -coverpkg=./... -c \ go test -covermode=count -coverpkg=./... -c \
-tags="coveragebin $GOTAGS" $GOFLAGS ) -tags="coveragebin $GOTAGS" $GOFLAGS )
@@ -54,9 +58,9 @@ function chasquid_cover() {
HOSTALIASES=${TBASE}/hosts \ HOSTALIASES=${TBASE}/hosts \
PATH=${UTILDIR}:${PATH} \ PATH=${UTILDIR}:${PATH} \
MDA_DIR=${TBASE}/.mail \ MDA_DIR=${TBASE}/.mail \
${TBASE}/../../chasquid.test \ "${TBASE}/../../chasquid.test" \
-test.run "^TestRunMain$" \ -test.run "^TestRunMain$" \
-test.coverprofile="$COVER_DIR/test-`date +%s.%N`.out" \ -test.coverprofile="$COVER_DIR/test-$(date +%s.%N).out" \
"$@" "$@"
} }
@@ -65,9 +69,9 @@ function chasquid_cover() {
# use the simpler add_user (below) for testing purposes. # use the simpler add_user (below) for testing purposes.
function chasquid-util-user-add() { function chasquid-util-user-add() {
CONFDIR="${CONFDIR:-config}" CONFDIR="${CONFDIR:-config}"
DOMAIN=$(echo $1 | cut -d @ -f 2) DOMAIN=$(echo "$1" | cut -d @ -f 2)
mkdir -p "${CONFDIR}/domains/$DOMAIN/" mkdir -p "${CONFDIR}/domains/$DOMAIN/"
go run ${TBASE}/../../cmd/chasquid-util/chasquid-util.go \ go run "${TBASE}/../../cmd/chasquid-util/chasquid-util.go" \
-C="${CONFDIR}" \ -C="${CONFDIR}" \
user-add "$1" \ user-add "$1" \
--password="$2" \ --password="$2" \
@@ -76,8 +80,8 @@ function chasquid-util-user-add() {
function add_user() { function add_user() {
CONFDIR="${CONFDIR:-config}" CONFDIR="${CONFDIR:-config}"
USERNAME=$(echo $1 | cut -d @ -f 1) USERNAME=$(echo "$1" | cut -d @ -f 1)
DOMAIN=$(echo $1 | cut -d @ -f 2) DOMAIN=$(echo "$1" | cut -d @ -f 2)
USERDB="${CONFDIR}/domains/$DOMAIN/users" USERDB="${CONFDIR}/domains/$DOMAIN/users"
mkdir -p "${CONFDIR}/domains/$DOMAIN/" mkdir -p "${CONFDIR}/domains/$DOMAIN/"
if ! [ -f "${USERDB}" ] || ! grep -E -q "key:.*${USERNAME}" "${USERDB}"; then if ! [ -f "${USERDB}" ] || ! grep -E -q "key:.*${USERNAME}" "${USERDB}"; then
@@ -87,7 +91,7 @@ function add_user() {
} }
function dovecot-auth-cli() { function dovecot-auth-cli() {
go run ${TBASE}/../../cmd/dovecot-auth-cli/dovecot-auth-cli.go "$@" go run "${TBASE}/../../cmd/dovecot-auth-cli/dovecot-auth-cli.go" "$@"
} }
function run_msmtp() { function run_msmtp() {
@@ -97,54 +101,54 @@ function run_msmtp() {
# msmtp binary is often g+s, which causes $HOSTALIASES to not be # msmtp binary is often g+s, which causes $HOSTALIASES to not be
# honoured, which breaks the tests. Copy the binary to remove the # honoured, which breaks the tests. Copy the binary to remove the
# setgid bit as a workaround. # setgid bit as a workaround.
cp -u "`command -v msmtp`" "${UTILDIR}/.msmtp-bin" cp -u "$(command -v msmtp)" "${UTILDIR}/.msmtp-bin"
HOSTALIASES=${TBASE}/hosts \ HOSTALIASES=${TBASE}/hosts \
${UTILDIR}/.msmtp-bin -C msmtprc "$@" "${UTILDIR}/.msmtp-bin" -C msmtprc "$@"
} }
function smtpc.py() { function smtpc.py() {
${UTILDIR}/smtpc.py "$@" "${UTILDIR}/smtpc.py" "$@"
} }
function mail_diff() { function mail_diff() {
${UTILDIR}/mail_diff "$@" "${UTILDIR}/mail_diff" "$@"
} }
function chamuyero() { function chamuyero() {
${UTILDIR}/chamuyero "$@" "${UTILDIR}/chamuyero" "$@"
} }
function generate_cert() { function generate_cert() {
( cd ${UTILDIR}/generate_cert/; go build ) ( cd "${UTILDIR}/generate_cert/" || exit 1; go build )
${UTILDIR}/generate_cert/generate_cert "$@" "${UTILDIR}/generate_cert/generate_cert" "$@"
} }
function loadgen() { function loadgen() {
( cd ${UTILDIR}/loadgen/; go build ) ( cd "${UTILDIR}/loadgen/" || exit 1; go build )
${UTILDIR}/loadgen/loadgen "$@" "${UTILDIR}/loadgen/loadgen" "$@"
} }
function conngen() { function conngen() {
( cd ${UTILDIR}/conngen/; go build ) ( cd "${UTILDIR}/conngen/" || exit 1; go build )
${UTILDIR}/conngen/conngen "$@" "${UTILDIR}/conngen/conngen" "$@"
} }
function minidns_bg() { function minidns_bg() {
( cd ${UTILDIR}/minidns; go build ) ( cd "${UTILDIR}/minidns" || exit 1; go build )
${UTILDIR}/minidns/minidns "$@" & "${UTILDIR}/minidns/minidns" "$@" &
MINIDNS=$! export MINIDNS=$!
} }
function fexp() { function fexp() {
( cd ${UTILDIR}/fexp/; go build ) ( cd "${UTILDIR}/fexp/" || exit 1; go build )
${UTILDIR}/fexp/fexp "$@" "${UTILDIR}/fexp/fexp" "$@"
} }
function timeout() { function timeout() {
MYPID=$$ MYPID=$$
( (
sleep $1 sleep "$1"
echo "timed out after $1, killing test" echo "timed out after $1, killing test"
kill -9 $MYPID kill -9 $MYPID
) & ) &
@@ -155,18 +159,18 @@ function success() {
} }
function skip() { function skip() {
echo skipped: $* echo "skipped: $*"
exit 0 exit 0
} }
function fail() { function fail() {
echo FAILED: $* echo "FAILED: $*"
exit 1 exit 1
} }
function check_hostaliases() { function check_hostaliases() {
if ! "${UTILDIR}/check-hostaliases"; then if ! "${UTILDIR}/check-hostaliases"; then
skip '$HOSTALIASES not working (probably systemd-resolved)' skip "\$HOSTALIASES not working (probably systemd-resolved)"
fi fi
} }
@@ -181,14 +185,14 @@ function wait_until_ready() {
# Wait for the given file to exist. # Wait for the given file to exist.
function wait_for_file() { function wait_for_file() {
while ! [ -e ${1} ]; do while ! [ -e "$1" ]; do
sleep 0.01 sleep 0.01
done done
} }
function wait_until() { function wait_until() {
while true; do while true; do
if eval "$@"; then if eval "$*"; then
return 0 return 0
fi fi
sleep 0.01 sleep 0.01
@@ -204,16 +208,16 @@ function generate_certs_for() {
CACHEDIR="${TBASE}/../.generate_certs_cache" CACHEDIR="${TBASE}/../.generate_certs_cache"
mkdir -p "${CACHEDIR}" mkdir -p "${CACHEDIR}"
touch -d "10 minutes ago" "${CACHEDIR}/.reference" touch -d "10 minutes ago" "${CACHEDIR}/.reference"
if [ "${CACHEDIR}/${1}/" -ot "${CACHEDIR}/.reference" ]; then if [ "${CACHEDIR}/$1/" -ot "${CACHEDIR}/.reference" ]; then
# Cache miss (either was not there, or was too old). # Cache miss (either was not there, or was too old).
mkdir -p "${CACHEDIR}/${1}/" mkdir -p "${CACHEDIR}/$1/"
( (
cd "${CACHEDIR}/${1}/" cd "${CACHEDIR}/$1/" || exit 1
generate_cert -ca -validfor=1h -host=${1} generate_cert -ca -validfor=1h -host="$1"
) )
fi fi
mkdir -p "${CONFDIR}/certs/${1}/" mkdir -p "${CONFDIR}/certs/$1/"
cp -p "${CACHEDIR}/${1}"/* "${CONFDIR}/certs/${1}/" cp -p "${CACHEDIR}/$1"/* "${CONFDIR}/certs/$1/"
} }
# Check the Python version, and skip if it's too old. # Check the Python version, and skip if it's too old.
@@ -229,7 +233,6 @@ function skip_if_python_is_too_old() {
function chasquid_ram_peak() { function chasquid_ram_peak() {
# Find the pid of the daemon, which we expect is running on the # Find the pid of the daemon, which we expect is running on the
# background somewhere within our current session. # background somewhere within our current session.
SERVER_PID=`pgrep -s 0 -x chasquid` SERVER_PID=$(pgrep -s 0 -x chasquid)
grep VmHWM "/proc/$SERVER_PID/status" | cut -d ':' -f 2-
echo $( cat /proc/$SERVER_PID/status | grep VmHWM | cut -d ':' -f 2- )
} }