#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of job array 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.
# Copyright (C) 2015 SchedMD LLC.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette@schedmd.com>
# 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        "28.8"
set array_size     4
set exit_code      0
set file_script    "test$test_id.bash"
set job_id         0
set not_supported  0

print_header $test_id

if {[get_array_config] < [expr $array_size + 1]} {
	send_user "\nWARNING: MaxArraySize is to small for this test\n"
	exit 0
}
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 "No error" {
			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
	}
}

################Test Starts Here################
make_bash_script $file_script "sleep 100"

spawn $sbatch -N1 --array=0-[expr $array_size -1] --output=/dev/null -t1 $file_script
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		send_user "\njob $job_id was submitted\n"
		exp_continue
	}
	-re "error" {
		send_user "\nFAILURE: sbatch did not submit jobs\n"
		set exit_code 1
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {[wait_for_job ${job_id}_1 RUNNING] != 0} {
	send_user "\nFAILURE: error starting job ${job_id}_1\n"
	cancel_job $job_id
	exit 1
}

sleep 5
suspend_job $job_id suspend
if {$not_supported == 1} {
	exec $bin_rm -f $file_script
	cancel_job $job_id
	exit 0
}


if {[wait_for_job ${job_id}_1 SUSPENDED] != 0} {
	send_user "\nFAILURE: error suspending job $job_id\n"
	cancel_job $job_id
	exit 1
}

suspend_job $job_id resume
if {[wait_for_job ${job_id}_1 RUNNING] != 0} {
	send_user "\nFAILURE: error resuming job $job_id\n"
	cancel_job $job_id
	exit 1
}
cancel_job $job_id

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