#!/bin/sh
set -e

# udisks2 test needs to load scsi_debug kernel module, up to Ubuntu Noble, this kernel module
# is part of the linux-modules-extra that might not be installed in some images (LXD VMs)
# this is the best effort to install it. We cannot specify linux-image-generic as
# test dependency in d/t/control because it can pull-in a package a for different version
# than the currently active kernel.
apt-get install -y linux-modules-extra-$(uname -r) || :

mkdir -p /var/run/udisks2
mkdir -p /var/lib/udisks2
# collect core dump in case of a crash
ulimit -c unlimited
RC=0
# succeeding test must not write anything to stderr, as per DEP-8
src/tests/integration-test 2>&1 || RC=$?
if [ "$RC" -gt 128 ]; then
    # give it some time to write the core file
    sleep 10
    if [ -e core ]; then
        echo "test failed with code $RC and core dump, copying to artifacts"
        cp core ${AUTOPKGTEST_ARTIFACTS:-$ADT_ARTIFACTS}
    fi
fi
exit $RC
