#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr add an account
#
# 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://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
source ./globals_accounting

set test_id     "21.10"
set exit_code   0
set acmatches   0
set damatches   0
set dcmatches   0

# Test Account and Cluster
set tc1		"test$test_id-cluster-1"
set ta1		"test$test_id-account.1"

# account Limits
array set acct {}
set acct(Cluster)       $tc1
set acct(Fairshare)     2500
set acct(GrpCPUMins)    1000
set acct(GrpCPURunMins) 100
set acct(GrpCPUs)       20
set acct(GrpJobs)       100
set acct(GrpMemory)     200
set acct(GrpSubmitJobs) 300
set acct(GrpNodes)      100
set acct(GrpWall)       00:45:00
set acct(MaxCPUMins)    100000
set acct(MaxCPUs)       200
set acct(MaxJobs)       500
set acct(MaxSubmitJobs) 400
set acct(MaxNodes)      200
set acct(MaxWall)       01:00:00
set acct(DefaultQos)    "test$test_id\_qos"
set acct(QosLevel)      "test$test_id\_qos"
set acct(Organization)  "Account_Org_A1"
set acct(Description)   "Test_Account_A1"

set access_err  0

print_header $test_id

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

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 set admin=admin.\n"
	exit 0
}

#
# Use sacctmgr to delete the test cluster
#
	set nothing 0
	set matches 0

set my_pid [spawn $sacctmgr -i delete cluster $tc1]

	expect {
		-re "privilege to perform this action" {
			set access_err 1
			exp_continue
		}
		-re "(There was a problem|Unknown condition|Bad format on|Bad MaxWall|Unknown option)" {
			send_user "FAILURE: there was a problem with the sacctmgr command\n"
			incr exit_code 1
		}
		-re "Problem getting" {
			send_user "FAILURE: there was a problem getting information from the database\n"
			incr exit_code 1
		}
		-re "Problem adding" {
			send_user "FAILURE: there was an unknown problem\n"
			incr exit_code 1
		}
		-re "No associations" {
			send_user "FAILURE: your command didn't return anything\n"
			incr exit_code 1
		}
		-re "Deleting clusters" {
			incr matches
			exp_continue
		}
		-re " Nothing deleted" {
			incr matches
			set nothing 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr delete not responding\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}
	if {$access_err != 0} {
		return 1
	}
	if {$matches != 1} {
		send_user "\nFAILURE: sacctmgr had a problem deleting cluster got $matches\n"
		incr exit_code 1
	}
	if { !$nothing } {
		if { ![check_acct_associations] } {
			send_user "\nFAILURE:  Our associations don't line up\n"
			incr exit_code 1
		}
	}

#
# Use sacctmgr to remove an account
#

	set matches 0
	set nothing 1
	set check "Deleting account"

	set my_pid [eval spawn $sacctmgr -i delete account $ta1]
	expect {
		-re "(There was a problem|Unknown condition|Bad format on|Bad MaxWall|Unknown option)" {
			send_user "FAILURE: there was a problem with the sacctmgr command\n"
			incr exit_code 1
		}
		-re "Problem getting" {
			send_user "FAILURE: there was a problem getting information from the database\n"
			incr exit_code 1
		}
		-re "Problem adding" {
			send_user "FAILURE: there was an unknown problem\n"
			incr exit_code 1
		}
		-re "No associations" {
			send_user "FAILURE: your command didn't return anything\n"
			incr exit_code 1
		}
		-re "$check" {
			incr matches
			exp_continue
		}
		-re " Nothing deleted" {
			incr matches
			set nothing 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr add not responding\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 1} {
		send_user "\nFAILURE:  sacctmgr had a problem deleting account.
	got $matches\n"
		incr exit_code 1
	}

	if { !$nothing } {
		if { ![check_acct_associations] } {
			send_user "\nFAILURE:  Our associations don't line up\n"
			incr exit_code 1
		}
	}

#
# Use sacctmgr to create a cluster
#
set sadd_pid [spawn $sacctmgr -i add cluster name=$tc1]
expect {
	-re "privilege to perform this action" {
		set access_err 1
		exp_continue
	}
	-re "Adding Cluster" {
		incr acmatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadd_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$access_err != 0} {
	send_user "\nWARNING: not authorized to perform this test\n"
	exit $exit_code
}
if {$acmatches != 1} {
	send_user "\nFAILURE:  sacctmgr had a problem adding clusters
	got $acmatches\n"
	set exit_code 1
}

if { ![check_acct_associations] } {
	send_user "\nFAILURE:  Our associations don't line up\n"
	set exit_code 1
}

#
# Delete Qos
#
set dmatches 0
set sadel_pid [spawn $sacctmgr -i delete qos $acct(DefaultQos)]
expect {
	-re "Nothing deleted" {
		incr dmatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr delete not responding\n"
		slow_kill $sadel_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$dmatches != 1} {
	send_user "\nFAILURE: sacctmgr had a problem deleting QOS got $dmatches\n"
	set exit_code 1
}

#
# Create tmp QOS
#
set qos_set 0
spawn $sacctmgr -i add qos $acct(DefaultQos)
expect {
	-re "Adding QOS" {
		set qos_set 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$qos_set != 1} {
	send_user "\nFAILURE QOS $dqos1 was not created\n"
	set exit_code 1
}

#
# Use sacctmgr to add an account
#

add_acct $ta1 [array get acct]

#
# Use sacctmgr to list and check the test associations
#
check_assoc_limit 2 account $ta1 [array get acct]

if { ![check_acct_associations] } {
	send_user "\nFAILURE:  Our associations don't line up\n"
	set exit_code 1
}

#
# Use sacctmgr to delete the test account
#
set sadel_pid [spawn $sacctmgr -i delete account $ta1]
expect {
	-re "Deleting account" {
		incr damatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr delete not responding\n"
		slow_kill $sadel_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$damatches != 1} {
	send_user "\nFAILURE: sacctmgr had a problem deleting cluster got $damatches\n"
	set exit_code 1
}

if { ![check_acct_associations] } {
	send_user "\nFAILURE:  Our associations don't line up\n"
	set exit_code 1
}

#
# Use sacctmgr to delete the test cluster
#
set sadel_pid [spawn $sacctmgr -i delete cluster $tc1]
expect {
	-re "Deleting clusters" {
		incr dcmatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr delete not responding\n"
		slow_kill $sadel_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$dcmatches != 1} {
	send_user "\nFAILURE: sacctmgr had a problem deleting cluster got $dcmatches\n"
	set exit_code 1
}


#
# Delete Qos
#
set dmatches 0
set sadel_pid [spawn $sacctmgr -i delete qos $acct(DefaultQos)]
expect {
	-re "Deleting QOS" {
		incr dmatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr delete not responding\n"
		slow_kill $sadel_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$dmatches != 1} {
	send_user "\nFAILURE: sacctmgr had a problem deleting QOS got $dmatches\n"
	set exit_code 1
}

#
# Use sacctmgr to list the test associations
#
set as2_list_pid [spawn $sacctmgr list associations account=$ta1 ]
expect {
	-re "$tc1 *$ta1" {
		exp_continue
		send_user "\nFAILURE: test associations still exist.\n"
		set exit_code 1
	}
	timeout {
		send_user "\nTest associations delete verified.\n"
		slow_kill $as2_list_pid
	}
	eof {
		wait
	}
}

if { ![check_acct_associations] } {
	send_user "\nFAILURE:  Our associations don't line up\n"
	set exit_code 1
}

if {$exit_code == 0} {
	print_success $test_id
}

exit $exit_code
