#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Validate that preemption by partition 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) 2011-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        34.1
set exit_code      0
set part_name_1    "test$test_id\_part1"
set part_name_2    "test$test_id\_part2"
set part_1_id      0
set job1           "test$test_id\_job1_sc"
set part_2_id      0
set job2           "test$test_id\_job2_sc"
set job_id         0
set nodes          ""
set file_in        "test$test_id\_sc"

print_header $test_id

if {[test_preempttype_part] == 0} {
	send_user "\nWARNING: This test requires that "
	send_user "PreemptType=preempt/partition_prio\n"
	exit $exit_code
}

proc create_part { part nodes priority pre_type } {

	global sinfo scontrol exit_code

	spawn $scontrol create partitionname=$part nodes=$nodes priority=$priority preemptmode=$pre_type
	expect {
		-re "invalid user" {
			send_user "\nWARNING: User not authorized to run this test\n"
			exit 0
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	log_user 0
	set match 0
	spawn $sinfo --noheader -o%P
	expect {
		-re "$part" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sinfo is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}

	}
	log_user 1

	if { $match != 1 } {
		send_user "\nFAILURE: scontrol did not create partition $part\n"
		set exit_code 1
	}
}

proc mod_part { part pre_type } {

	global sinfo scontrol exit_code

	spawn $scontrol update partitionname=$part preemptmode=$pre_type
	expect {
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	log_user 0
	set match 0
	spawn $sinfo --noheader -o%P
	expect {
		-re "$part" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sinfo is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}

	}
	log_user 1

	if { $match != 1 } {
		send_user "\nFAILURE: scontrol did not create partition $part\n"
		set exit_code 1
	}

}

proc delete_part { part } {

	global sinfo scontrol exit_code

	spawn $scontrol delete partitionname=$part
	expect {
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	log_user 0
	set match 0
	spawn $sinfo --noheader -o%P
	expect {
		-re "$part" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sinfo is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}

	}
	log_user 1

	if { $match == 1 } {
		send_user "\nFAILURE: scontrol did not delete partition $part\n"
		set exit_code 1
	}

}

proc sub_job { part job } {

	global sbatch number exit_code

	set job_id 0
	spawn $sbatch -o/dev/null --exclusive -N1 -p$part $job
	expect {
		-re "Submitted batch job ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sbatch is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if { $job_id == 0 } {
		send_user "\nFAILURE: sbatch did not submit job\n"
		cleanup 1
	} else {
		return $job_id
	}
}

proc check_state { exp_state job_id } {

	global scontrol exit_code

	# Wait a while for the jobs to change states
	sleep 5

	set state 0
	spawn $scontrol show job $job_id
	expect {
		-re "JobState=$exp_state" {
			set state 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if { $state != 1 } {
		send_user "\nFAILURE: job $job_id is not in state $exp_state\n"
		set exit_code 1
	}
}

proc clean_up { rc } {
	global part_id_1 part_id_2 part_name_1 part_name_2

	cancel_job $part_id_1
	cancel_job $part_id_2

	delete_part $part_name_1
	delete_part $part_name_2

	if {$rc != 0} {
		exit $rc
	}
}

if {[is_super_user] == 0} {
	send_user "\nWARNING: This test can't be run except as SlurmUser\n"
	exit 0
}

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

if { [wait_for_job $job_id DONE] != 0} {
	cancel_job $job_id
	exit 1
}

# Identify node to use for testing
set got_node 0
spawn $scontrol show job $job_id
expect {
	-re "NodeList=($alpha_numeric_nodelist)" {
		set nodes $expect_out(1,string)
		set got_node 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$got_node != 1} {
	send_user "\nFAILURE: no node was set for job\n"
	exit 1
}

###################### Test preempt cancel ######################
send_user "\n*** TEST PREEMPT CANCEL ***\n"

delete_part $part_name_1
delete_part $part_name_2
create_part $part_name_1 $nodes 1 cancel
create_part $part_name_2 $nodes 2 off

set part_id_1 [sub_job $part_name_1 $job1]
if { [wait_for_job $part_id_1 RUNNING] != 0} {
	send_user "\nFAILURE: job $part_id_1 failed to start\n"
	clean_up 1
}

set part_id_2 [sub_job $part_name_2 $job2]
if { [wait_for_job $part_id_2 RUNNING] != 0} {
	send_user "\nFAILURE: job $part_id_1 failed to start\n"
	clean_up 1
}

check_state PREEMPTED $part_id_1
cancel_job $part_id_2
cancel_job $part_id_1

###################### Test preempt suspend ######################
if { [test_gang] == 1 } {
	send_user "\n*** TEST PREEMPT SUSPEND ***\n"

	mod_part $part_name_1 suspend
	set part_id_1 [sub_job $part_name_1 $job1]
	if { [wait_for_job $part_id_1 RUNNING] != 0} {
		send_user "\nFAILURE: job $part_id_1 failed to start\n"
		clean_up 1
	}

	set part_id_2 [sub_job $part_name_2 $job2]
	# Wait for the job to start
	if { [wait_for_job $part_id_2 RUNNING] != 0} {
		send_user "\nFAILURE: job $part_id_2 failed to start\n"
		clean_up 1
	}

	check_state SUSPENDED $part_id_1

	if { [wait_for_job $part_id_2 DONE] != 0} {
		send_user "\nFAILURE: job $part_id_2 failed to complete\n"
		clean_up 1
	}
	if { [wait_for_job $part_id_1 RUNNING] != 0} {
		send_user "\nFAILURE: job $part_id_1 failed to start\n"
		clean_up 1
	}

	cancel_job $part_id_1
	cancel_job $part_id_2
}

###################### Test preempt requeue ######################
send_user "\n*** TEST PREEMPT REQUEUE ***\n"

mod_part $part_name_1 requeue
set part_id_1 [sub_job $part_name_1 $job1]
if { [wait_for_job $part_id_1 RUNNING] != 0} {
	send_user "\nFAILURE: job $part_id_1 failed to start\n"
	clean_up 1
}

set part_id_2 [sub_job $part_name_2 $job2]
if { [wait_for_job $part_id_2 RUNNING] != 0} {
	send_user "\nFAILURE: job $part_id_2 failed to start\n"
	clean_up 1
}

check_state PENDING $part_id_1

# Wait for job 1 to be requeued
wait_for_job $part_id_2 DONE
wait_for_job $part_id_1 RUNNING
check_state RUNNING $part_id_1

clean_up 0

if {$exit_code == 0} {
	exec $bin_rm $job1 $job2 $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
