#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr list accounts
############################################################################
# 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 almatches   0
set aclmatches  0
set damatches   0
set dcmatches   0
# Test Cluster #
set tc1 "${test_name}-cluster-1"
set tc2 "${test_name}-cluster-2"
# Cluster Limits
array set cluster {}
set cluster(Fairshare)     2500
set cluster(MaxCPUs)       1000000
set cluster(MaxJobs)       50
set cluster(MaxNodes)      300
set cluster(MaxWall)       01:00:00
# Test Accounts #
set ta1 "${test_name}-account.1"
set ta2 "${test_name}-account.2"
# Account Limits
array set acct {}
set acct(Cluster)       $tc1
set acct(Fairshare)     2500
set acct(MaxCPUs)       1000000
set acct(MaxJobs)       50
set acct(MaxNodes)      300
set acct(MaxWall)       01:00:00
set acct(DefaultQos)    "normal"
set acct(Organization)  "Account_Org_A1"
set acct(Description)   "Test_Account_A1"

set access_err  0
set timeout 60

if {[get_config_param "AccountingStorageType"] ne "accounting_storage/slurmdbd"} {
	skip "This test can't be run without a usable AccountStorageType"
}

#
# Verify if Administrator privileges
#
if {[get_admin_level] ne "Administrator"} {
	skip "This test can't be run without being an Accounting administrator.\nUse: sacctmgr mod user \$USER set admin=admin"
}

proc cleanup {} {
	global sacctmgr ta1 ta2 tc1 tc2

	run_command "$sacctmgr -i remove account $ta1,$ta2"
	run_command "$sacctmgr -i remove cluster $tc1,$tc2"
}

#
# Add test cluster
#
add_cluster "$tc1,$tc2" [array get cluster]

if {$access_err != 0} {
	skip "Not authorized to perform this test"
}

if {![check_acct_associations]} {
	fail "Our associations don't line up"
}

#
# Add a test account
#
add_acct "$ta1,$ta2" [array get acct]

#
# Use sacctmgr to list the test associations
#
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" {
		fail "Account 1 ($ta1) erroneously added to cluster 2 ($tc2)"
	}
	-re "$tc2.$ta2" {
		fail "Account 2 ($ta2) erroneously added to cluster 2 ($tc2)"
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}

if {$almatches != 2} {
	fail "Added associations not found (Need 2 matches and got $almatches instead)"
}

if {![check_acct_associations]} {
	fail "Our associations don't line up"
}

#
# Use sacctmgr to list the test accounts
#
spawn $sacctmgr -p -n list account
expect {
	-nocase -re "$ta1.$acct(Description).$acct(Organization)" {
		incr aclmatches
		exp_continue
	}
	-nocase -re "$ta2.$acct(Description).$acct(Organization)" {
		incr aclmatches
		exp_continue
	}
	timeout {
		fail "sacctmgr list associations not responding"
	}
	eof {
		wait
	}
}

if {$aclmatches != 2} {
	fail "Added account incorrect (Need 2 matches and got $aclmatches instead)"
}
