#!/usr/bin/env expect
############################################################################
# Purpose: Test of BLUEGENE SLURM functionality
#          Test creation of all blocks 1 midplane and smaller.
#
# 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 <http://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     "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 parameters
set sleep_time 1

proc run_and_test { size } {
	global number sbatch scontrol
	global file_out file_err file_in procs_per_cnode

	set job_id 0
	set exit_code 0

	set sbatch_pid [spawn $sbatch --output=$file_out --error=$file_err -t2 -N$size-$size $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
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {!$job_id || $exit_code} {
		send_user "\nFAILURE: batch submit failure\n"
		return 1
	}

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

	set matches 0

	if {$size > 1} {
		set requested_cpus "$procs_per_cnode\\*$size"
	} else {
		set requested_cpus "$procs_per_cnode"
	}

	set scon_pid [spawn $scontrol -d show job $job_id]
	expect {
		-re "CPUs=$requested_cpus" {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			slow_kill $scon_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 1} {
		send_user "\nFAILURE: Bluegene $size cnode block not created correctly\n"
		set exit_code 1
	}


	return $exit_code
}

proc run_bgl_test { } {
	global psets

	# make a 512 cnode block
	if {[run_and_test 512]} {
		return 1
	}

	if {[run_and_test 128]} {
		return 1
	}

	if {$psets >= 16} {
		if {[run_and_test 32]} {
			return 1
		}
	}

	return 0;
}


proc run_bgp_test { } {
	global psets

	# make a 512 cnode block
	if {[run_and_test 512]} {
		return 1
	}

	if {[run_and_test 256]} {
		return 1
	}
	if {[run_and_test 128]} {
		return 1
	}

	if {$psets >= 8} {
		if {[run_and_test 64]} {
			return 1
		}
		if {$psets >= 16} {
			if {[run_and_test 32]} {
				return 1
			}
			if {$psets >= 32} {
				if {[run_and_test 16]} {
					return 1
				}
			}
		}
	}
	return 0;
}

# TEST STARTS HERE

print_header $test_id

if {[test_bluegene] == 0} {
	send_user "\nWARNING: This test is only compatable with bluegene systems\n"
	exit $exit_code
}

if {[string compare [get_bluegene_layout] Dynamic]} {
	send_user "\nWARNING: This test is only compatable with dynamic bluegene systems\n"
	exit $exit_code
}

set part_name [default_partition]
set shared [partition_shared $part_name]
if {[string compare $shared FORCE]} {
	send_user "\nFAILURE: System configured Dynamic, but Partition $part_name has Shared=$shared\n"
	exit 1
}

set psets [get_bluegene_psets]

if {$psets == 0} {
	send_user "\nFAILURE: No psets are set on this system\n"
	exit 1
}

set procs_per_cnode [get_bluegene_procs_per_cnode]

if {$procs_per_cnode == 0} {
	send_user "\nFAILURE: Couldn't determine procs per cnode\n"
	exit 1
}

set type [get_bluegene_type]

if {$type == 0} {
	send_user "\nFAILURE: No bluegene type found \n"
	exit 1
}


#
# 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]

if {![string compare $type "Q"]} {
	set exit_code [run_bgp_test]
} elseif {![string compare $type "P"]} {
	set exit_code [run_bgp_test]
} elseif {![string compare $type "L"]} {
	set exit_code [run_bgl_test]
} else {
	send_user "\nFAILURE: unknown bluegene system type '$type'\n";
	exit 1
}


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