#!/usr/bin/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://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.31"
set env_flag_debug   0
set env_name_debug   "SLURM_DEBUG"
set env_valu_debug   1
set env_name_nnodes  "SLURM_NNODES"

if { [test_bluegene] } {
	set min_nodes        1
	set max_nodes        1024
	set env_valu_nprocs  5
} else {
	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

global env
set env($env_name_debug) $env_valu_debug

print_header $test_id

#
# Set target environment variables
#
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

#
# Spawn a job via srun using these environment variables
#
set srun_pid [spawn $srun -t1 $bin_env]
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 task_cnt      0
set max_node_val -1
spawn $bin_sort $env_valu_stdout
expect {
	-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
	}
	timeout {
		send_user "\nFAILURE: $bin_sort not responding\n"
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}

if {$task_cnt != $env_valu_nprocs} {
	send_user "\nFAILURE: did not process"
	send_user " $env_name_nprocs environment variable\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\n"
	set exit_code 1
}
if {$max_node_val > $max_nodes} {
	send_user "\nFAILURE: did not process"
	send_user " $env_name_nnodes environment variable\n"
	set exit_code 1
}

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