#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr add multiple accounts
#
# 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 <http://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.11"
set exit_code   0
set acmatches   0
set aamatches   0
set almatches   0
set lmatches    0
set damatches   0
set dcmatches   0
set not_support 0
set nam		Name
set fs		Fairshare
set mc          MaxCPUs
set mcm		MaxCPUMins
set mj		MaxJobs
set msj         MaxSubmitJobs
set mn		MaxNodes
set mw		MaxWall
set gcm         GrpCPUMins
set gc          GrpCPU
set gcrm        GrpCPURunMins
set gj		GrpJobs
set gmem        GrpMemory
set gsj 	GrpSubmitJobs
set gn		GrpNodes
set gw		GrpWall


set tc1		slurm_test-cluster-1
set tc2		slurm_test-cluster-2
set fs1		2500
set mc1         100
set mcm1	1000000
set mj1		50
set msj1        60
set mn1		300
set mw1		01:00:00
set gcm1        100
set gc1         50
set gcrm1       150
set gj1         100
set gmem1       60
set gsj1        500
set gn1         100
set gw1         00:45:00



set ta1		slurm_test-account.1
set ta2		slurm_test-account.2
set ds1		"Test Account A1"
set or1		"Account Org A1"
set dqos1        "test$test_id\_qos"
set access_err  0
set timeout 60
print_header $test_id

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_acct "" "$ta1,$ta2"
remove_cluster "$tc1,$tc2"
if {$access_err != 0} {
	send_user "\nWARNING: not authorized to perform this test\n"
	exit $exit_code
}

#
# Use sacctmgr to create a cluster
#
set sadd_pid [spawn $sacctmgr -i add cluster name=$tc1,$tc2 fairshare=$fs1 MaxCPUs=$mc1 MaxJobs=$mj1 MaxNodes=$mn1 MaxWall=$mw1]
expect {
	-re "privilege to preform 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
}

#
# Create tmp QOS
#
set qos_set 0
spawn $sacctmgr -i add qos $dqos1
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
#
set sadel_pid [spawn $sacctmgr -i add account cluster=$tc1 description="$ds1" \
fairshare=$fs1 MaxCPUs=$mc1 MaxJobs=$mj1 MaxSubmitJobs=$msj1 MaxCPUMins=$mcm1 \
MaxNodes=$mn1 MaxWall=$mw1 GrpCPUMins=$gcm1 GrpCPUs=$gc1 GrpCPURunMins=$gcrm1 \
GrpJobs=$gj1 GrpMemory=$gmem1 GrpSubmitJobs=$gsj1 GrpNodes=$gn1 GrpWall=$gw1 \
name=$ta1,$ta2 organization="$or1" DefaultQos=$dqos1 QosLevel=$dqos1]
expect {
	-re "Adding Account" {
		incr aamatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadel_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$aamatches != 1} {
	send_user "\nFAILURE:  sacctmgr had a problem adding account.
	got $aamatches\n"
	set exit_code 1
}

#
# Use sacctmgr to list the test associations
#
set as_list_pid [spawn $sacctmgr -p list associations cluster=$tc1,$tc2]
expect {
	-re "$tc1.$ta1" {
		incr almatches
		exp_continue
	}
	-re "$tc1.$ta2" {
		incr almatches
		exp_continue
	}
	-re "$tc2.$ta1" {
		set exit_code 1
		send_user "\nFAILURE: $nm1 account erroneously added to $tc2\n"
		exp_continue
	}
	-re "$tc2.$ta2" {
		exp_continue
		send_user "\nFAILURE: $nm2 account erroneously added to $tc2\n"
		set exit_code 1
	}
	timeout {
		send_user "\nFAILURE: sacctmgr list associations not responding\n"
		slow_kill $as_list_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$almatches != 2} {
	send_user "\nFAILURE: Added associations not found.\n"
	send_user "\n Need 2 matches and got $almatches instead.\n"
	set exit_code 1
}
# Check that the Association was set correctly
check_assoc_limit 2 account $ta1 $fs1 $gcm1 $gc1 $gcrm1 $gj1 $gn1 $gsj1 $gw1 $mc1 $mcm1 $mj1 $msj1 $mn1 $mw1 $gmem1 $dqos1

check_assoc_limit 2 account $ta2 $fs1 $gcm1 $gc1 $gcrm1 $gj1 $gn1 $gsj1 $gw1 $mc1 $mcm1 $mj1 $msj1 $mn1 $mw1 $gmem1 $dqos1

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,$ta2]
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,$tc2]
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 $dqos1]
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 association cluster=$tc1 ]
expect {
	-re "$tc1 *$ta1" {
		exp_continue
		send_user "\nFAILURE: test associations still exist.\n"
		set exit_code 1
	}
	-re "$tc1 *$ta2" {
		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
		} else {
	send_user "\nFAILURE\n"
		}

exit $exit_code
