#!/bin/sh
# test 20 successful reboots in a row
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# For bisecting/testing you can replace individual binaries in /lib/systemd
# with --copy /host/path/systemd-foo:/tmp/systemd-replace/systemd-foo
set -e

. `dirname $0`/assert.sh

if [ -z "$ADT_REBOOT_MARK" ]; then
    # enable persistent journal
    mkdir -p /var/log/journal
    # allow X to start even on headless machines
    mkdir -p /etc/X11/xorg.conf.d/
    cat << EOF > /etc/X11/xorg.conf.d/dummy.conf
Section "Device"
    Identifier "test"
    Driver "dummy"
EndSection
EOF


    ADT_REBOOT_MARK=0
    if [ -d /tmp/systemd-replace/ ]; then
        for f in /tmp/systemd-replace/*; do
            echo "Installing $f..."
            rm -f /lib/systemd/$(basename $f)
            cp $f /lib/systemd/
        done
    fi
else
    ret=0

    echo "waiting to boot..."
    TIMEOUT=35
    while [ $TIMEOUT -ge 0 ]; do
        state="$(systemctl is-system-running || true)"
        case $state in
            running|degraded)
                break
                ;;
            *)
                sleep 1
                TIMEOUT=$((TIMEOUT - 1))
		;;
        esac
    done

    echo "checking for failed unmounts for user systemd"
    JOURNAL=$(journalctl)
    if echo "$JOURNAL" | grep -E "systemd\[([2-9]|[1-9][0-9]+)\].*Failed unmounting"; then
        ret=1
    fi
    echo "checking for connection timeouts (non fatal)"
    if echo "$JOURNAL" | grep "Connection timed out"; then
        # systemd-udevd started to time out resolving group 'colord'
        # yet, not reproducible locally, investigating
        ret=0
    fi

    echo "checking that polkitd runs"
    if ! pidof polkitd; then
        echo "polkitd is NOT running"
        ret=1
    fi

    echo "checking failed jobs (non fatal)"
    if [ "$state" != "running" ]; then
        echo "systemctl is-system-running returns: $state"
        systemctl --no-pager --no-legend list-jobs > $ADT_ARTIFACTS/running-jobs.txt || true
    fi

    echo "checking that there are no running jobs"
    running="$(systemctl --no-pager --no-legend list-jobs || true)"
    if [ -n "$running" ]; then
        echo "running jobs after remaining timeout $TIMEOUT: $running"
        journalctl --sync
        journalctl -ab > $ADT_ARTIFACTS/journal.txt
        udevadm info --export-db > $ADT_ARTIFACTS/udevdb.txt
        ret=1
    fi

    if [ "$ret" != "0" ]; then
       exit $ret
    fi
fi

if [ "$ADT_REBOOT_MARK" -ge 5 ]; then
    exit 0
fi

echo "reboot #$ADT_REBOOT_MARK"
/tmp/autopkgtest-reboot $(($ADT_REBOOT_MARK + 1))
