#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Increase size of job with allocated GPUs
############################################################################
# Copyright (C) 2010 Lawrence Livermore National Security.
# 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 file_in1         "$test_dir/input1"
set file_in2         "$test_dir/input2"
set file_in3         "$test_dir/input3"
set file_out1        "$test_dir/output1"
set file_out2        "$test_dir/output2"
set job_id1          0
set job_id2          0

# Bug 9701: disabling test until bug 6252 is fixed
skip "Test disabled until bug 6252 is fixed"

if {![param_contains [get_config_param "SchedulerParameters"] "permit_job_expansion"]} {
    skip "This test is only compatible with SchedulerParameters=permit_job_expansion"
}

if {![check_config_select "cons_tres"]} {
    skip "This test is only compatible with select/cons_tres"
}
if {[get_config_param "FrontendName"] ne "MISSING"} {
    skip "This test is incompatible with front-end systems"
}

set nb_nodes [llength [get_nodes_by_state idle,alloc,comp]]
if {$nb_nodes < 2} {
    skip "Not enough nodes currently available ($nb_nodes avail, 2 needed)"
}
set gpu_cnt [get_highest_gres_count 2 "gpu"]
if {$gpu_cnt < 1} {
	skip "This test requires 1 or more GPUs on $nb_nodes nodes of the default partition"
}

proc cleanup {} {
	global job_id1 job_id2

	cancel_job [list $job_id1 $job_id2]

	file delete slurm_job_${job_id2}_resize.csh
	file delete slurm_job_${job_id2}_resize.sh
}

#
# Build input scripts
# file_in1: Determine GPUs allocated, wait for dependent job to exit,
#	expand allocation and run another GPU job
# file_in2: Determine GPUs allocated, shrink to size 0 and exit
# file_in3: Print the hostname and GPU IDs
#
make_bash_script $file_in1 "
	$scontrol -dd show job \${SLURM_JOBID}
	$srun ./$file_in3
	sleep 20	# Wait for job 2 submission
	while true; do
		$squeue -h -n test_child_$test_id | wc | grep -e ' *0 *0 *0'
		if \[ \${?} -eq 0 \]; then
			break
		fi
		sleep 5
	done
	$scontrol update JobId=\${SLURM_JOBID} NumNodes=ALL
	. slurm_job_\${SLURM_JOBID}_resize.sh
	$scontrol -dd show job \${SLURM_JOBID}
	$srun ./$file_in3
	$bin_rm -f slurm_job_\${SLURM_JOBID}_resize.csh
	$bin_rm -f slurm_job_\${SLURM_JOBID}_resize.sh
	exit 0"

make_bash_script $file_in2 "
	$scontrol -dd show job \${SLURM_JOBID}
	$scontrol update JobId=\${SLURM_JOBID} NumNodes=0
	. slurm_job_\${SLURM_JOBID}_resize.sh
	# JOB GETS CANCELLED HERE AS BATCH HOST GETS REMOVED FROM JOB ALLOCATION
	$bin_rm -f slurm_job_\${SLURM_JOBID}_resize.csh
	$bin_rm -f slurm_job_\${SLURM_JOBID}_resize.sh
	exit 0"

make_bash_script $file_in3 "
echo 'HOST:'\$SLURMD_NODENAME 'CUDA_VISIBLE_DEVICES:'\$CUDA_VISIBLE_DEVICES"

#
# Submit job to expand: uses one GPU one node
#
set job_id1 [submit_job -fail "-N1 --exclusive -J $test_name -t2 --gpus=1 --output=$file_out1 $file_in1"]
wait_for_job -fail $job_id1 "RUNNING"

#
# Submit job to shrink: uses one GPU one node
#
set job_id2 [submit_job -fail "-N1 --exclusive -J ${test_name}_child --dependency=expand:$job_id1 -t1 --gpus=1 --output=$file_out2 $file_in2"]

wait_for_job -fail $job_id1 "DONE"
wait_for_job -fail $job_id2 "DONE"

#
# Parse the output files from job 1
#
log_debug "Parse job 1 output"
wait_for_file -fail $file_out1
set match 0
spawn $bin_cat $file_out1
expect {
	-re "CUDA_VISIBLE_DEVICES" {
		incr match
		exp_continue
	}
	eof {
		wait
	}
}
if {$match != 3} {
	fail "Bad CUDA information about job 1 ($match != 3)"
}

#
# Parse the output files from job 2
# Not currently looking for anything, but do log it's contents before purge
#
log_debug "Parse job 2 output"
wait_for_file -fail $file_out2
set match 0
spawn $bin_cat $file_out2
expect {
	eof {
		wait
	}
}
