#!/usr/bin/env expect
############################################################################
# Purpose: Test for multiple, out of order srun --het-group options
############################################################################
# Copyright (C) 2017 SchedMD LLC.
# Written by Isaac Hartung <ihartung@schedmd.com>
#
# 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_in1     "test$test_id.in1"
set file_in2     "test$test_id.in2"
set file_in3     "test$test_id.in3"
set job_id       0
set matches      0

proc cleanup {} {
	global job_id scancel bin_rm file_in1 file_in2 file_in3 test_id

	if {$job_id != 0} {
		exec $scancel $job_id
	}
	exec $bin_rm -f $file_in1 $file_in2 $file_in3
}

# find out if we have enough nodes to test functionality
set node_count [get_partition_param [default_partition] "TotalNodes"]
if { $node_count < 3 } {
	skip "Insufficient nodes in default partition ($node_count < 3)"
}

if {[get_config_param "FrontendName"] ne "MISSING"} {
        skip "This test is incompatible with front-end systems"
}
if {[get_config_param "SchedulerType"] ne "sched/backfill"} {
	skip "This test requires SchedulerType = sched/backfill"
}

make_bash_script $file_in1 "$srun -l --mpi=none --het-group=0,2 ./$file_in2 : --het-group=1 ./$file_in3"
make_bash_script $file_in2 "$bin_echo FILE2"
make_bash_script $file_in3 "$bin_echo FILE3"

set timeout $max_job_delay
spawn $salloc -t1 -N1 : -N1 : -N1 ./$file_in1
expect {
	-re "Job submit/allocate failed" {
		skip "Unable to execute test due to system configuration"
	}
	-re "Granted job allocation ($number)" {
		incr matches
		set job_id $expect_out(1,string)
		if {$job_id == 0} {
			set job_id $expect_out(1,string)
		}
		exp_continue
	}
	-re "($number): FILE($number)" {
		if {$expect_out(1,string) == 0 && $expect_out(2,string) == 2} {
			incr matches
		} elseif {$expect_out(1,string) == 1 && $expect_out(2,string) == 3} {
			incr matches
		} elseif {$expect_out(1,string) == 2 && $expect_out(2,string) == 2} {
			incr matches
		} else {
			fail "Invalid output ($expect_out(1,string): FILE$expect_out(2,string))"
		}
		exp_continue
	}
	timeout {
		fail "salloc not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "Error submitting job"
}
if {$matches != 4} {
	fail "Invalid output ($matches != 4)"
}
