#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Check partition information, both long and short (--long and
#          --summarize options) and partition filtering (--partition option).
#
# 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) 2002-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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

set test_id       "4.3"
set def_part      0
set def_part_name ""
set exit_code     0
set matches       0

print_header $test_id

#
# Check the sinfo summarize format
# Just looking for the regular expression "\*" causes errors in
# looking for the default partition, so the logic looks for an
# alpha-numeric partition name followed by the state (up|down)
# then looks for the partition name suffix of "*"
#

spawn $sinfo --summarize
expect {
	-re "PARTITION" {
		incr matches
		exp_continue
	}
	-re "AVAIL" {
		incr matches
		exp_continue
	}
	-re "TIMELIMIT" {
		incr matches
		exp_continue
	}
	-re "NODES.A/I/O/T" {
		incr matches
		exp_continue
	}
	-re "NODELIST" {
		incr matches
		exp_continue
	}
	-re "MIDPLANELIST" {
		incr matches
		exp_continue
	}
	-re "STATE" {
		incr matches -10
		exp_continue
	}
	-re "($end_of_line)($alpha_numeric_under)(\[ \*\]) *up" {
		if (![string compare $expect_out(3,string) "*"]) {
			set def_part 1
			set def_part_name $expect_out(2,string)
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sinfo not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 5} {
	send_user "\nFAILURE: sinfo partition summarize format error\n"
	set exit_code 1
}
if {$def_part == 0} {
	send_user "\nFAILURE: sinfo shows no available default partition\n"
	set exit_code 1
}

#
# Check the sinfo summarize format with partition filter
#

set matches     0
spawn $sinfo --summarize --partition=$def_part_name
expect {
	-re "up" {
		incr matches
		exp_continue
	}
	-re "down" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sinfo not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 1} {
	send_user "\nFAILURE: sinfo partition filtering error\n"
	set exit_code 1
}

#
# Check the sinfo regular format
#

set matches     0
spawn $sinfo
expect {
	-re "PARTITION" {
		incr matches
		exp_continue
	}
	-re "AVAIL" {
		incr matches
		exp_continue
	}
	-re "TIMELIMIT" {
		incr matches
		exp_continue
	}
	-re "NODES " {
		incr matches
		exp_continue
	}
	-re "STATE" {
		incr matches
		exp_continue
	}
	-re "NODELIST" {
		incr matches
		exp_continue
	}
	-re "MIDPLANELIST" {
		incr matches
		exp_continue
	}
	-re "ROOT" {
		incr matches -10
		exp_continue
	}
	-re "OVERSUBS" {
		incr matches -10
		exp_continue
	}
	-re "GROUPS" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sinfo not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 6} {
	send_user "\nFAILURE: sinfo partition normal format error $matches\n"
	set exit_code 1
}

#
# Check the sinfo long format
#
if {[info exists env(SINFO_FORMAT)]} {
    unset env(SINFO_FORMAT)
}
set matches     0
spawn $sinfo --long
expect {
	-re "PARTITION" {
		incr matches
		exp_continue
	}
	-re "AVAIL" {
		incr matches
		exp_continue
	}
	-re "TIMELIMIT" {
		incr matches
		exp_continue
	}
	-re "JOB_SIZE" {
		incr matches
		exp_continue
	}
	-re "ROOT" {
		incr matches
		exp_continue
	}
	-re "OVERSUBS" {
		incr matches
		exp_continue
	}
	-re "GROUPS" {
		incr matches
		exp_continue
	}
	-re "NODES " {
		incr matches
		exp_continue
	}
	-re "STATE" {
		incr matches
		exp_continue
	}

	-re "NODELIST" {
		incr matches
		exp_continue
	}
	-re "MIDPLANELIST" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sinfo not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != 10} {
	send_user "\nFAILURE: sinfo partition long format error\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
