#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test that a job correctly uses the -E or --preserve-env flag.
#
# 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) 2008-2009 Lawrence Livermore National Security
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Dave Bremer <dbremer@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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          "1.45"
set exit_code        0
set file_in          "test$test_id.input"
set file_out         "test$test_id.output"
set job_id           0
set min_nodes        1
set max_nodes        3
set num_nodes_test1  ""
set num_nodes_test2  ""
set num_nodes_test3  ""
set num_procs        6
set num_procs_test1  ""
set num_procs_test2  ""
set num_procs_test3  ""

print_header $test_id

#
# Build input script file
#
make_bash_script $file_in "
   $bin_printenv SLURM_NNODES
   $srun -E -n1 -N1 $bin_printenv SLURM_NNODES
   $bin_printenv SLURM_NTASKS
   $srun --preserve-env -n1 -N1 $bin_printenv SLURM_NTASKS
   $srun -n1 -N1 $bin_printenv SLURM_NNODES
   $srun -n1 -N1 $bin_printenv SLURM_NTASKS"

#
# Run job to determine what nodes are available
#
spawn $sbatch -N$min_nodes-$max_nodes -n$num_procs -O -t1 --output=$file_out $file_in
expect {
	-re "nodes *: ($number)" {
		if {$expect_out(1,string) != 3} {
			send_user "\nFAILURE: failed to process --nodes option\n"
			set exit_code 1
		}
		exp_continue
	}
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job did not complete\n"
	cancel_job $job_id
	exit 1
}
if {[wait_for_file $file_out] != 0} {
	send_user "\nFAILURE: no output file\n"
	exit 1
}

# NOTE: There could be warning messages generated by the srun commands,
# For example: "srun: Job step creation temporarily disabled, retrying"
# would be due to nodes being powered down
set index 0
spawn $bin_cat $file_out
expect {
	-re "^($number)\r\n" {
		incr index
		if {$index == 1} {set num_nodes_test1 $expect_out(1,string)}
		if {$index == 2} {set num_nodes_test2 $expect_out(1,string)}
		if {$index == 3} {set num_procs_test1 $expect_out(1,string)}
		if {$index == 4} {set num_procs_test2 $expect_out(1,string)}
		if {$index == 5} {set num_nodes_test3 $expect_out(1,string)}
		if {$index == 6} {set num_procs_test3 $expect_out(1,string)}
		exp_continue
	}
	# If the step creation was delayed this message is listed.
	# Checking for it here will skip over the warning so we can get the
	# real value.  I would say this is a bug in expect, but without this
	# check we don't always get what we need.
	-re "Step created for job $number\r\n" {
		exp_continue
	}
	eof {
		wait
	}
}

if {$num_nodes_test1 != $num_nodes_test2} {
	send_user "\nFAILURE: SLURM_NNODES was not preserved ($num_nodes_test1 != $num_nodes_test2)\n"
	set exit_code 1
}

if {$num_procs_test1 != $num_procs_test2} {
	send_user "\nFAILURE: SLURM_NTASKS was not preserved ($num_procs_test1 != $num_procs_test2)\n"
	set exit_code 1
}

if {$num_nodes_test3 != 1} {
	send_user "\nFAILURE: SLURM_NNODES should be 1 ($num_nodes_test3 != 1)\n"
	set exit_code 1
}

if {$num_procs_test3 != 1} {
	send_user "\nFAILURE: SLURM_NTASKS should be 1 ($num_procs_test3 != 1)\n"
	set exit_code 1
}

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

exit $exit_code
