#! /bin/bash
# hpc-testing
# Copyright (C) 2025 SUSE LLC
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

test_libfabric()
{
    local host=$1
    local server_ip=$2
    local client_ip=$3

    local cmd="/usr/bin/runfabtests.sh"
    # runfabtest is broken at least up to 2.5.0.
    # When a client exits with ENOSYS/ENODATA, the server sometime stays up
    # runfabtest tries to kill it, but it usually ends up killing ssh and not the spawnerd process.
    # This causes mayhem in the next run as we have options mismatch
    # Use this weird macro to replace the kill -9 by a
    # kill -9 + cleanup + ignore exit code and reuse the client one
    local -r cleanedup_cmd='bash <(sed '\''/^[[:space:]]*kill -9 \$s_pid/,/s_ret=\$\?/c\
            kill -9 $s_pid 2> /dev/null\
            cleanup\
            s_ret=$c_ret\
        else\
            wait $s_pid\
            s_ret=$?\
        fi'\'' /usr/bin/runfabtests.sh)'
    local run_verbs=1
    local verbs_options=""

    local run_verbs_rxm=0
    local verbs_rxm_options=""

    case $(get_suse_version $host) in
	15|15.1|15.2|15.3|15.4|15.5)
	    verbs_options="-e fi_rma_bw,fi_rdm_cntr_pingpong"
	    ;;
	15.6)
	    verbs_options=""
	    ;;
        # Not fi_rma_bw actually works, but some of its settings trigger the expected -ENODATA
        # and fabtests handles it badly, leaving the server running and causing mayhem on the next run
	15.7|16.0|2025*)
	    verbs_options="-e fi_dgram_pingpong"
            cmd=$cleanedup_cmd
	    ;;
        2026*|16.1)
	    verbs_options="-e fi_dgram_pingpong"
            run_verbs_rxm=1
	    verbs_rxm_options="-e fi_dgram_pingpong"
            cmd=$cleanedup_cmd
	    ;;
	*)
	    # N/A
	    true
	    ;;
    esac
    if [ $run_verbs -ne 0 ]; then
        tp $host "${cmd} -v -t short ${verbs_options} verbs $server_ip $client_ip"
    fi
    if [ $run_verbs_rxm -ne 0 ]; then
        tp $host "${cmd} -v -t short ${verbs_rxm_options} 'verbs;ofi_rxm' $server_ip $client_ip"
    fi
}

