#!/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-2006 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://computing.llnl.gov/linux/slurm/>.
# 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.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

if {[test_cray]} {
	send_user "\nWARNING: This test is incompatible with Cray systems\n"
	exit $exit_code
}

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

make_bash_script $task_prolog "
  $bin_id >> $file_out_pre
  echo print HEADER
  echo export TEST=prolog_qa
  echo unset DISPLAY
"

make_bash_script $task_epilog "
  $bin_id >> $file_out_post
  $bin_sleep 200
"

make_bash_script $file_in "
  echo TEST==\$TEST
  echo DISPLAY==\${DISPLAY}X
  #env
"

#
# Submit a slurm job that will execute $tasks tasks
# Note: If running on more than one node and writing to an
#    NFS file, overwritting of data has been observed, this
#    causes the test to fail
#
set matches 0
set timeout [expr $max_job_delay + 30]
if { [test_bluegene] } {
	set node_cnt 1-1024
} else {
	set node_cnt 1-1
}

set srun_pid [spawn $srun -N$node_cnt -n$tasks -O -t1 --task-prolog=$task_prolog --task-epilog=$task_epilog $file_in]
expect {
	-re "(HEADER|TEST==prolog_qa|DISPLAY==X)" {
		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"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$exit_code != 0} {
	exit $exit_code
}
set exp_matches [expr $tasks * 3]
if {$matches != $exp_matches} {
	send_user "\nFAILURE: prolog exported env var failure ($matches != $exp_matches)\n"
	set exit_code 1
} else {
	send_user "\nSo far, so good\n\n"
}

#
# 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
#
set matches 0
if {[wait_for_file $file_out_pre] == 0} {
	set timeout 5
	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 ($matches:$tasks)\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 ($matches:$tasks)\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
