#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr modify limits for nested accounts with multiple users
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 tc1         "${test_name}-cluster-1"
set ta1         "${test_name}-account.1"
set ta2         "${test_name}-account.2"
set ta3         "${test_name}-account.3"
set tu1         "${test_name}-user.1"
set tu2         "${test_name}-user.2"
set cpusval1    2
set cpusval2    3
set cpusval3    4
set nodesval    5
set prio1       6
set prio2       7
set prio3       8
set access_err  0

# Cluster
array set clus_req {}

# Accounts
array set acct_req {}
set acct_req(cluster) $tc1

# User
array set user_req {}
set user_req(cluster) $tc1
set user_req(account) $ta1,$ta2,$ta3

# Mod account 1
array set acct_mod_desc {}
set acct_mod_desc(cluster) $tc1
array set acct_mod_acct_vals {}
array set acct_mod_assoc_vals {}
set acct_mod_assoc_vals(parent) $ta1

# Mod account 2
array set acct_mod_desc2 {}
array set acct_mod_acct_vals2 {}
array set acct_mod_assoc_vals2 {}
set acct_mod_assoc_vals2(maxcpus) $cpusval1
set acct_mod_assoc_vals2(priority) $prio1

# Mod account 3
array set acct_mod_desc3 {}
array set acct_mod_acct_vals3 {}
array set acct_mod_assoc_vals3 {}
set acct_mod_assoc_vals3(maxcpus) $cpusval2
set acct_mod_assoc_vals3(priority) $prio2

# Mod account 4
array set acct_mod_desc4 {}
array set acct_mod_acct_vals4 {}
array set acct_mod_assoc_vals4 {}
set acct_mod_assoc_vals4(maxnodes) $nodesval

# Mod User 1
array set user_mod_desc {}
set user_mod_desc(account) $ta2
array set user_mod_acct_vals {}
array set user_mod_assoc_vals {}
set user_mod_assoc_vals(maxcpus) $cpusval3
set user_mod_assoc_vals(priority) $prio3

proc cleanup {} {
	global ta1 ta2 ta3 tc1 tu1 tu2

	remove_user "" "" "$tu1,$tu2"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1"
}

#
# 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"
}

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

# Add the cluster
if [add_cluster "$tc1" [array get clus_req]] {
	fail "Unable to add cluster"
}

# Add the accounts (3 of them)
if [add_acct "$ta1,$ta2,$ta3" [array get acct_req]] {
	fail "Unable to add accounts"
}

# Add the users (2 of them)
if [add_user "$tu1,$tu2" [array get user_req]] {
	fail "Unable to add users"
}

# Modify the 2nd and 3rd accounts - make them children of the first
# No need to check the changes. This is done in a different test.
if [mod_acct "$ta2,$ta3" [array get acct_mod_desc] [array get acct_mod_assoc_vals] [array get acct_mod_acct_vals]] {
	fail "Unable to modify 2nd and 3rd accounts to be children of the first"
}

# Modify the top account (give it TRESlimit=cpu=number)
if [mod_acct "$ta1" [array get acct_mod_desc2] [array get acct_mod_assoc_vals2] [array get acct_mod_acct_vals2]] {
	fail "Unable to modify the top account giving it TRESlimit=cpu=number"
}

