#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr add user
############################################################################
# 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_accounting

set exit_code   0
set tc1		"test$test_id-cluster-1"
set tc2		"test$test_id-cluster-2"
set tc3		"test$test_id-cluster-3"
set dqos1       "test$test_id\_qos"
set ta1		"test$test_id-account.1"
set ta2		"test$test_id-account.2"
set ta3		"test$test_id-account.3"
set tu1         "test$test_id-user.1"
set tu2         "test$test_id-user.2"
set access_err  0

# Cluster
array set clus_req {}

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

# User1
array set user_req1 {

	adminlevel      Operator
	fairshare       2500
	maxcpumin       1000000
	maxjob          50
	maxnode         300
	maxwall         01:00:00

}
set user_req1(cluster) $tc1,$tc2,$tc3
set user_req1(account) $ta1,$ta2,$ta3
set user_req1(defaultaccount) $ta2

array set user_req2 {

	adminlevel    Operator
	fairshare     2500
	grpcpumins    100
	grpcpurunmins 200
	grpcpus       1000
	grpjob        300
	grpmemory     1000
	grpnode       10000
	grpsubmitjob  3000
	grpwall       00:45:00
	maxcpumin     1000000
	maxcpus       1000
	maxjob        50
	maxnode       300
	maxsubmitjobs 500
	maxwall       01:00:00
}
set user_req2(cluster) $tc1
set user_req2(account) $ta1
set user_req2(defaultqos) $dqos1

set timeout 60

#
# Check accounting config and bail if not found.
#
if {[get_config_param "AccountingStorageType"] ne "accounting_storage/slurmdbd"} {
	skip "This test can't be run without a usable AccountStorageType"
}

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

#make sure we have a clean system and permission to do this work
remove_user "" "" "$tu1,$tu2"
remove_acct "" "$ta1,$ta2,$ta3"
remove_cluster "$tc1,$tc2,$tc3"
remove_qos "$dqos1"
if {$access_err != 0} {
	skip "Not authorized to perform this test"
}

#add cluster
incr exit_code [add_cluster "$tc1,$tc2,$tc3" ""]
if { $exit_code } {
	remove_user "" "" "$tu1"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	fail "Unable to add cluster ($tc1,$tc2,$tc3)"
}

#add accounts
incr exit_code [add_acct "$ta1,$ta2,$ta3" [array get acct_req]]
if { $exit_code } {
	remove_user "" "" "$tu1"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	fail "Unable to add account ($ta1,$ta2,$ta3)"
}

#add users
# account adminlevel cluster defaultaccount fs maxcpu maxjob maxnodes maxwall name
incr exit_code [add_user $tu1 [array get user_req1]]
if { $exit_code } {
	remove_user "" "" "$tu1"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	fail "Unable to add user ($tu1)"
}

#
# 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 withassoc]
expect {
	-re "There was a problem" {
	        log_error "There was a problem with the sacctmgr command"
	    	incr exit_code 1
	}
	-re "($tu1.$ta2.$user_req1(adminlevel).($tc1|$tc2|$tc3).($ta1|$ta2|$ta3).$user_req1(fairshare).$user_req1(maxcpumin).$user_req1(maxjob).$user_req1(maxnode).$user_req1(maxwall).)" {
		incr matches
		exp_continue
	}
	timeout {
		log_error "sacctmgr list associations not responding"
		slow_kill $my_pid
		incr exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 9} {
	log_error "Account addition 9 incorrect with only $matches"
	incr exit_code 1
}

#
# From here we test all the limits associated with the user
#

incr exit_code [add_qos "$dqos1" ""]
if { $exit_code } {
	remove_qos "$dqos1"
	fail "Unable to add qos ($dqos1)"
}


#
# Add user with all limits set
#
incr exit_code [add_user $tu2 [array get user_req2]]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	remove_qos "$dqos1"
	fail "Unable to add user ($tu2)"
}

if {![check_assoc_limit 3 user $tu2 [array get user_req2]]} {
	log_error "The association limits for user $tu2 are incorrect"
	set exit_code 1
}

# This is the end below here

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

if {$exit_code != 0} {
	fail "Test failed due to previous errors"
}
