#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          test for #BSUB entry functionality
#
#
# 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) 2015 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# 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 test_id       17.62
set exit_code     0
set file_in       "test$test_id\.sh"
set job_name      "test$test_id\_job"
set partition     [default_partition]
set job_time      1
set stdout        "/dev/null"
set stderr        "/tmp/err"
set job_id        0
set node_list     [get_idle_node_in_part [default_partition]]

if {[test_select_type_params "MEMORY"]} {
	set job_mem  100
} else {
	set job_mem  1
}

array set check_list { }
set check_list(JobName)      $job_name
set check_list(TimeLimit)    00:0$job_time\:00
set check_list(Partition)    $partition
set check_list(BatchHost)    $node_list
set check_list(MinMemoryCPU) $job_mem
set check_list(StdErr)       $stderr
set check_list(StdOut)       $stdout

make_bash_script $file_in "

#BSUB -o$stdout -e$stderr
#BSUB -m\"$node_list\"
#BSUB -J$job_name -M$job_mem
#BSUB -W$job_time -x -q$partition
sleep 10
"

# Run the job with sbatch
spawn $sbatch $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	send_user "\nFailure: sbatch did not submit job\n"
	exec $bin_rm -fr $file_in
	exit 1
}

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


foreach option [array names check_list] {
	#send_user "\n$option=$check_list($option)\n"
	if {[string compare $option "BatchHost"] == 0 && [test_front_end] != 0} {
		continue
	}

	set match 0
	log_user 0
	spawn $scontrol show job $job_id
	expect {
		-re "$option=$check_list($option)" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	log_user 1

	# Check that each value is found
	if {$match != 1} {
		send_user "\nFAILURE: $option was not found\n"
		send_user "\nFAILURE: Could be due to small configured MinJobAge value\n"
		set exit_code 1
	}
}

cancel_job $job_id
if {$exit_code == 0} {
	exec $bin_rm -fr $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
