#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Basic SHMEM test via srun.
############################################################################
# Copyright (C) 2012 SchedMD LLC.
# Written by Morris Jette <jette@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 exit_code          0
set file_in            "test$test_id.input"
set file_out           "test$test_id.output"
set file_err           "test$test_id.error"
set test_prog          "test$test_id.prog"
set job_id             0
set task_cnt           8

proc cleanup {} {
	global bin_rm file_in file_out file_err test_prog

	exec $bin_rm -f $file_in $file_out $file_err $test_prog ${test_prog}.o
}

if {[get_config_param "FrontendName"] ne "MISSING"} {
	skip "This test is incompatible with front-end systems"
} elseif {![file exists $oshcc]} {
	skip "$oshcc does not exists"
}

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $test_prog ${test_prog}.o
spawn $oshcc  -o $test_prog ${test_prog}.c -lshmem
expect {
	eof {
		wait
	}
}
if {![file exists $test_prog]} {
	skip "Unable to build test program"
}

# Delete left-over stdout/err files
file delete $file_out $file_err

#
# Build input script file
#
make_bash_script $file_in "
  $srun -n $task_cnt ./$test_prog
"

#
# Spawn an sbatch job that uses stdout/err and confirm their contents
#
set timeout $max_job_delay
set no_start 0
set sbatch_pid [spawn $sbatch -N 1-$task_cnt -n $task_cnt --output=$file_out --error=$file_err -t1 $file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "Batch job submission failed" {
		set no_start 1
		exp_continue
	}
	-re "Unable to contact" {
		fail "Slurm appears to be down"
	}
	timeout {
		slow_kill $sbatch_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}

if {$no_start != 0} {
	cancel_job $job_id
	skip "Partition too small for test"
}
if {$job_id == 0} {
	fail "Batch submit failure"
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	log_error "Waiting for job to complete"
	set exit_code 1
}

#
# Check for desired output in stdout
#
if {[wait_for_file $file_out] == 0} {
	set matches 0

	spawn $bin_cat $file_out
	expect {
		-re "PE ($number): 1,2,3,4,5,6,7,8" {
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	incr task_cnt -1
	if {$matches != $task_cnt} {
		log_error "Unexpected output ($matches of $task_cnt)"
		set exit_code 1
	}
} else {
	set exit_code 1
}

if {$exit_code != 0} {
	log_debug "Check contents of $file_err"
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
