#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test that switch windows in uses are not re-used.
############################################################################
# Copyright (C) 2005-2006 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 exit_code   0

#
# Report the slurm network and use it to establish window parameters
#
# windows_per_node    - switch windows available per node
# windows_iterations - job steps to run after initial switch window allocation
#                       for a persistent job step
# windows_used        - switch windows to use per job step
# step_delay          - run time of persistent job step (seconds)
#
log_user 0
set step_delay 0
spawn $scontrol show config
expect {
	-re "switch/none" {
		set windows_per_node 0
		set windows_iterations 5
		set windows_used 4
		set step_delay 10
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
log_user 1
if {$step_delay == 0} {
	skip "Not running compatible switch, this test is not applicable"
}

#
# Submit a slurm allocate job, then run a bunch of job
# steps within that allocation
#
set timeout $max_job_delay
set job_id 0
set matches 0

set srun_pid [spawn $salloc -N1 --verbose -t2 $bin_bash]
expect -re "Granted job allocation ($number)"
set job_id $expect_out(1,string)
send "$reset_bash_prompt\r"
expect -re "\"$test_prompt\""

# start initial job step to claim some switch windows
expect -re $test_prompt
send "$srun -N1 -O -n$windows_used $bin_sleep $step_delay &\r"

# start more job steps to check see if any switch window conflicts occur
for {set inx 0} {$inx < $windows_iterations} {incr inx} {
        expect {
                -re "error" {
                        log_error "Some error occurred"
                        set exit_code 1
                        exp_continue
                }
		-re $test_prompt {
			log_debug "Spawning step $inx"
			send "$srun -N1 -O -n$windows_used true\r"
		}
		timeout {
			slow_kill $srun_pid
			fail "Salloc not responding"
		}
		eof {
			fail "srun aborted"
		}
	}
}

# wait for initial job step to complete
expect -re $test_prompt
log_debug "(sleeping for $step_delay seconds, for job step zero to complete)"
exec $bin_sleep $step_delay
send "exit\r"

expect {
	-re "error" {
		log_error "some error occurred"
		set exit_code 1
		exp_continue
	}
	timeout {
		slow_kill $srun_pid
		fail "salloc not responding"
	}
	eof {
		wait
	}
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}

