#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validates that the OR dependency option is enforced
#          when a job runs to completion
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 slow_id       0
set fast_id       0
set dep_id        0

set nb_nodes [get_partition_param [default_partition] "TotalNodes"]
if {[check_config_select "linear"]} {
	if {$nb_nodes < 2} {
		skip "This test is incompatible with select/linear and only one node"
	}
} elseif {[default_part_exclusive]} {
	if {$nb_nodes < 2} {
		skip "This test is incompatible with exclusive node allocations and only one node"
	}
}

proc cleanup {} {
	global slow_id fast_id dep_id

	cancel_job [list $slow_id $fast_id $dep_id]
}

# Submit job 1 of 3
spawn $sbatch -t3 -o /dev/null --wrap "sleep 120"
expect {
	-re "Submitted batch job ($number)" {
		set slow_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not reponding"
	}
	eof {
		wait
	}
}
if {$slow_id == 0} {
	fail "sbatch did not submit job"
}

# Submit job 2 of 3
spawn $sbatch -t3 -o /dev/null --wrap "sleep 30"
expect {
	-re "Node count specification invalid" {
		skip "Can't test with less than two nodes"
	}
	-re "Submitted batch job ($number)" {
		set fast_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not reponding"
	}
	eof {
		wait
	}
}
if {$fast_id == 0} {
	fail "sbatch did not submit job"
}

# Submit dependency job, 3 of 3
spawn $sbatch --dependency=afterok:$slow_id?afterok:$fast_id -o /dev/null --wrap "sleep 120"
expect {
	-re "Submitted batch job ($number)" {
		set dep_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not reponding"
	}
	eof {
		wait
	}
}
if {$dep_id == 0} {
	fail "sbatch did not submit job"
}

# Check that dependent job is pending
set match 0
spawn $squeue --job=$dep_id -o"%t|%r" --noheader
expect {
	-re "PD|Dependency" {
		incr match 1
		exp_continue
	}
	timeout {
		fail "squeue is not reponding"
	}
	eof {
		wait
	}
}
subtest {$match != 0} "Check that dependent job is in the pending state and has a reason of Dependency"

# Wait for the fast job to finish after submitting dependent job
subtest {![wait_for_job -fail $fast_id "DONE"]} "Wait for the fast job to finish after submitting dependent job"

# Wait for dependency job to start once the fast job is complete
subtest {![wait_for_job -fail $dep_id RUNNING]} "Wait for dependency job to start once the fast job is complete"

# Slow job should still be running
subtest [check_job_state $slow_id RUNNING] "Slow job should still be running"
