#!/usr/bin/env expect
############################################################################
# Purpose: Test --hint mutual exclusive properties
############################################################################
# Copyright (C) 2021 SchedMD LLC
# Written by Brian Christiansen <brian@schedmd.com>
#
# 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 job_id 0


proc cleanup {} {
	global job_id

	cancel_job $job_id
}


proc check_error {client option prog error} {
	global sbatch salloc srun test_name number job_id

	set cmd "$client -v -H -J$test_name -t1 --hint=nomultithread $option $prog"
	set job_id 0
	set matches 0
	spawn {*}$cmd
	expect {
		-ex "$error" {
			incr matches
			exp_continue
		}
		-re "job ($number)" {
			set job_id $expect_out(1,string)
		}
		timeout {
			fail "[file tail $client] not responding"
		}
		eof {
			wait
		}
	}
	subtest {$matches == 1} "Submission should return '$error'"
	cancel_job $job_id
}


proc mutual_error_tests {} {
	global sbatch salloc srun test_name number job_id

	set clients [list "$sbatch" "$salloc" "$srun"]
	set options [list "--threads-per-core=1" "--ntasks-per-core=1" "-B1"]
	set error "Following options are mutually exclusive with --hint: --ntasks-per-core, --threads-per-core, -B and --cpu-bind (other then --cpu-bind=verbose). Ignoring --hint."

	foreach client $clients {
		set prog "hostname"
		if {$client == $sbatch} {
			set prog "--wrap=hostname"
		}
		foreach option $options {
			check_error $client $option $prog $error
		}
	}

	# Test srun --hint only with works with --cpu-bind=verbose
	set client $srun
	set options [list "--cpu-bind=threads" "--cpu-bind=threads,verbose"]
	foreach option $options {
		check_error $client $option "hostname" $error
	}

	# Test if srun --cpu-bind=verbose --hint=nomultithread actually enabled cpu-bind
	set output [run_command_output -subtest "$srun --hint=nomultithread --cpu-bind=verbose /bin/true"]
	subtest {[regexp  "cpu-bind=" $output]} "The output should contain line starting with cpu-bind=, indicating that --cpu-bind=verbose is in effect"
}

mutual_error_tests

# Test SLURM_HINT env variable
set env(SLURM_HINT) "nomultithread"
mutual_error_tests

# Options are given a higher precedence than the environment. Options that conflict
# with the environment cause the conflicting environment variables to be overridden.
# Thus, when --ntasks-per-core or --threads-per-core options are specified
# in conjunction with SLURM_HINT, since these conflict, the SLURM_HINT is ignored.

# We will use 2 threads per core and tasks per core if available in order to
# demonstrate that the nomultithread hint is being ignored.
set node_name [get_nodes_by_request "--ntasks-per-core=2 -t1"]
set threads_per_core [expr [get_node_param $node_name "ThreadsPerCore"] > 1 ? 2 : 1]

set client "$sbatch"
set prog "--wrap=hostname"
set options [list "--threads-per-core=$threads_per_core --ntasks-per-core=$threads_per_core"]
foreach option $options {
	set cmd "$client -w $node_name -v -H -J$test_name -t1 $option $prog"

	set job_id 0
	set matches 0
	spawn {*}$cmd
	expect {
		"mutually exclusive" {
			incr matches
			exp_continue
		}
		-re "job ($number)" {
			set job_id $expect_out(1,string)
		}
		timeout {
			fail "[file tail $client] not responding"
		}
		eof {
			wait
		}
	}
	subtest {$job_id != 0} "$client should have given an id"
	subtest {$matches == 0} "$client should not find 'mutually exclusive'" "got $matches matches"

	set threads_req [get_job_param $job_id "ReqB:S:C:T"]
	set tasks_req [get_job_param $job_id "NtasksPerN:B:S:C"]
	set threads_match "0:0:*:$threads_per_core"
	set tasks_match "0:0:*:$threads_per_core"
	subtest {$threads_match eq $threads_req} "ReqB:S:C:T should be $threads_match" "$threads_req != $threads_match"
	subtest {$tasks_match eq $tasks_req} "NtasksPerN:B:S:C should be $tasks_match" "$tasks_req != $tasks_match"
	cancel_job $job_id
}

# Test using --cpu-bind=verbose and --hint for salloc/srun sequence
set output [run_command_output -subtest "$salloc --hint=nomultithread $srun --cpu-bind=verbose /bin/true"]
subtest {[regexp  "cpu-bind=" $output]} "The output should contain line starting with cpu-bind=, indicating that --cpu-bind=verbose is in effect"

set output [run_command_output -subtest "$salloc --hint=nomultithread /bin/bash -c 'SLURM_CPU_BIND=verbose $srun /bin/true'"]
subtest {[regexp  "cpu-bind=" $output]} "The output should contain line starting with cpu-bind=, indicating that SLURM_CPU_BIND=verbose srun .. is in effect"

set output [run_command_output -subtest "SLURM_CPU_BIND=verbose $salloc --hint=nomultithread $srun /bin/true"]
subtest {[regexp  "cpu-bind=" $output]} "The output should contain line starting with cpu-bind=, indicating that SLURM_CPU_BIND=verbose srun .. is in effect"
