#!/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_dir/input"
set job_id      0

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

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

#
# Delete left-over input script
# Build input script file
#
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
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 {
		fail "srun not responding"
	}
	eof {
		wait
	}
}

subtest {$matches == 4} "Expecting multi-program output" "$matches != 4"

#
# 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 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 {$expect_out(1,string) ne "date"} {
			incr matches
		}
		if {$expect_out(1,string) ne "hostname"} {
			incr matches
		}
		if {$matches == 4} {
			slow_kill $srun_pid
		}
		exp_continue
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "Failed to get job id"
}

subtest {$matches == 4} "Expecting to generate full list of executables" "$matches != 4"

#
# Submit a slurm job that asks for more tasks than specified in our
# configuration file
#
set matches   0
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 {
		fail "srun not responding"
	}
	eof {
		wait
	}
}

subtest {$matches == 1} "Srun output should note lack of an executable for task 5"

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
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"
		}
		exp_continue
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}

subtest {$matches == 2} "Should not process MPMD line continuation" "$matches != 2"
