#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Tests #Slurm entry functionality in a batch script.
############################################################################
# Copyright (C) 2005-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 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 exit_code   0
set file_in     "test$test_id.input"
set file_out    "test$test_id.output"
set job_name    "TEST_NAME"
set delay       1

proc cleanup {} {
	global bin_rm file_in file_out

	exec $bin_rm -f $file_in $file_out
}

if {[get_config_param "EnforcePartLimits"] eq "NO"} {
	skip "This test is incompatible EnforcePartLimits = NO"
}

make_bash_script $file_in "
#SBATCH --job-name=$job_name
$bin_sleep $delay
"

set timeout $max_job_delay
set job_id 0
spawn $sbatch -o $file_out -vvvv $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "sbatch submit failure"
}
set matches 0
spawn $scontrol show job $job_id
expect {
	-re "Name=$job_name" {
		incr matches
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	log_error "Did not set job name from batch script"
	set exit_code 1
}
cancel_job $job_id

#
# 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 "#SBATCH -N1000000k
$bin_sleep $delay
"
set job_id  0
set matches 0
spawn $sbatch -o $file_out $file_in
expect {
	-re "More processors requested than permitted" {
		log_debug "This error was expected, no worries"
		incr matches
		exp_continue
	}
	-re "Node count specification invalid" {
		log_debug "This error was expected, no worries"
		incr matches
		exp_continue
	}
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	log_error "sbatch didn't read the correct options from batch file"
	set exit_code 1
}
if {$job_id != 0} {
	log_error "sbatch didn't reject job with invalid size"
	cancel_job $job_id
	set exit_code 1
}

make_bash_script $file_in "
#SBATCH -N650000
$bin_sleep $delay
"

set job_id  0
spawn $sbatch -N1 -o $file_out $file_in
expect {
	-re "Node count specification invalid" {
		log_error "sbatch read from the batch file options over writing the commandline options"
		set exit_code 1
		exp_continue
	}
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	cancel_job $job_id
	fail "sbatch didn't reject job with invalid size"
}

if {[wait_for_job $job_id DONE] != 0} {
	cancel_job $job_id
	fail "Error completing job $job_id"
}

#
# Post-processing
#
if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
