#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of node selection from within a job step on existing allocation
#
# NOTE:    This assumes node names are of the form <alpha><number>, where
#          the value of <number> indicates the nodes relative location.
#          Change tha node name parsing logic as needed for other formats.
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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

# Spawn an allocation to run on 2
# hosts with 4 tasks.
#
set job_id 0
spawn $salloc --gres=craynetwork:0 -N 2 -n 4 -t 1 $bin_bash
expect {
	-re "Node count specification invalid|More processors requested|not available" {
		skip "Can't test srun task distribution"
	}
	-re "Unable to contact" {
		fail "Slurm appears to be down"
	}
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
		log_debug "Allocation started"
		reset_bash_prompt
		exp_continue
	}
	-re "$test_prompt" {
	}
	timeout {
		fail "salloc not responding"
	}
}
if {$job_id == 0} {
	fail "salloc failed to allocate a job"
}
# srun the nodelist allocated by slurm.
set host_num 0
set host_list ""
send "$srun -l -n 1 -N 1 $bin_printenv SLURM_NODELIST\r"
expect {
	-re "($number): *($re_word_str)" {
		set host_num $expect_out(1,string)
		set host_list $expect_out(2,string)
		exp_continue
	}
	timeout {
		fail "srun not responding"
	}
	-re $test_prompt {
	}
}

# Convert the nodelist into a list of hostnames.
set count 0
set host1 ""
set host2 ""
send "$srun -l -n1 -N1 $scontrol show hostnames $host_list\r"
expect {
	-re "($number): *($re_word_str)" {
		if { $count == 0 } {
			set host1 $expect_out(2,string)
			incr count
			exp_continue
		}
		if { $count == 1 } {
			set host2 $expect_out(2,string)
			incr count
			exp_continue
		}
	}
	timeout {
		fail "scontrol not responding"
	}
	-re $test_prompt {
	}
}
log_debug "Got exec hosts $host1 $host2"

# Reset the spawn process to be initial allocation
# then srun a first task.
set hostname ""
send "$srun -l -n 1 -N 1 $bin_printenv SLURM_STEP_NODELIST\r"
expect {
	-re "($number): *($re_word_str)" {
		set hostname $expect_out(2,string)
		exp_continue
	}
	-re $test_prompt {
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}

}

# Force the second task on the second node, at the end
# make sure the task ran on the specified node.
set hostname ""
send "$srun -l -n 1 -N 1 -w $host2 $bin_printenv SLURM_STEP_NODELIST\r"
expect {
	-re "($number): *($re_word_str)" {
		set hostname $expect_out(2,string)
		exp_continue
	}
	-re $test_prompt {
		send "exit\r"
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}

if {$host2 ne $hostname} {
	fail "The execution hostname ($hostname) != requested hostname ($host2)"
}
