#!/bin/bash
# Tests for `rpm-ostree ex container`.
set -xeuo pipefail

dn=$(cd $(dirname $0) && pwd)
export topsrcdir=$(cd $dn/.. && pwd)
. ${dn}/common/libtest-core.sh
. ${dn}/common/libcomposetest.sh

if test "$(id -u)" = 0; then
    fatal "Don't run $0 as root"
fi

LOGDIR=${LOGDIR:-$(pwd)/ex-compose-logs}

tmpdir=$(mktemp -d /var/tmp/rpm-ostree-container.XXXXXX)
touch ${tmpdir}/.tmpdir
cleanup_tmp() {
    # This sanity check ensures we don't delete something else
    if test -z "${TEST_SKIP_CLEANUP:-}" && test -f ${tmpdir}/.tmpdir; then
        rm -rf ${tmpdir}
    fi
}
trap cleanup_tmp EXIT

cd ${tmpdir}
rpm-ostree ex container init
setup_rpmmd_repos ${tmpdir}/rpmmd.repos.d

echo "Results in ${LOGDIR}"
rm ${LOGDIR} -rf
mkdir -p ${LOGDIR}

# Ideally pass $(cwd) down into parallel somehow
export test_tmpdir=${tmpdir}
# We use -j 1 since we can't actually be parallel at the moment due
# to locking issues with metadata downloads at least.  Down the line
# we should try to do a dry run pull first like the compose tests do.
ls ${dn}/ex-container-tests/test-*.sh | sort |
    parallel -j 1 --tag --halt soon,fail=1 --joblog joblog --results ${LOGDIR} --line-buffer {}
