#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test routing all signals through slurmctld rather than slurmd
#          (undocumented --ctld option).
############################################################################
# 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 exit_code    0
set file_in      "test$test_id.input"
set file_out     "test$test_id.output"
set file_prog    "test$test_id.prog"

proc cleanup {} {
	global file_in file_out file_prog

	exec rm -f $file_in $file_out $file_prog
}

#
# 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.
# 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 {
		slow_kill $sbatch_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "Job submission failed"
}
if {[wait_for_job $job_id RUNNING] != 0} {
	cancel_job $job_id
	fail "Error starting job $job_id"
}
# 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 {
		fail "scancel not responding"
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	log_error "scancel verbose option failed"
	set exit_code 1
}

if {[wait_for_job $job_id DONE] != 0} {
	cancel_job $job_id
	fail "Error completing job $job_id"
}

#
# Test output file
#
if {[wait_for_file $file_out] == 0} {
	set got_signal 0
	spawn cat $file_out
	expect {
		-re "ERROR" {
			log_error "Failure running test program"
			set exit_code 1
			exp_continue
		}
		-re "Received SIGUSR1" {
			set got_signal 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$got_signal != 1} {
		log_error "SIGUSR1 not received"
		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 {
		slow_kill $sbatch_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}

if {[wait_for_job $job_id RUNNING] != 0} {
	cancel_job $job_id
	fail "Error starting job $job_id"
}
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 {
		fail "scancel not responding"
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	log_error "scancel verbose option failed"
	set exit_code 1
}

if {[wait_for_job $job_id DONE] != 0} {
	cancel_job $job_id
	fail "Error completing job $job_id"
}

#
# Test output file
#
if {[wait_for_file $file_out] == 0} {
	set matches 0
	spawn cat $file_out
	expect {
		-re "FINI" {
			log_error "Job not stopped"
			set exit_code 1
			exp_continue
		}
		-re "CANCELLED" {
			set matches 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$matches != 1} {
		fail "Missing JOB CANCELLED error message"
	}
} else {
	fail "Output file ($file_out) did not appear"
}

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