#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that scontrol show assoc_mgr shows the data cached in
#          the slurmctld
############################################################################
# Copyright (C) 2015 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# 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

set test_acct    "test$test_id\_acct"
set test_user    ""
set exit_code    0

if {![is_super_user]} {
	skip "This test can't be run except as SlurmUser or root"
} elseif {![param_contains [get_config_param "AccountingStorageEnforce"] "limits"]} {
	skip "AccountingStorageEnforce limits must be set"
}

proc mod_assoc_vals { } {
	global sacctmgr test_acct test_user

	set modified 0
	spawn $sacctmgr mod -i account $test_acct where user=$test_user set \
	    GrpCpus=3 MaxJobs=2 MaxCPUs=6 Priority=9
	expect {
		-re "Modified account associations" {
			set modified 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}

	if {!$modified} {
		fail "Account limits were not modified"
	}
}

proc clear_assoc_vals { } {
	global sacctmgr test_acct test_user

	set modified 0
	spawn $sacctmgr mod -i account $test_acct where user=$test_user set \
	    GrpCpus=-1 MaxJobs=-1 MaxCPUs=-1 Priority=-1
	expect {
		-re "Modified account associations" {
			set modified 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}

	if {!$modified} {
		fail "Account limits were not modified"
	}
}

proc delete_test_acct { } {
	global test_acct sacctmgr

	set deleted 0
	spawn $sacctmgr delete -i account $test_acct
	expect {
		-re "Deleting accounts..." {
			set deleted 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}
}

# Remove any vestigial accounts
delete_test_acct

# Get username
set test_user [get_my_user_name]

if {[get_config_param "AccountingStorageType"] eq "accounting_storage/slurmdbd"} {
	# Add test Account
	set acct_added 0
	set cluster [get_config_param "ClusterName"]
	spawn $sacctmgr add -i account $test_acct cluster=$cluster
	expect {
		-re "Adding Account" {
			set acct_added 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}

	if {!$acct_added} {
		fail "Could not add test account $test_acct"
	}

	# Add user to test account
	set user_added 0
	spawn $sacctmgr add -i user $test_user account=$test_acct
	expect {
		-re "Associations" {
			set user_added 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not reponding"
		}
		eof {
			wait
		}
	}

	if {!$user_added} {
		fail "Could not add user to test account"
	}

	set match 0
	spawn $bin_bash -c "exec $scontrol -o show assoc_mgr | $bin_grep Account=$test_acct| $bin_grep UserName=$test_user"
	expect {
		-re " Account=$test_acct" {
			incr match
			exp_continue
		}
		-re " UserName=$test_user" {
			incr match
			exp_continue
		}
		-re " GrpTRES=" {
			incr match
			exp_continue
		}
		-re " MaxJobs=" {
			incr match
			exp_continue
		}
		-re " MaxTRESPJ=" {
			incr match
			exp_continue
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}

	if {$match != 5} {
		log_error "1. scontrol show assoc_mgr did not show correct information ($match != 5)"
		set exit_code 1
	}

	# Set association limits and check that controller is updated
	mod_assoc_vals

	set match 0
	spawn $bin_bash -c "exec $scontrol -o show assoc_mgr | $bin_grep Account=$test_acct| $bin_grep UserName=$test_user"
	expect {
		-re " Account=$test_acct" {
			incr match
			exp_continue
		}
		-re " UserName=$test_user" {
			incr match
			exp_continue
		}
		-re " Priority=9" {
			incr match
			exp_continue
		}
		-re " GrpTRES=cpu=3" {
			incr match
			exp_continue
		}
		-re " MaxJobs=2" {
			incr match
			exp_continue
		}
		-re " MaxTRESPJ=cpu=6" {
			incr match
			exp_continue
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}

	if {$match != 6} {
		log_error "2. scontrol show assoc_mgr did not show correct information ($match != 6)"
		set exit_code 1
	}

	# Clear associtation limits and check controller is updated
	clear_assoc_vals

	set match 0
	set limit_match 0
	spawn $bin_bash -c "exec $scontrol show assoc_mgr | $bin_grep Account=$test_acct| $bin_grep UserName=$test_user"
	expect {
		-re " Account=$test_acct" {
			incr match
			exp_continue
		}
		-re " UserName=$test_user" {
			incr match
			exp_continue
		}
		-re " Priority=0" {
			incr match
			exp_continue
		}
		-re " GrpTRES=cpu=3" {
			incr limit_match
			exp_continue
		}
		-re " MaxJobs=2" {
			incr limit_match
			exp_continue
		}
		-re " MaxTRESPJ=cpu=6" {
			incr limit_match
			exp_continue
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}

	if {$match != 3 && $limit_match} {
		log_error "3. scontrol show assoc_mgr did not show correct information ($match != 3)"
		set exit_code 1
	}

	delete_test_acct

} else {

	set match 0
	spawn $scontrol show assoc_mgr
	expect {
		-re "Current Association Manager state" {
			incr match
			exp_continue
		}
		-re "No users currently cached in Slurm" {
			incr match
			exp_continue
		}
		-re "No associations currently cached in Slurm" {
			incr match
			exp_continue
		}
		-re "No QOS currently cached in Slurm" {
			incr match
			exp_continue
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}

	if {$match != 4} {
		log_error "4. scontrol show assoc_mgr did not show correct information ($match != 4)"
		set exit_code 1
	}
}

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