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

proc inc21_30_3 {} {

# test GrpJob limits
	global salloc srun acct bin_sleep jobmatch job_id1 job_id2 job_id3 number exit_code grjobs_num

	send_user "\nStarting GrpJob limit test\n\n"
	set check_num $grjobs_num

	set jobmatch 0
	for {set inx 0} {$inx <= $check_num} {incr inx} {
		spawn $salloc --account=$acct -n1 $srun $bin_sleep 100
		expect {
			-re "Granted job allocation ($number)" {
				set job_id($inx) $expect_out(1,string)
				if {$inx < $check_num} {
					send_user "\njob $inx $job_id($inx) was submitted\n"
					incr jobmatch
				} else {
					send_user "\nFAILURE: Job $inx $job_id($inx) wasn't suppose to work but it did, limit was $check_num?\n"
					set exit_code 1
				}
			}
			-re "job ($number) queued and waiting for resources" {
				set job_id($inx) $expect_out(1,string)
				if {$inx >= $check_num} {
					send_user "\nJob $inx $job_id($inx) is waiting for resources. This is expected\n"
					incr jobmatch
				} else {
					send_user "\nFAILURE: Job $inx $job_id($inx) was suppose to work but it didn't, limit was $check_num?\n"
					set exit_code 1
				}
			}
			timeout {
				send_user "\nFAILURE: salloc is not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}
	}

	if {$jobmatch==0} {
		send_user "\nFAILURE: jobs were not submitted\n"
		set exit_code 1
	}

	#checks the job state
	if [info exists job_id($check_num)] {
		check_state $job_id($check_num)
	} else {
		send_user "\nFAILURE: didn't attempt to start enough jobs\n"
		set exit_code 1
	}

	for {set inx 0} {$inx <= $check_num} {incr inx} {
		if [info exists job_id($inx)] {
			# cancels the jobs
			cancel_job $job_id($inx)
		}
	}
}





















