#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validates that sgather -p option preserves file state.
#
# 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.
############################################################################
# Copyright (C) 2011-2013 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id       "32.9"
set job_id        0
set hostname      ""
set file_in       "test${test_id}.in"
set file_out      "test${test_id}.out"
set sgather_tmp   "/tmp/test${test_id}"
set sgather_out   "test${test_id}_sgather.out"
set exit_code     0

print_header $test_id

if {[file executable $sgather] == 0} {
	send_user "\nWARINING $sgather does not exist\n"
	exit 0
}

if {[test_front_end] != 0} {
	send_user "\nWARNING: This test is incompatible with front-end systems\m"
	exit 0
}
if {[test_multiple_slurmd] != 0} {
	send_user "\nWARNING: This test is incompatible with multiple slurmd systems\n"
	exit 0
}
if {[slurmd_user_root] == 0} {
	send_user "\nWARNING: This test is incompatible with SlurmdUser != root\n"
	exit 0
}

#
# Remove any vestigial files
#
exec $bin_rm -f $file_in $file_out

# Set env PATH to slurm dir
set env(PATH) $slurm_dir/bin:$env(PATH)

make_bash_script $file_in "
env | grep SLURM_NNODES
$bin_rm -f ${sgather_out}\*
$srun $bin_cp -f $sgather $sgather_tmp
$srun touch -t01020304 $sgather_tmp
$srun ls -l $sgather_tmp
$sgather -p $sgather_tmp $sgather_out
ls -l ${sgather_out}\*
$bin_rm -f ${sgather_out}\*
$srun ls -l $sgather_tmp
$srun $bin_rm -f $sgather_tmp
exit 0
"

spawn $sbatch -N1-2 -o $file_out -t2 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch job was not submitted\n"
	exit 1
}

wait_for_job $job_id "DONE"

set matches   0
set node_cnt 99
if {[wait_for_file $file_out] == 0} {
	spawn $bin_cat $file_out
	expect {
		-re "SLURM_NNODES=($number)" {
			set node_cnt $expect_out(1,string)
			exp_continue
		}
		-re " Jan +2 " {
			incr matches
			exp_continue
		}
		-re "No such file or directory" {
			send_user "\nError is expected. No worries.\n"
			incr rm_cnt
			exp_continue
		}
		eof {
			wait
		}
	}
}
if {$matches != [expr $node_cnt * 2]} {
	send_user "\nFAILURE: Failed to preserve file time ($matches != [expr $node_cnt * 2])\n"
	set exit_code 1
}

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