#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          sacctmgr verify wckey
#
# 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 <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 test_id     "21.23"
set exit_code   0
set file_in     "test.$test_id.input"
set ta		"test$test_id-account.1"
set wk          twckey
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 { [test_track_wckey] == 0} {
	send_user "\nWARNING: This test can't be run without TrackWCKey being set\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 set admin=admin.\n"
	exit 0
}

set using_slurmdbd [test_using_slurmdbd]

set cn [get_cluster_name]

#
# Identify the user and his current default account
#
set acct_name ""
set user_name [get_my_user_name]

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 $ta cluster=$cn]
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 and wckey
#
set sadd_pid [spawn $sacctmgr -i create user name=$user_name account=$ta cluster=$cn wckeys=$wk]
expect {
	 timeout {
		send_user "\nFAILURE: sacctmgr add not responding\n"
		slow_kill $sadd_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# 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 {
		send_user "\nFAILURE: salloc not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$using_slurmdbd} {
	sleep 5
}

spawn $sacct -p -j $job_id --format=wckey
	expect {
		-re "$wk" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
if {$job_id == 0} {
	send_user "\nFAILURE: salloc failed to initiate job\n"
	set exit_code 1
} elseif {$matches != 2} {
	send_user "\nFAILURE: salloc failed to use desired wckey\n"
	set exit_code 1
}

#
# Spawn a job via sbatch using this wckey
#
make_bash_script $file_in "$bin_id"
set job_id 0
spawn $sbatch -N1 --account=$ta --wckey=$wk --output=none $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: did not get sbatch job_id\n"
	set exit_code 1
} else {
	set matches 0
	spawn $scontrol show job $job_id
	expect {
		-re "WCKey=$wk" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$using_slurmdbd} {
		sleep 5
	}

	spawn $sacct -p -j $job_id --format=wckey
	expect {
		-re "$wk" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches != 2} {
		send_user "\nFAILURE: sbatch failed to use specified wckey\n"
		set exit_code 1
	}
}

#
# Spawn a job via srun using this wckey
#
set job_id 0
spawn $srun -N1 -v --account=$ta --wckey=$wk $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_id\n"
	set exit_code 1
} else {
	set matches 0
	spawn $scontrol show job $job_id
	expect {
		 -re "WCKey=$wk" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$using_slurmdbd} {
		sleep 5
	}

	spawn $sacct -p -j $job_id --format=wckey
	expect {
		-re "$wk" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches != 2} {
		send_user "\nFAILURE: srun failed to use specified wckey\n"
		set exit_code 1
	}
}

#
# Use sacctmgr to delete the test account
#
set damatches 0
set sadel_pid [spawn $sacctmgr -i delete account $ta]
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} {
	exec $bin_rm -f $file_in
	print_success $test_id
}
exit $exit_code
