#!/usr/bin/expect
############################################################################
# Purpose: Test of BLUEGENE SLURM functionality
#          Test 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) 2006-2007 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
# 
# This file is part of SLURM, a resource management program.
# For details, see <https://computing.llnl.gov/linux/slurm/>.
# 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     "8.5"
set exit_code   0
set file_in     "test$test_id.input"
set file_out    "test$test_id.output"
set file_err    "test$test_id.error"
set job_id      0
set matches     0

# job paramters
set sleep_time 1

print_header $test_id

if {[test_bluegene] == 0} {
	send_user "\nWARNING: This test is only compatable with bluegene systems\n"
	exit $exit_code
}
#
# Delete left-over input script files
# Build input script file
#
exec $bin_rm -f $file_in
exec echo "#!$bin_bash"    >$file_in
exec echo "$bin_sleep $sleep_time "     >>$file_in
exec $bin_chmod 700 $file_in

#
set timeout [expr $max_job_delay + $sleep_time]

# make a 512 cnode block

set sbatch_pid [spawn $sbatch --output=$file_out --error=$file_err -t2 -N512-512 $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"
		slow_kill $sbatch_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to run
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to run\n"
	set exit_code 1
}

set matches 0
set cpus_per_node 1

spawn $scontrol show job $job_id
expect {
	-re "BP_List=$alpha_numeric " {
		incr matches
		exp_continue
	}
	-re "AllocCPUs=($number)" {
		set cpu_count $expect_out(1,string)
		set cpus_per_node [expr $cpu_count / 512]
		if {($cpu_count != 512) && ($cpu_count != 1024)} {
			send_user "\nFAILURE: Bad node count allocated\n"
			set exit_code 1
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: Blue Gene 512 cnode block not created correctly\n"
	set exit_code 1
}
if {$exit_code != 0} {
	exit $exit_code
}

# make a 128 cnode block


set sbatch_pid [spawn $sbatch --output=$file_out --error=$file_err -t2 -N128-128 $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"
		slow_kill $sbatch_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to run
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to run\n"
	set exit_code 1
}

set matches 0

spawn $scontrol show job $job_id
expect {
	-re "BP_List=$alpha_numeric\[$number-$number\] " {
		incr matches
		exp_continue
	}
	-re "AllocCPUs=($number)" {
		set cpu_count $expect_out(1,string)
		set node_count [expr $cpu_count / $cpus_per_node]
		if {$node_count != 128} {
			send_user "\n=======================================\n"
			send_user "\nFAILURE: Bad node count allocated\n"
			send_user "This can indicate a low configured value of Numpsets\n"
			send_user "  in bluegene.conf (few available I/O nodes).\n"
			send_user "This could indicate Static Bluegene partitioning\n"
			send_user "  with no small blocks (1/4 of a base partition).\n"
			send_user "This could also indicate MinNodes for the partition\n"
			send_user "  being too high for this test.\n"
			send_user "=======================================\n"
			set exit_code 1
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: Blue Gene 128 cnode block not created correctly\n"
	set exit_code 1
}

# make a 32 cnode block

set sbatch_pid [spawn $sbatch --output=$file_out --error=$file_err -t2 -N32-32 $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"
		slow_kill $sbatch_pid
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to run
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to run\n"
	set exit_code 1
}

set matches 0
spawn $scontrol show job $job_id
expect {
#	Could be one or multiple node cards (e.g. "bgl000[0]" or "bgl000[0-1]")
	-re "BP_List=$alpha_numeric\[$number" {
		incr matches
		exp_continue
	}
	-re "AllocCPUs=($number)" {
		set cpu_count $expect_out(1,string)
		set node_count [expr $cpu_count / $cpus_per_node]
		if {$node_count != 32} {
			send_user "\n=======================================\n"
			send_user "FAILURE: Bad node count allocated\n"
			send_user "This can indicate a low configured value of Numpsets\n"
			send_user "  in bluegene.conf (few available I/O nodes).\n"
			send_user "This could indicate Static Bluegene partitioning\n"
			send_user "  with no small blocks (1/16 of a base partition).\n"
			send_user "This could also indicate MinNodes for the partition\n"
			send_user "  being too high for this test.\n"
			send_user "=======================================\n"
			set exit_code 1
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: Blue Gene 32 cnode block not created correctly\n"
	set exit_code 1
}

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