#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Confirms that the sbatch --array option is submitted and
#          scancel cancels the job array.
############################################################################
# Copyright (C) 2011-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 exit_code   0
set file_script "test$test_id.sh"
set file_in     "test$test_id.input"
set file_out    "test$test_id.output"
set array_id    ""
set job_id      ""
set min_task_id 5
set max_task_id 8

proc cleanup {} {
	global file_in file_out file_script

	file delete $file_in $file_out $file_script
}

set array_size [get_config_param "MaxArraySize"]
if {$array_size < $min_task_id} {
	skip "MaxArraySize is too small"
}
if {$array_size > $max_task_id} {
	set array_size $max_task_id
}

proc scontrol_check { job_id } {
	global scontrol re_word_str array_id exit_code

	spawn $scontrol show job $job_id
	expect {
		-re "JobState=($re_word_str)" {
			set tmp $expect_out(1,string)
			if {[string compare $tmp "CANCELLED"]} {
				log_error "Job was not cancelled"
				set exit_code 1
			}
			exp_continue
		}
		timeout {
			fail "scontrol not responding"
		}
		eof {
			wait
		}
	}
}

proc multi_squeue_check {job_id} {

	global squeue number array_id exit_code

	set array_in 0
	spawn $squeue -r
	expect {
		-re "$job_id\_($number\)" {
			set array_id $expect_out(1,string)
			if {$array_id != $array_in} {
				log_error "Array IDs do not match $array_id != $array_in"
				set exit_code 1
			}
			incr array_in
			exp_continue
		}
		timeout {
			fail "squeue is not responding"
		}
		eof {
			wait
		}
	}
	return $array_in
}

proc multi_scontrol_check { job_id } {
	global scontrol number array_id

	set task_cnt 0
	spawn $scontrol show job $job_id
	expect {
		-re "ArrayTaskId=($number)-($number)" {
			set task_cnt [expr $expect_out(2,string) - $expect_out(1,string) + 1]
			exp_continue
		}
		timeout {
			fail "scontrol not responding"
		}
		eof {
			wait
		}
	}

	return $task_cnt
}

############Test Starts Here###########
make_bash_script $file_script "sleep 10"

# submit a batch job with an array of $array_size
spawn $sbatch -N1 --array=0-[expr $array_size - 1] --begin=midnight --input=$file_in --output=$file_out $file_script
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		log_debug "Job $job_id was submitted"
		exp_continue
	}
	-re "error" {
		fail "sbatch did not submit jobs"
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}

# checks all the job array indexes
set task_cnt [multi_scontrol_check $job_id]
if {$task_cnt != $array_size} {
	log_error "Job count found by scontrol bad ($task_cnt != $array_size)"
	set exit_code
}

# uses squeue to check for the jobs
set job_cnt [multi_squeue_check $job_id]
if {$job_cnt != $array_size} {
	log_error "Job count found by squeue bad ($job_cnt != $array_size)"
	set exit_code 1
}

# cancel a job with a specific job array index
spawn $scancel -v $job_id\_$array_id
expect {
	-re "Terminating job" {
		exp_continue
	}
	timeout {
		fail "scancel not responding"
	}
	eof {
		wait
	}
}

# checks to see the job was cancelled
scontrol_check $job_id\_$array_id

# cancels the entire job array
spawn $scancel -v $job_id
expect {
	-re "Terminating job" {
		exp_continue
	}
	timeout {
		fail "scancel not responding"
	}
	eof {
		wait
	}
}

# checks that all the job indexes where cancelled
scontrol_check $job_id

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