#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that scontrol show assoc_mgr shows the data cached in
#          the slurmctld
#
# 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) 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_id      2.25
set test_acct    "test$test_id\_acct"
set test_user    ""
set exit_code    0

print_header $test_id

if {![test_super_user]} {
	send_user "\nWARNING: This test can't be run except as SlurmUser or root\n"
	exit 0
} elseif {![test_limits_enforced]} {
	send_user "\nWARNING: AccountingStorageEnforce limits must be set\n"
	exit 0
}

proc mod_assoc_vals { } {
	global sacctmgr test_acct test_user exit_code

	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 {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {!$modified} {
		send_user "\nFAILURE: account limits were not modified\n"
		exit 1
	}
}

proc clear_assoc_vals { } {
	global sacctmgr test_acct test_user exit_code

	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 {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {!$modified} {
		send_user "\nFAILURE: account limits were not modified\n"
		exit 1
	}
}

proc delete_test_acct { } {
	global test_acct sacctmgr exit_code

	set deleted 0
	spawn $sacctmgr delete -i account $test_acct
	expect {
		-re "Deleting accounts..." {
			set deleted 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}

# Remove any vestigial accounts
delete_test_acct

# Get username
set test_user [get_my_user_name]

if {[test_using_slurmdbd]} {
	# Add test Account
	set acct_added 0
	set cluster [get_cluster_name]
	spawn $sacctmgr add -i account $test_acct cluster=$cluster
	expect {
		-re "Adding Account" {
			set acct_added 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {!$acct_added} {
		send_user "\nFAILURE: could not add test account $test_acct\n"
		exit 1
	}

	# 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 {
			send_user "\nFAILURE: sacctmgr is not reponding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {!$user_added} {
		send_user "\nFAILURE: could not add user to test account\n"
		exit 1
	}

	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 {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 5} {
		send_user "\nFAILURE: 1 scontrol show assoc_mgr did not show correct information ($match != 5).\n"
		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 {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 6} {
		send_user "\nFAILURE: 2 scontrol show assoc_mgr did not show correct information ($match != 6).\n"
		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 {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 3 && $limit_match} {
		send_user "\nFAILURE: 3 scontrol show assoc_mgr did not show correct information ($match != 3).\n"
		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 {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 4} {
		send_user "\nFAILURE: 4 scontrol show assoc_mgr did not show correct information ($match != 4)\n"
		set exit_code 1
	}
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
