#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of MPMD (--multi-prog option).
############################################################################
# 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 file_in     "test$test_id.input"
set exit_code   0

proc cleanup {} {
	global bin_rm file_in

	exec $bin_rm -f $file_in
}

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

#
# 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   $bin_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 {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$matches != 4} {
	fail "Did not get expected multi-program output"
} else {
	log_debug "So far, so good"
}

#
# 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    0
set matches   0
set srun_pid [spawn $srun -N1 -n4 --overcommit -l -t1 --multi-prog --debugger-test -v ./$file_in]
expect {
	-re "launching StepId=($number)\\.($re_word_str)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "executable:($re_word_str)" {
		if {[string compare $expect_out(1,string) "date"] != 0} {
			incr matches
		}
		if {[string compare $expect_out(1,string) "hostname"] != 0} {
			incr matches
		}
		if {$matches == 4} {
			slow_kill $srun_pid
		}
		exp_continue
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$matches != 4} {
	log_error "Did not generate full list of executables."
	set exit_code 1
}
if {$job_id == 0} {
	log_error "Failed to get job id"
	set exit_code 1
} else {
	cancel_job $job_id
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
} else {
	log_debug "So far, so good"
}

#
# 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" {
		log_debug "No worries. This error is expected"
		incr matches
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	log_error "Did not note lack of a executable for task 5."
	set exit_code 1
}


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_echo aaaa \
task:%t:\\
offset:%o \\
bbbb
"
close $file
exec $bin_chmod 700 $file_in

set matches   0
set srun_pid [spawn $srun -N1 -n4 --overcommit -l -t1 --multi-prog ./$file_in]
expect {
	-re "($number): aaaa task:($number):offset:($number) bbbb" {
		if {$expect_out(1,string) == 0 && $expect_out(2,string) == 0 && $expect_out(3,string) == 0} {
			incr matches
		} elseif {$expect_out(1,string) == 3 && $expect_out(2,string) == 3 && $expect_out(3,string) == 1} {
			incr matches
		} else {
			log_error "Processing MPMD line continuation"
			set exit_code 1
		}
		exp_continue
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$matches != 2} {
	fail "Processing MPMD line continuation ($matches != 2)"
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
