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

proc cleanup {} {
	global bin_rm script

	exec $bin_rm -f $script
}

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 {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if { $job_id1 == 0 } {
	fail "sbatch did not submit job"
}

if {[wait_for_job $job_id1 "RUNNING"] != 0} {
	log_error "error waiting for job $job_id1 to start"
	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 {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if { $job_id2 == 0 } {
	fail "sbatch did not submit job"
}

if {[wait_for_job $job_id1 "DONE"] != 0} {
	log_error "error waiting for job $job_id1 to complete"
	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 {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

if { $match != 1 } {
	log_error "Job $job_id1 did not exit with exit code 0, which could be due to a small configured MinJobAge value"
	set exit_code 1
}

# Check that the job with dependency is in the correct state and has correct
# reason
set match 0
if {[param_contains [get_config_param "DependencyParameters"] "kill_invalid_depend"]} {
	spawn $scontrol show job $job_id2
	expect {
		-re "JobState=CANCELLED Reason=Dependency Dependency=afternotok:${job_id1}" {
			incr match 1
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
		if { $match != 1 } {
			log_error "Job $job_id2 should be in cancelled state and should have Reason=DependencyNeverSatisfied"
			set exit_code 1
		}
	}
} else {
	# Wait for job 2 reason to populate
	sleep 10
	spawn $squeue --job=$job_id2 -o"%t|%r" --noheader
	expect {
		-re "PD|DependencyNeverSatisfied" {
			incr match 1
		}
		timeout {
			fail "squeue is not responding"
		}
		eof {
			wait
		}
	}
	if { $match != 1 } {
		log_error "Job $job_id2 should be in pending state and should have Reason=DependencyNeverSatisfied, which could be due to a small configured MinJobAge value"
		set exit_code 1
	}
	if {[cancel_job $job_id2] != 0} {
		set exit_code 1
	}
}

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