#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of srun task-prolog and task-epilog option.
#
# 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) 2005 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# UCRL-CODE-217948.
# 
# This file is part of SLURM, a resource management program.
# For details, see <http://www.llnl.gov/linux/slurm/>.
#  
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id              "1.49"
set exit_code            0
set cwd                  "[$bin_pwd]"
set tasks                4
set file_in		 "$cwd/test$test_id.in"
set task_prolog          "$cwd/test$test_id.prolog"
set task_epilog          "$cwd/test$test_id.epilog"
set file_out_pre         "$cwd/test$test_id.output_pre"
set file_out_post        "$cwd/test$test_id.output_post"

print_header $test_id

#
# Delete left-over scripts and rebuild, 
# The sleep command just forces slurmd to kill the user's (long running) epilog
#
exec $bin_rm -f $task_prolog $task_epilog $file_in
exec $bin_rm -f $file_out_pre $file_out_post
exec $bin_touch $file_out_pre
exec $bin_touch $file_out_post

exec echo "#!$bin_bash"                  >$task_prolog
exec echo "$bin_id >> $file_out_pre"    >>$task_prolog
exec echo "echo export TEST=prolog_qa"  >>$task_prolog

exec echo "#!$bin_bash"                  >$task_epilog
exec echo "$bin_id >> $file_out_post"   >>$task_epilog
exec echo "$bin_sleep 200"              >>$task_epilog

exec echo "#!$bin_bash"                  >$file_in
exec echo "echo TEST==\$TEST"           >>$file_in
#exec echo "env"                        >>$file_in

exec $bin_chmod 700 $task_prolog $task_epilog $file_in

#
# Submit a slurm job that will execute two tasks
#
set matches 0
set timeout $max_job_delay
spawn $srun -N1-2 -n$tasks -O -t1 --task-prolog=$task_prolog --task-epilog=$task_epilog $file_in
expect {
	-re "TEST==prolog_qa" {
		incr matches
		exp_continue
	}
	-re "error" {
		send_user "\nFAILURE: Error running srun\n"
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$exit_code != 0} {
	exit $exit_code
}
if {$matches != $tasks} {
	send_user "\nFAILURE: prolog exported env var failure ($matches != $tasks)\n"
	set exit_code 1
}

#
# Get my id to compare with output
#
set my_uid -1
spawn $bin_id
expect {
	-re "uid=($number)" {
		set my_uid $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}

#
# Make sure we have two records in both prolog and epilog output (one for each task)
# Wait a few seconds for various delays
#
exec $bin_sleep 5
set timeout 5
set matches 0
spawn $bin_cat $file_out_pre
expect {
	"uid=$my_uid" {
		incr matches
		exp_continue
	}
	eof {
		wait
	}
}
if {$matches != $tasks} {
	send_user "\nFAILURE: task prolog output is missing or uid mismatch\n"
	set exit_code 1
}


set matches 0
spawn $bin_cat $file_out_post
expect {
	"uid=$my_uid" {
		incr matches
		exp_continue
	}
	eof {
		wait
	}
}
if {$matches != $tasks} {
	send_user "\nFAILURE: task epilog output is missing or uid mismatch\n"
	set exit_code 1
}
	

if {$exit_code == 0} {
	exec $bin_rm -f $task_prolog $task_epilog $file_in 
	exec $bin_rm -f $file_out_pre $file_out_post
	send_user "\nSUCCESS\n"
}
exit $exit_code
