#!/usr/bin/env expect
############################################################################
# Purpose: Test of srun functionality
#          Test of hostfile option (-hostfile).
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Copyright (C) 2008-2009  Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@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 num_nodes   2
set num_tasks   2
set idle_nodes  0
set max_nodes   0
set task_count  0
set hostfile    "$test_dir/hostfile"

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

# Determine if we have enough nodes to test functionality
spawn $scontrol show partition [default_partition]
expect {
	-re "not found" {
		fail "Default partition doesn't exist"
	}
	-re "MaxNodes=($number)" {
		set max_nodes $expect_out(1,string)
		exp_continue
	}
	-re "MaxNodes=UNLIMITED" {
		set max_nodes 999999
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

set idle_nodes [llength [get_nodes_by_state]]
if { ($idle_nodes < 3) || ($max_nodes < 3) } {
	if { $max_nodes == 999999 } {
		skip "Default partition must have at least 3 idle nodes and MaxNodes >= 3 to run this test on. IDLE:$idle_nodes MaxNodes:UNLIMITED"
	} else {
		skip "Default partition must have at least 3 idle nodes and MaxNodes >= 3 to run this test on. IDLE:$idle_nodes MaxNodes:$max_nodes"
	}
}

exec $bin_rm -f %hostfile
set node0       0
set node1       0
set node2       0
set timeout $max_job_delay
for {set i 0} {$i<3} {incr i} {
	if { $i==1 } {
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			fail "Node names not set from previous srun"
		}
		set env(SLURM_HOSTFILE) $hostfile
		set 1node0 $node2
		set 1node1 $node0
		set 1node2 $node1
		set file [open $hostfile "w"]
		puts $file "$node2\n$node0\n$node1"
		close $file
	} elseif { $i==2 } {
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			fail "Node names not set from previous srun"
		}
		set env(SLURM_HOSTFILE) $hostfile
		set 2node0 $node1
		set 2node1 $node0
		set 2node2 $node2
		set file [open $hostfile "w"]
		puts $file "$node1\n$node0\n$node2"
		close $file
	}

	#
	# execute srun with a specific node count
	#
	set node0  0
	set node1  0
	set node2  0
	set task_cnt 0
	if { $i == 0} {
		spawn $srun -N3 -t1 -l $bin_printenv SLURMD_NODENAME
	} else {
		spawn $srun -t1 -l --distribution=arbitrary $bin_printenv SLURMD_NODENAME
	}
	expect {
		-re "($number): ($re_word_str)" {
			incr task_cnt
			set task_id $expect_out(1,string)
			if {$task_id == 0} {
				set node0 $expect_out(2,string)
			} elseif {$task_id == 1} {
				set node1 $expect_out(2,string)
			} else {
				set node2 $expect_out(2,string)
			}
			exp_continue
		}
		timeout {
			fail "srun not responding"
		}
		eof {
			wait
		}
	}
	if { $task_cnt != 3 } {
		log_warn "No worries, test just can not run here"
	} elseif { $i == 1 } {
		if {$node0 ne $1node0} {
			fail "Task 0 not distributed by hostfile ($node0 != $1node0)"
		} elseif {$node1 ne $1node1} {
			fail "Task 1 not distributed by hostfile ($node1 != $1node1)"
		} elseif {$node2 ne $1node2} {
			fail "Task 2 not distributed by hostfile ($node2 != $1node2)"
		}
	} elseif { $i == 2 } {
		if {$node0 ne $2node0} {
			fail "Task 0 not distributed by hostfile ($node0 != $2node0)"
		} elseif {$node1 ne $2node1} {
			fail "Task 1 not distributed by hostfile ($node1 != $2node1)"
		} elseif {$node2 ne $2node2} {
			fail "Task 2 not distributed by hostfile ($node2 != $2node2)"
		}
	}
}
