#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that sinfo -O (--Format) option displays the
#          correct user specified values.
#
# 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) 2015 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.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
source ./globals_accounting

set test_id             4.13
set test_acct           "test$test_id\_acct"
set test_part           "test$test_id\_part"
set test_node           [get_idle_node_in_part [default_partition]]
set alpha_numeric_dot   "\[a-zA-Z0-9/_.:\-\]+"
set exit_code           0

array set node_sinfo_vals { }
array set part_sinfo_vals { }

array set node_info {
	AllocMem           allocmem
	CPUTot             cpus
	CPULoad            cpusload
	CoresPerSocket     cores
	TmpDisk            disk
	Features           features
	FreeMem            freemem
	Gres               gres
	NodeAddr           nodeaddr
	NodeHostName       nodehost
	RealMemory         memory
	State              statelong
	Sockets            sockets
	ThreadsPerCore     threads
	Version            version
	Weight             weight
}

array set part_info {
	State              available
	MaxCPUsPerNode     maxcpuspernode
	Nodes              nodelist
	TotalNodes         nodes
	PreemptMode        preemptmode
	PriorityJobFactor  priorityjobfactor
	PriorityTier       prioritytier
	OverSubscribe      oversubscribe
	RootOnly           root
}

print_header $test_id
set def_part [default_partition]

proc cleanup { } {

	global scontrol test_part exit_code

	spawn $scontrol delete partitionname=$test_part
	expect {
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

}

if { ![test_super_user] } {
        send_user "\nWARNING: This test can't be run without being a super user of the cluster.\n"
	exit 0
}

send_user "\n=== Testing Node Information ===\n"
set first_option 1
foreach option [array names node_info] {

	if {$first_option == 1} {
		set first_option 0
	} else {
		log_user 0
	}
	spawn $scontrol show node $test_node
	expect {
		-re "\\m$option=($alpha_numeric_dot)\\M" {
			set node_sinfo_vals($node_info($option)) $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	log_user 1
}

set first_option 1
foreach option [array names node_sinfo_vals] {

	set match 0
	spawn $sinfo -n$test_node -O$option -h
	expect {
		-nocase -re "$node_sinfo_vals($option)" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sinfo is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 1} {
		send_user "\nFAILURE: Node information $option output does "
		send_user "not match sinfo output. "
		send_user "Looking for value: $option = $node_sinfo_vals($option)\n"
		set exit_code 1
	}
}

#
# Add test partition
#
spawn $scontrol create partitionname=$test_part nodes=$test_node
expect {
	timeout {
		send_user "\nFAILURE: scontrol is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

set match 0
spawn $scontrol show partitionname=$test_part
expect {
	-re "PartitionName=$test_part" {
		set match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$match != 1} {
	send_user "\nFAILURE: test partition was not created\n"
	exit 1
}

send_user "\n=== Testing Partition Information ===\n"
set first_option 1
foreach option [array names part_info] {

	set part_sinfo_vals($part_info($option)) "UNKNOWN"
	set match 0
	if {$first_option == 1} {
		set first_option 0
	} else {
		log_user 0
	}
	spawn $scontrol show partition $test_part
	expect {
		-re "\\m$option=($alpha_numeric_dot)\\M" {
			set part_sinfo_vals($part_info($option)) \
			    $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	log_user 1
}

set first_option 1
foreach option [array names part_sinfo_vals] {

	set match 0
	spawn $sinfo -p$test_part -O$option -h
	expect {
		-nocase -re "$part_sinfo_vals($option)" {
			set match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sinfo is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 1} {
		send_user "\nFAILURE: Partition information $option "
		send_user "output does not match sinfo output. "
		send_user "Looking for value: "
		send_user "$option = $part_sinfo_vals($option)\n"
		set exit_code 1
	}
}

cleanup

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
} else {
	send_user "\nFAILURE\n"
}

exit $exit_code
