#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Confirm that sbatch stdout, and stderr options work (--output
#          and --error option respectively, including use of %j specification)
#
# 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) 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 <http://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 test_id     "17.5"
set user_name   ""
set file_in     "test$test_id.input"
set file_script "test$test_id.bash"
set file_out    "test$test_id.output"
set file_err    "test$test_id.error"
set file_out_j  "test$test_id.j.%j.output"
set file_err_j  "test$test_id.j.%j.error"
set file_out_u  "test$test_id.%u.output"
set file_err_u  "test$test_id.%u.error"
set exit_code   0

print_header $test_id

#
# Delete left-over stdin/out/err files
# Build stdin file
#
exec $bin_rm -f $file_in $file_script $file_out $file_err
make_bash_script $file_in "
  $bin_id
  $bin_sleep aaa
  exit 0
"

set cwd "[$bin_pwd]"
make_bash_script $file_script "
  cd /tmp
  $sbatch -N1 --workdir=$cwd --output=$file_out --error=$file_err -t1 $cwd/$file_in
  exit 0
"

#
# Get the name of the current user
#
spawn $bin_id -un
expect {
	-re "($alpha_numeric_under)" {
		set user_name $expect_out(1,string)
	}
	eof {
		wait
	}
}

#
# Spawn a sbatch command to varify that the default output file is
# created and contain the correct contents
#
set job_id 0
spawn $sbatch -t1 -N1 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	send_user "\nFAILURE: failed to submit job\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

#
# Check for desired output
#
set def_out "slurm-$job_id\.out"
set got_stdout 0
if {[file exist $def_out]} {
	spawn $bin_cat $def_out
	expect {
		-re "uid=" {
			incr got_stdout
			exp_continue
		}
		-re "$sleep_error_message" {
			incr got_stdout
			send_user "\nThis error is expected do not worry\n"
			exp_continue
		}
		eof {
			wait
		}
	}
}
if { $got_stdout != 2 } {
	send_user "\nFAILURE: Unexpected stdout contents $got_stdout != 2 \n"
	set exit_code 1
}

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

#
# Spawn a shell via sbatch that submits from a different directory and uses stdout/err
# and confirm their contents
#
set job_id 0
spawn ./$file_script
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	send_user "\nFAILURE: failed to submit job\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

#
# Check for desired output
#
if {[wait_for_file $file_out] != 0} {
	send_user "\nFAILURE: Missing stdout\n"
	set exit_code 1
}
if {[wait_for_file $file_err] != 0} {
	send_user "\nFAILURE: Missing stderr\n"
	set exit_code 1
}
exec $bin_rm -f $file_script $file_out $file_err

#
# Spawn a shell via sbatch that uses stdout/err and confirm their contents
#
set job_id 0
spawn $sbatch -N1 --output=$file_out --error=$file_err -t1 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	send_user "\nFAILURE: failed to submit job\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

#
# Check for desired output
#
set got_stdout 0
if {[wait_for_file $file_out] == 0} {
	spawn $bin_cat $file_out
	expect {
		-re "uid=" {
			set got_stdout 1
			exp_continue
		}
		eof {
			wait
		}
	}
}
if { $got_stdout == 0 } {
	send_user "\nFAILURE: Unexpected stdout contents\n"
	set exit_code 1
}
set got_stderr 0
if {[wait_for_file $file_err] == 0} {
	spawn $bin_cat $file_err
	expect {
		-re "$sleep_error_message" {
			send_user "\nNo worries, this error is expected\n"
			set got_stderr 1
			exp_continue
		}
		-re "Specify time as a positive integer.*\n" {
			set got_stderr 1
			exp_continue
		}
		eof {
			wait
		}
	}
}
if {$got_stderr == 0} {
	send_user "\nFAILURE: Unexpected stderr contents\n"
	set exit_code 1
}

#
# Spawn a shell via sbatch that uses stdout/err with %j in their names
#
set job_id 0
spawn $sbatch -N1 --output=$file_out_j --error=$file_err_j -t1 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	send_user "\nFAILURE: failed to submit job\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

#
# Check for desired output
#
set file_out_j_glob  "test$test_id.j.$job_id.output"
set file_err_j_glob  "test$test_id.j.$job_id.error"
if {[wait_for_file $file_out_j_glob] == 0} {
	exec $bin_rm -f $file_out_j_glob
} else {
	send_user "\nFAILURE: file format of %j in stdout failed\n"
	set exit_code 1
}
if {[wait_for_file $file_err_j_glob] == 0} {
	exec $bin_rm -f $file_err_j_glob
} else {
	send_user "\nFAILURE: file format of %j in stderr failed\n"
	set exit_code 1
}

#
# Spawn a shell via sbatch that uses stdout/err with %u in their names
#
set job_id 0
spawn $sbatch -N1 --output=$file_out_u --error=$file_err_u -t1 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	send_user "\nFAILURE: failed to submit job\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

#
# Check for desired output
#
set file_out_u_glob  "test$test_id.$user_name.output"
set file_err_u_glob  "test$test_id.$user_name.error"
if {[wait_for_file $file_out_u_glob] == 0} {
	exec $bin_rm -f $file_out_u_glob
} else {
	send_user "\nFAILURE: file format of %u in stdout failed\n"
	set exit_code 1
}
if {[wait_for_file $file_err_u_glob] == 0} {
	exec $bin_rm -f $file_err_u_glob
} else {
	send_user "\nFAILURE: file format of %u in stderr failed\n"
	set exit_code 1
}

#
# Spawn a program to run for a while with no output or error
#
set job_id 0
spawn $sbatch --output=none --error=none -t1 -N1 $file_in
expect {
	 -re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	send_user "\nFAILURE: failed to submit job\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

#
# Check for stdout/err files
#
exec sleep 5
set file_err2 slurm-$job_id.err
set file_out2 slurm-$job_id.out
send_user "\nChecking for files $file_err2 and $file_out2\n"
if { [file exists $file_out2] } {
	send_user "\nFAILURE: Found unexpected stdout file $file_out2\n"
	set exit_code 1
}
if { [file exists $file_err2] } {
	send_user "\nFAILURE: Found unexpected stderr file $file_err2\n"
	set exit_code 1
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_in $file_script $file_out $file_err
	send_user "\nSUCCESS\n"
}
exit $exit_code
