#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test scancel signal of full job including batch script (--full option).
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
# Note:    This script generates and then deletes files in the working directory
#          named test6.12.input, test6.12.output, and test6.12.error
############################################################################
# Copyright (C) 2015 SchedMD LLC.
# Written by Morris Jette <jette@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 test_id     "6.15"
set file_in1    "test$test_id.input1"
set file_in2    "test$test_id.input2"
set file_out    "test$test_id.output"

set exit_code   0
set job_id      0
set step_id	0

print_header $test_id

#
# Delete vestigial files
#
file delete $file_out

#
# Build input script files
#
make_bash_script $file_in1 "
$bin_sleep 5
$bin_sleep 5
$bin_sleep 5
$bin_sleep 5
"

make_bash_script $file_in2 "
time ./$file_in1 &
time $srun $file_in1 &
wait
"

#
# Spawn a sbatch job
#
set timeout $max_job_delay
set sbatch_pid [spawn $sbatch --output=$file_out -t2 $file_in2]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		slow_kill $sbatch_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to start running, then STOP it for a while and CONTINUE
#
if {[wait_for_job $job_id "RUNNING"] != 0} {
	send_user "\nFAILURE: waiting for job to start running\n"
	exit 1
}
exec $bin_sleep 2

send_user "\nStop the full job\n"
spawn $scancel --full -s STOP $job_id
expect {
	eof {
		wait
	}
}
send_user "\nWait a while\n"
exec $bin_sleep 20
send_user "\nNow resume the job\n"
spawn $scancel --full -s CONT $job_id
expect {
	eof {
		wait
	}
}

#
# Wait for completion and confirm run times
#
if {[wait_for_job $job_id DONE] != 0} {
	send_user "\nFAILURE: error completing job $job_id\n"
	cancel_job $job_id
	exit 1
}

# Now make sure that both the script and the step processes are stopped.
# Both should run for at least 35 seconds (20 seconds between SIGSTOP and
# SIGCONT) plus 15 to 20 seconds for $file_in1.
if {[wait_for_file $file_out] == 0} {
	set matches 0
	set real_time 0
	spawn cat $file_out
	expect {
		-re "real($whitespace)0m($number)" {
			set real_time $expect_out(2,string)
			if {$real_time < 35} {
				send_user "\nFAILURE: Job not stopped\n"
				set exit_code 1
			}
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$matches < 2} {
		send_user "\nFAILURE: Missing run time messages ($matches < 2)\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
	exec $bin_rm -f $file_in1 $file_in2 $file_out
}
exit $exit_code
