#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of account number and job dependencies (--account, --begin
#          and --depedency options).
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
# Note:    This script generates and then deletes files in the working directory 
#          named test1.42.input
# Note:    This test will fail for SLURM versions <0.4
############################################################################
# Copyright (C) 2004 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# UCRL-CODE-217948.
# 
# This file is part of SLURM, a resource management program.
# For details, see <http://www.llnl.gov/linux/slurm/>.
#  
# 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 test_id     "1.42"
set file_in     "test$test_id.input"
set exit_code   0
set job_id1     0
set job_id2     0

print_header $test_id

global env
set env(SLURM_ACCOUNT) QA_ACCT

#
# Delete left-over input script plus stdout/err files
# Build input script file
#
exec $bin_rm -f $file_in
exec echo "#!$bin_bash"     >$file_in
exec echo "$bin_sleep 5"  >>$file_in
exec $bin_chmod 700 $file_in

#
# Spawn a srun batch job that just sleeps for a while
#
set timeout $max_job_delay
spawn $srun --batch --output=/dev/null --error=/dev/null --account=MY_ACCT -t1 $file_in
expect {
	-re "jobid ($number) submitted" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$job_id1 == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Submit a dependent job
#
set match_acct  0
set match_state 0
spawn $srun -v --dependency=$job_id1 -t1 $scontrol show job $job_id1
expect {
	-re "launching ($number).0" {
		set job_id2 $expect_out(1,string)
		exp_continue
	}
	-re "JobState=COMPLETE" {
		set match_state 1
		exp_continue
	}
	-re "Account=MY_ACCT" {
		set match_acct 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$match_acct == 0} {
	send_user "\nFAILURE: Account information not processed\n"
	set exit_code 1
}
if {$match_state == 0} {
	send_user "\nFAILURE: Dependent job not completed\n"
	set exit_code 1
}

#
# Confirm dependency info within second job as well as environment 
# variable based SLURM account
#
set match_acct
set match_jobid 0
spawn $scontrol show job $job_id2
expect {
	-re "Dependency=($number)" {
		set match_jobid $expect_out(1,string)
		exp_continue
	}
	-re "Account=QA_ACCT" {
		set match_acct
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$match_acct == 0} {
	send_user "\nFAILURE: Account information not processed\n"
	set exit_code 1
}
if {$match_jobid != $job_id1} {
	send_user "\nFAILURE: Dependency information not processed\n"
	set exit_code 1
}

#
# Submit a job to run at noon tomorrow
#
set job_id1 0
spawn $srun --batch --output=/dev/null --error=/dev/null --begin=noon-tomorrow $file_in
expect {
	-re "jobid ($number) submitted" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$job_id1 == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}
exec $bin_sleep 5
set match 0
spawn $scontrol show job $job_id1
expect {
	-re "JobState=PENDING" {
		incr match
		exp_continue
	}
	-re "StartTime=($number)/($number)-12:00:00" {
		incr match
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$match != 2} {
	send_user "\nFAILURE: unexpected JobState or StartTime\n"
	set exit_code 1
}
# Reset start time and test for completion
spawn $scontrol update JobId=$job_id1 StartTime=now
expect {
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
set delayed 0
set is_done 0
while { $delayed < $max_job_delay } {
	exec $bin_sleep 10
	incr delayed +10
	spawn $scontrol show job $job_id1
	expect {
		-re "JobState=COMPLETED" {
			set is_done 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$is_done == 1} {
		break
	}
}
if {$is_done == 0} {
	send_user "\nFAILURE: unexpected JobState\n"
	exec $scancel --quiet $job_id1
	set exit_code 1
}


if {$exit_code == 0} {
	exec $bin_rm -f $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
