#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test that an srun program can move from the foreground to the
#          background.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2011-2013 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of SLURM, a resource management program.
# For details, see <http://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 test_id     "1.68"
set file_in     "test$test_id.bash"
set test_timer  "test$test_id.test_timer"
set test_srun   "test$test_id.test_srun"
set stop_srun   "test$test_id.stop_srun"
set job_id      0
set exit_code   0

print_header $test_id

if {[test_bluegene]} {
	send_user "\nWARNING: This test is incompatible with Bluegene systems\n"
	exit $exit_code
} elseif {[test_alps]} {
	send_user "\nWARNING: This test is incompatible with Cray systems\n"
	exit $exit_code
} elseif {[test_launch_poe]} {
	send_user "\nWARNING: This test is incompatible with POE systems\n"
	exit $exit_code
}

#remove any remaining files
exec $bin_rm -f $test_timer $test_srun $file_in $stop_srun

make_bash_script $test_timer "
for i in \{1..10\}
do
	echo \"time \$i\"
	sleep 5
done
echo \"Test finished\"
"

make_bash_script $stop_srun "
$bin_sleep 15
srun_proc=\$(ps -C srun -o pid=)
echo \"sending job to the background, pid=\$srun_proc\"
kill -s stop \$srun_proc
"

make_bash_script $test_srun "
set -m
./$stop_srun &
$srun -v -t1 ./$test_timer
bg
$bin_sleep 25
echo \"sending job to foreground\"
fg
"

make_bash_script $file_in "
bash -i ./$test_srun
"

set timeout $max_job_delay
set fg_match 0
set bg_match 0
set bg_time_num 0
set fini_match 0
set time_num 0
spawn bash -i $file_in
expect {
	-re "sending job to the background" {
		if {$time_num == 0} {
			send_user "\nFAILURE: srun is not generating output\n"
			set exit_code 1
		}
		set bg_time_num $time_num
		set bg_match 1
		exp_continue
	}
	-re "sending job to foreground" {
		if {$time_num == $bg_time_num} {
			send_user "\nFAILURE: srun is not progressing ($time_num == $bg_time_num)\n"
			set exit_code 1
		}
		set fg_match 1
		exp_continue
	}
	-re "time ($number)" {
		incr time_num
		exp_continue
	}
	-re "Test finished" {
		set fini_match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$fg_match != 1} {
	send_user "\nFAILURE: srun was not sent to the foreground\n"
	set exit_code 1
}
if {$bg_match != 1} {
	send_user "\nFAILURE: srun was not sent to the background\n"
	set exit_code 1
}
if {$time_num != 10} {
	send_user "\nFAILURE: srun is not generating output ($time_num != 10)\n"
	set exit_code 1
}
if {$fini_match != 1} {
	send_user "\nFAILURE: srun did not finish the program submitted\n"
	set exit_code 1
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
	exec $bin_rm -f $test_timer $test_srun $file_in $stop_srun
}
exit $exit_code



