#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.30
#          Tests if the Grpwall 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_12 { } {
	global salloc srun acct bin_sleep grpwall_num number exit_code qostest

	log_info "Starting Grpwall test (Within: inc21.30.12)"
	set job_id_list     [list]
	set jobs            5.0
	set grpwall_per_job [expr $grpwall_num * 1.1 / $jobs]
	set sleep_time      [expr int(ceil($grpwall_per_job * 60))]
	set job_time        [expr int(ceil($grpwall_per_job))]
	set timeout         120

	# Since wall is a decayed variable lets reset it to make sure the test
	# gets exactly what we would expect.
	incr exit_code [reset_qos_usage "" $qostest]

	log_debug "Running $jobs jobs of $sleep_time seconds of duration to ensure that we reach the Grpwall limit of $grpwall_num minutes (Within: inc21.30.12)"
	for {set i 0} {$i < $jobs} {incr i} {
		lappend job_id_list [submit_job -fail "--account=$acct --qos=$qostest -N1 -t$job_time --wrap '$bin_sleep $sleep_time' -o /dev/null -e /dev/null"]
	}
	foreach job_id $job_id_list {
		wait_job_reason $job_id COMPLETED
	}

	log_debug "Submiting the final job and check that it is set Pending with Reason QOSGrpWallLimit (Within: inc21.30.12)"
	set job_id [submit_job -fail "--account=$acct --qos=$qostest -N1 -t$job_time --wrap '$bin_sleep $sleep_time' -o /dev/null -e /dev/null"]
	lappend job_id_list $job_id
	# Subtest of the limit
	set exit_code [wait_job_reason $job_id PENDING QOSGrpWallLimit]

	# Reset the QoS usage
	incr exit_code [reset_qos_usage "" $qostest]

	# Cancel jobs
	cancel_job $job_id_list
}
