############################################################################
# Purpose: Test of SLURM functionality
#          to be called from test21.34
#          Tests if the GrpCPUmins limit is enforced
############################################################################
# Copyright (C) 2015 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of SLURM, a resource management program.
# For details, see <http://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.
############################################################################

# Test GrpCPUmins
proc inc21_34_1 { qostest } {
	global salloc srun nthreads acct bin_sleep grpcpumin_num number
	global totcpus test_node exit_code

	send_user "\nStarting GrpCpuMins test (Within: inc21.34.1) \n\n"

	if  { [test_enforce_safe_set] == 0 } {
		send_user "\nWARNING: This test can't be run without AccountingStorageEnforce having \"safe\" in it\n"
		return
	}

	set job_id1 0
	set job_id2 0
	set timeout [expr $grpcpumin_num * 120]
	set timelimit [expr $grpcpumin_num / $nthreads]
	# Since GrpCpuMins is a decayed variable lets reset it to make sure
	# the test gets exactly what we would expect.
	reset_qos_usage "" $qostest

	spawn $salloc --account=$acct -w$test_node -n$nthreads -t$timelimit $srun $bin_sleep 1
	expect {
		-re "Granted job allocation ($number)" {
			set job_id1 $expect_out(1,string)
			send_user "\nJob $job_id1 has been submitted. (Within: inc21.34.1)\n"
		}
		-re "Pending job allocation ($number)" {
			set job_id1 $expect_out(1,string)
			send_user "\nFAILURE: This job should not be pending (Within: inc21.34.1)\n"
			set exit_code 1
		}
		-re "error" {
			send_user "\nFAILURE: Job allocation should not have failed. (Within: inc21.34.1)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc not responding. (Within: inc21.34.1)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	cancel_job $job_id1

	reset_qos_usage "" $qostest

	spawn $salloc --account=$acct -w$test_node -n$nthreads -t[expr $timelimit + 1] $srun $bin_sleep 1
	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.34.1)\n"
		}
		-re "Granted job allocation ($number)" {
			set job_id2 $expect_out(1,string)
			send_user "\nFAILURE: Job should be pending but is not. (Within: inc21.34.1)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc not responding. (Within: inc21.34.1)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	cancel_job $job_id2
}
