#!/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 file_in		 "$test_dir/in"
set task_prolog          "$test_dir/prolog"
set task_epilog          "$test_dir/epilog"
set file_out_pre         "$test_dir/output_pre"
set file_out_post        "$test_dir/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, overwriting of data has been observed, this
#    causes the test to fail
#
set timeout [expr $max_job_delay + 30]
set tasks   4
set node_cnt 1-1

set output [run_command_output -fail "$srun -N$node_cnt -n$tasks -O -t1 --task-prolog=$task_prolog --task-epilog=$task_epilog $file_in"]

subtest {[regexp -all "(HEADER|TEST==prolog_qa|DISPLAY==X)" $output] == [expr $tasks * 3]} "Prolog should export env vars"
subtest {[regexp -all "error: TaskEpilog failed status=9" $output] == $tasks} "srun should report epilog killed by slurmd"
subtest {[regexp -all "error:" $output] == $tasks} "No more error should be reported"

#
# 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
wait_for_file -fail $file_out_pre
set timeout 5
spawn $bin_cat $file_out_pre
expect {
	"uid=$my_uid" {
		incr matches
		exp_continue
	}
	eof {
		wait
	}
}

subtest {$matches == $tasks} "Task prolog output should be present and match our uid" "$matches:$tasks"

set matches 0
spawn $bin_cat $file_out_post
expect {
	"uid=$my_uid" {
		incr matches
		exp_continue
	}
	eof {
		wait
	}
}

subtest {$matches == $tasks} "Task epilog should be present and match our uid" "$matches:$tasks"
