#!/usr/bin/env expect
############################################################################
# Purpose:  Test of route/topology plugin
############################################################################
#  Copyright (C) 2014 Bull S. A. S.
#		Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois.
#  Written by Rod Schultz <Rod.Schultz@bull.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://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

# The test program calls the route_g_splithostlist api on testcases
# in a testcase file.
#
# The test program is also given a path to a directory containing
# a slurm.conf, topology.conf and testcase file that assumes those config.
# These directories are named etc33.1.#
#
# The parser if the testcase file is very simple.
# -- lines beginning with # are comments.
# -- line beginning with a line feed ends a testcase.
# -- The first line in the testcase is the node list expression to be split
#    The following lines in the testcase are the expected resulting node lists.

set test_prog   "$test_name.prog"

if {[get_config_param "FrontendName"] ne "MISSING"} {
	skip "Test incompatible with front-end systems"
}

proc cleanup {} {
	global test_prog

	file delete $test_prog
}

# Procedure to run a configuration
proc run_config { test_config } {
	global test_prog cfgcount cfgok wd

	incr cfgcount
	set cfgdir "$wd/$test_config"
	set tc "$wd/$test_config/testcases"
	set local_ok 0

	log_debug "./${test_prog} --configdir=${cfgdir} --testcases=${tc}"
	spawn ./${test_prog} --configdir=${cfgdir} --testcases=${tc}
	expect {
		-re "Failed cases 0" {
			incr cfgok
			incr local_ok
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$local_ok == 0} {
		log_warn "*** At least one testcase in $test_config failed!"
	}
}

#
# Rebuild program
#
if [compile_against_libslurm -full $test_prog] {
	fail "Cannot compile test program"
}

log_user 0
set cfgcount 0
set cfgok 0
set wd [pwd]

# Set the default slurm.conf params for all tests
set params "ClusterName=test33
ControlMachine=peta0000
TopologyPlugin=topology/tree
TreeWidth=50

DebugFlags=Route\n"

# Set the params if needed for certain systems
if {[get_config_param "SwitchType"] eq "switch/cray"} {
	set params "$params
MPIParams=ports=12000-13000\nProctrackType=proctrack/cray_aries\n"
}

###############################################
# test 33.1.1
###############################################
log_info "Test33.1.1"
set file_in "$wd/etc.33.1.1/slurm.conf"
file delete $file_in
set fd [open "$file_in" a]
puts $fd "$params
RoutePlugin=route/topology

# ================================
# Node and Partition Definitions
# ================================

NodeName=peta\[10000-19999\]
PartitionName=all  Nodes=peta\[10000-19999\] Default=YES"

close $fd
run_config "etc.33.1.1"
file delete $file_in

###############################################
# test 33.1.2
###############################################
log_info "Test33.1.2"
set file_in "$wd/etc.33.1.2/slurm.conf"
file delete $file_in
set fd [open "$file_in" a]
puts $fd "$params
RoutePlugin=route/topology

# ================================
# Node and Partition Definitions
# ================================

NodeName=peta\[10000-19999\]
PartitionName=all  Nodes=peta\[10000-19999\] Default=YES"

close $fd
run_config "etc.33.1.2"
file delete $file_in

###############################################
# test 33.1.3
###############################################
log_info "Test33.1.3"
set file_in "$wd/etc.33.1.3/slurm.conf"
file delete $file_in
set fd [open "$file_in" a]
puts $fd "$params

# ================================
# Node and Partition Definitions
# ================================

NodeName=peta\[10000-10399\]
PartitionName=all  Nodes=peta\[10000-10399\] Default=YES"

close $fd
run_config "etc.33.1.3"
file delete $file_in

###############################################
# test 33.1.4
###############################################
log_info "Test33.1.4"
set file_in "$wd/etc.33.1.4/slurm.conf"
file delete $file_in
set fd [open "$file_in" a]
puts $fd "$params
RoutePlugin=route/topology

# ================================
# Node and Partition Definitions
# ================================

NodeName=peta\[10000-10399\]
PartitionName=all  Nodes=peta\[10000-10399\] Default=YES"

close $fd
run_config "etc.33.1.4"
file delete $file_in

if {$cfgok != $cfgcount} {
	fail "Some of the configurations failed"
}

###############################################
# test 33.1.5
###############################################
log_info "Test33.1.5"
set file_in "$wd/etc.33.1.5/slurm.conf"
file delete $file_in
set fd [open "$file_in" a]
puts $fd "$params
RoutePlugin=route/topology

# ================================
# Node and Partition Definitions
# ================================

NodeName=peta\[10000-10399,99999\]
PartitionName=all  Nodes=peta\[10000-10399,99999\] Default=YES"

close $fd
run_config "etc.33.1.5"
file delete $file_in

if {$cfgok != $cfgcount} {
	fail "Some of the configurations failed"
}
