#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test accounting for GPU resources with various allocation options
#
# 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) 2018 SchedMD LLC
# Written by Morris Jette
#
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id        "39.19"
set exit_code      0
set file_in1       "test$test_id.input1"
set file_in2       "test$test_id.input2"
set file_out       "test$test_id.output"

#
# Validate the job, batch step and step 0 of a job have the proper GPU counts
# No step to test if step_gpus == -1
#
# NOTE: AllocGRES and ReqGRES values for all steps (including batch step)
#	are reported based upon the job specification
#
proc test_acct { job_id job_gpus step_gpus have_gpu_types } {
	global alpha_numeric_under number
	global bin_cat bin_rm bin_grep exit_code file_out sacct

	if {$job_id == 0} {
		return
	}

	sleep 2
	send_user "\nJob $job_id Expected job GPUs:$job_gpus  Step GPUs:$step_gpus\n"
	exec $bin_rm -f $file_out
	exec >$file_out $sacct --job=$job_id --parsable2 --start=today -o JobID,AllocGRES,ReqGRES,AllocTRES

	spawn $bin_cat $file_out
	expect {
		eof {
			wait
		}
	}

	log_user 0
	if {$step_gpus != -1} {
		set match 0
		spawn $bin_grep $job_id.0 $file_out
		expect {
			-re "gpu:($number)" {
				incr match
				if {$expect_out(1,string) != $job_gpus} {
					send_user "\nFAILURE: bad step GPU count reported by sacct ($expect_out(1,string) != $step_gpus)\n"
					set exit_code 1
				}
				exp_continue
			}
			-re "gres/gpu=($number)" {
				incr match
				if {$expect_out(1,string) != $step_gpus} {
					send_user "\nFAILURE: bad step GPU count reported by sacct ($expect_out(1,string) != $step_gpus)\n"
					set exit_code 1
				}
				exp_continue
			}
			-re "gres/gpu:($alpha_numeric_under)=($number)" {
				if {$have_gpu_types != 0} {
					incr match
					if {$expect_out(2,string) != $step_gpus} {
						send_user "\nFAILURE: bad step GPU count reported by sacct ($expect_out(2,string) != $step_gpus)\n"
						set exit_code 1
					}
				}
				exp_continue
			}
			eof {
				wait
			}
		}
		if {$match != 3} {
			send_user "\nFAILURE: Missing step GPU count report from sacct ($match != 3)\n"
		}
	}

	set match 0
	spawn $bin_grep $job_id.batch $file_out
	expect {
		-re "gpu:($number)" {
			incr match
			if {$expect_out(1,string) != $job_gpus} {
				send_user "\nFAILURE: bad batch GPU count reported by sacct ($expect_out(1,string) != $job_gpus)\n"
				set exit_code 1
			}
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != 2} {
		send_user "\nFAILURE: missing batch GPU count report from sacct ($match != 2)\n"
	}

	set match 0
	spawn $bin_grep $job_id| $file_out
	expect {
		-re "gpu:($number)" {
			incr match
			if {$expect_out(1,string) != $job_gpus} {
				send_user "\nFAILURE: Bad job GPU count reported by sacct ($expect_out(1,string) != $job_gpus)\n"
				set exit_code 1
			}
			exp_continue
		}
		-re "gres/gpu=($number)" {
			incr match
			if {$expect_out(1,string) != $job_gpus} {
				send_user "\nFAILURE: Bad job GPU count reported by sacct ($expect_out(1,string) != $job_gpus)\n"
				set exit_code 1
			}
			exp_continue
		}
		-re "gres/gpu:($alpha_numeric_under)=($number)" {
			if {$have_gpu_types != 0} {
				incr match
				if {$expect_out(2,string) != $job_gpus} {
					send_user "\nFAILURE: Bad job GPU count reported by sacct ($expect_out(2,string) != $job_gpus)\n"
					set exit_code 1
				}
			}
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != 3} {
		send_user "\nFAILURE: Missing job GPU count report from sacct ($match != 3)\n"
	}
	exec $bin_rm -f $file_out
	log_user 1
}

#
# Validate the job, batch step and step 0 of a job have the proper GPU counts
# No step to test if step_gpus == -1
#
# NOTE: AllocGRES and ReqGRES values for all steps (including batch step)
#	are reported based upon the job specification
#
proc test_out_file { file_out target } {
	global alpha_numeric_under number bin_cat exit_code

	if {[wait_for_file $file_out] != 0} {
		send_user "\nFAILURE: no output file\n"
		exit 1
	}

	set match 0
	spawn $bin_cat $file_out
	expect {
		-re "TRES=.*,gres/gpu=($number)" {
			set match $expect_out(1,string)
			exp_continue
		}
		-re "TRES=.*,gres/gpu:($alpha_numeric_under)=($number)" {
			set match $expect_out(2,string)
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != $target} {
		send_user "\nFAILURE: Failed to account for proper GPU count ($match != $target)\n"
		set exit_code 1
	}
}

print_header $test_id

if { [test_account_storage] == 0 } {
	send_user "\nWARNING: This test can't be run without a usable AccountStorageType\n"
	exit 0
}

set store_tres [string tolower [get_acct_store_tres]]
set store_gpu [string first "gres/gpu" $store_tres]
if {$store_gpu == -1} {
	send_user "\nWARNING: This test requires accounting for GPUs\n"
	exit $exit_code
} elseif {[test_front_end]} {
	send_user "\nWARNING: This test is incompatible with front-end systems\n"
	exit $exit_code
}

if {[test_cons_tres]} {
	send_user "\nValid configuration, using select/cons_tres\n"
} else {
	send_user "\nWARNING: This test is only compatible with select/cons_tres\n"
	exit 0
}

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
send_user "\nDefault partition node count is $nb_nodes\n"
if {$nb_nodes > 2} {
	set nb_nodes 2
}
set gpu_cnt [get_gpu_count $nb_nodes]
if {$gpu_cnt < 0} {
	send_user "\nFAILURE: Error getting GPU count\n"
	exit 1
}
if {$gpu_cnt < 2} {
	send_user "\nWARNING: This test requires 2 or more GPUs in the default partition\n"
	exit 0
}

get_node_config
set cpus_per_node [expr $sockets_per_node * $cpus_per_socket]
set sockets_with_gpus [get_gpu_socket_count $gpu_cnt $sockets_per_node]

send_user "GPUs per node is $gpu_cnt\n"
send_user "Sockets with GPUs $sockets_with_gpus\n"
send_user "Sockets per node is $sockets_per_node\n"
send_user "CPUs per socket is $cpus_per_socket\n"
send_user "CPUs per node is $cpus_per_node\n"

if {$cpus_per_node < 3} {
	send_user "\nWARNING: This test requires 3 or more CPUs per node in the default partition\n"
	exit 0
}

#
# Test --gpus-per-node option by job
#
make_bash_script $file_in1 "
	$scontrol -dd show job \${SLURM_JOBID} | grep gpu
	exit 0"

send_user "\n\nTEST 1: --gpus-per-node option by job\n"

set target [expr $nb_nodes * 2]
exec $bin_rm -f $file_out
set job_id 0
set timeout $max_job_delay
spawn $sbatch --gres=craynetwork:0 --gpus-per-node=2 -N$nb_nodes -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

if {[wait_for_file $file_out] != 0} {
	send_user "\nFAILURE: no output file\n"
	exit 1
}
set have_gpu_types 0
set match 0
spawn $bin_cat $file_out
expect {
	-re "TRES=.*,gres/gpu=($number)" {
		set match $expect_out(1,string)
		exp_continue
	}
	-re "TRES=.*,gres/gpu:($alpha_numeric_under)=($number)" {
		if {$match == 0} {
			set have_gpu_types 1
			set match $expect_out(2,string)
		}
		exp_continue
	}
	eof {
		wait
	}
}
set target [expr $nb_nodes * 2]
if {$match != $target} {
	send_user "\nFAILURE: failed to account for proper GPU count ($match != $target)\n"
	set exit_code 1
}
test_acct $job_id $target -1 $have_gpu_types

#
# Test --gpus option by job
#
send_user "\n\nTEST 2: --gpus option by job\n"

exec $bin_rm -f $file_out
if {$nb_nodes >= 2 || $gpu_cnt >= 2} {
	set target 3
} else {
	set target 2
}
set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus=$target -N$nb_nodes -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

test_out_file $file_out $target
test_acct $job_id $target -1 $have_gpu_types

#
# Test --gpus-per-task option by job
#
send_user "\n\nTEST 3: --gpus-per-task option by job\n"

exec $bin_rm -f $file_out
if {$cpus_per_node >= 2 && $nb_nodes >= 2} {
	set nb_tasks 3
} elseif {$cpus_per_node >= 2 || $nb_nodes >= 2} {
	set nb_tasks 2
} else {
	set nb_tasks 1
}
set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus-per-task=1 -N$nb_nodes -n$nb_tasks -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

test_out_file $file_out $nb_tasks
test_acct $job_id $nb_tasks -1 $have_gpu_types

#
# Test --gpus-per-socket option by job
#
send_user "\n\nTEST 4: --gpus-per-socket option by job\n"

exec $bin_rm -f $file_out

if {$sockets_with_gpus >= 2} {
	set nb_sockets 2
	set cpus_per_task $cpus_per_socket
} else {
	set nb_sockets 1
	set cpus_per_task 1
}
set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus-per-socket=1 -N$nb_nodes  --ntasks=$nb_nodes --sockets-per-node=$nb_sockets --cpus-per-task=$cpus_per_task -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

set target [expr $nb_nodes * $nb_sockets]
test_out_file $file_out $target
test_acct $job_id $target -1 $have_gpu_types

#
# Test --gpus-per-node option by step
#
make_bash_script $file_in1 "
	$srun ./$file_in2
	exit 0"

make_bash_script $file_in2 "
	if \[ \$SLURM_PROCID -eq 0 \]; then
		$scontrol show step \${SLURM_JOBID}.\${SLURM_STEPID}
	fi
	exit 0"

send_user "\n\nTEST 5: --gpus-per-node option by step\n"

set target [expr $nb_nodes * 2]
exec $bin_rm -f $file_out
set job_id 0
set timeout $max_job_delay
spawn $sbatch --gres=craynetwork:0 --gpus-per-node=2 -N$nb_nodes -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

set target [expr $nb_nodes * 2]
test_out_file $file_out $target
test_acct $job_id $target $target $have_gpu_types

#
# Test --gpus option by step
#
send_user "\n\nTEST 6: --gpus option by step\n"

exec $bin_rm -f $file_out
if {$nb_nodes >= 2 || $gpu_cnt >= 2} {
	set target 3
} else {
	set target 2
}
set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus=$target -N$nb_nodes -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

test_out_file $file_out $target
test_acct $job_id $target $target $have_gpu_types

#
# Test --gpus-per-task option by step
#
send_user "\n\nTEST 7: --gpus-per-task option by step\n"

exec $bin_rm -f $file_out
if {$cpus_per_node >= 2 && $nb_nodes >= 2} {
	set nb_tasks 3
} elseif {$cpus_per_node >= 2 || $nb_nodes >= 2} {
	set nb_tasks 2
} else {
	set nb_tasks 1
}
set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus-per-task=1 -N$nb_nodes -n$nb_tasks -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

test_out_file $file_out $nb_tasks
test_acct $job_id $nb_tasks $nb_tasks $have_gpu_types

#
# Test --gpus-per-socket option by step
#
send_user "\n\nTEST 8: --gpus-per-socket option by step\n"

exec $bin_rm -f $file_out

if {$sockets_with_gpus >= 2} {
	set nb_sockets 2
	set cpus_per_task $cpus_per_socket
} else {
	set nb_sockets 1
	set cpus_per_task 1
}
set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus-per-socket=1 -N$nb_nodes  --ntasks=$nb_nodes --sockets-per-node=$nb_sockets --cpus-per-task=$cpus_per_task -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

set target [expr $nb_nodes * $nb_sockets]
test_out_file $file_out $target
test_acct $job_id $target $target $have_gpu_types

#
# Test --gpus-per-task option by step
#
send_user "\n\nTEST 9: --gpus-per-task option by step\n"

exec $bin_rm -f $file_out
if {$cpus_per_node >= 2 && $nb_nodes >= 2} {
	set job_tasks 3
	set step_tasks 2
} elseif {$cpus_per_node >= 2 || $nb_nodes >= 2} {
	set job_tasks 2
	set step_tasks 2
} else {
	set job_tasks 1
	set step_tasks 1
}
if {$nb_nodes >= 2} {
	set step_nodes 2
} else {
	set step_nodes 1
}
make_bash_script $file_in1 "
	$srun -N$step_nodes -n$step_tasks ./$file_in2
	exit 0"

set job_id 0
spawn $sbatch --gres=craynetwork:0 --gpus-per-task=1 -N$nb_nodes -n$job_tasks -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job $job_id did not complete\n"
	cancel_job $job_id
	exit 1
}

test_out_file $file_out $step_tasks
test_acct $job_id $job_tasks $step_tasks $have_gpu_types

if {$exit_code == 0} {
	exec $bin_rm -f $file_in1 $file_in2 $file_out
	send_user "\nSUCCESS\n"
}
exit $exit_code
