#!./common/bats/bin/bats
# -*-sh-*-

source ./common/test_helper_functions.bash || exit 1
source ./common/functions                  || exit 1

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

PKG=GEOPM
module=geopm
testname=perf-tools/geopm
library=libgeopm
header=geopm.h
rpm=geopm-$LMOD_FAMILY_COMPILER-$LMOD_FAMILY_MPI-ohpc

@test "[$testname] Verify $module module is loaded and matches rpm version ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    module list $module | grep "1) $module" >& .cmd_output || exit 1
    run grep $module .cmd_output
    assert_success

    # check version against rpm
    local version="$(rpm -q --queryformat='%{VERSION}\n' $rpm)"
    run cat .cmd_output
    assert_output "  1) $module/$version"

    rm -f .cmd_output
}

@test "[$testname] Verify module ${PKG}_DIR is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    DIR=${PKG}_DIR
    if [ -z ${!DIR} ];then
        flunk "${PKG}_DIR directory not defined"
    fi

    if [ ! -d ${!DIR} || -z "${!DIR}" ];then
        flunk "directory ${!DIR} does not exist"
    fi
}

# ----------
# Binaries
# ----------

@test "[$testname] Verify module ${PKG}_BIN is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    BIN=${PKG}_BIN
    if [ -z ${!BIN} ];then
        flunk "${PKG}_BIN directory not defined"
    fi

    if [ ! -d ${!BIN} || -z "${!BIN}" ];then
        flunk "directory ${!BIN} does not exist"
    fi
}

@test "[$testname] Verify availability of geopmagent binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which geopmagent
    assert_success
}

@test "[$testname] Verify availability of geopmbench binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which geopmbench
    assert_success
}

@test "[$testname] Verify availability of geopmctl binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which geopmctl
    assert_success
}

@test "[$testname] Verify availability of geopmlaunch binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which geopmlaunch
    assert_success
}

@test "[$testname] Verify availability of geopmread binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which geopmread
    assert_success
}

@test "[$testname] Verify availability of geopmwrite binary ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    run which geopmwrite
    assert_success
}

# ----------
# Man pages
# ----------

@test "[$testname] Verify availability of man page for geopm ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    DIR=${PKG}_DIR

    if [ -z ${!DIR} ];then
        flunk "${PKG}_DIR directory not defined"
    fi

    if [ ! -d ${!DIR} || -z "${!DIR}" ];then
        flunk "directory ${!DIR} does not exist"
    fi

    run man -w geopm
    assert_success
    assert_output "${!DIR}/share/man/man7/geopm.7"
}

# ----------
# Lib Tests
# ----------

@test "[$testname] Verify module ${PKG}_LIB is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    LIB=${PKG}_LIB

    if [ -z ${!LIB} ];then
        flunk "${PKG}_LIB directory not defined"
    fi

    if [ ! -d ${!LIB} ];then
        flunk "directory ${!LIB} does not exist"
    fi
}

@test "[$testname] Verify dynamic library available in ${PKG}_LIB ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    LIB=${PKG}_LIB

    if [ -z ${!LIB} ];then
        flunk "${PKG}_LIB directory not defined"
    fi

    if [ ! -s ${!LIB}/${library}.so ];then
        flunk "${library}.so does not exist"
    fi
}

@test "[$testname] Verify static library is not present in ${PKG}_LIB ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    LIB=${PKG}_LIB

    if [ -z ${!LIB} ];then
        flunk "${PKG}_LIB directory not defined"
    fi

    if [ -e ${!LIB}/${library}.a ];then
        flunk "${library}.a exists when not expecting it"
    fi
}

# --------------
# Include Tests
# --------------

@test "[$testname] Verify module ${PKG}_INC is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    INC=${PKG}_INC

    if [ -z ${!INC} ];then
        flunk "${PKG}_INC directory not defined"
    fi

    if [ ! -d ${!INC} ];then
        flunk "directory ${!INC} does not exist"
    fi
}

@test "[$testname] Verify header file is present in ${PKG}_INC ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    INC=${PKG}_INC

    if [ -z ${!INC} ];then
        flunk "${PKG}_INC directory not defined"
    fi

    if [ ! -s ${!INC}/${header} ];then
        flunk "directory $header file does not exist"
        run ls ${!INC}/${header}
    fi
}
