#!/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).
############################################################################
# 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 def_part      0
set def_part_name ""
set exit_code     0
set matches       0

#
# 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 "($eol)($re_word_str)(\\*) *up" {
		if (![string compare $expect_out(3,string) "*"]) {
			set def_part 1
			set def_part_name $expect_out(2,string)
		}
		exp_continue
	}
	timeout {
		fail "sinfo not responding"
	}
	eof {
		wait
	}
}

if {$matches != 5} {
	log_error "sinfo partition summarize format error"
	set exit_code 1
}
if {$def_part == 0} {
	log_error "sinfo shows no available default partition"
	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 {
		fail "sinfo not responding"
	}
	eof {
		wait
	}
}

if {$matches != 1} {
	log_error "sinfo partition filtering error"
	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 {
		fail "sinfo not responding"
	}
	eof {
		wait
	}
}

if {$matches != 6} {
	log_error "sinfo partition normal format error $matches"
	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 {
		fail "sinfo not responding"
	}
	eof {
		wait
	}
}

if {$matches != 10} {
	fail "sinfo partition long format error"
}
if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
