#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test basic sacct functionality and accuracy.
#
# 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) 2005 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# UCRL-CODE-217948.
# 
# This file is part of SLURM, a resource management program.
# For details, see <http://www.llnl.gov/linux/slurm/>.
#  
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id     "12.2"
set exit_code   0
set file_prog   "test$test_id.prog"
set job_id      0
set matches     0

# job paramters
set mem_size    100000
set sleep_time  25 
set ret_code    42

print_header $test_id

#
# Check if accounting is enabled
#
set supported 0
log_user 0
spawn $scontrol show config
expect {
	-re "jobacct/log" {
		set supported 1
		exp_continue
	}
	eof {
		wait
	}
}
log_user 1
if {$supported == 0} {
	send_user "\nWARNING: job accounting not configured on this system\n"
	exit 0
}

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $file_prog
exec $bin_make -f /dev/null $file_prog
exec $bin_chmod 700 $file_prog

#
# Run a simple job
#
set timeout [expr $max_job_delay + $sleep_time]
spawn $srun --output=none --error=none -v -t2 -N1 $file_prog $ret_code $sleep_time $mem_size
expect {
	-re "launching ($number)\.0" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job initiation failed\n"
	exit 1
}

# Wait for data to get logged
exec $bin_sleep 5

#
# Report basic sacct info
#
 
spawn $sacct --noheader --jobstep=$job_id.0 --fields jobstep,jobname,status,error
expect {
	-re "$job_id\.0" {
		incr matches
		exp_continue
	}
	-re "$file_prog" {
		incr matches
		exp_continue
	}
	-re "FAILED" {
		incr matches
		exp_continue
	}
	-re "$ret_code" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacct not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 4} {
	send_user "\nFAILURE: sacct reporting failed\n"
	set exit_code 1
}

#
# Report the sacct accouting info
#
set elapsed_time 0
spawn $sacct --noheader  --jobstep=$job_id.0 --fields elapsed
expect {
	-re "($number):($number)\.($number)" {
		set mins $expect_out(1,string)
		set secs $expect_out(2,string)
		set elapsed_time [expr $mins * 60 + $secs]
	}
	-re "($number)\.($number)" {
		set elapsed_time $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacct not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

set diff_time [expr $elapsed_time - $sleep_time]
set error_time [expr abs($diff_time)]
if {$error_time > 5} {
	send_user "\nFAILURE: sacct elapsed time error of $error_time\n"
	set exit_code 1
} else {
	send_user "\nSUCCESS: sacct elapsed time error of $error_time\n"
}

set mem_used 0
spawn $sacct --noheader  --jobstep=$job_id.0 --fields vsize
expect {
	-re "($number)" {
		set mem_used  $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacct not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

set diff_mem [expr $mem_used - $mem_size]
set error_mem [expr abs($diff_mem)]
if {$error_mem > 4000} {
	send_user "\nFAILURE: sacct memory use error of $error_mem\n"
	set exit_code 1
} else {
	send_user "\nSUCCESS: sacct memory use error of $error_mem\n"
}


if {$exit_code == 0} {
	exec rm -f $file_prog
	send_user "\nSUCCESS\n"
}
exit $exit_code
