#!/usr/bin/env expect
############################################################################
# Purpose: Test hetjob step output file labling and different commands/args
############################################################################
# Copyright (C) 2017 SchedMD LLC.
# Written by Morris Jette <jette@schedmd.com>
# 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_out    test${test_id}.output
set het_job_id	0

proc cleanup {} {
	global bin_rm file_out

	exec $bin_rm -f $file_out
}

if {[get_config_param "SchedulerType"] ne "sched/backfill"} {
	skip "This test requires SchedulerType = sched/backfill"
}

set nb_nodes [get_partition_param [default_partition] "TotalNodes"]
if {$nb_nodes < 2} {
	skip "Need 2 or more nodes in default partition"
}

exec $bin_rm -f $file_out
set timeout $max_job_delay
set srun_pid [spawn $srun -t1 -n1 -l -o $file_out --mpi=none $bin_echo one : -n1 $bin_echo two]
expect {
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {[wait_for_file $file_out] == 0} {
	set matches 0
	spawn $bin_sort $file_out
	expect {
		-re "0: one" {
			incr matches
			exp_continue
		}
		-re "1: two" {
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$matches != 2} {
		fail "srun output file contents invalid"
	}
} else {
	fail "File not found ($file_out)"
}
