############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.30
#          Tests if the GrpCpuRunMins limit is enforced
############################################################################
# Copyright (C) 2014 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.
############################################################################

proc inc21_30_10 { } {

	global salloc srun acct bin_sleep grpcpurunmin_num number exit_code

	send_user "\nStarting GrpCpuRunMins test (Within: inc21.30.10)\n\n"
	set job_id1 0
	set job_id2 0
	set target_time [expr $grpcpurunmin_num / 4]
	set time_limit [expr $target_time - 3]
	if {$time_limit < 1} {
		send_user "\nInvalid time limit. (Within: inc21.30.10)\n"
		return
	}

# There may be some GrpCPUmins data left from previous jobs, so use less than the limit
	spawn $salloc --account=$acct -n4 --time=$time_limit $srun $bin_sleep 10
	expect {
		-re "Granted job allocation ($number)" {
			set job_id1 $expect_out(1,string)
			send_user "\nJob $job_id1 has been submitted. (Within: inc21.30.10)\n"
		}
		-re "Pending job allocation ($number)" {
			set job_id1 $expect_out(1,string)
			send_user "\nFAILURE: Job should be running, but is not. (Within: inc21.30.10)\n"
			set exit_code 1
		}
		-re "error" {
			send_user "\nFAILURE: Job allocation should not have failed. (Within: inc21.30.10)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc not responding. (Within: inc21.30.10)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	spawn $salloc --account=$acct -n4 -t[expr $target_time + 1] $srun $bin_sleep 10
	expect {
		-re "Pending job allocation ($number)" {
			set job_id2 $expect_out(1,string)
			send_user "\nJob $job_id2 is waiting for resources. This is expected. (Within: inc21.30.10)\n"
		}
		-re "Granted job allocation ($number)" {
			send_user "\nFAILURE: Job should be pending, but is not. (Within: inc21.30.10)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc not responding. (Within: inc21.30.10)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	# Cancel both jobs
	cancel_job $job_id1
	cancel_job $job_id2
}
