#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr clear (modify) resource values
############################################################################
# Written by Bill Brophy <bill.brophy@bull.com>
#  Copyright (C) 2012 Bull S. A. S.
#		Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois.
#  This program 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 version 2 of the License.
#  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
############################################################################
source ./globals_accounting

set tc2         "${test_name}-cluster-2"
set sr1         [format "%s%s" $test_name "res_1"]
set sr2         [format "%s%s" $test_name "res_2"]
set sr3         [format "%s%s" $test_name "res_3"]

set res1_desc   sr1
set res1_cnt    80
set res1_srvtyp server_type_1
set res1_srv    server_1
set res1_type   license
set res1_allw   50

set res2_desc   "sr2&sr3"
set res2_cnt    100
set res2_srvtyp server_type_2
set res2_srv    server_2
set res2_type   license

set ser         server
set ect1	    40
set ect1a	    25
set access_err  0

# First test resource
array set resource1 {}
set resource1(description)           $res1_desc
set resource1(count)                 $res1_cnt
set resource1(ServerType)            $res1_srvtyp
set resource1(Server)                $res1_srv
set resource1(type)                  $res1_type
set resource1(percentallowed)        $res1_allw

# First check test resources
array set resource1_chck {}
set resource1_chck(description)      $res1_desc
set resource1_chck(count)            $res1_cnt
set resource1_chck(ServerType)       $res1_srvtyp
set resource1_chck(Server)           $res1_srv

# Limit changes to the first resource
array set resource1_chng {}
set resource1_chng(count)            50

# Second and thrid test resources
array set resource2 {}
set resource2(description)           $res2_desc
set resource2(count)                 $res2_cnt
set resource2(ServerType)            $res2_srvtyp
set resource2(Server)                $res2_srv
set resource2(type)                  $res2_type

array set resource2_chck {}
set resource2_chck(description)      $res2_desc
set resource2_chck(count)            $res2_cnt
set resource2_chck(ServerType)       $res2_srvtyp
set resource2_chck(Server)           $res2_srv

# Cluster
array set clus_req {}

set timeout 60

#
# Check accounting config and bail if not found.
#
if {[get_config_param "AccountingStorageType"] ne "accounting_storage/slurmdbd"} {
	skip "This test can't be run without a usable AccountStorageType"
}

#
# Check that current user is root or SlurmUser
#
if {![is_super_user]} {
	skip "Test can only be ran as SlurmUser"
}

#if {[get_admin_level] ne "Administrator"} {
#	skip "This test can't be run without being an Accounting administrator.\nUse: sacctmgr mod user \$USER set admin=admin"
#}

proc cleanup {} {
	global sr2 sr3 tc2

	#
	# Clean up and exit
	#
	remove_res $sr2,$sr3
	remove_cluster $tc2
}

# Make sure we have a clean system and permission to do this work
cleanup
if {$access_err != 0} {
	skip "Not authorized to perform this test"
}

#
# Add cluster
#
if [add_cluster "$tc2" [array get clus_req]] {
	fail "Unable to add cluster ($tc2)"
}

#
# get name of this cluster
#
set cluster [get_config_param "ClusterName"]
set resource1(cluster) "$cluster,$tc2"

#
# add a global resource designating multiple clusters
#
add_resource $sr1 [array get resource1]

#
# Use sacctmgr list to verify the test global resource fields
#
if {[check_resource_limits $sr1 [array get resource1_chck]] == 1} {
	fail "list resource output is incorrect"
}

#
# use scontrol show license to verify the cluster license was created
#
set matches 0
wait_for -subtest {$matches == 2} {
	set output [run_command_output -fail "$scontrol show license"]
	set matches 0
	incr matches [regexp "LicenseName=($sr1@$resource1(Server))" $output]
	incr matches [regexp "Total=$ect1 Used=0 Free=$ect1 Reserved=0 Remote=yes" $output]
}

#
# modify resources
#
if {[mod_resource $sr1 [array get resource1_chng]] == 1} {
	fail "Resource modify not working ($matches != 1)"
}

#
# Use sacctmgr to list the test resource modifications
#
# Use sacctmgr list to verify the test resource fields
#
set resource1_chck(count) $resource1_chng(count)
if {[check_resource_limits $sr1 [array get resource1_chck]] == 1} {
	fail "List resource output is incorrect"
}

#
# use scontrol to verify the modified cluster license
#
set matches 0
wait_for -subtest {$matches == 2} {
	set output [run_command_output -fail "$scontrol show license $sr1@$resource1(Server)"]
	set matches 0
	incr matches [regexp "LicenseName=$sr1@$resource1(Server)" $output]
	incr matches [regexp "Total=$ect1a Used=0 Free=$ect1a Reserved=0 Remote=yes" $output]
}

#
# remove resource
#
if [remove_res $sr1] {
	fail "Unable to remove resource ($sr1)"
}

#
# use scontrol to verify cluster license was removed
#
set matches 1
wait_for -subtest {$matches == 0} {
	set output [run_command_output -fail "$scontrol show license $sr1@$resource1(Server)"]
	set matches 0
	incr matches [regexp "LicenseName=$sr1@$resource1(Server)" $output]
}

#
# add multiple global resources in a single call
#
add_resource "$sr2,$sr3" [array get resource2]

#
# Use sacctmgr list to verify both global resources were added
#
# Check resource 2
if {[check_resource_limits $sr2 [array get resource2_chck]] == 1} {
	fail "List resource output is incorrect"
}

# Check resource 3
if {[check_resource_limits $sr3 [array get resource2_chck]] == 1} {
	fail "List resource output is incorrect"
}
