#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test for seff
############################################################################
# Copyright (C) 2020 SchedMD LLC
# Written by Marcin Stolarek <cinek@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm 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 2 of the License, or (at your option)
# any later version.
#
# Slurm 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 Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

if {[file executable $seff] == 0} {
	skip "seff was not found"
}

set out [run_command_output -fail -xfail "$seff -h"]
subtest {[regexp "Usage:" $out]} "seff -h should return Usage:"

set job_id [submit_job -fail "-H --wrap=\"sleep 10\" -o /dev/null -e /dev/null"]
wait_for_command_match "$sacct -n -j $job_id" "PENDING"
set out [run_command_output -fail "$seff $job_id" ]
subtest {[regexp "State: PENDING" $out]} "Job should be returned as in PENDING state by seff"
subtest {[regexp "Cores: 1" $out]} "Job should be reported as 1 core"
subtest {[regexp "Efficiency not available for jobs in the PENDING state." $out]} "Job should be reported as efficiency not available for PENDING state"

run_command -fail "$scontrol release $job_id"
wait_for_command_match  "$sacct -X -n -j $job_id" "RUNNING"

set out [run_command_output -fail "$seff $job_id"]
subtest {[regexp "State: RUNNING"  $out]} "Job should be reported as RUNNING by seff"
subtest {[regexp "WARNING: Efficiency statistics may be misleading for RUNNING jobs." $out]} "seff output should contain warning about staistics for RUNNING job"

wait_for_command_match "$sacct -X -n -j $job_id" "COMPLETED"
set out [run_command_output -fail "$seff $job_id"]
subtest {[regexp "State: COMPLETED"  $out]} "Job should be reported as COMPLETED by seff"
subtest {[regexp "Cores: \(\\d+\)" $out {} cores] || \
         [regexp "Cores per node: \(\\d+\)" $out {} cores]} "seff should report the number of cores (per node) based on the configuration"
# On select/linear or other configs, cores could be > 1
if {$cores > 5} {
	subskip "Not subtesting CPU Efficency if Cores ($cores) > 5"
} else {
	subtest {[regexp -line "CPU Efficiency: 0.00% of 00:00:$cores\\d core-walltime" $out]} "Job efficiency should be 0.00% of >=10s*Cores core-walltime"
}
