#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Confirm node selection from within a job step on existing allocation
#          (--relative, --nodes and --nprocs options).
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Copyright (C) 2008 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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     "1.87"
set exit_code   0
set file_in     "test$test_id.input"
set prompt      "QA_PROMPT: "

print_header $test_id

if {[test_front_end] != 0} {
	send_user "\nWARNING: This test is incompatible with front-end systems\n"
	exit 0
}

# find out if we have enough nodes to test functionality
set partition [default_partition]
set node_count [get_node_cnt_in_part $partition]
if { $node_count < 4 } {
	send_user "WARNING: Insufficient nodes in default partition ($node_count < 4)\n"
	exit $exit_code
}

#
# Build input script file
#
make_bash_script $file_in "
  export PS1=\"$prompt\"
  $bin_bash -norc
"

#
# Submit a 4 node job
#
set timeout $max_job_delay
set srun_pid [spawn $salloc -N4 ./$file_in]
expect {
	-re "More processors requested than permitted" {
		send_user "\nWARNING: can't test srun task distribution\n"
		exec $bin_rm -f $file_in
		exit $exit_code
	}
	-re "Node count specification invalid" {
		send_user "\nWARNING: can't test srun task distribution\n"
		exec $bin_rm -f $file_in
		exit $exit_code
	}
	-re "configuration is not available" {
		send_user "WARNING: partition too small for test\n"
		slow_kill $srun_pid
		exec $bin_rm -f $file_in
		exit 0
	}
	-re "Required node not available" {
		send_user "\nWARNING: can't test srun task distribution\n"
		exec $bin_rm -f $file_in
		exit $exit_code
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	"$prompt" {
#		Printing this message seems to damage expect's buffers
#		send_user "Job initiated\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		send_user "\nFAILURE: srun terminated\n"
		exit 1
	}
}
exec $bin_rm -f $file_in

#
# Get node names
#
set host_0      ""
set host_1      ""
set host_2      ""
set host_3      ""
send "$srun -l $bin_printenv SLURMD_NODENAME\r"
expect {
	-re "($number): *($alpha_numeric_under)" {
		set host_num $expect_out(1,string)
		if { $host_num == 0 } {
			set host_0 $expect_out(2,string)
		}
		if { $host_num == 1 } {
			set host_1 $expect_out(2,string)
		}
		if { $host_num == 2 } {
			set host_2 $expect_out(2,string)
		}
		if { $host_num == 3 } {
			set host_3 $expect_out(2,string)
		}
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	"$prompt" {
#		Printing this message seems to damage expect's buffers
#		send_user "srun completed\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}

#
# Verify node count
#
if {[string compare $host_0 ""] == 0} {
	send_user "\nFAILURE: Did not get hostname of task 0\n"
	set exit_code   1
}
if {[string compare $host_1 ""] == 0} {
        send_user "\nFAILURE: Did not get hostname of task 1\n"
	set exit_code   1
}
if {[string compare $host_2 ""] == 0} {
        send_user "\nFAILURE: Did not get hostname of task 2\n"
	set exit_code   1
}
if {[string compare $host_3 ""] == 0} {
        send_user "\nFAILURE: Did not get hostname of task 3\n"
	set exit_code   1
}
if {$exit_code != 0} {
	exit $exit_code
}

#send_user "\nhost_0:$host_0:host_1:$host_1:host_2:$host_2:host_3:$host_3:\n"
#
# Get two nodes relative (starting at) node 0
#
set matches 0

send "$srun -l -N2 -n2 --relative=0 $bin_printenv SLURMD_NODENAME\r"
expect {
	-re "($number): *($alpha_numeric_under)" {
		set host_num $expect_out(1,string)
		if { $host_num == 0 } {
			if {[string compare $expect_out(2,string) $host_0] == 0} {
				incr matches
			} else {
				send_user "\nFAILURE: wrong node responded\n"
				set exit_code   1
			}
		}
		if { $host_num == 1 } {
			if {[string compare $expect_out(2,string) $host_1] == 0} {
				incr matches
			} else {
				send_user "\nFAILURE: wrong node responded\n"
				set exit_code   1
			}
		}
		if { $host_num > 1 } {
			send_user "\nFAILURE: too many tasks responded\n"
			set exit_code   1
		}
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	"$prompt" {
#		Printing this message seems to damage expect's buffers
#		send_user "srun completed\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$matches < 2} {
	send_user "\nFAILURE: required nodes failed to respond\n"
	set exit_code   1
}

#
# Get two nodes relative (starting at) node 2
#
set matches 0
send "$srun -l -N2 -n2 --relative=2 $bin_printenv SLURMD_NODENAME\r"
expect {
	-re "($number): *($alpha_numeric_under)" {
		set host_num $expect_out(1,string)
		if { $host_num == 0 } {
			if {[string compare $expect_out(2,string) $host_2] == 0} {
				incr matches
			} else {
				send_user "\nFAILURE: wrong node responded\n"
				set exit_code   1
			}
		}
		if { $host_num == 1 } {
			if {[string compare $expect_out(2,string) $host_3] == 0} {
				incr matches
			} else {
				send_user "\nFAILURE: wrong node responded\n"
				set exit_code   1
			}
		}
		if { $host_num > 1 } {
			send_user "\nFAILURE: too many tasks responded\n"
			set exit_code   1
		}
		exp_continue
	}
	 -re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	"$prompt" {
#		Printing this message seems to damage expect's buffers
#		send_user "srun completed\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$matches < 2} {
	send_user "\nFAILURE: required nodes failed to respond\n"
	set exit_code   1
}

#
# Get two nodes relative (starting at) node 3
# Since there is only one node left, we check for a error message
#
set error 0
send "$srun -l -N2 -n2 -O --relative=3 $bin_printenv SLURMD_NODENAME\r"
expect {
	-re "error:" {
		send_user "This error is expected, no worries\n"
		incr error
		exp_continue
	}
	-re "($number): *($alpha_numeric_under)" {
		send_user "\nFAILURE: running where we shouldn't be able to run\n"
		set exit_code   1
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	"$prompt" {
#		Printing this message seems to damage expect's buffers
#		send_user "srun completed\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$error == 0} {
	send_user "\nFAILURE: No error for inconsistent node count\n"
	set exit_code   1
}

#
# Post-processing
#
send "exit\r"
expect {
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}

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