#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test the qalter -r option of altering a job's rerunnable flag
#
# 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) 2013 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 test_id      "20.6"
set file_in      "test$test_id\.input"
set job_id       0
set exit_code    0

print_header $test_id

if {[file executable $qalter] == 0} {
	send_user "\nWARNING: $qalter does not exits\n"
	exit 0
}
if {[file executable $qsub] == 0} {
	send_user "\nWARNING: $qsub does not exits\n"
	exit 0
}

# Clean up any vestigial files
exec $bin_rm -f $file_in

make_bash_script $file_in "sleep 50"

proc check_rerun { arg job_id } {
	global scontrol exit_code

	set check_val 0
	spawn $scontrol show job $job_id
	expect {
		-re "Requeue=$arg" {
			set check_val 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$check_val != 1} {
		send_user "\nFAILURE: requeue for job $job_id was not"
		send_user " set correctly\n"
		set exit_code 1
	}
}

spawn $qsub -l walltime=1:00 -o /dev/null $file_in
expect {
	-re "($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: qsub is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job was not submitted\n"
	set exit_code 1
}

spawn $qalter -r y $job_id
expect {
	timeout {
		send_user "\nFAILURE: qalter is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

# 1 for "y" and 0 for "n"
check_rerun 1 $job_id

spawn $qalter -r n $job_id
expect {
	timeout {
		send_user "\nFAILURE: qalter is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

# 1 for "y" and 0 for "n"
check_rerun 0 $job_id

if {[cancel_job $job_id] != 0} {
	set exit_code 1
}

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