#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test that a job correctly uses the -E or --preserve-env flag.
############################################################################
# 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 file_in          "$test_dir/input"
set file_out         "$test_dir/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  ""

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

#
# Build input script file
# It includes line prefix for a convenience in later parsing
#
make_bash_script $file_in "
result\[0\]=\$($bin_printenv SLURM_NNODES)
result\[1\]=\$($srun -E -n1 -N1 $bin_printenv SLURM_NNODES)
result\[2\]=\$($bin_printenv SLURM_NTASKS)
result\[3\]=\$($srun --preserve-env -n1 -N1 $bin_printenv SLURM_NTASKS)
result\[4\]=\$($srun -n1 -N1 $bin_printenv SLURM_NNODES)
result\[5\]=\$($srun -n1 -N1 $bin_printenv SLURM_NTASKS)
echo Result: \${result\[*\]}
"

#
# Run job to determine what nodes are available
#
set job_id [submit_job -fail "-N$min_nodes-$max_nodes -n$num_procs -O -t1 --output=$file_out $file_in"]

wait_for_job -fail $job_id "DONE"
wait_for_file -fail $file_out

set output [run_command -fail "$bin_cat $file_out"]
regexp 	"Result:.($number).($number).($number).($number).($number).($number)" \
	$output - num_nodes_test1 num_nodes_test2 num_procs_test1 \
	          num_procs_test2 num_nodes_test3 num_procs_test3

subtest {$num_nodes_test1 == $num_nodes_test2} "SLURM_NNODES should be preserved" "$num_nodes_test1 != $num_nodes_test2"
subtest {$num_procs_test1 == $num_procs_test2} "SLURM_NTASKS should be preserved" "$num_procs_test1 != $num_procs_test2"

subtest {$num_nodes_test3 == 1} "SLURM_NNODES should be 1" "$num_nodes_test3 != 1"
subtest {$num_procs_test3 == 1} "SLURM_NTASKS should be 1" "$num_procs_test3 != 1"
