#!/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) 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 slow_id       0
set fast_id       0
set dep_id        0
set exit_code     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"
	}
}

# 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
	}
}
if {$match == 0} {
	log_error "Job $dep_id is in the wrong state should be state PENDING and have REASON Dependency"
	set exit_code 1
}

# Wait for the fast job to finish after submitting dependent job
if {[wait_for_job $fast_id "DONE"] != 0} {
	log_error "Error waiting for job $fast_id to complete"
	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]} {
	log_error "Job $dep_id should be running"
	set exit_code 1
}

if {![check_job_state $dep_id RUNNING]} {
	log_error "Job was not on the desired state"
	set exit_code 1
}

# Slow job should still be running
if {![check_job_state $slow_id RUNNING]} {
	log_error "Job was not on the desired state"
	set exit_code 1
}

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

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