#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that preemption by partition is enforced
############################################################################
# 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 part_name_1    "${test_name}_part1"
set part_name_2    "${test_name}_part2"
set nodes          ""
set file_in        "$test_dir/job_script"
set job1           "$test_dir/job_script1"
set job2           "$test_dir/job_script2"
set job_id         0
set part_id_1      0
set part_id_2      0

if {[get_config_param "PreemptType"] ne "preempt/partition_prio"} {
	skip "This test requires that PreemptType=preempt/partition_prio"
}
if {![is_super_user]} {
	skip "This test can't be run except as SlurmUser"
}

proc sub_job { part job } {
	return [submit_job -fail "-o/dev/null --exclusive -N1 -p$part $job"]
}

proc cleanup {} {
	global job_id part_id_1 part_id_2 part_name_1 part_name_2 scontrol

	cancel_job [list $job_id $part_id_1 $part_id_2]

	run_command "$scontrol delete partitionname=$part_name_1"
	run_command "$scontrol delete partitionname=$part_name_2"
}

make_bash_script $file_in "sleep 5"
make_bash_script $job1 "sleep 120"
make_bash_script $job2 "sleep 30"

# Get a list of nodes that we can use
spawn $sbatch -N1 -t1 -o/dev/null --exclusive $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not reponding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "sbatch did not submit job"
}

wait_for_job -fail $job_id "DONE"

# Identify node to use for testing
set got_node 0
spawn $scontrol show job $job_id
expect {
	-re "NodeList=($re_word_str)" {
		set nodes $expect_out(1,string)
		set got_node 1
		exp_continue
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}
if {$got_node != 1} {
	fail "No node was set for job"
}

###################### Test preempt cancel ######################
log_info "*** TEST PREEMPT CANCEL ***"

run_command "$scontrol delete partitionname=$part_name_1"
run_command "$scontrol delete partitionname=$part_name_2"
run_command -fail "$scontrol create partitionname=$part_name_1 nodes=$nodes priority=1 preemptmode=cancel"
run_command -fail "$scontrol create partitionname=$part_name_2 nodes=$nodes priority=2 preemptmode=off"

set part_id_1 [sub_job $part_name_1 $job1]
wait_for_job -fail $part_id_1 "RUNNING"

set part_id_2 [sub_job $part_name_2 $job2]
wait_for_job -fail $part_id_2 "RUNNING"

sleep 5
if {![check_job_state $part_id_1 PREEMPTED]} {
	fail "Job was not in the desired state"
}
cancel_job $part_id_2
cancel_job $part_id_1

###################### Test preempt suspend ######################
if [param_contains [get_config_param "PreemptMode"] "GANG"] {
	log_info "*** TEST PREEMPT SUSPEND ***"

	run_command -fail "$scontrol update partitionname=$part_name_1 preemptmode=suspend"
	set part_id_1 [sub_job $part_name_1 $job1]
	wait_for_job -fail $part_id_1 "RUNNING"

	set part_id_2 [sub_job $part_name_2 $job2]
	# Wait for the job to start
	wait_for_job -fail $part_id_2 "RUNNING"

	sleep 5
	if {![check_job_state $part_id_1 SUSPENDED]} {
		fail "Job was not in the desired state"
	}

	wait_for_job -fail $part_id_2 "DONE"
	wait_for_job -fail $part_id_1 "RUNNING"

	cancel_job $part_id_1
	cancel_job $part_id_2
}

###################### Test preempt requeue ######################
log_info "*** TEST PREEMPT REQUEUE ***"

run_command -fail "$scontrol update partitionname=$part_name_1 preemptmode=requeue"
set part_id_1 [sub_job $part_name_1 $job1]
wait_for_job -fail $part_id_1 "RUNNING"

set part_id_2 [sub_job $part_name_2 $job2]
wait_for_job -fail $part_id_2 "RUNNING"

sleep 5
if {![check_job_state $part_id_1 PENDING]} {
	fail "Job was not in the desired state"
}

# Wait for job 1 to be requeued
wait_for_job -fail $part_id_2 "DONE"
wait_for_job -fail $part_id_1 "RUNNING"

sleep 5
if {![check_job_state $part_id_1 RUNNING]} {
	fail "Job was not in the desired state"
}
