#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that a job is held and reason is set to launch
#          failed requeued held when the prolog fails
#
# 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) 2015 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <http://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         31.2
set exit_code       0
set good_prolog     "test$test_id\_good.sh"
set bad_prolog      "test$test_id\_bad.sh"
set test_node       [get_idle_node_in_part [default_partition]]
set job_id          0
set test_script     "test$test_id\.sh"

print_header $test_id

if { [test_super_user] == 0 } {
	send_user "WARNING: Test can only be run as SlurmUser\n"
	exit $exit_code
}
if {[test_front_end] != 0} {
        send_user "\nWARNING: This test is incompatible with front-end systems\n"
        exit 0
}

proc clean_up { } {

	global cwd config_dir exit_code bin_rm bin_cp good_prolog bad_prolog
	global scontrol test_node test_script

	exec $bin_cp -v $cwd/slurm.conf.orig $config_dir/slurm.conf
	exec $bin_rm $test_script $good_prolog $bad_prolog $cwd/slurm.conf.orig
	reconfigure

	spawn $scontrol update node=$test_node state=idle
	expect {
		timeout {
			send_user "\nFAILURE: scontrol is not respinding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}

make_bash_script $good_prolog "
exit 0
"
make_bash_script $bad_prolog "
exit 1
"
make_bash_script $test_script "
sleep 20
"

# Get the location of the slurm.conf file
set config_dir [get_conf_path]
if { $exit_code != 0 } {
	exit $exit_code
}

#
# Copy slurm.conf file
#
set cwd [$bin_pwd]
copy_conf $config_dir $cwd
if { $exit_code != 0 } {
	exit $exit_code
}

# Ensure that Prolog and nohold_on_prolog_fail params are removed
exec $bin_sed -i {s/^\(Prolog=\)/#\1/gI} $config_dir/slurm.conf
exec $bin_sed -i {s/nohold_on_prolog_fail//gI} $config_dir/slurm.conf

# Append Prolog config to the slurm.conf
exec $bin_echo "Prolog=$cwd/$good_prolog" >> $config_dir/slurm.conf
reconfigure

spawn $sbatch -t1 -N1 -w$test_node --exclusive -o/dev/null $test_script
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"
	set exit_code 1
}
if {$exit_code} {
	cancel_job $job_id
	clean_up
	exit $exit_code
}

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

# Check that the job was submitted with no error
set match 0
spawn $squeue -h -j$job_id -o%t|%r
expect {
	-re "R.None" {
		set match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: squeue is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$match == 0} {
	send_user "\nFAILURE: job $job_id is not in the correct state."
	send_user "Job $job_id should be Running and have reason: None\n"
	set exit_code 1
}

cancel_job $job_id

if {$exit_code} {
	clean_up
	exit $exit_code;
}

exec $bin_sed -i {s/^\(Prolog=\)/#\1/gI} $config_dir/slurm.conf

# Append Prolog config to the slurm.conf
exec $bin_echo "Prolog=$cwd/$bad_prolog" >> $config_dir/slurm.conf
reconfigure

set job_id 0
spawn $sbatch -t1 -N1 -w$test_node --exclusive -o/dev/null $test_script
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} {
	set exit_code 1
}
if {$exit_code} {
	send_user "\nFAILURE: sbatch did not submit job\n"
	clean_up
	exit $exit_code
}


# Wait a bit for the pending reason to be set
sleep 10

# Check that the job is held due to the failed prolog or JobHeldAdmin
# Either state is possible due to race conditions
set cleaning 0
set match 0
spawn $squeue -h -j$job_id -o%t|%r
expect {
	-re "PD.Cleaning" {
		set cleaning 1
		exp_continue
	}
	-re "PD.launch failed requeued held" {
		set match 1
		exp_continue
	}
	-re "PD.JobHeldAdmin" {
		set match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: squeue is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$cleaning == 1} {
	send_user "\nWARNING: Waiting longer for job cleanup\n"
	sleep 30
	set match 0
	spawn $squeue -h -j$job_id -o%t|%r
	expect {
		-re "PD.launch failed requeued held" {
			set match 1
			exp_continue
		}
		-re "PD.JobHeldAdmin" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: squeue is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}
if {$match == 0} {
	send_user "\nFAILURE: job $job_id is not in the correct state. "
	send_user "Job $job_id should be Pending and have reason: "
	send_user "\"launch failed requeued held\" or \"JobHeldAdmin\"\n"
	set exit_code 1
}

if {$exit_code} {
	cancel_job $job_id
	clean_up
	exit $exit_code
}

# Check that the node that the job ran on is in fact drained
set match 0
spawn $sinfo -h -o%T -n$test_node
expect {
	-re "drained" {
		set match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sinfo is not repsonding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$match == 0} {
	send_user "\nFAILURE: node $test_node was not drained when "
	send_user "it should be\n"
	set exit_code 1
}

cancel_job $job_id
clean_up

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
