#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that the MaxSpareNodeCount 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>
# All rights reserved
############################################################################
source ./globals

set test_id      "29.8"
set node_name    ""
set file_in      "test$test_id.script"
set spare_node   0
set new_node     ""
set fail_node    ""
set job_id       0
set exit_code    0

print_header $test_id

if {![slurmctld_plug_stack_nonstop]} {
	send_user "\nWARNING: This test is only compatible when SlurmctldPlugstack includes nonstop.\n"
	exit $exit_code
}
if {![test_super_user]} {
	send_user "\nWARNING: This test is only suitable for a super user (to restore down nodes).\n"
	exit $exit_code
}

make_bash_script $file_in "$bin_sleep 100"

proc get_max_spare_node_cnt {} {
	global smd number exit_code

	set spare_node_cnt 0
	spawn $smd -c
	expect {
		-re "MaxSpareNodeCount: ($number)" {
			set spare_node_cnt $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: smd is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	return $spare_node_cnt
}

proc get_node {job_id} {
	global scontrol squeue alpha_numeric_nodelist alpha_numeric_under exit_code

	set node_list ""
	spawn $squeue -j $job_id --noheader -o NodeList=%N
	expect {
		-re "NodeList=($alpha_numeric_nodelist)" {
			set node_list $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: squeue is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	set node_name ""
	spawn $scontrol show hostnames $node_list
	expect {
		-re "($alpha_numeric_under)" {
			set node_name $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	return $node_name
}

proc reset_state {node} {
	global scontrol exit_code

	spawn $scontrol update NodeName=$node State=RESUME
	expect {
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}

#
# Submit batch script to test
#
set job_id 0
spawn $sbatch -N2 --no-kill --output=/dev/null $file_in
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
}

#
# Wait for job to start
#
if {[wait_for_job $job_id "RUNNING"] != 0} {
        send_user "\nFAILURE: waiting for job to start\n"
        cancel_job $job_id
        exit 1
}
set node_name [get_node $job_id]

set spare_node [get_max_spare_node_cnt]
for {set count 0} {$count <= $spare_node} {incr count 1} {
	set drain_match 0
	spawn $smd -d $node_name $job_id -R test
	expect {
		-re "Job $job_id node $node_name is being drained" {
			set drain_match 1
			exp_continue
		}
		timeout {
			send_user "\FAILURE: smd is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$drain_match != 1} {
		send_user "\nFAILURE: smd did not drain node\n"
	}

	set replace_match 0
	spawn $smd -r $node_name $job_id
	expect {
		-re "Job $job_id got node $node_name replaced with node ($alpha_numeric_under)" {
			set new_node $expect_out(1,string)
			set replace_match 1
			exp_continue
		}
		-re "Job has reached MaxSpareNodeCount limit" {
			send_user "\nDo not worry this error is expected\n"
			set replace_match 2
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: smd is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$replace_match == 0} {
		send_user "\nFAILURE: smd did not replace node or smd did not reach MaxSpareNodeLimit when it should have\n"
		set exit_code 1
	}

	reset_state $node_name
	set node_name $new_node
	if {$exit_code != 0 || $replace_match == 2} {
		break
	}
}

spawn $sinfo --noheader -o "State=%T NodeName=%N"
expect {
	-re "State=fail NodeName=($alpha_numeric_nodelist)" {
		set fail_node $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sinfo is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

cancel_job $job_id

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


