#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test for sbatch --signal
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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

set file_in     "$test_dir/input"
set file_out    "$test_dir/output"
set file_prog   "$test_dir/program"
set elps_time   0
set job_id      0

#
# Cannot run the test if OverTimeLimit is set, since we test time limits.
#
regexp "($number)" [get_config_param "OverTimeLimit"] {} overtimelim
if {$overtimelim != 0} {
	skip "Cannot run this test when OverTimeLimit is set. Exiting now."
}

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

#
# Delete left-over programs and rebuild them.
#
# We will be using test1.53.prog.c since it is exactly what we need
run_command -fail "$bin_cc -o $file_prog test1.53.prog.c"

make_bash_script $file_in "
function gettime \{
	printf \"Elapsed time: %s secs\n\" \"$\[\$(date +\"%s\") - T]\"
}\

echo \"starting...\"
T=\"$\(date +%s)\"
trap \"echo Got SIGINT 2; gettime; exit 1\" 2
$srun $file_prog &
while :
do
	:
done
"

################Run sbatch with --signal=################
log_info "Start --signal test to signal steps"

spawn $sbatch -t3 --signal=2@60 -o$file_out $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	fail "sbatch did not submit job"
}

wait_for_job -fail $job_id "DONE"

set sig 0
spawn $bin_cat $file_out
expect {
	-re "Received SIGINT" {
		incr sig 1
		exp_continue
	}
	-re "Job ran for ($number) secs" {
		set elps_time $expect_out(1,string)
		incr sig 1
		exp_continue
	}
	timeout {
		fail "cat is not responding"
	}
	eof {
		wait
	}
}

if {$sig != 2} {
	fail "sbatch did not sent signal properly"
}

if {$elps_time < 59 || $elps_time > 121} {
	fail "sbatch sent signal at the wrong time"
}

# Job step gets signaled and exits. The batch script runs until timeout.
spawn $scontrol show job $job_id
expect {
	-re "JobState=TIMEOUT" {
		exp_continue
	}
	-re "JobState=" {
		fail "Bad job exit state"
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

# Remove output file so we do not get it mixed up with the new one
exec $bin_rm -f $file_out

################Run sbatch with --signal=B:################
log_info "Start --signal test to signal bash script"

set job_id 0
spawn $sbatch -t3 --signal=B:2@60 -o$file_out $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	fail "sbatch did not submit job"
}

wait_for_job -fail $job_id "DONE"

set sig 0
spawn $bin_cat $file_out
expect {
	-re "Got SIGINT 2" {
		incr sig 1
		exp_continue
	}
	-re "Elapsed time: ($number) secs" {
		set elps_time $expect_out(1,string)
		incr sig 1
		exp_continue
	}
	timeout {
		fail "cat is not responding"
	}
	eof {
		wait
	}
}

if {$sig != 2} {
	fail "sbatch did not sent signal properly"
}

if {$elps_time < 59 || $elps_time >121} {
	fail "sbatch sent signal at the wrong time"
}

# Job gets signaled and exits.
# Without job exit code of zero, it is treated as a job failure.
spawn $scontrol show job $job_id
expect {
	-re "JobState=FAILED" {
		exp_continue
	}
	-re "JobState=" {
		fail "Bad job exit state"
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}
