#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test some invalid combinations of --gpu options
############################################################################
# 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 job_id      0
set job_id_list [list]

set nb_nodes [get_partition_param [default_partition] "TotalNodes"]

set gpu_cnt [get_highest_gres_count 1 "gpu"]

if {![check_config_select "cons_tres"]} {
	skip "The test is only compatible with cons_tres"
}

if {$gpu_cnt < 1} {
	skip "This test requires 1 or more GPUs on 1 node of the default partition"
}
log_debug "GPU count is $gpu_cnt"

proc cleanup {} {
	global job_id_list

	cancel_job $job_id_list
}

#
# Request more GPUs per node than exist on a single node
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-node=[expr $gpu_cnt + 1] -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Batch job submission failed: Requested node configuration is not available" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Specify 1 node and more GPUs than exist on a single node
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-node=$gpu_cnt --gres=gpu:[expr $gpu_cnt + 1] -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Batch job submission failed: Requested node configuration is not available" $output]} "Job should be rejected at submit time"

if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Specify inconsistent --cpus-per-task and --gpus-per-task/--cpus-per-gpu
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-task=1 --cpus-per-gpu=1 --cpus-per-task=2 -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: --cpus-per-gpu is mutually exclusive with --cpus-per-task" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Specify inconsistent tasks per node
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-task=1 --gpus-per-node=1 -n2 -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Batch job submission failed: Requested node configuration is not available" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Specify gpus-per-socket, but no sockets-per-node count
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-socket=1 -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: --gpus-per-socket option requires --sockets-per-node specification" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Specify inconsistent tasks per node (variation of test 4)
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-task=1 --gpus-per-node=2 --ntasks-per-node=1 -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec. Based on --gres=gpu/--gpus-per-node and --gpus-per-task requested number of tasks per node differ from --ntasks-per-node." $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request more GPUs per node than total --gpus
#
set output [run_command_output -xfail -subtest "$sbatch --gres=gpu:2 --gpus=1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to ensure --gpus >= --gres=gpu/--gpus-per-node >= --gpus-per-socket" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request more GPUs per socket than total --gpus
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-socket=2 --sockets-per-node=1 --gpus=1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to ensure --gpus >= --gres=gpu/--gpus-per-node >= --gpus-per-socket" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request --gpus-per-socket without --sockets-per-node
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-socket=2 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: --gpus-per-socket option requires --sockets-per-node specification" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request more --gpus-per-task than total --gpus
#
set output [run_command_output -xfail -subtest "$sbatch --gpus-per-task=1 -n2 --gpus=1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec. Based on --gpus and --gpus-per-task number of requested tasks differ from -n/--ntasks." $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request --gpus not being a multiple --gres=gpu:
#
set output [run_command_output -xfail -subtest "$sbatch --gres=gpu:2 --gpus=3 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec, --gpus is not multiple of --gres=gpu/--gpus-per-node" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request inconsistent --gres=gpu: and --gpus-per-socket
#
set output [run_command_output -xfail -subtest "$sbatch --gres=gpu:2 --gpus-per-socket=1 --sockets-per-node=1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec. Based on --gres=gpu/--gpus-per-node and --gpus-per-socket required number of sockets differ from --sockets-per-node." $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request inconsistent number of nodes (-N) with implicite --gpus and --gres=gpu:
#
set output [run_command_output -xfail -subtest "$sbatch --gres=gpu:2 --gpus=4 -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec. Based on --gpu and --gres=gpu/--gpus-per-node required nodes \\(2\\) doesn't fall between min_nodes \\(1\\) and max_nodes \\(1\\) boundaries." $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request --gpus not being multiple of --gpus-per-task
#
set output [run_command_output -xfail -subtest "$sbatch --gpus=3 --gpus-per-task=2 -N1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec, --gpus not multiple of --gpus-per-task" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request inconsistent -n and implicite number of tasks from --gpus and --gpus-per-task
#
set output [run_command_output -xfail -subtest "$sbatch --gpus=4 --gpus-per-task=2 -n1 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec. Based on --gpus and --gpus-per-task number of requested tasks differ from -n/--ntasks." $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}

#
# Request more nodes (-N) than --gpus
#
set output [run_command_output -xfail -subtest "$sbatch --gpus=1 -N2 --output=/dev/null -t1 --wrap $bin_hostname"]
subtest {[regexp "sbatch: error: Failed to validate job spec, --gpus < -N" $output]} "Job should be rejected at submit time"
if {[regexp "Submitted batch job ($number)" $output - job_id]} {
	lappend job_id_list $job_id
}
