#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of job suspend/resume.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2005-2007 The Regents of the University of California.
# Copyright (C) 2008 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of SLURM, a resource management program.
# For details, see <http://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.
############################################################################
source ./globals

set test_id        "3.7"
set exit_code      0
set file_out1      "test$test_id.output.1"
set file_out2      "test$test_id.output.2"
set file_prog      "test$test_id.prog"
set file_prog_sh1  "test$test_id.prog.sh1"
set file_prog_sh2  "test$test_id.prog.sh2"
set job_id1        0
set job_id2        0
set host_name      ""
set not_supported  0

print_header $test_id

if {[test_gang] != 0} {
	send_user "\nWARNING: This test can't be run with gang scheduling\n"
	exit 0
}
if {[is_super_user] == 0} {
	send_user "\nWARNING: This test can't be run except as SlurmUser\n"
	exit 0
}

set conf_test 0
log_user 0
spawn $scontrol show config
expect {
	-re "launch/poe" {
		incr conf_test
		exp_continue
	}
	-re "proctrack/pgid" {
		incr conf_test
		exp_continue
	}
	eof {
		wait
	}
}
log_user 1
if {$conf_test == 2} {
	send_user "\nWARNING: launch/poe plus proctrack/pgid are incompatible with this test\n"
	exit 0
}

proc suspend_job { job_id operation } {
	global exit_code scontrol not_supported

	spawn $scontrol $operation $job_id
	expect {
		-re "Requested operation not supported" {
			send_user "\nWARNING: job suspend not supported\n"
			set not_supported 1
			exp_continue
		}
		-re "error" {
			send_user "\nFAILURE: scontrol error\n"
			set exit_code 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}

proc check_output { file_name } {
	global exit_code bin_cat
	set match1 0
	set match2 0

	spawn $bin_cat $file_name
	expect {
		-re "JobSuspended" {
			set match1 1
			exp_continue
		}
		-re "AllDone" {
			set match2 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: cat not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if { $match1 == 0 } {
		send_user "\nFAILURE: job never suspended\n"
		set exit_code 1
	}
	if { $match2 == 0 } {
		send_user "\nFAILURE: job failed to run to completion\n"
		set exit_code 1
	}
}

#
# Delete left-over program and rebuild it
#
file delete $file_prog $file_out1 $file_out2
make_bash_script $file_prog_sh1 "$srun ./$file_prog"
make_bash_script $file_prog_sh2 "./$file_prog"
exec $bin_cc -o $file_prog ${file_prog}.c
exec $bin_chmod 700 $file_prog

#
# Submit two jobs to the same node,.
# The first job includes srun, second only the application
#
set srun_pid [spawn $sbatch -N1 -t2 --output=$file_out1 $file_prog_sh1]
expect {
	-re "Submitted batch job ($number)" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id1 == 0} {
	send_user "\nFAILURE: srun failed to report jobid\n"
	exit 1
}
# Get the job's allocate host name
if {[wait_for_job $job_id1 RUNNING] != 0} {
	send_user "\nFAILURE: waiting for job $job_id1 to run\n"
	exit 1
}
spawn $scontrol show job $job_id1
expect {
	-re " NodeList=($alpha_numeric_under)" {
		set host_name $expect_out(1,string)
		exp_continue
	}
	-re "MidplaneList=($alpha_numeric_under)" {
		set host_name $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		cancel_job $job_id1
		exit 1
	}
	eof {
		wait
	}
}
if {[string compare $host_name ""] == 0} {
	send_user "\nFAILURE: did not get hostname for job $job_id1\n"
	cancel_job $job_id1
	exit 1
}
# Submit another job to that same node
set srun_pid [spawn $sbatch -N1 -t2 --output=$file_out2 $file_prog_sh2]
expect {
	-re "Submitted batch job ($number)" {
		set job_id2 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id2 == 0} {
	send_user "\nFAILURE: srun failed to report jobid\n"
	cancel_job $job_id1
	exit 1
}

#
# Stop job 1 and let job 2 begin,
# Stop job 2 and let job 1 complete,
# Then resume job 2
# The sleep commands are added so the program can recognize
# that it has been suspended.
# The long initial sleep is due to POE launching jobs slowly
#
sleep 10
suspend_job $job_id1 suspend
if {$not_supported == 1} {
	exec $bin_rm -f $file_out1 $file_out2 $file_prog $file_prog_sh1 $file_prog_sh2
	cancel_job $job_id1
	exit 0
}
if {$not_supported == 0} {
	if {[wait_for_job $job_id2 RUNNING] != 0} {
		send_user "\nFAILURE: waiting for job $job_id2 to run\n"
		cancel_job $job_id1
		cancel_job $job_id2
		exit 1
	}
	sleep 5
	suspend_job $job_id2 suspend
	suspend_job $job_id1 resume
	if {[wait_for_job $job_id1 DONE] != 0} {
		send_user "\nFAILURE: waiting for job $job_id1 to complete\n"
		cancel_job $job_id1
		cancel_job $job_id2
		exit 1
	}
	suspend_job $job_id2 resume
}
if {[wait_for_job $job_id2 DONE] != 0} {
	send_user "\nFAILURE: waiting for job $job_id1 to complete\n"
	cancel_job $job_id1
	cancel_job $job_id2
	exit 1
}
if {$not_supported == 0} {
	wait_for_file $file_out1
	wait_for_file $file_out2

	check_output $file_out1
	check_output $file_out2
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_out1 $file_out2 $file_prog $file_prog_sh1 $file_prog_sh2
	send_user "\nSUCCESS\n"
} else {
	send_user "\nFAILURE: May be due to use of gang scheduler, a race conditions, or the ProcTrack plugin not identifying the application as part of the job.\n"
}
exit $exit_code
