#!/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

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)"
}

#
# Submit a 4 node job
#
set timeout $max_job_delay
spawn $salloc -N4 $bin_bash
expect {
	-re "Granted job allocation" {
		reset_bash_prompt
		exp_continue
	}
	-re "More processors requested than permitted" {
		skip "Can't test srun task distribution"
	}
	-re "Node count specification invalid" {
		skip "Can't test srun task distribution"
	}
	-re "configuration is not available" {
		skip "Partition too small for test"
	}
	-re "Required node not available" {
		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 {
		fail "srun not responding"
	}
	eof {
		fail "srun terminated"
	}
}

#
# 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 {
		fail "srun not responding"
	}
	eof {
		wait
	}
}

#
# Verify node count
#
subtest {$host_0 ne ""} "Should get hostname of task 0"
subtest {$host_1 ne ""} "Should get hostname of task 1"
subtest {$host_2 ne ""} "Should get hostname of task 2"
subtest {$host_3 ne ""} "Should get hostname of task 3"

#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 {$expect_out(2,string) eq $host_0} {
				incr matches
			} else {
				fail "Wrong node responded"
			}
		}
		if { $host_num == 1 } {
			if {$expect_out(2,string) eq $host_1} {
				incr matches
			} else {
				fail "Wrong node responded"
			}
		}
		if { $host_num > 1 } {
			fail "Too many tasks responded"
		}
		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 {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
subtest {$matches >= 2} "Required node should respond"

#
# 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 {$expect_out(2,string) eq $host_2} {
				incr matches
			} else {
				fail "Wrong node responded"
			}
		}
		if { $host_num == 1 } {
			if {$expect_out(2,string) eq $host_3} {
				incr matches
			} else {
				fail "Wrong node responded"
			}
		}
		if { $host_num > 1 } {
			fail "Too many tasks responded"
		}
		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 {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
subtest {$matches >= 2} "Required node should respond"

#
# 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)" {
		fail "Running where we shouldn't be able to run"
	}
	-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 {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
subtest {$error != 0} "Error expected for inconsistent node count"

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