#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test scancel signal option (--signal and --verbose options).
############################################################################
# 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>
# CODE-OCEC-09-009. All rights reserved.
#
# 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_out2    "$test_dir/output2"
set file_prog    "$test_name.prog"
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"
}

proc cleanup {} {
	global job_id file_prog

	cancel_job $job_id
	file delete $file_prog
}

#
# 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.
#
cleanup
run_command -fail "$bin_cc -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 [submit_job -fail "--output=$file_out -t1 $file_in"]

# Wait for the prog.c to be WAITING for signals
wait_for_file -fail $file_out
wait_for_command_match -fail "$bin_cat $file_out" "WAITING"

#
# Test verbose scancel
#
set matches 0
spawn $scancel --signal=USR1 --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 {
		fail "scancel not responding"
	}
	eof {
		wait
	}
}
subtest {$matches == 1} "Test scancel with --signal=USR1"

wait_for_job -fail $job_id DONE

#
# Test output file
#
wait_for_file -fail $file_out
set got_signal 0
spawn cat $file_out
expect {
	-re "ERROR" {
		fail "Failure running test program"
	}
	-re "Received SIGUSR1" {
		set got_signal 1
		exp_continue
	}
	eof {
		wait
	}
}
subtest {$got_signal == 1} "Test output file for SIGUSR1"

#
# 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 "
 $bin_date
 $bin_sleep 10
 $bin_date
 $bin_sleep 10
 $bin_date
 echo FINI
"

#
# Submit a job so we have something to work with
#
set job_id [submit_job -fail "--output=$file_out2 -t1 $file_in"]

wait_for_job -fail $job_id RUNNING
sleep 5

#
# Test verbose scancel
#
set matches 0
spawn $scancel --batch --signal=STOP --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 {
		fail "scancel not responding"
	}
	eof {
		wait
	}
}
subtest {$matches == 1} "Test scancel with --signal=STOP"

wait_for_job -fail $job_id DONE

#
# Test output file
#
wait_for_file -fail $file_out2
spawn cat $file_out2
expect {
	-re "FINI" {
		fail "Job not stopped"
	}
	eof {
		wait
	}
}

