#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test for sbatch --signal
############################################################################
# Copyright (C) 2014 SchedMD LLC
# Written by Nathan Yee <nyee32@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

set exit_code   0
set file_in     "test$test_id\_sc"
set file_out    "test$test_id\_output"
set file_prog   "test$test_id.prog"
set elps_time   0
set job_id      0

proc cleanup {} {
	global file_prog file_in file_out

	exec rm -f $file_prog $file_in $file_out
}

#
# 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."
}

#
# Delete left-over programs and rebuild them.
#
exec $bin_rm -f $file_prog $file_in $file_out
# We will be using test1.53.prog.c since it is exactly what we need
exec $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"
}

if {[wait_for_job $job_id "DONE"] != 0} {
	log_error "Error waiting for job $job_id to complete"
	set exit_code 1
	cancel_job $job_id
}

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} {
	log_error "sbatch did not sent signal properly"
	set exit_code 1
}

if {$elps_time < 59 || $elps_time > 121} {
	log_error "sbatch sent signal at the wrong time"
	set exit_code 1
}

# 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=" {
		log_error "Bad job exit state"
		set exit_code 1
		exp_continue
	}
	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"
}

if {[wait_for_job $job_id "DONE"] != 0} {
	log_error "Error waiting for job $job_id to complete"
	set exit_code 1
	cancel_job $job_id
}

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} {
	log_error "sbatch did not sent signal properly"
	set exit_code 1
}

if {$elps_time < 59 || $elps_time >121} {
	log_error "sbatch sent signal at the wrong time"
	set exit_code 1
}

# 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=" {
		log_error "Bad job exit state"
		set exit_code 1
		exp_continue
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
