#!./common/bats/bin/bats

export BATS_JUNIT_CLASS=Singularity
load ./common/test_helper_functions || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

check_rms
rm=$RESOURCE_MANAGER

setup() {
    if [ -z "$MODULEPATH" ];then
        . /etc/profile.d/lmod.sh || exit 1
    fi
    if [ x$rm = "xpbspro" ];then
        . /etc/profile.d/pbs.sh || exit 1
    fi
    module purge        || exit 1
    module load singularity || exit 1
}

echo " "
echo " "
echo "-------------------------------------------------------"
echo "Runtimes: Singularity"
echo "-------------------------------------------------------"

if [ "x$DISTRO_FAMILY" == "xCentOS" ];then
    export DISTRO=centos
    export BOOTSTRAP_DEF=/opt/ohpc/pub/doc/contrib/singularity-ohpc-2.3/examples/${DISTRO}/Singularity
else
    export DISTRO=opensuse
    export BOOTSTRAP_DEF=/opt/ohpc/pub/doc/contrib/singularity-ohpc/examples/${DISTRO}/Singularity
fi

@test "[singularity] check for RPM" {
    check_if_rpm_installed "singularity${DELIM}"
}

@test "[singularity] create image" {
    singularity create /tmp/${DISTRO}.img
    assert_success
}

@test "[singularity] bootstrap image" {
    singularity bootstrap /tmp/${DISTRO}.img ${BOOTSTRAP_DEF}
    assert_success
}

@test "[singularity] exec image" {
    singularity exec /tmp/${DISTRO}.img cat /etc/os-release
    assert_success
}

@test "[singularity] exec image via $rm" {
    export -f run_serial_binary check_exe
    mv /tmp/$DISTRO.img ~ohpc-test/.
    su ohpc-test -c "run_serial_binary singularity exec ~/${DISTRO}.img cat /etc/os-release"
    assert_success

    run rm -rf ~ohpc-test/${DISTRO}.img
    assert_success
}