# Check the changes
set matches 0
spawn $sacctmgr -n -P list assoc account=$ta1,$ta2,$ta3 format="account,user,maxtres,priority"
expect {
	-re "There was a problem" {
		fail "There was a problem with the sacctmgr command"
	}
	-re "(($ta1|$ta2|$ta3)\\|($tu1|$tu2){0,6}\\|cpu=$cpusval1\\|$prio1)" {
		incr matches
		exp_continue
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}
if {$matches != 9} {
	fail "Account modification 1 incorrect ($matches != 9)"
}

# Modify the child account (give it TRESlimit=cpu)
if [mod_acct "$ta2" [array get acct_mod_desc3] [array get acct_mod_assoc_vals3] [array get acct_mod_acct_vals3]] {
	fail "Unable to modify the child account giving it TRESlimit=cpu"
}

# Check the changes
set matches 0
spawn $sacctmgr -n -P list assoc account=$ta1,$ta2,$ta3 format="account,user,maxtres,priority"
expect {
	-re "There was a problem" {
		fail "There was a problem with the sacctmgr list assoc command"
	}
	-re "(($ta1|$ta3)\\|($tu1|$tu2){0,6}\\|cpu=$cpusval1\\|$prio1)|($ta2\\|($tu1|$tu2){0,6}\\|cpu=$cpusval2\\|$prio2)" {
		incr matches
		exp_continue
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}
if {$matches != 9} {
	fail "Account modification 2 incorrect ($matches != 9)"
}

# Modify the child accounts (give them TRESlimit=node_limit)
if [mod_acct "$ta2,$ta3" [array get acct_mod_desc4] [array get acct_mod_assoc_vals4] [array get acct_mod_acct_vals4]] {
	fail "Unable to modify the child accounts giving them TRESlimit=node_limit"
}

# Check the changes
set matches 0
spawn $sacctmgr -n -P list assoc account=$ta1,$ta2,$ta3 format="account,user,maxtres"
expect {
	-re "There was a problem" {
		fail "There was a problem with the sacctmgr list assoc command"
	}
	-re "($ta1\\|($tu1|$tu2){0,6}\\|cpu=$cpusval1)|($ta2\\|($tu1|$tu2){0,6}\\|cpu=$cpusval2,node=$nodesval)|($ta3\\|($tu1|$tu2){0,6}\\|cpu=$cpusval1,node=$nodesval)" {
		incr matches
		exp_continue
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}
if {$matches != 9} {
	fail "Account modification 3 incorrect ($matches != 9)"
}

# Modify a child user
set acct_mod_assoc_vals4(maxnodes) -1
if [mod_acct "$ta2,$ta3" [array get acct_mod_desc4] [array get acct_mod_assoc_vals4] [array get acct_mod_acct_vals4]] {
	fail "Unable to modify accounts"
}

if [mod_user "$tu1" [array get user_mod_desc] [array get user_mod_assoc_vals] [array get user_mod_user_vals]] {
	fail "Unable to modify a child user"
}

# Check the changes
set matches 0
spawn $sacctmgr -n -P list assoc account=$ta1,$ta2,$ta3 format="account,user,maxtres,priority"
expect {
	-re "There was a problem" {
		fail "There was a problem with the sacctmgr list assoc command"
	}
	-re "(($ta1|$ta3)\\|($tu1|$tu2){0,6}\\|cpu=$cpusval1\\|$prio1)|($ta2\\|($tu1){0,6}\\|cpu=$cpusval3\\|$prio3)|($ta2\\|($tu2){0,6}\\|cpu=$cpusval2\\|$prio2)" {
		incr matches
		exp_continue
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}
if {$matches != 9} {
	fail "Account modification 4 incorrect ($matches != 9)"
}


# Unset the user specific values
set user_mod_assoc_vals(priority) -1
set user_mod_assoc_vals(maxcpus) -1
if [mod_user "$tu1" [array get user_mod_desc] [array get user_mod_assoc_vals] [array get user_mod_user_vals]] {
	fail "Unable to unset the user specific values"
}

# Check the changes
set matches 0
spawn $sacctmgr -n -P list assoc account=$ta1,$ta2,$ta3 format="account,user,maxtres,priority"
expect {
	-re "There was a problem" {
		fail "There was a problem with the sacctmgr list assoc command"
	}
	-re "(($ta1|$ta3)\\|($tu1|$tu2){0,6}\\|cpu=$cpusval1\\|$prio1)|($ta2\\|($tu1|$tu2){0,6}\\|cpu=$cpusval2\\|$prio2)" {
		incr matches
		exp_continue
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}
if {$matches != 9} {
	fail "Account modification 5 incorrect ($matches != 9)"
}
