#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.30
#          Tests if the QOS/Parent QOS MaxWall limit is used as
#          job's TimeLimit if the job did't request --time
############################################################################
# Copyright (C) 2013 SchedMD LLC
# Written by Alejandro Sanchez <alex@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.
############################################################################

# Checks the timelimit of the job
proc check_timelimit { job } {

	global scontrol job_id exit_code maxwall_num

	set timelimit_match 0
	spawn $scontrol show job $job
	expect {
		-re "TimeLimit=00:0$maxwall_num:00" {
			incr timelimit_match
		}
		timeout {
			log_error "scontrol not responding"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$timelimit_match != 1} {
		log_error "Job should have as TimeLimit the QOS MaxWall"
		set exit_code 1
	}
}

proc inc21_30_17 { } {
	global salloc srun acct bin_sleep number maxwall_num exit_code skips

	# If the partition has DefaultTime set it will mess up this test.
	# Since there is currently no way to use scontrol to change default
	# time to "none" just skip this subtest.
	if { [get_partition_default_time_limit ""] != -1 } {
		log_warn "SKIP: Default partition has a default time limit set, cannot run inc21.30.17"
		incr skips
		return
	}

	log_info "Starting Qos/Parent MaxWall applied to job without -t test (Within: inc21.31.17)"
	set job_id 0

	spawn $salloc --account=$acct -N1 $srun $bin_sleep 10
	expect {
		-re "Granted job allocation ($number)" {
			set job_id $expect_out(1,string)
			log_debug "Job $job_id has been submitted"
			check_timelimit $job_id
		}
		-re "Pending job allocation ($number)" {
			set job_id $expect_out(1,string)
			log_error "job $job_id should not be waiting for resources (Within: inc21.30.17)"
			set exit_code 1
		}
		-re "error" {
			log_error "Job allocation should not have failed. (Within: inc21.30.17)"
			set exit_code 1
		}
		timeout {
			log_error "salloc is not responding (Within: inc21.30.17)"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	# Cancel job
        cancel_job $job_id
}
