#!/usr/bin/env 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 <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

set test_id     "21.27"
set tc1		"test$test_id-cluster-1"
set tc2		"test$test_id-cluster-2"
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 tu3		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
#
spawn $sacctmgr -i delete user $tu1,$tu2,$tu3
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete user not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i delete account $ta1,$ta2,$ta3
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete account not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i delete cluster $tc1,$tc2
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete cluster not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}


#
# Build test associations
#
spawn $sacctmgr -i add cluster $tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add cluster not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add account $ta1 cluster=$tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add account $ta1 not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add account $ta2 parent=$ta1 cluster=$tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add account $ta2 not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add user $tu1 account=$ta1 cluster=$tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add user $tu1 not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add user $tu2 account=$ta1 cluster=$tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add user $tu2 not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add user $tu3 account=$ta2 cluster=$tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add user $tu3 not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i delete cluster $tc1
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete cluster $tc1  not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add cluster $tc2
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add cluster $tc2  not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr -i add account $ta3 cluster=$tc2
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add account $ta3  not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

spawn $sacctmgr show problem
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr show problem  not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}


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

	set my_pid [spawn $sacctmgr -p show problem]
	expect {
		-re "$ta1..Account has no Associations" {
			incr matches
			exp_continue
		}
		-re "$ta2..Account has no Associations" {
			incr matches
			exp_continue
		}
		-re "$tc2.$ta3..Account has no users" {
			incr matches
			exp_continue
		}
		-re "$tu3.User has no Associations" {
			incr matches
			exp_continue
		}
		-re "$tu1.User does not have a uid" {
			incr matches
			exp_continue
		}
		-re "$tu2.User does not have a uid" {
			incr matches
			exp_continue
		}

		timeout {
			send_user "\nFAILURE: sacctmgr show 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
#
spawn $sacctmgr -i delete user $tu1,$tu2,$tu3
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete users not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
spawn $sacctmgr -i delete account $ta1,$ta2,$ta3
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete accounts not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
spawn $sacctmgr -i delete cluster $tc1,$tc2
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr delete clusters not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

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