#!/usr/bin/expect
############################################################################
# Purpose: Test of srun functionality
#          Test of hostfile option (-hostfile) inside of an allocation.
#
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
############################################################################
# Copyright (C) 2002-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@llnl.gov>
# UCRL-CODE-217948.
#
# This file is part of SLURM, a resource management program.
# For details, see <http://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.59"
set exit_code   0
set file_in     "test$test_id.input"
set num_nodes   3
set node_count  0
set task_count  0
set job_id      0
set hostfile    "test$test_id.hostfile"

print_header $test_id

exec $bin_rm -f $hostfile
if { [test_front_end] } {
	send_user "\nWARNING: This test incompatible with front-end systems\n"
	exit $exit_code
} elseif {[test_serial]} {
	send_user "\nWARNING: This test is incompatible with a serial system\n"
	exit 0
}

if {[test_launch_poe]} {
	set node_name_env MP_I_UPMD_HOSTNAME
} else {
	set node_name_env SLURMD_NODENAME
}

# 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 < 3 } {
	send_user "WARNING: system must have at least 3 \
		   nodes to run this test on.  This system \
		   only has $node_count.\n"
	exit $exit_code
}

set node0 0
set node1 0
set node2 0
set node3 0
set node4 0

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

set timeout $max_job_delay
set salloc_pid [spawn $salloc -N$num_nodes -v -t1 ./$file_in]
expect {
	-re "salloc: Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re $prompt {
		#send_user "Job initiated\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $salloc_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
exec $bin_rm -f $file_in
if {$job_id == 0} {
	send_user "\nFAILURE: salloc failure\n"
	exit 1
}

for {set i 0} {$i<4} {incr i} {
	set extra ""

	if { $i==1 } {
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		send "export SLURM_HOSTFILE=$hostfile\r"
		expect {
			-re $prompt {
				#send_user "srun completed\n"
			}
		}
		set 1node0 $node0
		set 1node1 $node1
		set 1node2 $node2
		set file [open $hostfile "w"]
		puts $file "$node2\n$node0\n$node1"
		close $file
	} elseif { $i==2 } {
		if { $node_count < 3 } {
			send_user "WARNING: system must have at least 3 \
				   nodes to finish this test.  This system \
				   only has $node_count.\n"
			exit $exit_code
		}
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		set 2node0 $node1
		set 2node1 $node0
		set 2node2 $node0
		set 2node3 $node0
		set 2node4 $node0
		set file [open $hostfile "w"]
		puts $file "$node1\n$node0\n$node0\n$node0\n$node0"
		close $file
		set extra "-n5"
	} elseif { $i==3 } {
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		set 3node0 $node1
		set 3node1 $node0
		set 3node2 $node0
		set 3node3 $node0
		set 3node4 0
		set file [open $hostfile "w"]
		puts $file "$node1\n$node0\n$node0\n$node0\n$node0"
		close $file
		set extra "-n4"
	}
	set node0 0
	set node1 0
	set node2 0
	set node3 0
	set node4 0

	#
	# execute srun with a specific node count
	#
	send "$srun -l $extra -O $bin_printenv $node_name_env\r"
	expect {
		-re "($number): *($alpha_numeric_under)" {
			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)
			} elseif {$task_id == 2} {
				set node2 $expect_out(2,string)
			} elseif {$task_id == 3} {
				set node3 $expect_out(2,string)
			} elseif {$task_id == 4} {
				set node4 $expect_out(2,string)
			} elseif {$task_id == 5} {
				set node5 $expect_out(2,string)
			}
			exp_continue
		}
		-re $prompt {
			#send_user "srun completed\n"
		}
		-re "Granted job allocation ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			exec $scancel --quiet $job_id
			set exit_code 1
		}
		eof {
		}
	}
	if { $i == 1 } {
		if { [string compare $node0 $1node2] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node1 $1node0] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node2 $1node1] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		}
	} elseif { $i == 2 } {
		if { [string compare $node0 $2node0] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node1 $2node1] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node2 $2node2] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node3 $2node3] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node4 $2node4] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		}
	} elseif { $i==3 } {
		if { [string compare $node0 $3node0] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node1 $3node1] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node2 $3node2] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node3 $3node3] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node4 $3node4] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		}
	}
}
send "unset SLURM_HOSTFILE\r"
expect {
	-re $prompt {
		#send_user "srun completed\n"
	}
}

# try with commandline -w option
for {set i 0} {$i<5} {incr i} {
	set extra ""

	if { $i==1 } {
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		set 1node0 $node0
		set 1node1 $node1
		set 1node2 $node2
		set extra " -m arbitrary -w $node2,$node0,$node1"
	} elseif { $i==2 } {
		if { $node_count < 3 } {
			send_user "WARNING: system must have at least 3 \
				   nodes to finish this test.  This system \
				   only has $node_count.\n"
			exit $exit_code
		}
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		set 2node0 $node1
		set 2node1 $node0
		set 2node2 $node0
		set 2node3 $node0
		set 2node4 $node0
		set extra " -m arbitrary -w $node1,$node0,$node0,$node0,$node0"
	} elseif { $i==3 } {
		if { $node1 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		set 4node0 $node1
		set extra " -w $node1"
	} elseif { $i==4 } {
		if { $node0 == 0 || $node1 == 0 || $node2 == 0 } {
			send_user "\nFAILURE: node names not set from \
				previous srun\n"
			exit 1
		}
		set 4node0 $node1
		set 4node1 $node0
		set 4node2 $node0
		set 4node3 $node0
		set 4node4 0
		set extra " -m arbitrary -w $node1,$node0,$node0,$node0,$node0 -n4"
	}

	set node0 0
	set node1 0
	set node2 0
	set node3 0
	set node4 0

	#
	# execute srun with a specific node count
	#

	send "$srun -l $extra -O $bin_printenv $node_name_env\r"

	expect {
		-re "($number): *($alpha_numeric_under)" {
			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)
			} elseif {$task_id == 2} {
				set node2 $expect_out(2,string)
			} elseif {$task_id == 3} {
				set node3 $expect_out(2,string)
			} elseif {$task_id == 4} {
				set node4 $expect_out(2,string)
			} elseif {$task_id == 5} {
				set node5 $expect_out(2,string)
			}
			exp_continue
		}
		-re "error" {
			if { $i == 4 } {
				send_user "\nThis error is expected, no worries\n"
			} else {
				set exit_code 1
			}
			exp_continue
		}
		-re $prompt {
			#send_user "srun completed\n"
		}
		-re "slurm job ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			exec $scancel --quiet $job_id
			set exit_code 1
		}
		eof {
		}
	}
	if { $i == 1 } {
		if { [string compare $node0 $1node2] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node1 $1node0] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node2 $1node1] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		}
	} elseif { $i == 2 } {
		if { [string compare $node0 $2node0] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node1 $2node1] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node2 $2node2] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node3 $2node3] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		} elseif { [string compare $node4 $2node4] } {
			send_user "\nFAILURE: tasks not distributed by hostfile\n"
			set exit_code 1
		}
	} elseif { $i == 3 } {
		if { [string compare $node2 $4node0] } {
			send_user "\nFAILURE: we only asked for 1 node in the allocation and we were unable to get it\n"
			set exit_code 1
		}
	}
}

send "exit\r"
expect {
	-re "error.*Exit 1" {
		send_user "This error is expected, no worries\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		exit 1
	}
	eof {
		wait
	}
}

if {$exit_code == 0} {
	exec $bin_rm -f $hostfile
	send_user "\nSUCCESS\n"
}
exit $exit_code
