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

print_header $test_id

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
if {[test_linear]} {
	if {$nb_nodes < 2} {
		send_user "\nWARNING: This test is incompatible with select/linear and only one node\n"
		exit $exit_code
	}
} elseif {[default_part_exclusive]} {
	if {$nb_nodes < 2} {
		send_user "\nWARNING: This test is incompatible with exclusive node allocations and only one node\n"
		exit $exit_code
	}
}

# 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 {
		send_user "\nFAILURE: sbatch is not reponding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$slow_id == 0} {
	send_user "\nFAILURE: sbatch did not submit job\n"
	exit 1
}

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

# 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 {
		send_user "\nFAILURE: sbatch is not reponding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$dep_id == 0} {
	send_user "\nFAILURE: sbatch did not submit job\n"
	exit 1
}

# 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 {
		send_user "\nFAILURE: squeue is not reponding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$match == 0} {
	send_user "\nFAILURE: job $dep_id is in the wrong state should be "
	send_user "state PENDING and have REASON Dependency\n"
	set exit_code 1
}

# Wait for the fast job to finish after submitting dependent job
if {[wait_for_job $fast_id "DONE"] != 0} {
	send_user "\nFAILURE: error waiting for job $fast_id to complete\n"
	set exit_code 1
	cancel_job $fast_id
}

# Wait for dependency job to start once the fast job is complete
if {[wait_for_job $dep_id RUNNING]} {
	send_user "\nFAILURE: job $dep_id should be running\n"
	set exit_code 1
}

check_job_state $dep_id RUNNING

# Slow job should still be running
check_job_state $slow_id RUNNING

# Cancel leftover jobs
cancel_job $fast_id
cancel_job $slow_id
cancel_job $dep_id

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
