#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of nice value specification (--nice option).
#
# 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) 2005-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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 test_id     "17.23"
set exit_code   0
set file_in     "test$test_id.input"
set job_id1     0
set job_id2     0
set job_id3     0
set job_prio1   0
set job_prio2   0
set job_prio3   0

print_header $test_id

#
# Build input script file
#
make_bash_script $file_in "$bin_sleep 60"

#
# Submit three jobs with differing nice values
#
spawn $sbatch --output=/dev/null --error=/dev/null -t2 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id1 == 0} {
	send_user "\nFAILURE: sbatch submit failed\n"
	exit 1
}

spawn $sbatch --output=/dev/null --error=/dev/null -t2 --nice $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id2 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id2 == 0} {
	send_user "\nFAILURE: sbatch submit failed\n"
	cancel_job $job_id1
	exit 1
}

spawn $sbatch --output=/dev/null --error=/dev/null -t2 --nice=200 $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id3 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id3 == 0} {
	send_user "\nFAILURE: sbatch submit failed\n"
	cancel_job $job_id1
	cancel_job $job_id2
	exit 1
}

exec $bin_rm -f $file_in

#
# Get the priority of each job job with scontrol
#
spawn $scontrol show job $job_id1
expect {
	-re "Priority=($number)" {
		set job_prio1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
spawn $scontrol show job $job_id2
expect {
	-re "Priority=($number)" {
		set job_prio2 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
spawn $scontrol show job $job_id3
expect {
	-re "Priority=($number)" {
		set job_prio3 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Make sure the job priorities are as expected
#
if {$job_prio1 == 0 || $job_prio2 == 0 || $job_prio3 == 0} {
	send_user "\nFAILURE: failed to job priorities of each submitted job\n"
	set exit_code 1
} elseif {$job_prio1 < 1000 || $job_prio2 < 1000 || $job_prio3 < 1000} {
	send_user "\nWARNING: PriorityWeight factors result in a job priority too low for this test\n"
} else {
	set diff2 [expr $job_prio1 - $job_prio2]
	set diff3 [expr $job_prio1 - $job_prio3]
#	Target for diff2 is 101
	if {$diff2 < 91 || $diff2 > 111} {
		send_user "\nFAILURE: job2 priority delta bad ($diff2, target is 101)\n"
		set exit_code 1
	}
#	Target for diff3 is 202
	if {$diff3 < 192 || $diff3 > 212} {
		send_user "\nFAILURE: job3 priority delta bad ($diff3, target is 202)\n"
		set exit_code 1
	}
}

cancel_job $job_id1
cancel_job $job_id2
cancel_job $job_id3
if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code

