#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test some invalid combinations of --gpu 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.4"
set exit_code   0
set job_id      0

print_header $test_id

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]

set gpu_cnt [get_gpu_count 1]
if {$gpu_cnt < 0} {
	send_user "\nFAILURE: Error getting GPU count\n"
	exit 1
}
if {$gpu_cnt < 1} {
	send_user "\nWARNING: This test requires 1 or more GPUs in the default partition\n"
	exit 0
}
send_user "\nGPU count is $gpu_cnt\n"

#
# Request more GPUs per node than exist on a single node
#
send_user "\n\n==== TEST 1 ====\n"
spawn $sbatch --gpus-per-node=[expr $gpu_cnt + 1] -N1 --output=/dev/null -t1 --wrap $bin_hostname
expect {
	-re "Submitted batch job ($number)" {
		cancel_job $expect_out(1,string)
		send_user "\nFAILURE: batch request not rejected\n"
		set exit_code 1
		exp_continue
	}
	-re "error: " {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Specify 1 node and more GPUs than exist on a single node
#
send_user "\n\n==== TEST 2 ====\n"
spawn $sbatch --gpus-per-node=$gpu_cnt --gres=[expr $gpu_cnt + 1] -N1 --output=/dev/null -t1 --wrap $bin_hostname
expect {
	-re "Submitted batch job ($number)" {
		cancel_job $expect_out(1,string)
		send_user "\nFAILURE: batch request not rejected\n"
		set exit_code 1
		exp_continue
	}
	-re "error: " {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Specify inconsistent --cpus-per-task and --gpus-per-task/--cpus-per-gpu
#
send_user "\n\n==== TEST 3 ====\n"
spawn $sbatch --gpus-per-task=1 --cpus-per-gpu=1 --cpus-per-task=2 -N1 --output=/dev/null -t1 --wrap $bin_hostname
expect {
	-re "Submitted batch job ($number)" {
		cancel_job $expect_out(1,string)
		send_user "\nFAILURE: batch request not rejected\n"
		set exit_code 1
		exp_continue
	}
	-re "error: " {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Specify inconsistent tasks per node
#
send_user "\n\n==== TEST 4 ====\n"
spawn $sbatch --gpus-per-task=1 --gpus-per-node=1 -n2 -N1 --output=/dev/null -t1 --wrap $bin_hostname
expect {
	-re "Submitted batch job ($number)" {
		cancel_job $expect_out(1,string)
		send_user "\nFAILURE: batch request not rejected\n"
		set exit_code 1
		exp_continue
	}
	-re "error: " {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Specify gpus-per-socket, but no sockets-per-node count
#
send_user "\n\n==== TEST 5 ====\n"
spawn $sbatch --gpus-per-socket=1 -N1 --output=/dev/null -t1 --wrap $bin_hostname
expect {
	-re "Submitted batch job ($number)" {
		cancel_job $expect_out(1,string)
		send_user "\nFAILURE: batch request not rejected\n"
		set exit_code 1
		exp_continue
	}
	-re "error: " {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Specify inconsistent tasks per node (variation of test 4)
#
send_user "\n\n==== TEST 6 ====\n"
spawn $sbatch --gpus-per-task=1 --gpus-per-node=2 --ntasks-per-node=1 -N1 --output=/dev/null -t1 --wrap $bin_hostname
expect {
	-re "Submitted batch job ($number)" {
		cancel_job $expect_out(1,string)
		send_user "\nFAILURE: batch request not rejected\n"
		set exit_code 1
		exp_continue
	}
	-re "error: " {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
