#!/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 exit_code   0
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 {[string compare [get_admin_level] "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
}

# verify a clean system and permission to do this work
remove_cluster "$tc2"
remove_res "$sr1"
if {$access_err != 0} {
	skip "Not authorized to perform this test"
}

#
# add cluster
#
incr exit_code [add_cluster "$tc2" [array get clus_req]]
if { $exit_code } {
	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} {
	log_error "list resource output is incorrect"
	incr exit_code 1
}

#
# use scontrol show license to verify the cluster license was created
#
set matches 0
set my_pid [eval spawn $scontrol show license]
expect {
	-re "LicenseName=($sr1@$resource1(Server))" {
		incr matches
		exp_continue
	}
	-re "Total=$ect1 Used=0 Free=$ect1 Reserved=0 Remote=yes" {
		incr matches
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$matches != 2} {
	log_error "License output is incorrect ($matches != 2). Expected: LicenseName=$sr1@$resource1(Server). Expected: Total=$ect1 Used=0 Free=$ect1 Reserved=0 Remote=yes"
	incr exit_code 1
}

#
# modify resources
#
if {[mod_resource $sr1 [array get resource1_chng]] == 1} {
	log_error "Resource modify not working ($matches != 1)"
	incr exit_code 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} {
	log_error "List resource output is incorrect"
	incr exit_code 1
}

#
# use scontrol to verify the modified cluster license
#
set matches 0
set my_pid [eval spawn $scontrol show license $sr1@$resource1(Server)]
expect {
	-re "LicenseName=$sr1@$resource1(Server)" {
		incr matches
		exp_continue
	}
	-re "Total=$ect1a Used=0 Free=$ect1a Reserved=0 Remote=yes" {
		incr matches
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$matches != 2} {
	log_error "License output is incorrect ($matches != 2). Expected: LicenseName=$sr1@$resource1(Server). Expected: Total=$ect1 Used=0 Free=$ect1 Reserved=0 Remote=yes"
	incr exit_code 1
}

#
# remove resource
#
incr exit_code [remove_res $sr1]

#
# use scontrol to verify cluster license was removed
#
set matches 0
set my_pid [eval spawn $scontrol show license]
expect {
	-re "LicenseName=$sr1@$resource1(Server)" {
		incr matches
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$matches != 0} {
	log_error "scontrol failed to remove license $sr1@$resource1(Server)"
	incr exit_code 1
}

#
# 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} {
	log_error "List resource output is incorrect"
	incr exit_code 1
}

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

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