#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Confirm that sbatch stdout, and stderr options work (--output
#          and --error option respectively, including use of %j specification)
############################################################################
# Copyright (C) 2006-2007 The Regents of the University of California.
# Copyright (C) 2008 Lawrence Livermore National Security.
# 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 user_name   ""
set file_in     "$test_dir/input"
set file_script "$test_dir/script"
set file_out    "$test_dir/output"
set file_err    "$test_dir/error"
set file_err_perc        "$test_dir/error.%%"
set file_err_perc_result "$test_dir/error.%"
set file_out_perc        "$test_dir/output.%%"
set file_out_perc_result "$test_dir/output.%"
set file_out_j  "$test_dir/output.j.%j"
set file_err_j  "$test_dir/error.j.%j"
set file_out_u  "$test_dir/output.%u"
set file_err_u  "$test_dir/error.%u"

#
# Delete left-over stdin/out/err files
# Build stdin file
#
make_bash_script $file_in "
  $bin_id
  $bin_sleep aaa
  exit 0
"

#
# Get the name of the current user
#

set user_name [get_my_user_name]

proc test_job_output_files {job_id file_out file_err} {
	#
	# Wait for job to complete
	#
	wait_for_job -fail $job_id "DONE"

	#
	# Check for desired output
	#
	subtest {![wait_for_file $file_out]} "Check for file $file_out"
	subtest {![wait_for_file $file_err]} "Check for file $file_err"
}

#
# Spawn a sbatch command to verify that the default output file is
# created and contain the correct contents
#
set job_id [submit_job -fail "-t1 -N1 $file_in"]
set def_out "slurm-$job_id\.out"
test_job_output_files $job_id $def_out $def_out

#
# Check for desired output
#
set output [run_command_output -fail "$bin_cat $def_out"]
set got_stdout [regexp -all "uid=" $output]
incr got_stdout [regexp -all "$sleep_error_message" $output]

subtest {$got_stdout == 2} "Check for expected stdout contents" "$got_stdout != 2"

# Remove the output file after it has been checked
exec $bin_rm -f $def_out

#
# Spawn a shell via sbatch that uses stdout/err and confirm their contents
#
set job_id [submit_job -fail "-N1 --output=$file_out --error=$file_err -t1 $file_in"]

#
# Wait for job to complete
#
wait_for_job -fail $job_id "DONE"
wait_for_file -fail $file_out
wait_for_file -fail $file_err

#
# Check for desired output
#
set output [run_command_output -fail "$bin_cat $file_out"]
subtest {[regexp "uid=" $output]} "Check for expected stdout contents"

set output [run_command_output -fail "$bin_cat $file_err"]
subtest {[regexp "$sleep_error_message" $output] ||
         [regexp "Specify time as a positive integer.*\n" $output]} "Check for expected stderr contents"

#
# Spawn a shell via sbatch that uses literal %% in their file names
#
set job_id [submit_job -fail "-N1 --output=$file_out_perc --error=$file_err_perc -t1 $file_in"]
test_job_output_files $job_id $file_out_perc_result $file_err_perc_result

#
# Spawn a shell via sbatch that uses stdout/err with %j in their names
#
set job_id [submit_job -fail "-N1 --output=$file_out_j --error=$file_err_j -t1 $file_in"]
test_job_output_files $job_id "$test_dir/output.j.$job_id" "$test_dir/error.j.$job_id"

#
# Perform previous test using environment variables
#
run_command "$bin_rm $test_dir/output.j.$job_id $test_dir/error.j.$job_id"
set job_id [submit_job -fail -env "SBATCH_OUTPUT=$file_out_j SBATCH_ERROR=$file_err_j" "-N1 -t1 $file_in"]
test_job_output_files $job_id "$test_dir/output.j.$job_id" "$test_dir/error.j.$job_id"

#
# Spawn a shell via sbatch that uses stdout/err with %u in their names
#
set job_id [submit_job -fail "-N1 --output=$file_out_u --error=$file_err_u -t1 $file_in"]
test_job_output_files $job_id "$test_dir/output.$user_name" "$test_dir/error.$user_name"

#
# Spawn a program to run for a while with no output or error
#
set job_id [submit_job -fail "--output=none --error=none -t1 -N1 $file_in"]

#
# Wait for job to complete
#
wait_for_job -fail $job_id "DONE"

#
# Check for stdout/err files
#
set file_out2 slurm-$job_id.out
set file_err2 slurm-$job_id.err
log_debug "Checking for files $file_err2 and $file_out2"
subtest {[wait_for_file -timeout 5 $file_out2]} "Stdout file should not be created with --output=none"
subtest {[wait_for_file -timeout 5 $file_err2]} "Stderr file should not be created with --error=none"
