#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr add multiple users
#
#
# 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_accounting

set test_id     "21.16"
set exit_code   0
set tc1		slurm_test-cluster-1
set tc2		slurm_test-cluster-2
set tc3		slurm_test-cluster-3
set dqos1       "test$test_id\_qos"
set ta1		slurm_test-account.1
set ta2		slurm_test-account.2
set ta3		slurm_test-account.3
set tu1         slurm_test-user.1
set tu2         slurm_test-user.2
set tu3         slurm_test-user.3
set tu4         slurm_test-user.4
set tu5         slurm_test-user.5
set access_err  0

# Cluster
array set clus_req {}

# Account
array set acct_req {}
set acct_req(cluster) $tc1,$tc2,$tc3

# Users
array set user_req1 {
	adminlevel      Operator
	defaultaccount  slurm_test-account.2
	fairshare       2500
	maxcpumins      1000000
	maxjobs         50
	maxnodes        300
	maxwall         01:00:00
}
set user_req1(cluster) $tc1,$tc2,$tc3
set user_req1(account) $ta1,$ta2,$ta3

array set user_req2 {
	adminlevel      Operator
	defaultaccount  slurm_test-account.2
	fairshare       2500
	grpcpumin       100
	grpcpurunmin    200
	grpcpus         1000
	grpjobs         300
	grpmemory       1000
	grpnodes        10000
	grpsubmitjobs   3000
	grpwall         00:45:00
	maxcpumins      1000000
	maxjobs         50
	maxnodes        300
	maxwall         01:00:00
}
set user_req2(cluster) $tc1
set user_req2(account) $ta1

print_header $test_id

set timeout 60

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

#make sure we have a clean system and permission to do this work
remove_user "" "" "$tu1,$tu2,$tu3"
remove_acct "" "$ta1,$ta2,$ta3"
remove_cluster "$tc1,$tc2,$tc3"
remove_qos "$dqos1"
if {$access_err != 0} {
	send_user "\nWARNING: not authorized to perform this test\n"
	exit $exit_code
}

#add cluster
incr exit_code [add_cluster "$tc1,$tc2,$tc3" [array get clus_req]]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3,$tu4,$tu5"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	exit $exit_code
}

#add accounts
incr exit_code [add_acct "$ta1,$ta2,$ta3" [array get acct_req]]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3,$tu4,$tu5"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	exit $exit_code
}

#add users
# account adminlevel cluster defaultaccount fs maxcpu maxjob maxnodes maxwall name
incr exit_code [add_user "$tu1,$tu2,$tu3" [array get user_req1]]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3,$tu4,$tu5"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	exit $exit_code
}

#
# Use sacctmgr to list the test user additions
#
set matches 0
set my_pid [spawn $sacctmgr -n -p list user format="User,DefaultA,Admin,clus,acc,fair,maxcpum,maxj,maxn,maxw"  names=$tu1,$tu2,$tu3 withassoc]
expect {
	-re "There was a problem" {
		send_user "FAILURE: there was a problem with the sacctmgr command\n"
	    	incr exit_code 1
	}
	-re "(($tu1|$tu2|$tu3).$ta2.$user_req1(adminlevel).($tc1|$tc2|$tc3).($ta1|$ta2|$ta3).$user_req2(fairshare).$user_req1(maxcpumins).$user_req1(maxjobs).$user_req1(maxnodes).$user_req1(maxwall).)" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr list associations not responding\n"
		slow_kill $my_pid
		incr exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 27} {
	send_user "\nFAILURE:  Account addition 27 incorrect with only $matches.\n"
	incr exit_code 1
}

# This is the end below here

incr exit_code [remove_user "" "" "$tu1,$tu2,$tu3"]
incr exit_code [remove_acct "" "$ta1,$ta2,$ta3"]
incr exit_code [remove_cluster "$tc1,$tc2,$tc3"]

if {$exit_code == 0} {
	print_success $test_id
} else {
	send_user "\nFAILURE\n"
}
exit $exit_code

#
# From here we test all the limits associated with multiple users
#

incr exit_code [add_qos "$dqos1"]
if { $exit_code } {
	remove_qos "$dqos1"
	exit $exit_code
}

#
# Add user with all limits set
#
incr exit_code [add_user "$tu4,$tu5" [array get user_req2]]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3,$tu4,$tu5"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	remove_qos "$dqos1"
	exit $exit_code
}

# Check Limits of both users
check_assoc_limit 3 user $tu4 $user_req2(fairshare) $user_req2(grpcpumin) $user_req2(grpcpus) $user_req2(grpcpurunmin) $user_req2(grpjob) $user_req2(grpnodes) $user_req2(grpsubmitjobs) $user_req(grpwall)

check_assoc_limit 3 user $tu5 $user_req2(fairshare) $user_req2(grpcpumin) $user_req2(grpcpus) $user_req2(grpcpurunmin) $user_req2(grpjob) $user_req2(grpnodes) $user_req2(grpsubmitjobs) $user_req(grpwall)
