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

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
log_debug "$env_valu_nnodes $env_valu_nprocs"
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 {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	fail "Did not get job_id"
}

#
# Look at output file to confirm processing of environment variables
#
if {[wait_for_file $env_valu_stdout] != 0} {
	fail "Did not process $env_name_stdout environment variable"
}

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 {
		fail "$bin_sort not responding"
	}
	eof {
		wait
	}
}

if {$stale_cnt != 0} {
	log_warn "Stale file handles $task_cnt for $stale_cnt tasks"
	incr task_cnt $stale_cnt
}
if {$task_cnt != $env_valu_nprocs} {
	log_error "Did not process $env_name_nprocs environment variable ($task_cnt != $env_valu_nprocs)"
	set exit_code 1
}

# Note zero origin conversion
incr max_node_val
if {$max_node_val < $min_nodes} {
	log_error "Did not process $env_name_nnodes environment variable ($max_node_val < $min_nodes)"
	set exit_code 1
}
if {$max_node_val > $max_nodes} {
	log_error "Did not process $env_name_nnodes environment variable ($max_node_val > $max_nodes)"
	set exit_code 1
}

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