#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of sbatch --bbf 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) 2016 SchedMD LLC
# Written by Morris Jette <jette(at)schedmd.com>
#
# 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.45"
set file_bbf    "test$test_id.bbf"
set file_script "test$test_id.bash"
set file_out    "test$test_id.output"

set exit_code   0

print_header $test_id

#
# Build job script
#
exec $bin_rm -f $file_bbf $file_script $file_out
make_bash_script $file_script "
  echo test_script
  exit 0
"

#
# Build BBF file to get inserted into script above
#
set fd [open $file_bbf "w"]
puts $fd "echo test_bbf"
puts $fd "exit 0"
close $fd
exec $bin_chmod 400 $file_bbf

#
# NOTE: The "bbf" file does not need to contain Burst Buffer options, but can
# contain text to be inserted at the start of the script (after "#! shell").
#
set job_id 0
spawn $sbatch -t1 -n1 --bbf=$file_bbf -o $file_out $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: file $file_out is missing\n"
        set exit_code 1
} else {
	set match 0
	spawn $bin_cat $file_out
	expect {
		-re "test_bbf" {
			incr match
			exp_continue
		}
		-re "test_script" {
			send_user "\nFAILURE: bbf file not inserted at start of script\n"
			set exit_code 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != 1} {
		send_user "\nFAILURE: bbf file not inserted at start of script\n"
		set exit_code 1
	}
}

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