#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Bluegene/P only: Confirm that salloc can reserve the HTC partitons.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2012 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Phil Eckert <eckert2@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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id     "8.25"

proc salloc_module { htc } {

	global salloc scancel smap bin_bash alpha_numeric max_job_delay number

	set exit_code   1
	set block      ""
	set job_id      0
	set timeout $max_job_delay
	set node_cnt 16

#
#	Allocate an HTC block.
#
	set salloc_pid [spawn $salloc --reboot -N$node_cnt --conn-type=$htc]
	expect {
		-re "Granted job allocation ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		-re "salloc: Block ($alpha_numeric) is ready for job" {
			set block $expect_out(1,string)
			send_user "the block=$block \r"
			set smap_pid [spawn $bin_bash -c "$smap -cDb | grep $block"]
			expect {
				-re ".*$block .*$htc" {
					set exit_code  0
					send_user "Cancel job. \r"
					set salloc_pid [spawn $scancel $job_id]
					exp_continue
				}
				timeout {
					send_user "\nFAILURE: salloc not responding\n"
					if {$job_id != 0} {
						cancel_job $job_id
					}
					slow_kill [expr 0 - $salloc_pid]
					exit 1
				}
				eof {
					wait
				}
			}
		}
	
		-re "salloc: error: invalid conn-type argument" {
			send_user "\nWARNING: invalid conn-type argument\n"
			exit 0
		}
		-re "More processors requested than permitted" {
			send_user "\nWARNING: can't test salloc task distribution\n"
			exit 0
		}
		-re "Node count specification invalid" {
			send_user "\nWARNING: can't test salloc task distribution\n"
			exit 0
		}
		-re "Configuration not available" {
			send_user "\nWARNING: partition too small for test\n"
			exit 0
		}
		timeout {
			send_user "\nFAILURE: salloc not responding\n"
			if {$job_id != 0} {
				cancel_job $job_id
			}
			slow_kill [expr 0 - $salloc_pid]
			exit 1
		}
		eof {
			wait
		}
	}
	
	if {$exit_code == 0} {
		send_user "\nSUCCESS for $htc\n"
		return 0
	}
	
	send_user "Cancel job. \r"
	set salloc_pid [spawn $scancel $job_id]

	return 1
}

# TEST STARTS HERE

print_header $test_id

#
# Vefiy that this is a bluegene system, and  that is of a BG/P type.
#
if { [test_bluegene] == 0 } {
	send_user "\nWARNING: This test is incompatible with non-Bluegene systems\n"
	exit 0
}

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

if {[string compare $type "P"]} {
	send_user "\nWARNING: Not a Bluegene/P system \n"
	exit 0
}


#
# Test each HTC type.
#
set list "HTC_S HTC_V HTC_D"
set type ""

foreach type $list {
	set return [salloc_module $type]
	if { $return == 1 } {
		send_user "\nFAILURE for $type\n"
		exit 1
	}
}

#
# Was successful.
#
send_user "\nSUCCESS\n"
exit 0
