#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr delete N (abort) test
#
#
# 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://www.schedmd.com/slurmdocs/>.
# 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.28"
set exit_code   0
set tc1		slurm_test-cluster-1
set tc2		slurm_test-cluster-2
set tc3		slurm_test-cluster-3
set fs1		2500
set fs2		1700
set fs3		1
set mc1		1000000
set mc2		700000
set mc3		1
set mj1		50
set mj2		70
set mj3		1
set mn1		300
set mn2		700
set mn3		1
set mw1		01:00:00
set mw2		00:07:00
set mw3		00:01:00
set ta1		slurm_test-account.1
set ta2		slurm_test-account.2
set ta3		slurm_test-account.3
set ds1		testacct1
set ds2		testacct2
set or1		acctorg1
set or2		acctorg2
set qs1		normal
set tu1         slurm_test-user.1
set tu2         slurm_test-user.2
set tu3         slurm_test-user.3
set aln		None
set ala		Administrator
set alo		Operator
set fs		fairshare
set mc		maxcpu
set mj		maxjob
set mn		maxnode
set mw		maxwall
set access_err  0
set lista       "$ta1 $ta2 $ta3"
set listc       "$tc1 $tc2 $tc3"
set listu       "$tu1 $tu2 $tu3"

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"
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" "" "" "" "" "" "" "" "" "" "" "" ""]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	exit $exit_code
}

#add accounts
incr exit_code [add_acct "$tc1,$tc2,$tc3" "" "$ta1,$ta2,$ta3" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3"
	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 "$tc1,$tc2,$tc3" "$ta1,$ta2,$ta3" $tu1,$tu2,$tu3 "" "$alo" "$ta2" "" "" "" "$fs1" "" "" "" "" "" "" "$mc1" "" "$mj1" "$mn1" "" "$mw1" ]
if { $exit_code } {
	remove_user "" "" "$tu1,$tu2,$tu3"
	remove_acct "" "$ta1,$ta2,$ta3"
	remove_cluster "$tc1,$tc2,$tc3"
	exit $exit_code
}

################################################################
#
# Proc: list_test_users
#
# Purpose: Use sacctmgr to list the test user additions
#
# Returns: Number representing the number of entries found.
#
# Input: None
#
################################################################
proc list_test_users { } {
	global sacctmgr
	global tu1 tu2 tu3 ta1 ta2 ta3 tc1 tc2 tc3 fs1 mc1 mj1 mn1 mw1 alo
	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"
		}
		-re "(($tu1|$tu2|$tu3).$ta2.$alo.($tc1|$tc2|$tc3).($ta1|$ta2|$ta3).$fs1.$mc1.$mj1.$mn1.$mw1.)" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr list associations not responding\n"
			slow_kill $my_pid
		}
		eof {
			wait
		}
	}

	return $matches
}
################################################################
#
# Proc: list_test_object
#
# Purpose: Use sacctmgr to list the test object deletions
#
# Returns: Number representing the number of entries found.
#
# Input: None
#
################################################################
proc list_test_object { to1 to2 to3 tname} {
	global sacctmgr
	set tmatches 0
	set my_pid [spawn $sacctmgr -n -p list $tname $to1,$to2,$to3]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command\n"
		}
		-re "($to1|$to2|$to3)" {
			incr tmatches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr list associations not responding\n"
			slow_kill $my_pid
		}
		eof {
			wait
		}
	}

	return $tmatches
}
################################################################
#
# Begin here with abort delete test
#
foreach atest $lista {
	set my_pid [spawn $sacctmgr del account $atest]
	expect {
		"(N/y):" {
			send "N"
			exp_continue
			timeout {
				send_user "\nFAILURE: sacctmgr list account timing out.\n"
				slow_kill $my_pid
			}
		}
	}
}


set count [list_test_users]
if { $count != 27 } {
	send_user "\nFAILURE 1: This test$test_id concludes that sacctmgr did not abort delete of accounts $count.\n"
	exit 1
}

#
foreach ctest $listc {
	set my_pid [spawn $sacctmgr del cluster $ctest]
	expect {
		"(N/y):" {
			send "N"
			exp_continue
			timeout {
				send_user "\nFAILURE: sacctmgr list cluster timing out.\n"
				slow_kill $my_pid
			}
		}
	}
}


set count [list_test_users]
if { $count != 27 } {
	send_user "\nFAILURE 2: This test$test_id concludes that sacctmgr did not abort delete of $clu $count\n"
	exit 1
}

#
foreach utest $listu {
	set my_pid [spawn $sacctmgr del user $atest]
	expect {
		"(N/y):" {
			send "N"
			exp_continue
			timeout {
				send_user "\nFAILURE: sacctmgr list $usr timing out.\n"
				slow_kill $my_pid
			}
		}
	}
}

set count [list_test_users]
if { $count != 27 } {
	send_user "\nFAILURE 3: This test$test_id concludes that sacctmgr did not abort delete of users $count.\n"
	exit 1
}

################################################################
#
# Begin here with delete test
#
#
foreach udtest $listu {
	set my_pid [spawn $sacctmgr del user $udtest]
	expect {
		"(N/y):" {
			send "y"
			exp_continue
			timeout {
				send_user "\nFAILURE: sacctmgr list $usr timing out.\n"
				slow_kill $my_pid
			}
		}
	}
}

if { [list_test_object $tu1 $tu2 $tu3 user] != 0 } {
	send_user "\nFAILURE 4: This test$test_id concludes that sacctmgr did not delete users.\n"
	exit 1
}

#

foreach adtest $lista {
	set my_pid [spawn $sacctmgr del account $adtest]
	expect {
		"(N/y):" {
			send "y"
			exp_continue
			timeout {
				send_user "\nFAILURE: sacctmgr list account timing out.\n"
				slow_kill $my_pid
			}
		}
	}
}


if { [list_test_object $ta1 $ta2 $ta3 account] != 0 } {
	send_user "\nFAILURE 5: This test$test_id concludes that sacctmgr did not delete accounts.\n"
	exit 1
}

#
foreach cdtest $listc {
	set my_pid [spawn $sacctmgr del cluster $cdtest]
	expect {
		"(N/y):" {
			send "y"
			exp_continue
			timeout {
				send_user "\nFAILURE: sacctmgr list cluster timing out.\n"
				slow_kill $my_pid
			}
		}
	}
}


if { [list_test_object $tc1 $tc2 $tc3 cluster] != 0 } {
	send_user "\nFAILURE 6: This test$test_id concludes that sacctmgr did not delete $clu\n"
	exit 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
