#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test sacct filtering of records by account and job name.
############################################################################
# Copyright (C) SchedMD LLC.
# Copyright (C) 2008-2010 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Joseph Donaghy <donaghy1@llnl.gov> and
#            Nathan Yee <nyee32@schedmd.com>
# 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
source ./globals_accounting
source ./inc12.3.1
source ./inc12.3.2

set job_id_1    0
set job_id_2    0
set job_name_1  "JOB1"
set job_name_2  "JOB2"
set test_acct	"${test_name}-account.1"
set step_id     0
set user_name   [get_my_user_name]

if {[get_config_param "FrontendName"] ne "MISSING"} {
	skip "This test can't be run on a front end system"
}

#
# 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 {[get_admin_level] ne "Administrator"} {
	skip "This test can't be run without being an Accounting administrator. Use: sacctmgr mod user \$USER set admin=admin"
}

#
# Make sure we have enough nodes to test functionality
#
set nodes [get_nodes_by_request "-N3"]
if {[llength $nodes] != 3} {
	skip "This test requires to be able to run a job on as least 3 nodes"
}
lassign $nodes node1 node2 node3

proc cleanup {} {
	global sacctmgr test_acct user_name

	run_command "$sacctmgr -i delete user $user_name account=$test_acct"
	run_command "$sacctmgr -i delete account $test_acct"
}

#
# Add a test account
#
run_command -fail "$sacctmgr -i add account $test_acct"
run_command -fail "$sacctmgr -i create user name=$user_name account=$test_acct"

# Wait for assocs being available
wait_for_command -pollinterval 0.1 -fail "$scontrol show assoc_mgr accounts=$test_acct users=$user_name | $bin_grep -i 'Account=$test_acct UserName=$user_name'"

#
# Submit two jobs with different nodes and names
#
set job_id_1 [submit_job -fail "-N2 -w$node1,$node2 -t1 -A $test_acct -J $job_name_1 -o none --wrap '$bin_id'"]
set job_id_2 [submit_job -fail "-N2 -w$node2,$node3 -t1 -A $test_acct -J $job_name_2 -o none --wrap '$bin_id'"]

wait_for_job -fail $job_id_1 "DONE"
wait_for_job -fail $job_id_2 "DONE"

#
# Wait for accounting data to be propagated to slurmdbd
# NOTE: Sometimes the complete RPC reaches the DB before the start RPC.
#       We use sacct -S to ensure that the start time is stored in the DB.
#       Note that subsequent sacct -N queries will need start time stored to
#       work properly because they need that to search for active node indexes
#       along with the events table.
#
wait_for_command_match -fail "$sacct -A ${test_name}-account.1 --name=$job_name_1 -X -n -P -j $job_id_1 -S now-60 -o jobid,jobname" "$job_id_1.$job_name_1"
wait_for_command_match -fail "$sacct -A ${test_name}-account.1 --name=$job_name_2 -X -n -P -j $job_id_2 -S now-60 -o jobid,jobname" "$job_id_2.$job_name_2"

#
# Execute sub-tests based upon these jobs
#
inc12_3_1 $job_id_1 $job_id_2 $job_name_1 $job_name_2 $test_acct
inc12_3_2 $job_id_1 $job_id_2 $job_name_1 $job_name_2 $test_acct $node1 $node2 $node3
