#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr clear (modify) QoS values
#
#
# 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) 2010 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Joseph Donaghy <donaghy1@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_accounting

set test_id     "test21.29"
set test_nu     "test21-29"
set usagefactor_reset  1.000000
set priority_reset     0
set exit_code   0
set access_err  0

# QoS Limits
array set qos {}
set qos_name          [format "%s%s" $test_nu "qosnormal"]
set qos(GrpCPUMins)    240000
set qos(GrpCPUs)       80
set qos(GrpJobs)       500
set qos(GrpSubmitJobs) 800
set qos(GrpNodes)      10
set qos(GrpWall)       01:00:00
set qos(MaxCPUs)       80
set qos(MaxCPUMins)    60000
set qos(MaxJobs)       50
set qos(MaxSubmitJobs) 100
set qos(MaxNodes)      2
set qos(MaxWall)       01:00:00
set qos(Description)   "tqos1"
set qos(Preempt)       "normal"
set qos(PreemptMode)   "cluster"
set qos(Priority)      2500
set qos(UsageFactor)   5.000000

print_header $test_id

set timeout 60

#
# Check accounting config and bail if not found.
#
if { [test_account_storage] == 0 } {
	send_user "\nWARNING: This test can't be run without a usable AccountStorageType\n"
	exit 0
}

if { [string compare [check_accounting_admin_level] "Administrator"] } {
	send_user "\nWARNING: This test can't be run without being an Accounting administrator.\nUse: sacctmgr mod user \$USER set admin=admin.\n"
	exit 0
}

# verify a clean QOS entity and permission to do this work
remove_qos "$qos_name"
if {$access_err != 0} {
	send_user "\nWARNING: not authorized to perform this test\n"
	exit $exit_code
}

#add qos
add_qos $qos_name [array get qos]

# Use sacctmgr list to verify the test qos fields
set exit_code [check_qos_limits $qos_name  [array get qos]]

# Setup the qos array so that we clear all the limits
foreach option [array name qos] {
	if {[string compare $option "PreemptMode"] != 0 &&
	    [string compare $option "Preempt"] != 0} {
		set qos($option) -1
	}
}
set qos(UsageFactor) $usagefactor_reset
set qos(Description) ""
set qos(Priority) $priority_reset

#modify qos
set exit_code [mod_qos $qos_name [array get qos]]

# Check that the modified QoS limits are correct
set exit_code [check_qos_limits $qos_name [array get qos]]

# Next is clean up and the end
incr exit_code [remove_qos "$qos_name"]

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
} else {
	send_user "\nFAILURE\n"
}
exit $exit_code
