#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Verify that Slurm directed environment variables are processed:
#          SLURM_DEBUG, SLURM_NNODES, SLURN_NPROCS, SLURM_OVERCOMMIT,
#          SLURM_STDOUTMODE.
#
# 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) 2002 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@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.31"
set env_flag_debug   0
set env_name_debug   "SLURM_DEBUG"
set env_valu_debug   1
set env_name_nnodes  "SLURM_NNODES"
set file_in          "test$test_id.input"

print_header $test_id

set min_nodes        1
set max_nodes        2
set env_valu_nprocs  5
set env_valu_nnodes  "$min_nodes-$max_nodes"
set env_name_nprocs  "SLURM_NPROCS"
set env_name_stdout  "SLURM_STDOUTMODE"
set env_valu_stdout  "test$test_id.output"
set env_name_overcom "SLURM_OVERCOMMIT"
set env_valu_overcom 1
set exit_code        0
set job_id           0
set timeout          $max_job_delay

#
# Set target environment variables
#
global env
set env($env_name_debug)   $env_valu_debug
set env($env_name_nnodes)  $env_valu_nnodes
send_user "$env_valu_nnodes $env_valu_nprocs\n";
set env($env_name_nprocs)  $env_valu_nprocs
set env($env_name_stdout)  $env_valu_stdout
set env($env_name_overcom) $env_valu_overcom
exec $bin_rm -f $env_valu_stdout

make_bash_script $file_in "env | $bin_grep MP_; env | $bin_grep SLURM_"

#
# Spawn a job via srun using these environment variables
#
set srun_pid [spawn $srun -t1 ./$file_in]
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
		set env_flag_debug 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	send_user "\nFAILURE: did not get job_id\n"
	exit 1
}

#
# Look at output file to confirm processing of environment variables
#
if {[wait_for_file $env_valu_stdout] != 0} {
	send_user "\nFAILURE: did not process"
	send_user " $env_name_stdout environment variable\n"
	exit 1
}

set stale_cnt     0
set task_cnt      0
set max_node_val -1
spawn $bin_sort $env_valu_stdout
expect {
#	POE options
	-re "MP_CHILD=($number)" {
		incr task_cnt
		exp_continue
	}
	-re "MP_ENDPOINTS=($number)" {
		set node_id $expect_out(1,string)
		if {$node_id > $max_node_val} {
			set max_node_val $node_id
		}
		exp_continue
	}

#	Linux options
	-re "SLURM_NODEID=($number)" {
		set node_id $expect_out(1,string)
		if {$node_id > $max_node_val} {
			set max_node_val $node_id
		}
		exp_continue
	}
	-re "SLURM_PROCID" {
		incr task_cnt
		exp_continue
	}
	-re "Stale file handle" {
		incr stale_cnt
		exp_continue
	}

	timeout {
		send_user "\nFAILURE: $bin_sort not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}

if {$stale_cnt != 0} {
	send_user "\nWARNING: Stale file handles $task_cnt for $stale_cnt tasks\n"
	incr task_cnt $stale_cnt
}
if {$task_cnt != $env_valu_nprocs} {
	send_user "\nFAILURE: did not process"
	send_user " $env_name_nprocs environment variable"
	send_user " ($task_cnt != $env_valu_nprocs)\n"
	set exit_code 1
}

# Note zero origin conversion
incr max_node_val
if {$max_node_val < $min_nodes} {
	send_user "\nFAILURE: did not process"
	send_user " $env_name_nnodes environment variable"
	send_user " ($max_node_val < $min_nodes)\n"
	set exit_code 1
}
if {$max_node_val > $max_nodes} {
	send_user "\nFAILURE: did not process"
	send_user " $env_name_nnodes environment variable"
	send_user " ($max_node_val > $max_nodes)\n"
	set exit_code 1
}

#
# Post processing
#
if {$exit_code == 0} {
	exec $bin_rm -f $env_valu_stdout $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
