#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM sacct functionality
#          sacct options b, g, j, l, n, p, u, v.
#
# 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 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     "12.4"
set exit_code   0
set test_acct   "test$test_id\_acct"
set timeout 60
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.\n"
	send_user "Use: sacctmgr mod user \$USER set admin=admin.\n"
	exit 0
}

set using_slurmdbd [test_using_slurmdbd]

set cluster [get_cluster_name]
#
# Identify the user and his current default account
#
set acct_name ""
set user_name ""
set user_gid ""
spawn $bin_id -u -n
expect {
	 -re "($alpha_numeric_under)" {
		set user_name $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}

spawn $bin_id -g
expect {
	 -re "($alpha_numeric_under)" {
		set user_gid $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}

set s_pid [spawn $sacctmgr show user $user_name]
expect {
	-re "$user_name *($alpha_numeric_under)" {
		set acct_name $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "FAILURE: sacctmgr add not responding\n"
		slow_kill $s_pid
		exit 1
	}
	eof {
		wait
	}
}

#
# Use sacctmgr to add an account
#
set aamatches 0
set sadd_pid [spawn $sacctmgr -i add account $test_acct cluster=$cluster]
expect {
	-re "Adding Account" {
		incr aamatches
		exp_continue
	}
	-re "Nothing new added" {
		send_user "\nWARNING: vestigial account $test_acct found\n"
		incr aamatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadd_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$aamatches != 1} {
	send_user "\nFAILURE:  sacctmgr had a problem adding account.\n"
	exit 1
}

#
# Add self to this new account
#
set sadd_pid [spawn $sacctmgr -i create user name=$user_name account=$test_acct cluster=$cluster]
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadd_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Spawn a job via srun using this account
#
set job_id 0
spawn $srun -N1 -v --account=$test_acct $bin_id
expect {
	-re "launching ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: did not get srun job_id3\n"
	set exit_code 1
} else {
	set matches 0
	spawn $scontrol show job $job_id
	expect {
		 -re "Account=$test_acct" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches != 1} {
		send_user "\nFAILURE: srun failed to use specified account\n"
		set exit_code 1
	}
}

if { $using_slurmdbd } {
	sleep 12
}

################################################################
#
# Proc: sacct_job
#
# Purpose:  Pass sacct options and test
#
# Returns: Number of matches.
#
# Input: Switch options not requiring arguments
#
################################################################

proc sacct_job { soption job_id} {
	global sacct using_slurmdbd
	set debug       0
	set exit_code   0
	set matches     0
	set not_support 0
	set total_opts "$sacct -$soption -X -p -j $job_id"

	send_user "\n"

	eval spawn $total_opts

	if { $soption == "-brief" || $soption == "b" } {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "JobID.State.ExitCode" {
				if {$debug} {send_user "\nmatch1\n"}
				incr matches
				exp_continue
			}
			-re "$job_id" {
				if {$debug} {send_user "\nmatch2\n"}
				incr matches
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}

	if { $soption == "-long" || $soption == "l" } {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "JobID.JobName.Partition.MaxVMSize" {
				if {$debug} {send_user "\nmatch3\n"}
				incr matches
				exp_continue
			}
			-re "MaxVMSizeNode.MaxVMSizeTask.AveVMSize.MaxRSS" {
				if {$debug} {send_user "\nmatch4\n"}
				incr matches
				exp_continue
			}
			-re "MaxRSSNode.MaxRSSTask.AveRSS.MaxPages" {
				if {$debug} {send_user "\nmatch5\n"}
				incr matches
				exp_continue
			}
			-re "MaxPagesNode.MaxPagesTask.AvePages.MinCPU" {
				if {$debug} {send_user "\nmatch6\n"}
				incr matches
				exp_continue
			}
			-re "MinCPUNode.MinCPUTask.AveCPU.NTasks" {
				if {$debug} {send_user "\nmatch7\n"}
				incr matches
				exp_continue
			}
			-re "AllocCPUS.Elapsed.State.ExitCode" {
				if {$debug} {send_user "\nmatch8\n"}
				incr matches
				exp_continue
			}
			-re "$job_id" {
				if {$debug} {send_user "\nmatch9\n"}
				incr matches
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}
		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}

	if { $soption == "-noheader" || $soption == "n" } {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "AllocCPUS|Account|AssocID|AveCPU|AvePages|AveRSS|AveVSize|BlockID	\
				|Cluster|CPUTime|CPUTimeRAW|Elapsed	\
				|Eligible|End|ExitCode|GID	\
				|Group|JobID|JobName|NodeList	\
				|MaxPages|MaxPagesNode|MaxPagesTask|MaxRSS	|
				|MaxRSSNode|MaxRSSTask|MaxVSize|MaxVSizeNode	|
				|MaxVSizeTask|MinCPU|MinCPUNode|MinCPUTask	|
				|NCPUS|NNodes|NTasks|Priority	|
				|Partition|QOS|QOSRAW|ReqCPUS	|
				|Reserved|ResvCPU|ResvCPURAW|Start	|
				|State|Submit|Suspended|SystemCPU	|
				|Timelimit|TotalCPU|UID|User	|
				|UserCPU|WCKey|WCKeyID" {
				if {$debug} {send_user "\nmatch10\n"}
				incr matches
				exp_continue
			}
			-re "$job_id" {
				if {$debug} {send_user "\nmatch11\n"}
				incr matches
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}

	if { $soption == "-parsable" || $soption == "p" } {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "JobID\\|JobName\\|Partition\\|Account\\|AllocCPUS\\|State\\|ExitCode\\|" {
				if {$debug} {send_user "\nmatch12\n"}
				incr matches
				exp_continue
			}
			-re "$job_id\\|" {
				if {$debug} {send_user "\nmatch13\n"}
				incr matches
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}

	if { $soption == "-parsable2" || $soption == "P" } {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "JobID\\|JobName\\|Partition\\|Account\\|AllocCPUS\\|State\\|ExitCode *" {
				if {$debug} {send_user "\nmatch14\n"}
				incr matches
				exp_continue
			}
			-re "$job_id\\|" {
				if {$debug} {send_user "\nmatch15\n"}
				incr matches
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}

	if { $soption == "-verbose" || $soption == "v" } {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "sacct: Accounting storage SLURMDBD plugin loaded " {
				if {$debug} {send_user "\nmatch16\n"}
				incr matches
				exp_continue
			}
			-re "JobID.JobName.Partition" {
				if {$debug} {send_user "\nmatch17\n"}
				incr matches
				exp_continue
			}
			-re "Account.AllocCPUS.State.ExitCode" {
				if {$debug} {send_user "\nmatch18\n"}
				incr matches
				exp_continue
			}
			-re "$job_id" {
				if {$debug} {send_user "\nmatch19\n"}
				incr matches
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}


}

################################################################
#
# Proc: sacct_vargs
#
# Purpose:  Pass sacct options with arguments and test
#
# Returns: Number of matches.
#
# Input: Switch options with argument
#
################################################################

proc sacct_vargs { soption vargs job_id} {
	global sacct
	set debug       0
	set exit_code   0
	set matches     0
	set not_support 0
	set total_opts "$sacct -$soption $vargs -X -p -j $job_id"

	send_user "\n"

	eval spawn $total_opts

	if { $soption == "g" || $soption == "-gid" || $soption == "-group" || $soption == "u" || $soption == "-uid" || $soption == "-user"} {
		expect {
			-re "SLURM accounting storage is disabled" {
				set not_support 1
				exp_continue
			}
			-re "JobID.JobName.Partition" {
				if {$debug} {send_user "\nmatch20\n"}
				incr matches
				exp_continue
			}
			-re "Account.AllocCPUS.State.ExitCode" {
				if {$debug} {send_user "\nmatch21\n"}
				incr matches
				exp_continue
			}
			-re "$job_id" {
				incr matches
				if {$debug} {send_user "\nmatch22\n"}
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sacct not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if {$not_support != 0} {
			send_user "\nWARNING: can not test without accounting enabled\n"
			exit 0
		}
		return $matches
	}
}
################################################################

set using_slurmdbd [test_using_slurmdbd]

set cluster [get_cluster_name]

#
# Use sacctmgr to add an account
#
set aamatches 0
set sadd_pid [spawn $sacctmgr -i add account $test_acct cluster=$cluster]
expect {
	-re "Adding Account" {
		incr aamatches
		exp_continue
	}
	-re "Nothing new added" {
		send_user "\nWARNING: vestigial account $test_acct found\n"
		incr aamatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadd_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$aamatches != 1} {
	send_user "\nFAILURE:  sacctmgr had a problem adding account.\n"
	exit 1
}

#
# Add self to this new account
#
set sadd_pid [spawn $sacctmgr -i create user name=$user_name account=$test_acct cluster=$cluster]
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadd_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Spawn a job via srun using this account
#
set job_id 0
spawn $srun -N1 -v --account=$test_acct $bin_id
expect {
	-re "launching ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: did not get srun job_id3\n"
	set exit_code 1
} else {
	set matches 0
	spawn $scontrol show job $job_id
	expect {
		 -re "Account=$test_acct" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches != 1} {
		send_user "\nFAILURE: srun failed to use specified account\n"
		set exit_code 1
	}

	if { $using_slurmdbd } {
		sleep 12
	}

	set matches [sacct_job b $job_id]
	if {$matches != 2} {
		send_user "\nFAILURE: sacct -b failed ($matches != 2)\n"
		set exit_code 1
	}

	set matches [sacct_job -brief $job_id]
	if {$matches != 2} {
		send_user "\nFAILURE: sacct --brief failed ($matches != 2)\n"
		set exit_code 1
	}

	set matches [sacct_vargs g $user_gid $job_id]
	if {$matches != 3} {
		send_user "\nFAILURE: sacct -g failed ($matches != 3)\n"
		set exit_code 1
	}

	set matches [sacct_vargs -gid $user_gid $job_id]
	if {$matches != 3} {
		send_user "\nFAILURE: sacct --gid failed ($matches != 3)\n"
		set exit_code 1
	}

	set matches [sacct_vargs -group $user_gid $job_id]
	if {$matches != 3} {
		send_user "\nFAILURE: sacct --group failed ($matches != 3)\n"
		set exit_code 1
	}

	set matches [sacct_job l $job_id]
	if {$matches != 7} {
		send_user "\nFAILURE: sacct --allusers failed ($matches != 7)\n"
		set exit_code 1
	}

	set matches [sacct_job -long $job_id]
	if {$matches != 7} {
		send_user "\nFAILURE: sacct -l failed ($matches != 7)\n"
		set exit_code 1
	}

	set matches [sacct_job n $job_id]
	if {$matches != 1} {
		send_user "\nFAILURE: sacct -n failed ($matches != 1)\n"
		set exit_code 1
	}

	set matches [sacct_job -noheader $job_id]
	if {$matches != 1} {
		send_user "\nFAILURE: sacct -n failed ($matches != 1)\n"
		set exit_code 1
	}

	set matches [sacct_job p $job_id]
	if {$matches != 2} {
		send_user "\nFAILURE: sacct -p failed ($matches != 2)\n"
		set exit_code 1
	}

	set matches [sacct_job -parsable $job_id]
	if {$matches != 2} {
		send_user "\nFAILURE: sacct --parsable failed ($matches != 2)\n"
		set exit_code 1
	}

	set matches [sacct_job P $job_id]
	if {$matches != 2} {
		send_user "\nFAILURE: sacct -P failed ($matches != 2)\n"
		set exit_code 1
	}

	set matches [sacct_job -parsable2 $job_id]
	if {$matches != 2} {
		send_user "\nFAILURE: sacct --parsable2 failed ($matches != 2)\n"
		set exit_code 1
	}

	set matches [sacct_vargs u $user_name $job_id]
	if {$matches != 3} {
		send_user "\nFAILURE: sacct -g failed ($matches != 3)\n"
		set exit_code 1
	}

	set matches [sacct_vargs -uid $user_name $job_id]
	if {$matches != 3} {
		send_user "\nFAILURE: sacct --gid failed ($matches != 3)\n"
		set exit_code 1
	}

	set matches [sacct_vargs -user $user_name $job_id]
	if {$matches != 3} {
		send_user "\nFAILURE: sacct --group failed ($matches != 3)\n"
		set exit_code 1
	}

	set matches [sacct_job v $job_id]
	if {$matches != 4} {
		send_user "\nFAILURE: sacct -v failed ($matches != 4)\n"
		set exit_code 1
	}

	set matches [sacct_job -verbose $job_id]
	if {$matches != 4} {
		send_user "\nFAILURE: sacct --verbose failed ($matches != 4)\n"
		set exit_code 1
	}
}

#
# Use sacctmgr to delete the test account
#
set damatches 0
set sadel_pid [spawn $sacctmgr -i delete account $test_acct]
expect {
	-re "Deleting account" {
		incr damatches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr delete not responding\n"
		slow_kill $sadel_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$damatches != 1} {
	send_user "\nFAILURE: sacctmgr had a problem deleting account\n"
	set exit_code 1
}

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