#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Validate that sacct -D shows correct job steps and states
#          when a job is requeued
#
# 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) 2014 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    12.7
set exit_code  0
set job_id     0
set node       ""
set file_in    "test$test_id\_sc"

print_header $test_id

proc mod_state { state } {

	global scontrol node exit_code

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

proc check_step { num } {

	global sacct job_id exit_code
	set steps 0
	spawn $sacct --job=$job_id\.batch -D --start=today --noheader --format=jobid -P
	expect {
		-re "batch" {
			incr steps
			send_user "got here with $steps\n"
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacct is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}

	}

	if {$num != $steps} {
		send_user "\nFAILURE: found $steps step(s) when expecting "
		send_user "$num steps\n"
		set exit_code 1
	}
}

proc check_sacct_states { states log_it } {
	global job_id sacct

	log_user $log_it
	set state_num 0
	if { $log_it == 1 } {
		spawn $sacct --job=$job_id --duplicates --parsable2 --start=today
	} else {
		spawn $sacct --job=$job_id --duplicates --parsable2 --start=today --noheader --format=state
	}
	expect {
		-re ($states) {
			incr state_num
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacct is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	log_user 1

	return $state_num
}

if {[test_using_slurmdbd] != 1} {
	send_user "\nWARNING: not using accounting_storage/slurmdbd\n"
	exit 0
}

make_bash_script $file_in "sleep 2"

# Start a batch job to get a  usable node
spawn $sbatch -t1 --exclusive -o/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"
	exit 1
}

wait_for_job $job_id RUNNING

set found 0
spawn $scontrol show job $job_id
expect {
	-re "NodeList=($alpha_numeric_nodelist)" {
		set node $expect_out(1,string)
		set found 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$found != 1} {
	send_user "\nFAILURE: was not able to get usable node\n"
	exit 1
}

cancel_job $job_id

make_bash_script $file_in "sleep 20"

# Submit job to be requeued
set job_id 0
spawn $sbatch -N1 -w$node --exclusive -o/dev/null --requeue $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"
	exit 1
}

wait_for_job $job_id RUNNING

# Set the node that the job is running on to down
mod_state "down"

# Wait a little bit for node state to change
sleep 5

# Set the node back to resume
mod_state "resume"

# Check the number of steps
check_step 0

# Check the job state
send_user "\n\nTest 1\n"
wait_for_job $job_id PENDING
set fail_count [check_sacct_states "NODE_FAIL" 1]
if {$fail_count != 1} {
	send_user "\nFAILURE: Bad NODE_FAIL count ($fail_count != 1)\n"
	set exit_code 1
}
set pend_count [check_sacct_states "PENDING" 0]
if {$pend_count != 1} {
	send_user "\nFAILURE: Bad PENDING count ($pend_count != 1)\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "So far, so good\n\n"
}

wait_for_job $job_id RUNNING

# Check the number of steps after job is running
check_step 0
send_user "\n\nTest 2\n"
set fail_count [check_sacct_states "NODE_FAIL" 1]
if {$fail_count != 1} {
	send_user "\nFAILURE: Bad NODE_FAIL count ($fail_count != 1)\n"
	set exit_code 1
}
set run_count [check_sacct_states "RUNNING" 0]
if {$run_count != 1} {
	send_user "\nFAILURE: Bad RUNNING count ($run_count != 1)\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "So far, so good\n\n"
}

# Requeue the job
spawn $scontrol requeue $job_id
expect {
	timeout {
		send_user "\nFAILURE: scontrol is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

# Wait a bit for the job to be requeued then check its state
sleep 8
send_user "\n\nTest 3\n"
set fail_count [check_sacct_states "NODE_FAIL" 1]
if {$fail_count != 1} {
	send_user "\nFAILURE: Bad NODE_FAIL count ($fail_count != 1)\n"
	set exit_code 1
}
set req_count [check_sacct_states "REQUEUE" 0]
if {$req_count != 1} {
	send_user "\nFAILURE: Bad REQUEUE count ($req_count != 1)\n"
	set exit_code 1
}
set canc_count [check_sacct_states "CANCELLED" 0]
if {$canc_count != 1} {
	send_user "\nFAILURE: Bad CANCELLED count ($canc_count != 1)\n"
	set exit_code 1
}
set pend_count [check_sacct_states "PENDING" 0]
if {$pend_count != 1} {
	send_user "\nFAILURE: Bad PENDING count ($pend_count != 1)\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "So far, so good\n\n"
}

wait_for_job $job_id RUNNING

# Check for steps after requeue
check_step 1
send_user "\n\nTest 4\n"
set fail_count [check_sacct_states "NODE_FAIL" 1]
if {$fail_count != 1} {
	send_user "\nFAILURE: Bad NODE_FAIL count ($fail_count != 1)\n"
	set exit_code 1
}
set req_count [check_sacct_states "REQUEUE" 0]
if {$req_count != 1} {
	send_user "\nFAILURE: Bad REQUEUE count ($req_count != 1)\n"
	set exit_code 1
}
set canc_count [check_sacct_states "CANCELLED" 0]
if {$canc_count != 1} {
	send_user "\nFAILURE: Bad CANCELLED count ($canc_count != 1)\n"
	set exit_code 1
}
set run_count [check_sacct_states "RUNNING" 0]
if {$run_count != 1} {
	send_user "\nFAILURE: Bad RUNNING count ($run_count != 1)\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "So far, so good\n\n"
}

wait_for_job $job_id DONE

# Check steps after job has completed
check_step 2
send_user "\n\nTest 5\n"
set fail_count [check_sacct_states "NODE_FAIL" 1]
if {$fail_count != 1} {
	send_user "\nFAILURE: Bad NODE_FAIL count ($fail_count != 1)\n"
	set exit_code 1
}
set req_count [check_sacct_states "REQUEUE" 0]
if {$req_count != 1} {
	send_user "\nFAILURE: Bad REQUEUE count ($req_count != 1)\n"
	set exit_code 1
}
set canc_count [check_sacct_states "CANCELLED" 0]
if {$canc_count != 1} {
	send_user "\nFAILURE: Bad CANCELLED count ($canc_count != 1)\n"
	set exit_code 1
}
set comp_count [check_sacct_states "COMPLETED" 0]
if {$comp_count != 2} {
	send_user "\nFAILURE: Bad COMPLETED count ($comp_count != 2)\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "So far, so good\n\n"
}

cancel_job $job_id

if {$exit_code == 0} {
	exec $bin_rm $file_in
	send_user "\nSUCCCESS\n"
}
exit $exit_code
