#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of srun task-prolog and task-epilog option.
############################################################################
# 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://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 cwd                  "[$bin_pwd]"
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"

#
# 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]
set tasks   4
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" {
		log_error "Error running srun"
		set exit_code 1
		exp_continue
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
set exp_matches [expr $tasks * 3]
if {$matches != $exp_matches} {
	log_error "Prolog exported env var failure ($matches != $exp_matches)"
	set exit_code 1
} else {
	log_debug "So far, so good"
}

#
# Get my id to compare with output
#

set my_uid [get_my_uid]

#
# 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} {
	log_error "Task prolog output is missing or uid mismatch ($matches:$tasks)"
	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} {
	fail "Task epilog output is missing or uid mismatch ($matches:$tasks)"
}


if {$exit_code == 0} {
	exec $bin_rm -f $task_prolog $task_epilog $file_in
	exec $bin_rm -f $file_out_pre $file_out_post
} else {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
