#!/bin/bash -ex

# some integration tests get confused by a running podman service
if [ "$(id -u)" = "0" ]; then
    systemctl stop podman.service
    systemctl stop podman.socket
    systemctl mask podman.service
    systemctl mask podman.socket
else
    systemctl stop --user podman.service
    systemctl stop --user podman.socket
    systemctl mask --user podman.service
    systemctl mask --user podman.socket
fi

if command -v sudo; then
    SUDO="sudo"
else
    SUDO=""
fi

$SUDO apt-get -y install locales
$SUDO sed -i '/en_US.UTF-8/s/^#//g' /etc/locale.gen
$SUDO locale-gen

# The podman e2e test suite is implemented in form of ginkgo tests
export DH_GOLANG_BUILDPKG="github.com/containers/podman/test/e2e"
export DH_GOLANG_INSTALL_ALL=1
# export DH_VERBOSE=1

# Given that we don't currently have a golang-github-containers-libpod-dev
# package that would ship the sources to the tests, let's run them from the
# source tree. However, we need to simulate an environment that ensures
# to avoid downloading source from the internet and instead use the golang
# packages from the distribution instead. For this, this script is copying
# the invocation from `debian/rules`.
# note that buildtags need to be kept in sync here

BUILDTAGS="apparmor seccomp selinux systemd libsubid cni"
dh_auto_configure -O--buildsystem=golang -O--builddirectory=_build
dh_auto_build -O--buildsystem=golang -O--builddirectory=_build --  -tags "${BUILDTAGS}"

# cf. https://github.com/containers/podman/issues/24802#issuecomment-2527979071
chown -R "$(id -u):$(id -g)" _build

# use binaries built by the package
export PODMAN_BINARY=/usr/bin/podman
export PODMAN_REMOTE_BINARY=/usr/bin/podman-remote
export QUADLET_BINARY=/usr/libexec/podman/quadlet

# bindings tests need access to podman-registry
PATH="$(pwd)/hack:${PATH}"
export PATH

# avoid accessing internet but use debian sources for gomodules
export GO111MODULE=off
export GOPATH="${PWD}/_build:/usr/share/gocode"
export GOCACHE="${PWD}/_build/go-build"
export GOPROXY="off"
GINKGO="go run github.com/onsi/ginkgo/v2/ginkgo"
GINKGO_FLAGS="-p -vv --timeout 90m --flake-attempts 3"
GINKGO_EXTRA_FLAGS=${GINKGO_EXTRA_FLAGS:-}

exec ${GINKGO} ${GINKGO_FLAGS} ${GINKGO_EXTRA_FLAGS} -tags "${BUILDTAGS} remote"  \
     run --no-color  \
     "_build/src/${DH_GOLANG_BUILDPKG}/."

