#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          test for #BSUB entry functionality
############################################################################
# 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 file_in       "$test_dir/input"
set job_name      "${test_name}_job"
set partition     [default_partition]
set job_time      1
set stdout        "/dev/null"
set stderr        "/tmp/err"
set job_id        0
set node_list     [lindex [get_nodes_by_state] 0]

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

# Default for LSF is KB, but Slurm is in MB
# Check values in MB, but set them in KB in the batch script.
if {[param_contains [get_config_param "SelectTypeParameters"] "CR_*MEMORY"]} {
	set job_mem 4
} else {
	set job_mem 1
}
set job_mem_kb "[expr $job_mem * 1024]"

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_kb
#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 {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	fail "sbatch did not submit job"
}

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

foreach option [array names check_list] {
	#log_debug "$option=$check_list($option)"
	if {$option eq "BatchHost" && [get_config_param "FrontendName"] ne "MISSING"} {
		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 {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}
	log_user 1

	# Check that each value is found
	if {$match != 1} {
		fail "$option was not found. Could be due to small configured MinJobAge value"
	}
}
