#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Tests #PBS entry functionality in a batch script.
#
# 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) 2005-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@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 test_id     "17.31"
set exit_code   0
set file_in     "test$test_id.input"
set file_out    "test$test_id.output"
set job_acct    "TEST_ACCT"
set job_name    "TEST_NAME"
set delay       1
set node_cnt    3
set ppn_cnt     2
set min_mem     1024

print_header $test_id

set available [available_nodes $partition ""]
if {$available < $node_cnt} {
	send_user "\nWARNING: not enough nodes currently available"
	send_user " ($available avail, $node_cnt needed)\n"
	exit $exit_code
}

log_user 0
set scontrol_pid [spawn $scontrol show nodes]
expect {
	-re " CPUs=($number)" {
		if {$expect_out(1,string) < $ppn_cnt} {
			set ppn_cnt $expect_out(1,string)
		}
		exp_continue
	}
	-re "RealMemory=($number)" {
		if {$expect_out(1,string) < $min_mem} {
			set min_mem $expect_out(1,string)
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
		slow_kill $scontrol_pid
		exp_continue
	}
	eof {
		wait
	}
}
log_user 1
send_user "Actual configuration: min_cpus=$ppn_cnt  min_mem=$min_mem\n\n"
append min_mem_mb $min_mem "mb"

#
# Build input script file
# NOTE: The initial sleep is so that all of the submissions have time
#   to occur before contending with a multitude of job step creations.
#   This is especially important on very slow systems (e.g. AIX).
#
make_bash_script $file_in "
#PBS -l nodes=$node_cnt+ppn=$ppn_cnt,mem=$min_mem_mb
$bin_sleep $delay
"

set job_id  0
set matches 0
set sbatch_pid [spawn $sbatch -vv -o $file_out $file_in]
#exp_internal 1
expect {
	-re "ntasks$whitespace: ($number) \\(set\\)" {
		set cpu_count $expect_out(1,string)
		set cpus_per_node [expr $cpu_count / $node_cnt]
		if { $cpus_per_node != $ppn_cnt } {
			send_user "\nFAILURE: PPN count not sent correctly asked for $ppn_cnt but got $cpus_per_node\n"
			set exit_code 1
		} else {
			incr matches
		}
		exp_continue
	}
	-re "nodes$whitespace: ($number)" {
		set nodes $expect_out(1,string)
		if { $nodes != $node_cnt } {
			send_user "\nFAILURE: Bad node count allocated\n"
			set exit_code 1
		} else {
			incr matches
		}
		exp_continue
	}
	-re "mem=($number)" {
		set mem $expect_out(1,string)
		if { $mem != $min_mem } {
			send_user "\nFAILURE: Min memory is different ($mem) than requested ($min_mem)\n"
			set exit_code 1
		} else {
			incr matches
		}
		exp_continue
	}
	-re "batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	"error: Batch job submission failed" {
		send_user "\nWARNING: this might be legitimate, depending upon configuration\n"
		exp_continue
	}

	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
		slow_kill $sbatch_pid
		exp_continue
	}
	eof {
		wait
	}
}
if {$matches != 3} {
	send_user "\nFAILURE: sbatch didn't read the correct options from batch file ($matches of 3)\n"
	set exit_code 1
}
if {$job_id != 0} {
	cancel_job $job_id
}

#
# Post-processing
#
if {$exit_code == 0} {
	exec $bin_rm -f $file_in $file_out
	send_user "\nSUCCESS\n"
}
exit $exit_code
