#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr show problems
#
#
# 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) 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://computing.llnl.gov/linux/slurm/>.
# 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

set test_id     "21.27"

set sho		show
set add		create
set del		delete
set pro		problem
set par		parent
set clu		Cluster
set cl1		pc1
set cl2		pc2
set acc		Account
set ac1		pa1
set ac2		pa2
set ac3		pa3
set usr		user
set us1		usr1
set us2		usr2
set us3		mysql

set access_err  0
set timeout     120

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

#
# Delete 
#
exec $sacctmgr -i $del $usr $us1,$us2,$us3
exec $sacctmgr -i $del $acc $ac1,$ac2,$ac3
exec $sacctmgr -i $del $clu $cl1,$cl2

#
# Build test associations
#
exec $sacctmgr -i $add $clu $cl1
exec $sacctmgr -i $add $acc $ac1 $clu=$cl1
exec $sacctmgr -i $add $acc $ac2 $par=$ac1 $clu=$cl1
exec $sacctmgr -i $add $usr $us1 $acc=$ac1 $clu=$cl1
exec $sacctmgr -i $add $usr $us2 $acc=$ac1 $clu=$cl1
exec $sacctmgr -i $add $usr $us3 $acc=$ac2 $clu=$cl1
exec $sacctmgr -i $del $clu $cl1
exec $sacctmgr -i $add $clu $cl2
exec $sacctmgr -i $add $acc $ac3 $clu=$cl2
exec $sacctmgr $sho $pro

#
# Test for problems
#
	set exit_code 0
	set matches 0
	set nothing 0

	set my_pid [spawn $sacctmgr -p $sho $pro]
	expect {
		-re "$ac1..Account has no Associations" {
			incr matches
			exp_continue
		}
		-re "$ac2..Account has no Associations" {
			incr matches
			exp_continue
		}
		-re "$cl2.$ac3..Account has no users" {
			incr matches
			exp_continue
		}
		-re "$us3.User has no Associations" {
			incr matches
			exp_continue
		}
		-re "$us1.User does not have a uid" {
			incr matches
			exp_continue
		}
		-re "$us2.User does not have a uid" {
			incr matches
			exp_continue
		}

		timeout {
			send_user "\nFAILURE: sacctmgr delete not responding\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 6} {
		send_user "\nFAILURE: sacctmgr found $matches of 6 problems\n"
		incr exit_code 1
	}


#
# Delete 
#
exec $sacctmgr -i $del $usr $us1,$us2,$us3
exec $sacctmgr -i $del $acc $ac1,$ac2,$ac3
exec $sacctmgr -i $del $clu $cl1,$cl2

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

