#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test routing all signals through slurmctld rather than slurmd
#          (undocumented --ctld 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.
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# UCRL-CODE-217948.
#
# 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.13"
set exit_code    0
set file_in      "test$test_id.input"
set file_out     "test$test_id.output"
set file_prog    "test$test_id.prog"

print_header $test_id

#
# Cannot run the test if OverTimeLimit is set, since we test time limits.
#
set overtimelim [get_over_time_limit]
if {$overtimelim != 0} {
	log_warn "Cannot run this test when OverTimeLimit is set. Exiting now."
	exit 0
}

#
# Delete left-over programs and rebuild them.
# We use our own program to get ulimit values since the output
# of the ulimit program is inconsistent across systems.
#
exec rm -f $file_prog $file_in $file_out
exec $bin_cc -O -o $file_prog ${file_prog}.c
make_bash_script $file_in "
 $srun ./$file_prog
"

#
# Submit a job so we have something to work with
#
set job_id 0
set sbatch_pid [spawn $sbatch --output=$file_out -t1 $file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job submission failed\n"
	exit 1
}
if {[wait_for_job $job_id RUNNING] != 0} {
	send_user "\nFAILURE: error starting job $job_id\n"
	cancel_job $job_id
	exit 1
}
# Allow time for poe to start the step
# This could take a while with launch/poe
sleep 30

#
# Test verbose scancel
#
set matches 0
spawn $scancel --signal=USR1 --ctld --verbose $job_id
expect {
	-re "Signal ($number) to job $job_id" {
		set sig_num $expect_out(1,string)
		if {$sig_num != 9} {
			incr matches
		} else {
			incr matches -1
		}
		exp_continue
	}
	-re "Job is in transitional state, retrying" {
		incr matches -1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scancel not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: scancel verbose option failed\n"
	set exit_code 1
}

if {[wait_for_job $job_id DONE] != 0} {
	send_user "\nFAILURE: error completing job $job_id\n"
	cancel_job $job_id
	exit 1
}

#
# Test output file
#
if {[wait_for_file $file_out] == 0} {
	set got_signal 0
	spawn cat $file_out
	expect {
		-re "ERROR" {
			send_user "\nFAILURE: Failure running test program\n"
			set exit_code 1
			exp_continue
		}
		-re "Received SIGUSR1" {
			set got_signal 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$got_signal != 1} {
		send_user "\nFAILURE: SIGUSR1 not received\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

#
# Variation of above to test signal of batch script processes
# Note that job termination sends SIGCONT/SIGTERM, hence the
# duplicate sleep call
#
exec rm -f $file_in $file_out
make_bash_script $file_in "
 echo BEGIN
 $bin_sleep 10
 $bin_sleep 10
 $bin_sleep 10
 echo FINI
"

#
# Submit a job so we have something to work with
#
set job_id 0
set sbatch_pid [spawn $sbatch --output=$file_out -t1 $file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}

if {[wait_for_job $job_id RUNNING] != 0} {
	send_user "\nFAILURE: error starting job $job_id\n"
	cancel_job $job_id
	exit 1
}
sleep 5

#
# Test verbose scancel
#
set matches 0
spawn $scancel --batch --signal=STOP --ctld --verbose $job_id
expect {
	-re "Signal ($number) to batch job $job_id" {
		set sig_num $expect_out(1,string)
		if {$sig_num != 9} {
			incr matches
		} else {
			incr matches -1
		}
		exp_continue
	}
	-re "Job is in transitional state, retrying" {
		incr matches -1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scancel not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: scancel verbose option failed\n"
	set exit_code 1
}

if {[wait_for_job $job_id DONE] != 0} {
	send_user "\nFAILURE: error completing job $job_id\n"
	cancel_job $job_id
	exit 1
}

#
# Test output file
#
if {[wait_for_file $file_out] == 0} {
	set matches 0
	spawn cat $file_out
	expect {
		-re "FINI" {
			send_user "\nFAILURE: Job not stopped\n"
			set exit_code 1
			exp_continue
		}
		-re "CANCELLED" {
			set matches 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$matches != 1} {
		send_user "\nFAILURE: Missing JOB CANCELLED error message\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

if {$exit_code == 0} {
	exec rm -f $file_in $file_out $file_prog
	send_user "\nSUCCESS\n"
}
exit $exit_code
