#!/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).
############################################################################
# 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 exit_code   0
set file_in     "test$test_id.input"

if {[get_config_param "FrontendName"] ne "MISSING"} {
	skip "This test is incompatible with front-end systems"
}

# find out if we have enough nodes to test functionality
set node_count [get_partition_param [default_partition] "TotalNodes"]
if { $node_count < 4 } {
	skip "Insufficient nodes in default partition ($node_count < 4)"
}

#
# Build input script file
#
make_bash_script $file_in "
  $reset_bash_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" {
		exec $bin_rm -f $file_in
		skip "Can't test srun task distribution"
	}
	-re "Node count specification invalid" {
		exec $bin_rm -f $file_in
		skip "Can't test srun task distribution"
	}
	-re "configuration is not available" {
		slow_kill $srun_pid
		exec $bin_rm -f $file_in
		skip "Partition too small for test"
	}
	-re "Required node not available" {
		exec $bin_rm -f $file_in
		skip "Can't test srun task distribution"
	}
	-re "Unable to contact" {
		fail "Slurm appears to be down"
	}
	"$test_prompt" {
#		Printing this message seems to damage expect's buffers
#		log_debug "Job initiated"
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		fail "srun terminated"
	}
}
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): *($re_word_str)" {
		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" {
		fail "Slurm appears to be down"
	}
	"$test_prompt" {
#		Printing this message seems to damage expect's buffers
#		log_debug "Srun completed"
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}

#
# Verify node count
#
if {[string compare $host_0 ""] == 0} {
	log_error "Did not get hostname of task 0"
	set exit_code   1
}
if {[string compare $host_1 ""] == 0} {
    log_error "Did not get hostname of task 1"
	set exit_code   1
}
if {[string compare $host_2 ""] == 0} {
    log_error "Did not get hostname of task 2"
	set exit_code   1
}
if {[string compare $host_3 ""] == 0} {
    log_error "Did not get hostname of task 3"
	set exit_code   1
}
if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}

#log_debug "Host_0:$host_0:host_1:$host_1:host_2:$host_2:host_3:$host_3:"
#
# 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): *($re_word_str)" {
		set host_num $expect_out(1,string)
		if { $host_num == 0 } {
			if {[string compare $expect_out(2,string) $host_0] == 0} {
				incr matches
			} else {
				log_error "Wrong node responded"
				set exit_code   1
			}
		}
		if { $host_num == 1 } {
			if {[string compare $expect_out(2,string) $host_1] == 0} {
				incr matches
			} else {
				log_error "Wrong node responded"
				set exit_code   1
			}
		}
		if { $host_num > 1 } {
			log_error "Too many tasks responded"
			set exit_code   1
		}
		exp_continue
	}
	-re "Unable to contact" {
		fail "Slurm appears to be down"
	}
	"$test_prompt" {
#		Printing this message seems to damage expect's buffers
#		log_debug "srun completed"
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$matches < 2} {
	log_error "Required nodes failed to respond"
	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): *($re_word_str)" {
		set host_num $expect_out(1,string)
		if { $host_num == 0 } {
			if {[string compare $expect_out(2,string) $host_2] == 0} {
				incr matches
			} else {
				log_error "Wrong node responded"
				set exit_code   1
			}
		}
		if { $host_num == 1 } {
			if {[string compare $expect_out(2,string) $host_3] == 0} {
				incr matches
			} else {
				log_error "Wrong node responded"
				set exit_code   1
			}
		}
		if { $host_num > 1 } {
			log_error "Too many tasks responded"
			set exit_code   1
		}
		exp_continue
	}
	 -re "Unable to contact" {
		fail "Slurm appears to be down"
	}
	"$test_prompt" {
#		Printing this message seems to damage expect's buffers
#		log_debug "srun completed"
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$matches < 2} {
	log_error "Required nodes failed to respond"
	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:" {
		log_debug "This error is expected, no worries"
		incr error
		exp_continue
	}
	-re "($number): *($re_word_str)" {
		log_error "Running where we shouldn't be able to run"
		set exit_code   1
		exp_continue
	}
	-re "Unable to contact" {
		fail "Slurm appears to be down"
	}
	"$test_prompt" {
#		Printing this message seems to damage expect's buffers
#		log_debug "srun completed"
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$error == 0} {
	log_error "No error for inconsistent node count"
	set exit_code   1
}

#
# Post-processing
#
send "exit\r"
expect {
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
