#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of MPMD (--multi-prog option).
#
# 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) 2006 The Regents of the University of California.
# 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

set test_id     "1.54"
set file_in     "test$test_id.input"
set exit_code   0

print_header $test_id

if {[test_front_end] != 0} {
	send_user "\nWARNING: This test is incompatible with front-end systems\n"
	exit 0
} elseif {[test_serial]} {
	send_user "\nWARNING: This test is incompatible with serial systems\n"
	exit 0
} elseif {[test_launch_poe]} {
	send_user "\nWARNING: This test is incompatible with POE systems\n"
	exit 0
}

#
# Delete left-over input script
# Build input script file
#
exec $bin_rm -f $file_in
set file [open $file_in "w"]
puts $file "# multi-program configuration file
1-2   /bin/echo task:%t:offset:%o
0,3   echo task:%t:offset:%o
"
close $file
exec $bin_chmod 700 $file_in

#
# Submit a slurm job that will execute different programs and arguments by task number
#
set matches 0
set timeout $max_job_delay
set srun_pid [spawn $srun -N1 -n4 --overcommit -l -t1 --multi-prog ./$file_in]
expect {
	-re "($number): *task:($number):offset:($number)" {
		set label       $expect_out(1,string)
		set task_id     $expect_out(2,string)
		set task_offset $expect_out(3,string)
		if {$label == 0 && $task_id == 0 && $task_offset == 0} {
			incr matches
		}
		if {$label == 1 && $task_id == 1 && $task_offset == 0} {
			incr matches
		}
		if {$label == 2 && $task_id == 2 && $task_offset == 1} {
			incr matches
		}
		if {$label == 3 && $task_id == 3 && $task_offset == 1} {
			incr matches
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 4} {
	send_user "\nFAILURE: Did not get expected multi-program output\n"
	set exit_code 1
}
if {$exit_code != 0} {
	exit $exit_code
} else {
	send_user "\nSo far, so good\n\n"
}

#
# Submit a slurm job that will execute different executables and check debug info
#
# Timeout is max_job_delay (to spawn task) +
#       60 (job time limit) +
#       60 (slurmctld time limit check poll interval) +
#       KillWait
#
set timeout [expr $max_job_delay + 60 + 60 + 60]

exec $bin_rm -f $file_in
set file [open $file_in "w"]
puts $file "# multi-program configuration file
1-2   /bin/hostname
0,3   /bin/date
"
close $file
exec $bin_chmod 700 $file_in

set job_id    -
set matches   0
set srun_pid [spawn $srun -N1 -n4 --overcommit -l -t1 --multi-prog --debugger-test -v ./$file_in]
expect {
	-re "launching ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "executable:(/bin/)($alpha)" {
		if {[string compare $expect_out(2,string) "date"] != 0} {
			incr matches
		}
		if {[string compare $expect_out(2,string) "hostname"] != 0} {
			incr matches
		}
		if {$matches == 4} {
			slow_kill $srun_pid
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 4} {
	send_user "\nFAILURE: did not generate full list of executables.\n"
	set exit_code 1
}
if {$job_id == 0} {
	send_user "\nFAILURE: failed to get job id\n"
	set exit_code 1
} else {
	cancel_job $job_id
}

if {$exit_code != 0} {
	exit $exit_code
} else {
	send_user "\nSo far, so good\n\n"
}

#
# Submit a slurm job that asks for more tasks than specified in our
# configuration file
#
set matches   0
set srun_pid [spawn $srun -N1 -n5 --overcommit -l -t1 --multi-prog  ./$file_in]
expect {
	-re "Configuration file .* invalid" {
		send_user "\nNo worries. This error is expected\n"
		incr matches
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: did not note lack of a executable for task 5.\n"
	set exit_code 1
}

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