#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr modify account, descriptions, limits
#
# 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) 2008-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
source ./globals_accounting

set test_id     "21.13"
set exit_code   0
set tc1		slurm_test-cluster-1
set tc2		slurm_test-cluster-2
set tc3		slurm_test-cluster-3
set fs1		2500
set fs2		1700
set mc1		1000000
set mc2		700000
set mj1		50
set mj2		70
set mn1		300
set mn2		700
set mw1		01:00:00
set mw2		00:07:00
set ta1		slurm_test-account.1
set ta2		slurm_test-account.2
set ta3		slurm_test-account.3
set ds1		slurmtestaccount1
set ds2		slurmtestaccount2
set ds3		slurmtestaccount3
set or1		accountorg1
set or2		accountorg2
set or3		accountorg3
set qs1		normal
set par		parent
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
}

#
# Verify if Administrator privileges
#
 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
}

#make sure we have a clean system and permission to do this work
remove_cluster "$tc1,$tc2"
if {$access_err != 0} {
	send_user "\nWARNING: not authorized to perform this test\n"
	exit $exit_code
}
remove_acct "" "$ta1,$ta2,$ta3"

#add cluster
incr exit_code [add_cluster "$tc1,$tc2" "" "" "" "" "" "" "" "" "" "" "" ""]
if { $exit_code } {
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2"
	exit $exit_code
}

#add accounts
incr exit_code [add_acct "$tc1,$tc2" "" "$ta1,$ta2,$ta3" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""]
if { $exit_code } {
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2"
	exit $exit_code
}

# OK This is the real test, first test change the desc and org of the accounts
incr exit_code [mod_acct "" "" "$ta1,$ta2,$ta3" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" $ds2 $or2]
if { $exit_code } {
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2"
	exit $exit_code
}

#
# Use sacctmgr to list the test account modifications
#
set matches 0
set my_pid [spawn $sacctmgr -n -p list account account=$ta1,$ta2,$ta3 format="Account,Description,Organization"]
expect {
	-re "There was a problem" {
	        send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "($ta1|$ta2|$ta3).$ds2.$or2." {
		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 != 3} {
	send_user "\nFAILURE:  Account modification 1 incorrect with only $matches.\n"
	incr exit_code 1
}

# Next, test change the limits of the accounts
incr exit_code [mod_acct "" "" "$ta1" "" "" "" "$fs2" "" "" "" "" "" "" "$mc2" "" "$mj2" "$mn2" "" "$mw2" "" "" ""]
if { $exit_code } {
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2"
	exit $exit_code
}

#
# Use sacctmgr to list the test account modifications
#
set matches 0
set my_pid [spawn $sacctmgr -n -p list associations account=$ta1,$ta2,$ta3 format="Account,Cluster,Fairshare,MaxCPUM,MaxJobs,MaxNodes,MaxWall"]
expect {
	-re "There was a problem" {
	        send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "($ta1.($tc1|$tc2).$fs2.$mc2.$mj2.$mn2.$mw2.|($ta2|$ta3).($tc1|$tc2).1)" {
		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 != 6} {
	send_user "\nFAILURE:  Account modification 2 incorrect with only $matches.\n"
	incr exit_code 1
}

# Next, test change the desc and limits of the accounts
incr exit_code [mod_acct "" "" "$ta1" "" "" "" -1 "" "" "" "" "" "" -1 "" -1 -1 "" -1 "" $ds1 $or1]
if { $exit_code } {
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2"
	exit $exit_code
}

#
# Use sacctmgr to list the test account modifications
#
set matches 0
set my_pid [eval spawn $sacctmgr -n -p list account withassoc account=$ta1,$ta2,$ta3 format="Account,Description,Organization,Cluster,Fairshare,MaxCPUM,MaxJobs,MaxNodes,MaxWall"]
expect {
	-re "There was a problem" {
	        send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	# Any time you use () around something you need to combine all
	# those things together since you can miss some and they be thrown away
	-re "($ta1.$ds1.$or1.($tc1|$tc2).1.....|($ta2|$ta3).$ds2.$or2.($tc1|$tc2).1.....)" {
		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 != 6} {
	send_user "\nFAILURE:  Account modification 6 incorrect with $matches.\n"
	incr exit_code 1
}

# This is the end below here

incr exit_code [remove_acct "$tc1,$tc2" "$ta1,$ta2,$ta3"]
incr exit_code [remove_acct "" "$ta1,$ta2,$ta3"]
incr exit_code [remove_cluster "$tc1,$tc2"]

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

