#!/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 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 {$expect_out(3,string) eq "*"} {
			set def_part 1
			set def_part_name $expect_out(2,string)
		}
		exp_continue
	}
	timeout {
		fail "sinfo not responding"
	}
	eof {
		wait
	}
}

subtest {$matches == 5} "Check the sinfo summarize format"
subtest {$def_part != 0} "sinfo should show the default partition"

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

subtest {$matches == 1} "Check the sinfo summarize format with partition filter"

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

subtest {$matches == 6} "Check the sinfo regular format"

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

subtest {$matches == 10} "Check the sinfo long format"
