#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of --use-min-nodes option.
############################################################################
# Copyright (C) 2016 SchedMD LLC.
# Written by Alejandro Sanchez <alex@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 file_in     "test$test_id.input"
set file_script "test$test_id.bash"
set file_out    "test$test_id.output"
set file_err    "test$test_id.error"
set exit_code   0
set needed_nodes 2
set matches 0
set ncpus ""

proc cleanup {} {
	global bin_rm file_in file_script file_out file_err

	exec $bin_rm -f $file_in $file_script $file_out $file_err
}

if {[param_contains [get_config_param "SelectTypeParameters"] "CR_ONE_TASK_PER_CORE"]} {
	skip "This test is incompatible SelectTypeParameters=CR_ONE_TASK_PER_CORE"
}

log_user 0
spawn $sinfo -N -h -p [default_partition] -t idle -o "%c"
expect {
	-re "($number)" {
		incr matches
		if {$ncpus eq ""} {
			set ncpus $expect_out(1,string)
		} elseif {$expect_out(1,string) < $ncpus} {
			set ncpus $expect_out(1,string)
		}
		exp_continue
	}
	timeout {
		fail "sinfo not responding"
	}
	eof {
		wait
	}
}
log_user 1

if {$matches < $needed_nodes} {
	skip "$needed_nodes idle nodes required in default partition, only $matches found"
}

#
# Delete left-over stdin/out/err files
# Build stdin file
#
exec $bin_rm -f $file_in $file_script $file_out $file_err
make_bash_script $file_in "
  $bin_printenv SLURM_NNODES
  exit 0
"

set cwd "[$bin_pwd]"
make_bash_script $file_script "
  $sbatch -N1-2 --use-min-nodes -n$ncpus --output=$file_out --error=$file_err $cwd/$file_in
  exit 0
"

#
# Spawn a sbatch command to verify that the default output file is
# created and contain the correct contents
#
set job_id 0
spawn ./$file_script
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}

if { $job_id == 0 } {
	fail "Failed to submit job"
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	log_error "Waiting for job to complete"
	set exit_code 1
}

#
# Check for desired output
#
set nodes 0
if {[wait_for_file $file_out] == 0} {
	spawn $bin_cat $file_out
	expect {
		-re "($number)" {
			set nodes $expect_out(1,string)
			if {$nodes != 1} {
				log_error "Expected 1 allocated node"
				set exit_code 1
			}
			exp_continue
		}
		eof {
			wait
		}
	}
} else {
	fail "Job didn't produce the expected $file_out output file"
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
