#!/usr/bin/env expect
############################################################################
# Purpose:  Test GRES APIs.
############################################################################
# Copyright (C) 2014 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set exit_code   0
set test_prog   "test$test_id.prog"
set cfgdir      [pwd]

if {[get_config_param "SwitchType"] eq "switch/cray"} {
#	Many Cray-specific changes required in slurm.conf test files
	skip "This test can not run on a Cray system"
}

#
# Delete left-over programs and rebuild them
#
file delete $test_prog

if [compile_against_libslurm -full $test_prog] {
	fail "Cannot compile test program"
}

log_info "====test7.17.1===="
set cpu_match 0
spawn ./$test_prog "gpu:2" "$cfgdir" "/test7.17.1" 8
expect {
	-re "failure" {
		log_error "Failure running test program test7.17.1"
		set exit_code 1
		exp_continue
	}
	-re "cpu_alloc=8" {
		set cpu_match 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$cpu_match != 1} {
	log_error "Number of COREs is incorrect"
	set exit_code 1
}

log_info "====test7.17.2===="
set cpu_match 0
spawn ./$test_prog "gpu:2" "$cfgdir" "/test7.17.2" 16
expect {
	-re "failure" {
		log_error "Failure running program test7.17.2"
		set exit_code 1
		exp_continue
	}
	-re "cpu_alloc=16" {
		set cpu_match 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$cpu_match != 1} {
	log_error "Number of cpu is incorrect"
	set exit_code 1
}

log_info "====test7.17.3===="
set cpu_match 0
spawn ./$test_prog "gpu:2" "$cfgdir" "/test7.17.3" 16
expect {
	-re "failure" {
		log_error "Failure running test program test7.17.3"
		set exit_code 1
		exp_continue
	}
	-re "cpu_alloc=16" {
		set cpu_match 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$cpu_match != 1} {
	log_error "Number of cpu is incorrect should be 16"
	set exit_code 1
}

log_info "====test7.17.4===="
set cpu_match 0
spawn ./$test_prog "gpu:2" "$cfgdir" "/test7.17.4" 16
expect {
	-re "failure" {
		log_error "Failure running test program test7.17.4"
		set exit_code 1
		exp_continue
	}
	-re "cpu_alloc=ALL" {
		set cpu_match 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$cpu_match != 1} {
	log_error "Number of cpu is incorrect, should be ALL"
	set exit_code 1
}

#######################################
# Tests for expected failures
#######################################

# Run a job with invalid job allocation
log_info "====test7.17.5===="
set fail_match 0
spawn ./$test_prog "craynetworks:100" "$cfgdir" "/test7.17.5" 16
expect {
	-re "failure" {
		log_debug "This error is expected"
		set fail_match 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$fail_match != 1} {
	log_error "This test should have failed but did not"
	set exit_code 1
}

# Run with no gres.conf file
log_info "====test7.17.6===="
set fail_match 0
spawn ./$test_prog "gpu:2" "$cfgdir" "/test7.17.6" 0
expect {
	-re "failure" {
		log_debug "This error is expected"
		set fail_match 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$fail_match != 1} {
	log_error "This test should have failed but did not"
	set exit_code 1
}

# Test with invalid job allocation
log_info "====test7.17.7===="
set fail_match 0
spawn ./$test_prog "gpu:2" "$cfgdir" "/test7.17.7" 8
expect {
	-re "fatal" {
		log_debug "This error is expected"
		set fail_match 1
		#exp_continue
	}
	eof {
		wait
	}
}
if {$fail_match != 1} {
	fail "This test should have failed but did not"
}

if {$exit_code == 0} {
	file delete $test_prog
} else {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
