############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.30
#          Tests if the MaxNodesPU 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_16 { } {

	global salloc srun acct number bin_sleep maxnodespu_num exit_code

	send_user "\nStarting MaxNodesPerUser test (Within: inc21.30.16)\n"

	set job_id 0

	spawn $salloc --account=$acct -N$maxnodespu_num -t1 --exclusive $srun $bin_sleep 2
	expect {
		-re "Granted job allocation ($number)" {
			set job_id $expect_out(1,string)
			send_user "\nJob $job_id was submitted (Within: inc21.30.16)\n"
		}
		-re "error" {
			send_user "\nFAILURE: Job allocation should not have failed. (Within: inc21.30.16)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc is not responding (Within: inc21.30.16)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if { $job_id == 0 } {
		send_user "\nFAILURE: Job was not submitted (Within: inc21.30.16)\n"
		set exit_code 1
	} else {
		# Cancel job
		cancel_job $job_id
	}

	spawn $salloc --account=$acct -N[expr $maxnodespu_num + 1] -t1 --exclusive $srun $bin_sleep 2
	expect {
		-re "Pending job allocation ($number)" {
			set job_id $expect_out(1,string)
			send_user "\nThis error is expected. Do not worry (Within: inc21.30.16)\n"
		}
		-re "Granted job allocation" {
			send_user "\nFAILURE: salloc should not have granted an allocation (Within: inc21.30.16)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc is not responding (Within: inc21.30.16)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if { $job_id == 0 } {
		send_user "\nFAILURE: Job was not submitted (Within: inc21.30.16)\n"
		set exit_code 1
	} else {
		# Cancel job
		cancel_job $job_id
	}
}
