#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Confirms job array dependencies.
############################################################################
# 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 array_size    3
set top_array_task_id [expr $array_size - 1]
set job_id        0

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

if {$array_size > [get_config_param "MaxArraySize"]} {
	skip "MaxArraySize is too small"
}

#
# Submit a job array for first dependency test
#
spawn $sbatch -N1 -t1 -a 0-$top_array_task_id -o /dev/null -e /dev/null --wrap "sleep \$(( ( RANDOM % 10 ) + 1 ))"
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	fail "sbatch did not submit job"
}

#
# Submit a dependent job dependent upon a single job array element of that job array
#
set timeout $max_job_delay
set match_job_state 0
set srun_pid [spawn $srun -t1 --dependency=afterany:$job_id\_$top_array_task_id $scontrol show job $job_id\_$top_array_task_id]
expect {
	-re "JobState=COMPLETED|COMPLETING" {
		set match_job_state 1
		exp_continue
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$match_job_state != 1} {
	log_error "Dependent job not completed"
	set exit_code 1
}
if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
log_debug "So far, so good"

#
# Submit a job array for second dependency test
#
set job_id 0
spawn $sbatch -N1 -t1 -a 0-[expr $array_size - 1] -o /dev/null -e /dev/null --wrap "sleep \$(( ( RANDOM % 10 ) + 1 ))"
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	fail "sbatch did not submit job"
}

#
# Submit a dependent job dependent upon a full job array
#
set timeout $max_job_delay
set match_job_state 0
set srun_pid [spawn $srun -t1 --dependency=afterany:$job_id $scontrol show job $job_id]
expect {
	-re "JobState=COMPLETED|COMPLETING" {
		incr match_job_state
		exp_continue
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$match_job_state != $array_size} {
	fail "Dependent job not completed ($match_job_state != $array_size)"
}

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