#!/bin/sh
# Environment
set -eu

unset TMPDIR

TEST_PASS=0
TEST_FAIL=0
TEST_IGNORED=0

IGNORE_LIST=""

# Helper functions
pass() {
    TEST_PASS="$((TEST_PASS+1))"

    CURRENT_TIME="$(date +%s)"
    DURATION="$((CURRENT_TIME-START_TIME))"

    echo "PASS: $1 (${DURATION}s)"
}

fail() {
    for entry in $IGNORE_LIST; do
        if [ "$entry" = "$2" ]; then
            ignore "$1"
            return
        fi
    done

    TEST_FAIL="$((TEST_FAIL+1))"

    CURRENT_TIME="$(date +%s)"
    DURATION="$((CURRENT_TIME-START_TIME))"

    echo "FAIL: $1 (${DURATION}s)"

    if [ -f "$3" ]; then
        echo "---"
        cat "$3"
        echo "---"
    fi
}

ignore() {
    TEST_IGNORED="$((TEST_IGNORED+1))"
    echo "IGNORED: $*"
}

summary() {
    echo ""
    echo "SUMMARY: pass=$TEST_PASS, fail=$TEST_FAIL, ignored=$TEST_IGNORED"
}

# Workaround for broken gpg2
if [ -n "${http_proxy:-}" ] && [ -e /usr/bin/dirmngr ]; then
    dpkg-divert --divert /usr/bin/dirmngr.orig --rename --add /usr/bin/dirmngr
    (
    cat << EOF
#!/bin/sh
exec /usr/bin/dirmngr.orig --honor-http-proxy \$@
EOF
    ) > /usr/bin/dirmngr
    chmod +x /usr/bin/dirmngr
fi

# Override the GPG server
sed -i "s/^DOWNLOAD_VALIDATE.*/DOWNLOAD_VALIDATE=\"false\"/" /usr/share/lxc/templates/lxc-download
export DOWNLOAD_KEYSERVER="hkp://keyserver.ubuntu.com:80"
cd /usr/bin

# The actual tests
## Default testsuite
for testbin in lxc-test-*; do
    [ -x "$testbin" ] || continue
    echo "${testbin}" | grep -qv "\.in$" || continue
    STRING="lxc-tests: $testbin"

    # Some tests can't be run standalone
    [ "$testbin" = "lxc-test-may-control" ] && continue

    # Skip some tests when running in a container
    if [ -f /run/container_type ] || command -v systemd-detect-virt >/dev/null && systemd-detect-virt --quiet --container; then
        [ "$testbin" = "lxc-test-apparmor" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-device-add-remove" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-reboot" ] && \
            ignore "$STRING" && continue
    fi

    # Skip userns tests in unprivileged containers
    if [ -f /proc/self/uid_map ] && \
            ! grep -q "4294967295$" /proc/self/uid_map; then

        [ "$testbin" = "lxc-test-unpriv" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-usernic" ] && \
            ignore "$STRING" && continue
    fi

    # Skip some tests on old kernels
    if [ ! -f /proc/self/uid_map ] || [ ! -f /etc/subuid ] || \
       [ ! -f /etc/subgid ]; then
        [ "$testbin" = "lxc-test-unpriv" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-usernic" ] && \
            ignore "$STRING" && continue
    fi

    # Skip some tests because of broken busybox
    [ "$testbin" = "lxc-test-state-server" ] && \
        ignore "$STRING" && continue

    # Skip some tests due to cgroup v2 incompatibility
    if [ -e /sys/fs/cgroup/system.slice/memory.current ]; then

        [ "$testbin" = "lxc-test-apparmor-mount" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-autostart" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-no-new-privs" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-unpriv" ] && \
            ignore "$STRING" && continue

        [ "$testbin" = "lxc-test-usernic" ] && \
            ignore "$STRING" && continue
    fi

    OUT="$(mktemp)"
    START_TIME="$(date +%s)"
    if "./$testbin" > "$OUT" 2>&1; then
        pass "$STRING"
    else
        fail "$STRING" "$testbin" "$OUT"
    fi
    rm "$OUT"
done

## Python3 testsuite
STRING="python3: API"
if [ ! -f /usr/share/doc/python3-lxc/examples/api_test.py.gz ]; then
    ignore "$STRING"
else
    OUT="$(mktemp)"

    PYTEST="$(mktemp)"
    zcat /usr/share/doc/python3-lxc/examples/api_test.py.gz > "$PYTEST"
    if python3 "$PYTEST" > "$OUT" 2>&1; then
        pass "$STRING"
    else
        fail "$STRING" "python3" "$OUT"
    fi
    rm "$PYTEST"

    rm "$OUT"
fi

# Workaround for broken gpg2
if [ -n "${http_proxy:-}" ] && [ -e /usr/bin/dirmngr ]; then
    rm /usr/bin/dirmngr
    dpkg-divert --divert /usr/bin/dirmngr.orig --rename --remove /usr/bin/dirmngr
fi

# Test summary
summary

[ "$TEST_FAIL" != "0" ] && exit 1

exit 0
