#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          sacctmgr add an account to this cluster and try using it with
#          salloc, sbatch and srun.  We also test limits here as well.
#
# 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
source ./globals_accounting
source ./inc21.21.1
source ./inc21.21.2
source ./inc21.21.3
source ./inc21.21.4

set test_id     "21.21"
set exit_code   0
set file_in     "test.$test_id.input"
set ta		slurm_test-account.1
set timeout 60
print_header $test_id

if {[test_serial]} {
	send_user "\nWARNING: This test is not compatible with serial system\n"
	exit 0
}

proc _test_limits { } {
	global file_in srun sbatch squeue scancel bin_id number bin_sleep bin_rm ta
	# test maxcpumin maxcpu maxjob maxnode maxsubmit maxwall
	set maxcpumin 10
	set maxcpu 4
	set maxjob 2
	set maxnode 1
	set maxsubmit 4
	set maxwall 10
	set exit_code 0

	incr exit_code [mod_acct "" "" "$ta" "" "" "" "" "" "" "" "" "" "" "" "" $maxjob $maxnode $maxsubmit $maxwall "" "" ""]
	if { $exit_code } {
		return $exit_code
	}

	#
	# Start sub-test: test for maxnode
	#
	inc21_21_1 $maxnode

	#
	# Start sub-test: test for maxnode+1
	#
	inc21_21_2 $maxnode

	#
	# Start sub-test: testing maxwall
	#
	inc21_21_3 $maxwall

	#
	# Start sub-test: testing maxwall+1
	#
	inc21_21_4 $maxwall


	# This should overwrite the old file_in which has already been used,
	# so no big deal.
	make_bash_script $file_in "
	$bin_sleep 5
	"

	# test job max cnt and submit
	for {set inx 0} {$inx < $maxsubmit} {incr inx} {
		set job_id($inx) 0
		set mypid [spawn $sbatch -N1 -n1 --account=$ta --output=/dev/null --error=/dev/null -t5 $file_in]
		expect {
			-re "Submitted batch job ($number)" {
				set job_id($inx) $expect_out(1,string)
				exp_continue
			}
			-re "Unable to contact" {
				send_user "\nFAILURE: slurm appears to be down\n"
				set exit_code 1
				exp_continue
			}
			timeout {
				send_user "\nFAILURE: sbatch not responding\n"
				slow_kill $mypid
				set exit_code 1
			}
			eof {
				wait
			}
		}

		if { !$job_id($inx) } {
			send_user "\nFAILURE: sbatch didn't return jobid\n"
			set exit_code 1
			exit
		}

		if { $exit_code } {
			break
		}
	}

	if { $exit_code } {
		return $exit_code
	}

	# then submit one more over the limit and it should fail
	set mypid [spawn $sbatch -N1 -n1 --account=$ta --output=/dev/null --error=/dev/null -t5 $file_in]
	expect {
		-re "Job violates accounting policy" {
			send_user "\nThis error is expected, not a problem\n"
			exp_continue
		}
		-re "Submitted batch job ($number)" {
			send_user "\nFAILURE: job should not have run 3.\n"
			set exit_code 1
			exp_continue
		}
		-re "Unable to contact" {
			send_user "\nFAILURE: slurm appears to be down\n"
			set exit_code 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sbatch not responding\n"
			slow_kill $mypid
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if { $exit_code } {
		return $exit_code
	}

	set matches 0
	set mypid [spawn $squeue -o "\%i \%t \%r"]
	expect {
		-re "($job_id(2)|$job_id(3)).PD.AssociationJobLimit" {
			incr matches
			exp_continue
		}
		-re "($job_id(0)|$job_id(1)).R.None" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: squeue not responding\n"
			slow_kill $mypid
			set exit_code 1
		}
		eof {
			wait
		}
	}

	spawn $scancel --quiet --account=$ta
	expect {
		eof {
			wait
		}
	}


	if { $matches != 4 } {
		send_user "\nFAILURE: job's are in the state expected got $matches\n"
		set exit_code 1
		return $exit_code
	}

	return $exit_code
}

#
# 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 set admin=admin.\n"
	exit 0
}

#
# Identify the user and his current default account
#
set acct_name ""
set user_name ""
spawn $bin_id -u -n
expect {
	 -re "($alpha_numeric_under)" {
		set user_name $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 $ta]
expect {
	-re "Adding Account" {
		incr aamatches
		exp_continue
	}
	-re "Nothing new added" {
		send_user "\nWARNING: vestigial account $ta 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=$ta]
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 account
#
set job_id    0
set matches   0
set timeout   $max_job_delay
spawn $salloc -N1 --account=$ta
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 "Account=$ta" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: salloc 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 != 1} {
	send_user "\nFAILURE: salloc failed to use desired account\n"
	set exit_code 1
}

#
# Spawn a job via sbatch using this account
#
make_bash_script $file_in "$bin_id"
set job_id 0
spawn $sbatch -N1 --account=$ta --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 "Account=$ta" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches != 1} {
		send_user "\nFAILURE: sbatch failed to use specified account\n"
		set exit_code 1
	}
}

#
# Spawn a job via srun using this account
#
set job_id 0
spawn $srun -N1 -v --account=$ta $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 "Account=$ta" {
			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
	}
}


#
# Check to see if limits are enforced.
#
if { [test_limits_enforced] == 1 } {
	set exit_code [_test_limits]
	# wait around a bit to make sure jobs finish
	sleep 3
}

#
# Use sacctmgr to delete the test account
#
# First wait a few seconds for the job to complete
sleep 2
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
