#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of --deadline and --begin option and --time_min too long
############################################################################
# Copyright (C) 2020 SchedMD LLC.
# Written by Albert Gil <albert.gil@schedmd.com>
#
# 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 ""

proc cleanup {} {
	global job_list
	cancel_job $job_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'"
}

#
# 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 $job_block "RUNNING"

#
# Submit a salloc job with deadline that will rest pending until deadlines
#
spawn $salloc -w $node --begin=now+5 --deadline=now+75 --time-min=1 -J $test_name $bin_sleep 300
expect {
	-re "job ($number) queued and waiting for resources" {
		set job_id $expect_out(1,string)
	}
	-re "Granted job allocation ($number)" {
		fail "job granted and it shouldn't"
	}
	timeout {
		fail "salloc not responding"
	}
}
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 10 {$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 60 {$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"

#
# Wait and check that job state reach the deadline
#
set state "PENDING"
wait_for -timeout 60 {$state == "DEADLINE"} {
	set state [get_job_param $job_id "JobState"]
}
subtest {$state == "DEADLINE"} "Job should end with state DEADLINE" "$state != DEADLINE"

#
# 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"
