#!/bin/sh
# =============================================================================
# autopkgtest "syslog": self-test of the fake /dev/log module.
#
# Many containers have no syslog socket.  Daemons and tools such as sudo,
# sssd or cron then print warnings or block.  fx_syslog_start provides a
# socket at /dev/log and collects the messages in $FX_LOGDIR/syslog.log, which
# is shown or saved as an artifact if the test fails.
#
# Needs root (creates /dev/log) and perl (perl-base, Essential).
#
# HOW TO ADAPT THIS FILE
#  * In your own test, call fx_syslog_start BEFORE starting the daemon that
#    logs.  If the testbed already has a /dev/log socket nothing is changed
#    and nothing is captured ($FX_SYSLOG_CAPTURE is 0).
#  * To check that an action logged something, see fx_log_mark,
#    fx_log_expect and fx_log_absent below.
#  * Run from a checkout:  FX_LIBDIR=$PWD/lib sh debian/tests/syslog
# =============================================================================
set -eu

LIB="${FX_LIBDIR:-/usr/share/autopkgtest-helper}"
export FX_LIBDIR="$LIB"
. "$LIB/fixtures.sh"
fx_need_root
fx_init
fx_load syslog

# Remember whether the testbed brought its own /dev/log; the checks on the
# captured message only make sense when our fake one is in use.
had_socket=0
if [ -S /dev/log ]; then had_socket=1; fi

fx_syslog_start
[ -S /dev/log ] || fx_fail "/dev/log is not a socket"

# Send one datagram the way syslog(3) does: <14> is facility user, level info.
fx_syslog_send fxtest "hello syslog"

if [ "$had_socket" -eq 0 ]; then
    [ "$FX_SYSLOG_CAPTURE" -eq 1 ] || fx_fail "own /dev/log in use but FX_SYSLOG_CAPTURE is not 1"
    # The message must show up in the library's syslog log; the receiver
    # needs a moment to write it out, so fx_log_expect polls.
    fx_log_expect syslog "hello syslog"
    # A real client: logger(1) uses syslog(3) of libc, with its own framing.
    logger -t fxlibc "via syslog(3)"
    fx_log_expect syslog "fxlibc: via syslog(3)"

    # One line per message, in the order of arrival, even for messages without
    # a trailing newline (syslog(3) sends none).
    for n in one two three; do fx_syslog_send fxtest "ordered $n"; done
    fx_log_expect -n 3 syslog "fxtest: ordered"
    grep 'ordered' "$FX_LOGDIR/syslog.log" >"$FX_ROOT/ordered"
    [ "$(wc -l <"$FX_ROOT/ordered")" -eq 3 ] || fx_fail "messages share a line"
    fx_assert_contains "$FX_ROOT/ordered" "ordered one" "ordered two" "ordered three"
    first=$(grep -n 'ordered one' "$FX_LOGDIR/syslog.log" | cut -d: -f1)
    third=$(grep -n 'ordered three' "$FX_LOGDIR/syslog.log" | cut -d: -f1)
    [ "$first" -lt "$third" ] || fx_fail "messages out of order"

    # Checking an action: only entries after the mark count.
    fx_log_mark syslog
    fx_log_absent syslog "sudo: alice"          # hidden entries do not count
    fx_syslog_send -p 85 sudo "alice : TTY=pts/0 ; COMMAND=/bin/true"
    fx_log_expect syslog "sudo: alice : TTY=pts/0"
    fx_log_expect -E syslog 'COMMAND=/bin/(true|false)$'
    # The barrier must really wait for the receiver.  That is hard to see
    # while the receiver is fast, so stop it: the datagram queues up in the
    # socket and the marker cannot appear.  Both the barrier itself and
    # fx_log_absent must then report that the logger did not catch up, instead
    # of declaring the message absent.
    rpid=$(cat "$FX_RUNDIR/syslog.pid")
    kill -STOP "$rpid"
    rc=0
    fx_log_barrier_syslog 1 || rc=$?
    [ "$rc" -ne 0 ] || { kill -CONT "$rpid"; fx_fail "barrier returned although the receiver was stopped"; }
    if ( fx_log_absent -t 1 syslog "sudo: bob" ) >/dev/null 2>"$FX_ROOT/stopped.err"; then
        kill -CONT "$rpid"
        fx_fail "fx_log_absent succeeded without being able to look"
    fi
    kill -CONT "$rpid"
    fx_assert_contains "$FX_ROOT/stopped.err" "did not catch up"
    # With the receiver running again, the queued message is written, and the
    # barrier works.
    fx_log_barrier_syslog 5 || fx_fail "barrier did not return with a running receiver"
    grep -q -F -e "$_fx_bm" "$FX_LOGDIR/syslog.log" || fx_fail "barrier returned before its marker was logged"
    fx_log_absent syslog "sudo: carol"
    # The barrier marker must not break counting by other checks.
    fx_log_expect -n 1 syslog "fx-barrier"
    # Starting the module a second time, while the socket exists, must be a
    # harmless no-op (the child shell has its own library state).
    cat >"$FX_ROOT/sub.sh" <<SUB
. "$LIB/fixtures.sh"
fx_init
fx_load syslog
fx_syslog_start
SUB
    # second start inside the same state: socket exists, nothing to do
    sh "$FX_ROOT/sub.sh"
else
    # The testbed's own /dev/log is not ours to capture: no checks possible.
    [ "$FX_SYSLOG_CAPTURE" -eq 0 ] || fx_fail "FX_SYSLOG_CAPTURE is not 0 with a system /dev/log"
    if ( fx_log_expect -t 0 syslog "hello" ) >/dev/null 2>"$FX_ROOT/nolog.err"; then
        fx_fail "fx_log_expect worked without captured syslog"
    fi
    fx_assert_contains "$FX_ROOT/nolog.err" "no log 'syslog'"
fi

echo "syslog: all tests passed"
