#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that afternotok dependency is enforced.
#
# 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) 2014 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.37
set job_id1       0
set job_id2       0
set script        "test$test_id\_sc"
set exit_code     0

print_header $test_id

make_bash_script $script "sleep 10"

# Submit a job to depend on
spawn $sbatch -t1 -o/dev/null $script
expect {
	-re "Submitted batch job ($number)" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if { $job_id1 == 0 } {
	send_user "\nFAILURE: sbatch did not submit job\n"
	exit 1
}

if {[wait_for_job $job_id1 "RUNNING"] != 0} {
	send_user "\nFAILURE: error waiting for job $job_id1 to start\n"
	cancel_job $job_id1
	set exit_code 1
}

# Submit a job that depends on job above
spawn $sbatch -t1 -dafternotok:$job_id1 -o/dev/null $script
expect {
	-re "Submitted batch job ($number)" {
		set job_id2 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if { $job_id2 == 0 } {
	send_user "\nFAILURE: sbatch did not submit job\n"
	exit 1
}

if {[wait_for_job $job_id1 "DONE"] != 0} {
	send_user "\nFAILURE: error waiting for job $job_id1 to complete\n"
	cancel_job $job_id1
	set exit_code 1
}

# Check exit code of the first job
set match 0
spawn $scontrol show job $job_id1
expect {
	-re "ExitCode=0:0" {
		set match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if { $match != 1 } {
	send_user "\nFAILURE: Job $job_id1 did not exit with exit code 0\n"
	send_user "\nFAILURE: Could be due to small configured MinJobAge value\n"
	set exit_code 1
}

# Wait for job 2 reason to populate
sleep 10

# Check that the job with dependency is in the correct state and has correct
# reason
set match 0
spawn $squeue --job=$job_id2 -o"%t|%r" --noheader
expect {
	-re "PD|DependencyNeverSatisfied" {
		incr match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: squeue is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if { $match != 2 } {
	send_user "\nFAILURE: Job $job_id2 should be in pending state and "
	send_user "should have DependencyNeverSatisfied for a reason\n"
	send_user "\nFAILURE: Could be due to small configured MinJobAge value\n"
	set exit_code 1
}
if {[cancel_job $job_id2] != 0} {
	set exit_code 1
}

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