#!/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 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  ""

#
# 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} {
			log_error "Failed to process --nodes option"
			set exit_code 1
		}
		exp_continue
	}
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "Job not submitted"
}
if {[wait_for_job $job_id "DONE"] != 0} {
	cancel_job $job_id
	fail "Job did not complete"
}
if {[wait_for_file $file_out] != 0} {
	fail "No output file"
}

# 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)$eol" {
		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$eol" {
		exp_continue
	}
	eof {
		wait
	}
}

if {$num_nodes_test1 != $num_nodes_test2} {
	log_error "SLURM_NNODES was not preserved ($num_nodes_test1 != $num_nodes_test2)"
	set exit_code 1
}

if {$num_procs_test1 != $num_procs_test2} {
	log_error "SLURM_NTASKS was not preserved ($num_procs_test1 != $num_procs_test2)"
	set exit_code 1
}

if {$num_nodes_test3 != 1} {
	log_error "SLURM_NNODES should be 1 ($num_nodes_test3 != 1)"
	set exit_code 1
}

if {$num_procs_test3 != 1} {
	log_error "SLURM_NTASKS should be 1 ($num_procs_test3 != 1)"
	set exit_code 1
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_in $file_out
} else {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
