#!/usr/bin/env expect
############################################################################
# Purpose:  Test of slurm_spawn API (needed on IBM SP systems).
#
# Note:    This script generates and then deletes files in the working directory
#          named test7.3.prog and test7.3.io
############################################################################
# Copyright (C) 2004 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 io_prog     "test$test_id.io"
set test_prog   "test$test_id.prog"

proc cleanup {} {
	global io_prog test_prog

	file delete $io_prog $test_prog
}

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

#
# Delete left-over programs and rebuild them
#
file delete $io_prog $test_prog

exec $bin_cc -O -o $io_prog ${io_prog}.c
exec $bin_chmod 700 $io_prog

if [compile_against_libslurm ${test_prog}] {
	fail "Cannot compile test program"
}

#
# Spawn initial program via srun
#
# (Actually there is a bug in the Linux which causes Expect to record
# an eof from the spawned program before it really completes, so we
# exec it and then scan the output)
#
set timeout $max_job_delay
set job_id 0
set matches 0
set task_cnt 1
# Usage: test7.3.prog [min_nodes] [max_nodes] [tasks]
spawn ./$test_prog 1 2
expect {
	-re "job_id ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "Starting ($number) tasks" {
		set task_cnt $expect_out(1,string)
		exp_continue
	}
	-re "task 0 write to stdout" {
		incr matches
		exp_continue
	}
	-re "task 0 write to stderr" {
		incr matches
		exp_continue
	}
	-re "task 0 read from stdin:test message" {
		incr matches
		exp_continue
	}
	-re "task 1 write to stdout" {
		incr matches
		exp_continue
	}
	-re "task 1 write to stderr" {
		incr matches
		exp_continue
	}
	-re "task 1 read from stdin:test message" {
		incr matches
		exp_continue
	}
	-re "error" {
		fail "Some error occurred"
	}
	timeout {
		cancel_job $job_id
		fail "Spawn IO not responding"
	}
	eof {
		wait
	}
}

if {$matches != [expr $task_cnt * 3]} {
	fail "Spawn IO communications failure"
}
