#!/usr/bin/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 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://computing.llnl.gov/linux/slurm/>.
# 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_procs        6
set num_nodes_test1  ""
set num_nodes_test2  ""
set num_nodes_test3  ""
set num_procs_test1  ""
set num_procs_test2  ""
set num_procs_test3  ""


print_header $test_id

if { [test_xcpu] } {
	send_user "\nWARNING: This test is incompatable with XCPU systems\n"
	exit 0
}

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

#
# 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
}

spawn $bin_cat $file_out
expect {
	-re "($number)\r\n($number)\r\n($number)\r\n($number)\r\n($number)\r\n($number)" {
		set num_nodes_test1 $expect_out(1,string)
		set num_nodes_test2 $expect_out(2,string)
		set num_procs_test1 $expect_out(3,string)
		set num_procs_test2 $expect_out(4,string)
		set num_nodes_test3 $expect_out(5,string)
		set num_procs_test3 $expect_out(6,string)
	}
	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_NPROCS 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_NPROCS should be 1 ($num_procs_test3 != 1)\n"
	set exit_code 1
}

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

exit $exit_code
