#!/bin/sh

set -e

export LIBVIRT_DEFAULT_URI='lxc:///'

XML=debian/tests/smoke-lxc.xml
DOMAIN=sl

cleanup()
{
    if [ -z "$CLEANED_UP" ]; then
        virsh destroy ${DOMAIN} || true
        virsh undefine ${DOMAIN} || true
        CLEANED_UP=1
    fi
}

try_check_domain()
{
    for _ in $(seq 10); do
        check_domain && return
        sleep 2s
    done
    echo "Known to be unreliable on test infrastructure - skipping"
    exit 77
}

check_domain()
{
    rc=0
    virsh list | grep -qs "${DOMAIN}[[:space:]]\+running"
    rc=$((rc+$?))
    virsh lxc-enter-namespace --noseclabel ${DOMAIN} /bin/ls /bin/ls
    rc=$((rc+$?))
    return $rc
}

try_restart_libvirtd()
{
    for _ in $(seq 10); do
        systemctl restart libvirtd && return
        sleep 2s
    done
    # This turned out to be flaky, non reproducible outside of LP-infra and
    # is not what we want to test, Skip the test in this case
    echo "Restart failed while checking for container-survival-through restart - skipping".
    exit 77
}

trap cleanup EXIT

# This is an upstream bug due to combined cgroup v1/v2 handling
# https://libvirt.org/git/?p=libvirt.git;a=commit;h=9c1693eff4
# https://bugs.archlinux.org/task/70174
# https://bugzilla.opensuse.org/show_bug.cgi?id=1183247
# https://gitlab.com/libvirt/libvirt/-/issues/182
# https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1934966
# libvirt-lxc is unimportant on Ubuntu due to LXD being preferred for
# the same use case. Skip this test (until the upstream issue is
# resolved) in case v1&v2 cgroup are present in the test environment.
if [ -n "$(mount -t cgroup)" ] && [ -n "$(mount -t cgroup2)" ]; then
    echo "Libvirt >=7.1.0 is known to fail LXC handling with systemd <248 (issue 182) - skipping".
    exit 77
fi

set -x
virt-host-validate lxc || exit 0
virsh capabilities
virsh capabilities | grep -qs 'emulator>/usr/lib/libvirt/libvirt_lxc'
virsh capabilities | grep -qs 'os_type>exe'
virt-xml-validate ${XML}
virsh define ${XML}
rm -f /var/log/libvirt/lxc/sl.log
virsh start ${DOMAIN}
# Check virtlogd is running
grep -qs "starting up" /var/log/libvirt/lxc/sl.log
try_check_domain
# Make sure a restart doesn't termiante the domain
try_restart_libvirtd
try_check_domain
virsh destroy ${DOMAIN} || true
virsh undefine ${DOMAIN}
CLEANED_UP=1
set +x

echo 'Smoke test of lxc:/// succesful'
exit 0
