#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.30
#          Tests if the group nodes 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 <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.
############################################################################

# Test GrpNode limit
proc inc21_30_1 { wait_reason } {

	global srun salloc acct bin_sleep number grn_num check_reason
	global exit_code

	log_info "Starting GrpNode limit test (Within: inc21.30.1)"
	set job_id1  0
	set job_id2  0
	spawn $salloc -N$grn_num --account=$acct --exclusive -t1 $srun $bin_sleep 10
	expect {
		-re "Granted job allocation ($number)" {
			set job_id1 $expect_out(1,string)
			log_debug "Job $job_id1 has been submitted. (Within: inc21.30.1)"
		}
		-re "Pending job allocation ($number)" {
			log_error "Job should be running, but is not. (Within: inc21.30.1)"
			set job_id1 $expect_out(1,string)
			set exit_code 1
		}
		-re "error" {
			log_error "Job allocation should not have failed. (Within: inc21.30.1)"
			set exit_code 1
		}
		timeout {
			log_error "salloc is not reponding. (Within: inc21.30.1)"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$job_id1 == 0} {
		log_error "Jobs were not submitted. (Within: inc21.30.1)"
		set exit_code 1
		return
	}

	spawn $salloc -N1 --account=$acct --exclusive -t1 $srun $bin_sleep 10
	expect {
		-re "Pending job allocation ($number)" {
			set job_id2 $expect_out(1,string)
			log_debug "Job $job_id2 is waiting for resources. This is expected. (Within: inc21.30.1)"
		}
		-re "Granted job allocation ($number)" {
			log_error "This should be pended but is not. (Within: inc21.30.1)"
			set exit_code 1
		}
		timeout {
			log_error "salloc is not reponding. (Within: inc21.30.1)"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$job_id2 == 0} {
		log_error "Jobs were not submitted. (Within: inc21.30.1)"
		set exit_code 1
	} else {
		# checks the state of the job
		if {![check_job_state $job_id2 PENDING]} {
			log_error "Job was not on the desired state (Within: inc21.30.1)"
			set exit_code 1
		}
	}

	if {![check_reason $job_id2 $wait_reason]} {
		log_error "Bad job wait reason. (Within: inc21.30.1)"
		set exit_code 1
	}

	# Cancels remaining jobs
	cancel_job $job_id1
	cancel_job $job_id2
}
