#!/bin/sh
# run upstream system integration tests
# Author: Martin Pitt <martin.pitt@ubuntu.com>
set -e

# even after installing policycoreutils this fails with
# "Failed to install /usr/libexec/selinux/hll/pp"
BLACKLIST="TEST-06-SELINUX"

# Some tests are broken without QEMU; drop this once
# https://github.com/systemd/systemd/pull/3587 lands
if ! dpkg -l | grep -q '^ii.*qemu-system-'; then
    BLACKLIST="$BLACKLIST TEST-08-ISSUE-2730 TEST-09-ISSUE-2691 TEST-10-ISSUE-2467 TEST-11-ISSUE-3166"
fi

# modify the image build scripts to install systemd from the debs instead of
# from a "make install" as we don't have a built tree here. Also call
# systemd-nspawn from the system.
sed -i '/make.* install/ s%^.*$%    for p in `grep ^Package: '`pwd`'/debian/control | cut -f2 -d\\  |grep -Ev -- "-(udeb|dev)"`; do (cd /tmp; apt-get download $p \&\& dpkg-deb -x ${p}[._]*deb $initdir); done%; s_../../systemd-nspawn_systemd-nspawn_g' test/test-functions

# adjust path
sed -i 's_/usr/libexec/selinux/hll/pp_/usr/lib/selinux/hll/pp_' test/TEST-06-SELINUX/test.sh

for t in test/TEST*; do
    echo "$BLACKLIST" | grep -q "$(basename $t)" && continue
    echo "========== `basename $t` =========="
    rm -rf /var/tmp/systemd-test.*
    for op in clean setup run; do
        if ! (cd $t; TEST_BASE_DIR=../ INITRD=/initrd.img ./test.sh --$op); then
            for j in /var/tmp/systemd-test.*/journal/*/system.journal; do
                [ -e "$j" ] || continue
                echo "---- $j ----"
                journalctl --file=$j
            done
            exit 1
        fi
    done
    echo
done
