#!/usr/bin/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://computing.llnl.gov/linux/slurm/>.
# 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 exit_code   0
set nm		name
set qs		QOS
set qs1		[format "%s%s" $test_nu "qosnormal"]
set rs		-1
set dsc		description
set ds1		tqos1
set gc		GrpCpus
set gc1		80
set gcm		GrpCpuMins
set gcm1	240000
set gj		GrpJobs
set gj1		500
set gn		GrpNodes
set gn1		10
set gs		GrpSubmit
set gs1		800
set gw		GrpWall
set gw1		01:00:00
set mc		MaxCpus
set mc1		80
set mcm		MaxCpuMins
set mcm1	60000
set mj		MaxJobs
set mj1		50
set mn		MaxNodes
set mn1		2
set ms		MaxSubmit
set ms1		100
set msj		MaxSubmitJobs
set msj1	100
set mw		Maxwall
set mw1		01:00:00
set pre		Preempt
set pe1		normal
set pm		PreemptMode
set pm1		cluster
set pri		Priority
set pi1		2500
set pi3		0
set uf		UsageFactor
set uf1		5.000000
set uf3		1.000000
set access_err  0

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_NAME admin=admin.\n"
	exit 0
}

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

#add qos
spawn $sacctmgr -i add qos $nm=$qs1 $dsc=$ds1 $gc=$gc1 $gcm=$gcm1 $gj=$gj1 $gn=$gn1 $gs=$gs1 $gw=$gw1 $mc=$mc1 $mcm=$mcm1 $mj=$mj1 $mn=$mn1 $ms=$ms1 $msj=$msj1 $mw=$mw1 $pre=$pe1 $pm=$pm1 $pri=$pi1 $uf=$uf1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

# Use sacctmgr list to verify the test qos fields
#
set matches 0
set my_pid [spawn $sacctmgr -n -p list qos $qs1 format="$gc,$gcm,$gj,$gn,$gs,$gw,$mc,$mcm,$mj,$mn,$ms,$msj,$mw,$pre,$pm,$pri,$uf" ]
expect {
	-re "No valid entity in list command" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "invalid keyword" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Unknown condition" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Unknown field" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "($gc1.$gcm1.$gj1.$gn1.$gs1.01.00.00.$mc1.$mcm1.$mj1.$mn1.$ms1.$msj1.01.00.00.$pe1.$pm1.$pi1.$uf1)" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr list associations not responding\n"
		slow_kill $my_pid
		incr exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 1} {
	send_user "\n\n QoS matches is $matches.\n\n"
	send_user "\nFAILURE:  QOS field incorrect.\n"
		send_user "\nFAILURE: sacctmgr list associations not responding\n"
	incr exit_code 1
}

#modify qos
set matches 0
set my_pid [eval spawn $sacctmgr -i mod qos where $nm=$qs1 set $gc=$rs $gcm=$rs $gj=$rs $gn=$rs $gs=$rs $gw=$rs $mc=$rs $mcm=$rs $mj=$rs $mn=$rs $ms=$rs $msj=$rs $mw=$rs $pri=$rs $uf=$rs]
expect {
	-re "No valid entity in list command" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "invalid keyword" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Unknown condition" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Nothing modified" {
		send_user "FAILURE: sacctmgr command unable to modify\n"
	    	incr exit_code 1
	}
	-re "Unknown field" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Modified qos" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr modify associations not responding\n"
		slow_kill $my_pid
		incr exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 1} {
	send_user "\n\n QoS matches is $matches.\n\n"
	send_user "\nFAILURE:  QOS modify not working.\n"
	incr exit_code 1
}

#
# Use sacctmgr to list the test qos modifications
#
# Use sacctmgr list to verify the test qos fields
#
set matches 0
set my_pid [spawn $sacctmgr -n -p list qos $qs1 format="$gc,$gcm,$gj,$gn,$gs,$gw,$mc,$mcm,$mj,$mn,$ms,$msj,$mw,$pre,$pm,$pri,$uf" ]
expect {
	-re "No valid entity in list command" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "invalid keyword" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Unknown condition" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "Unknown field" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "(.............$pe1.$pm1.$pi3.$uf3)" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr list associations not responding\n"
		slow_kill $my_pid
		incr exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 1} {
	send_user "\n\n QoS matches is $matches.\n\n"
	send_user "\nFAILURE:  QOS field incorrect.\n"
		send_user "\nFAILURE: sacctmgr list associations not responding\n"
	incr exit_code 1
}

# Next is clean up and the end

incr exit_code [remove_qos "$qs1"]

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