#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test --deadline to be used as the timelimit.
#
#          At the moment this test only really works if the DefaultTime is
#          longer than 1 hour to verify the timelimit is set accordingly.
############################################################################
# Copyright (C) 2015 CEA/DAM/DIF
# Written by Aline Roy <aline.roy@cea.fr>
#
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set file_in     "$test_dir/input"
set job_id      0
set default_part [default_partition]

#
# We must be SlurmUser or root in order to change the partition MaxTime limit,
# otherwise this test may fail.
#
if {![is_super_user]} {
	skip "You must be SlurmUser or root to run this test"
}

proc cleanup {} {
	global job_id

	cancel_job $job_id
	# Restore partition MaxTime and DefaultTime.
	reconfigure
}

#
# Ensure that MaxTime is UNLIMITED so this test won't fail due to trying to set
# time limits greater than MaxTime. Ensure DefaultTime is UNLIMITED so the job
# time limit is what we expect.
#
run_command -fail "$scontrol update partitionname=$default_part MaxTime=-1 DefaultTime=-1"

set timelimit [get_partition_maximum_time_limit $default_part]
if { $timelimit != -1 } {
	fail "Unable to update partition MaxTime to UNLIMITED"
}
set timelimit [get_partition_default_time_limit $default_part]
if { $timelimit != -1 } {
	fail "Unable to update partition DefaultTime to UNLIMITED"
}

#
# Build input script file
#
exec $bin_rm -f $file_in
make_bash_script $file_in "$bin_sleep 1"

#
# Spawn a batch job that deadline is too late
#
set job_id [submit_job -fail "--output=/dev/null --error=/dev/null --deadline now+1hour $file_in"]

wait_for_job -fail $job_id DONE

#
# verify time limit
#
set timelimit [get_job_param $job_id "TimeLimit"]
subtest {[regexp "00:59:" $timelimit] ||
         [regexp "01:00:00" $timelimit]} "Verify that TimeLimit of the job ($job_id) is 1h"
