#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.30
#          Tests if the Grpwall limit is enforced
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 srun acct bin_sleep grpwall_num qostest

	log_info "Starting Grpwall test"
	set job_id_list     [list]

	# Raise an error to abort the catch block
	set exception_code [catch {

		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.
		if [reset_qos_usage "" $qostest] {
			error "Unable to reset QOS usage"
		}

		log_debug "Running $jobs jobs of $sleep_time seconds of duration to ensure that we reach the Grpwall limit of $grpwall_num minutes"
		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 "Submitting the final job and check that it is set Pending with Reason QOSGrpWallLimit"
		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"]
		subtest {! [wait_job_reason [lindex $job_id_list end] "PENDING" "QOSGrpWallLimit"]} "Job should achieve PENDING state with reason QOSGrpWallLimit"

		# Reset the QoS usage
		if [reset_qos_usage "" $qostest] {
			error "Unable to reset QOS usage"
		}

	} message] ; # Store the error message in $message

	# Cancel jobs
	cancel_job $job_id_list

	# Convert any errors into failures (after cleaning up)
	if {$exception_code == 1} { ; # errors only
		fail "Failure testing Group wall: $message"
	}
}
