#!/usr/bin/env -S bats --report-formatter junit --formatter tap
# -*-sh-*-

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

NODES=2
TASKS=`tasks_count 2`
ARGS=8

@test "[libs/OpenCoarrays] hello_multiverse binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s hello ];then
	flunk "hello binary does not exist"
    fi

    if echo "$LMOD_FAMILY_MPI" | grep -q mvapich ; then
	export MV2_ENABLE_AFFINITY=0
    fi

    if [[ "$LMOD_FAMILY_MPI" == "mpich" ]];then
	export FI_PROVIDER=sockets
    fi

    if [[ "$LMOD_FAMILY_MPI" =~ "openmpi" ]];then
        export OMPI_MCA_osc="^ucx"
        export OMPI_MCA_pml="^ucx"
    fi

    if [ "$LMOD_FAMILY_MPI" != "impi" ];then
    
	run run_mpi_binary ./hello "" $NODES $TASKS
	assert_success
    fi

}

@test "[libs/OpenCoarrays] hello_multiverse binary runs under resource manager using cafrun script ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    skip "cafrun not supported for all MPI variants"
    if [ ! -s hello ];then
		flunk "hello binary does not exist"
    fi

    if echo "$LMOD_FAMILY_MPI" | grep -q mvapich ; then
		export MV2_ENABLE_AFFINITY=0
    fi

    if [ "$LMOD_FAMILY_MPI" != "impi" ];then
		cafcmd="cafrun -np $TASKS ./hello"

		if [ "$rm" = "slurm" ];then
			srun $cafcmd
			assert_success
		elif [ "$rm" = "openpbs" ];then
			qsub -W block=true $cafcmd
			assert_success
		fi
	fi

}

