Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions test/default.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1054,14 +1054,81 @@ wait_for_log() {

# Last on purpose: these make the clamav-unofficial-sigs cron job fail, which
# writes to mail.log, mail.warn and mail.err, all checked clean above.
#
# The next test fails now and then in CI: the job succeeds and prints nothing,
# but its error never reaches syslog at all (not even mail.log), so it is not
# rsyslog being late. To tell the possible causes apart, it runs the job with
# a logger in front of the real one that keeps a copy of what it is given,
# and on failure writes these to the test output (TAP comments):
#
# - the owner of current-dbs.txt just before and after the job, in case it
# was not root-owned when the job wrote it
# - what the job handed to logger, at each priority, in case logger or
# rsyslog lost it
# - the cron line (its minute) and any update running when the job started,
# in case the container's own hourly job overlapped the test

SIGS_DIAG=/tmp/sigs-diag
SIGS_DBS=/var/lib/clamav-unofficial-sigs/configs/current-dbs.txt

# Install the copying logger in $SIGS_DIAG. Each call keeps its arguments and
# input in a file of its own.
sigs_diag_setup() {
docker exec mailserver_default /bin/sh -c "rm -rf $SIGS_DIAG && mkdir -m 1777 $SIGS_DIAG"
docker exec -i mailserver_default /bin/sh -c "cat > $SIGS_DIAG/logger && chmod 755 $SIGS_DIAG/logger" <<'EOF'
#!/bin/sh
f=$(mktemp /tmp/sigs-diag/logger.XXXXXX)
echo "logger $*" > "$f"
tee -a "$f" | /usr/bin/logger "$@"
EOF
}

# sigs_cron_job, with the copying logger first on the PATH
sigs_cron_job_diag() {
docker exec mailserver_default /bin/sh -c "grep -q '^enable_random=\"no\"' /etc/clamav-unofficial-sigs/user.conf || echo 'enable_random=\"no\"' >> /etc/clamav-unofficial-sigs/user.conf"
docker exec mailserver_default /bin/sh -c "grep -v '^#' /etc/cron.d/clamav-unofficial-sigs | grep . | { read -r m h dom mon dow user cmd; su -s /bin/sh \"\$user\" -c \"PATH=$SIGS_DIAG:\\\$PATH; export PATH; \$cmd\"; }"
}

sigs_owner() {
docker exec mailserver_default stat -c '%U (inode %i, modified %y)' $SIGS_DBS 2>&1
}

sigs_running() {
docker exec mailserver_default /bin/sh -c "ps -eo pid,user,lstart,args | grep '[c]lamav-unofficial-sigs[-a-z]*\.sh' || echo '(none)'"
docker exec mailserver_default ls -l /var/lib/clamav-unofficial-sigs/pid/ 2>&1 || true
}

# Write lines to the test output as TAP comments
sigs_diag_note() {
sed 's/^/# /' >&3
}

@test "checking clamav-unofficial-sigs: cron job stderr goes to mail.err" {
docker exec mailserver_default chown root /var/lib/clamav-unofficial-sigs/configs/current-dbs.txt
run sigs_cron_job
docker exec mailserver_default chown clamav /var/lib/clamav-unofficial-sigs/configs/current-dbs.txt
assert_success
assert_output ''
sigs_diag_setup
docker exec mailserver_default chown root $SIGS_DBS
before=$(sigs_owner)
running=$(sigs_running)
started=$(date -u +%T.%N)
run sigs_cron_job_diag
job_status=$status job_output=$output
after=$(sigs_owner)
docker exec mailserver_default chown clamav $SIGS_DBS
run wait_for_log 'clamav-unofficial-sigs.*current-dbs.txt: Permission denied' /var/log/mail.err
if [ "$status" -ne 0 ]; then
{
echo "diagnostics: job started at $started UTC, exit status $job_status"
echo "cron line: $(docker exec mailserver_default grep -v '^#' /etc/cron.d/clamav-unofficial-sigs | grep .)"
echo "current-dbs.txt before the job: $before"
echo "current-dbs.txt after the job: $after"
echo "updates running when the job started, and the pid directory:"
echo "$running"
echo "what the job handed to logger:"
docker exec mailserver_default /bin/sh -c "for f in $SIGS_DIAG/logger.*; do [ -e \"\$f\" ] || { echo '(logger was not called)'; break; }; echo \"--- \$f\"; cat \"\$f\"; done"
} | sigs_diag_note
fi
assert_success
assert_equal "$job_status" 0
assert_equal "$job_output" ''
}

@test "checking clamav-unofficial-sigs: cron job stdout goes to mail.warning" {
Expand Down