#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of --deadline and --begin option and --time_min too long
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 job_list [list]

#
# Get the node to use
#
set node [get_nodes_by_request "-N1 -t 5"]
if {[llength $node] != 1} {
	skip "This test needs to be able to submit a job with '-N1 -t 5'"
}

proc cleanup {} {
	global job_list

	cancel_job $job_list
}

#
# Submit a blocking job
#
set job_block [submit_job -fail "-w $node --exclusive -t 5 --wrap '$bin_sleep 300' -J $test_name -o /dev/null -e /dev/null"]
lappend job_list $job_block
wait_for_job -fail $job_block "RUNNING"

#
# Submit a job with deadline that will rest pending until deadlines
#
set job_id [submit_job -fail "-w $node --begin=now+15 --deadline=now+80 --time-min=1 --wrap '$bin_sleep 60' -J $test_name -o /dev/null -e /dev/null"]
lappend job_list $job_id

#
# Check that job goes from PENDING with reason BeginTime to finally
# end with DEADLINE.
#
set state  ""
set reason ""
wait_for -timeout 15 {$state == "PENDING" && $reason == "BeginTime"} {
	set state  [get_job_param $job_id "JobState"]
	set reason [get_job_param $job_id "Reason"]
}
subtest {$state == "PENDING" && $reason == "BeginTime"} "Job should be PENDING with reason BeginTime" "JobState=$state Reason=$reason"

wait_for -timeout 120 {$state == "DEADLINE" && $reason == "DeadLine"} {
	set state  [get_job_param $job_id "JobState"]
	set reason [get_job_param $job_id "Reason"]
}
subtest {$state == "DEADLINE" && $reason == "DeadLine"} "Job should end with state DEADLINE and reason DeadLine" "JobState=$state Reason=$reason"

#
# Test that the state is accounted properly
#
if {[get_config_param "AccountingStorageType"] ne "accounting_storage/slurmdbd"} {
	skip "Can not test without accounting enabled"
}

set sacct_state ""
wait_for -timeout 30 {[regexp "DEADLINE" $sacct_state]} {
	set sacct_state [run_command_output -fail "$sacct  -n -P -X -j $job_id -o State"]
}
subtest {[regexp "DEADLINE" $sacct_state]} "Job should be accounted with state DEADLINE" "$sacct_state != DEADLINE"
