#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of --bcast option.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2015 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

set test_id     "1.101"
set exit_code   0
set file_in     "test$test_id.input"
set job_id      0
set prompt      "PROMPT: "
set timeout $max_job_delay

print_header $test_id

if {[test_alps]} {
	send_user "\nWARNING: This test is incompatible with Cray systems\n"
	exit 0
} elseif {[test_bluegene]} {
	send_user "\nWARNING: This test is incompatible with Bluegene systems\n"
	exit 0
} elseif {[test_serial]} {
	send_user "\nWARNING: This test is incompatible with serial systems\n"
	exit 0
} elseif { [test_xcpu] } {
	send_user "\nWARNING: This test is incompatible with XCPU systems\n"
	exit 0
}

if {[slurmd_user_root] == 0} {
	send_user "\nWARNING: This test is incompatible with SlurmdUser != root\n"
	exit 0
}

make_bash_script $file_in "echo \$0"
set salloc_pid [spawn $salloc -N1-5 -t1 bash]
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
		send "export PS1=\"$prompt\"\r"
		exp_continue
	}
	-re "\"$prompt" {
		# skip this, just echo of setting prompt"
		exp_continue
	}
	-re "$prompt" {
		#send_user "Job initiated\n"
	}
	timeout {
		send_user "\nFAILURE: salloc not responding\n"
		if {$job_id != 0} {
			cancel_job $job_id
		}
		slow_kill [expr 0 - $salloc_pid]
		exit 1
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: did not get job_id\n"
	exit 1
}

send "$srun rm -f /tmp/test$test_id\r"
expect {
	-re "$prompt" {
		#break
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
}

set node_cnt 99
send "echo \$SLURM_NNODES\r"
expect {
	-re "($number)" {
		if { $node_cnt == 99 } {
			set node_cnt $expect_out(1,string)
		}
		exp_continue
	}
	-re "$prompt" {
		#break
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
}

set exec_cnt 0
send "$srun -l --bcast=/tmp/test$test_id ./$file_in\r"
expect {
	-re "not supported" {
		send_user "\nWARNING: srun --bcast option not supported on this system type (e.g. front-end)\n"
		set exec_cnt $node_cnt
		exp_continue
	}
	-re "($number): /tmp/test$test_id" {
		incr exec_cnt
		exp_continue
	}
	-re "$prompt" {
		#break
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
}

send "$srun -l rm -v /tmp/test$test_id\r"
expect {
	-re "$prompt" {
		#break
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
}

send "exit\r"

if {$exec_cnt != $node_cnt} {
	send_user "\nFAILURE: executable count != node count ($exec_cnt != $node_cnt)\n"
	set exit_code 1
}

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