#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr verify wckey
############################################################################
# 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 <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

set file_in     "$test_dir/input"
set ta          "${test_name}-account.1"
set wk          "${test_name}-wckey"
set wk2         "${test_name}-wckey2"
set wk3         "${test_name}-wckey3"
set timeout     60
set reconfigured false
set config_path [get_conf_path]
set config_file $config_path/slurm.conf
set cn [get_config_param "ClusterName"]
set user_name [get_my_user_name]

proc cleanup {} {
	global reconfigured config_file
	global sacctmgr user_name ta cn wk wk2 wk3

	if {$reconfigured} {
		restore_conf $config_file
		reconfigure
	}

	run_command -none "$sacctmgr -i delete user name=$user_name account=$ta cluster=$cn"
	run_command -none "$sacctmgr -i delete account $ta"

	run_command -none "$sacctmgr -i delete user $user_name cluster=$cn wckey=$wk,$wk2,$wk3"
}

#
# 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."
}

set accounting_storage_type [get_config_param "AccountingStorageType"]

cleanup

#
# Identify the user and his current default account
#
set acct_name ""
set output [run_command_output -fail "$sacctmgr -nP show user format=DefaultAccount $user_name"]
if {![regexp "$re_word_str" $output acct_name]} {
	fail "Not able to found DefaultAccount"
}

#
# Use sacctmgr to add an account
#
run_command -fail "$sacctmgr -i add account $ta cluster=$cn"

#
# Add self to this new account and wckey
#
run_command -fail "$sacctmgr -i create user name=$user_name account=$ta cluster=$cn wckeys=$wk"

#
# Enable TrackWCKey
#
if {[get_config_param "TrackWCKey"] ne "yes"} {
	log_info "Enabling TrackWCKey..."

	save_conf $config_file

	exec $bin_sed -i /^\[\t\s\]*TrackWCKey\[\t\s\]*=/Id $config_file
	exec $bin_echo TrackWCKey=yes >> $config_file
	reconfigure -fail
	set reconfigured true
}

#
# Spawn a job via salloc using this wckey
#
set job_id     0
set matches    0
set timeout    $max_job_delay
spawn $salloc -N1 --account=$ta --wckey=$wk
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
		send "$scontrol show job $job_id\r"
		send "exit\r"
		exp_continue
	}
	-re "WCKey=$wk" {
		incr matches
		exp_continue
	}
	timeout {
		fail "salloc not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "salloc failed to initiate job"
}
if {$matches != 1} {
	fail "salloc failed to use desired wckey"
}

if {$accounting_storage_type eq "accounting_storage/slurmdbd"} {
	sleep 5
}

set output [run_command_output -fail "$sacct -p -j $job_id --format=wckey"]
subtest {[regexp "$wk" $output]} "Verify WCKey in sacct"

#
# Spawn a job via sbatch using this wckey
#
make_bash_script $file_in "$bin_id"
set job_id [submit_job -fail "-N1 --account=$ta --wckey=$wk --output=none $file_in"]

set output [run_command_output -fail "$scontrol show job $job_id"]
subtest {[regexp "WCKey=$wk" $output]} "Verify WCKey in scontrol"

if {$accounting_storage_type eq "accounting_storage/slurmdbd"} {
	sleep 5
}

set output [run_command_output -fail "$sacct -p -j $job_id --format=wckey"]
subtest {[regexp "$wk" $output]} "Verify WCKey in sacct"

#
# Spawn a job via srun using this wckey
#
set output [run_command_output -fail "$srun -N1 -v --account=$ta --wckey=$wk $bin_id"]
if {![regexp "launching StepId=($number)\\.$re_word_str" $output - job_id]} {
	fail "srun failed"
}

set output [run_command_output -fail "$scontrol show job $job_id"]
subtest {[regexp "WCKey=$wk" $output]} "Verify WCKey in scontrol"

if {$accounting_storage_type eq "accounting_storage/slurmdbd"} {
	sleep 5
}

set output [run_command_output -fail "$sacct -p -j $job_id --format=wckey"]
subtest {[regexp "$wk" $output]} "Verify WCKey in sacct"

#
# Add more test WCKeys the user
#
# $wk should be there before, so only $wk2 and $wk3 was added
set output [run_command_output -fail "$sacctmgr -i add user $user_name cluster=$cn wckey=$wk,$wk2,$wk3"]
if {[regexp -all "W =" $output] != 2} {
	fail "sacctmgr had problems adding two WCKey at once"
}

#
# Verify that we have 3 WCKeys in DB
#
set output [run_command_output -fail "$sacctmgr list -p wckey user=$user_name"]
subtest {[regexp "\n$wk\|$cn\|$user_name\|" $output]} "Verify wk1"
subtest {[regexp "\n$wk2\|$cn\|$user_name\|" $output]} "Verify wk2"
subtest {[regexp "\n$wk3\|$cn\|$user_name\|" $output]} "Verify wk3"

#
# Use sacctmgr to delete test WCKeys
#
run_command -fail "$sacctmgr -i delete user $user_name wckey=$wk,$wk2,$wk3"

#
# Use sacctmgr to verify if WCKeys were removed
#
set output [run_command_output -fail "$sacctmgr -p list wckey where user=$user_name"]
subtest {[regexp "\n$wk\|$cn\|$user_name\|" $output]} "Verify WCKey = $wk has been deleted"
subtest {[regexp "\n$wk2\|$cn\|$user_name\|" $output]} "Verify WCKey = $wk2 has been deleted"
subtest {[regexp "\n$wk3\|$cn\|$user_name\|" $output]} "Verify WCKey = $wk3 has been deleted"
