#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM sreport functionality
#          sreport h, n, p, P, t, V options.
#
# 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) 2009 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Joseph Donaghy <donaghy1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
# 
# This file is part of SLURM, a resource management program.
# For details, see <https://computing.llnl.gov/linux/slurm/>.
# 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     "22.2"
set exit_code   0
set timeout 60
print_header $test_id

#
# Check accounting config and bail if not found.
#
if { [test_account_storage] == 0 } {
	send_user "\nWARNING: This test can't be run without a usable AccountStorageType\n"
	exit 0
}

################################################################
#
# Proc: sreport_opt
#
# Purpose:  Pass sreport options and test
#
# Returns: Number of matches.
#
# Input: Switch options not requiring arguments
#
################################################################

proc sreport_opt { soption } {
	global number sreport
	set debug       0
	set exit_code   0
	set matches     0
	set not_support 0
	send_user "$sreport -$soption \n"

	if { $soption == "-help"|| $soption == "-usage" || $soption == "h" } {

	spawn $sreport -$soption
	expect {
		-re "SLURM accounting storage is disabled" {
			set not_support 1
			exp_continue
		}
		-re "sreport...OPTION.....COMMAND" {
			if {$debug} {send_user "\nmatch1\n"}
			incr matches
			exp_continue
		}
		-re "Valid .OPTION. values are" {
			if {$debug} {send_user "\nmatch2\n"}
			incr matches
			exp_continue
		}
		-re "Valid .COMMAND. values are" {
			if {$debug} {send_user "\nmatch3\n"}
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sreport not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$not_support == 1} {
		send_user "\nWARNING: can not test without accounting enabled\n"
		exit 0
	}

	if {$matches != 3} {
		send_user "\nFAILURE: sreport -$soption failed ($matches)\n"
		set exit_code 1
	}
	return $matches
}

	if { $soption == "-noheader" || $soption == "n" } {

	spawn $sreport -$soption job sizes
	expect {
		-re "SLURM accounting storage is disabled" {
			set not_support 1
			exp_continue
		}
		-re "Cluster|Account|0-49 cpus|50-249 cpus|250-499 cpus|500-999 cpus|1000 cpus|of cluster" {	
			if {$debug} {send_user "\nmatch4\n"}
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sreport not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$not_support == 1} {
		send_user "\nWARNING: can not test without accounting enabled\n"
		exit 0
	}
	if {$matches != 0} {
		send_user "\nFAILURE: sreport -$soption failed ($matches)\n"
		set exit_code 1
	}
	return $matches
}

	if { $soption == "-parsable" || $soption == "p" } {

	spawn $sreport -$soption job sizes
	expect {
		-re "SLURM accounting storage is disabled" {
			set not_support 1
			exp_continue
		}
		-re "Cluster\\|Account\\|0-49 cpus\\|50-249 cpus\\|250-499 cpus\\|" {
			if {$debug} {send_user "\nmatch5\n"}
			incr matches
			exp_continue
		}
		-re "500-999 cpus\\|>= 1000 cpus\\|% of cluster\\|" {
			if {$debug} {send_user "\nmatch6\n"}
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sreport not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$not_support != 0} {
		send_user "\nWARNING: can not test without accounting enabled\n"
		exit 0
	}
	return $matches
}

	if { $soption == "-parsable2" || $soption == "P" } {

	spawn $sreport -$soption job sizes
	expect {
		-re "SLURM accounting storage is disabled" {
			set not_support 1
			exp_continue
		}
		-re "Cluster\\|Account\\|0-49 cpus\\|50-249 cpus\\|250-499 cpus\\|" {
			if {$debug} {send_user "\nmatch7\n"}
			incr matches
			exp_continue
		}
		-re "500-999 cpus\\|>= 1000 cpus\\|% of cluster" {
			if {$debug} {send_user "\nmatch8\n"}
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sreport not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$not_support == 1} {
		send_user "\nWARNING: can not test without accounting enabled\n"
		exit 0
	}

	if {$matches != 2} {
		send_user "\nFAILURE: sreport -$soption failed ($matches)\n"
		set exit_code 1
	}
		return $matches
	}

	if { $soption == "-version" || $soption == "V" } {

	spawn $sreport -$soption
	expect {
		-re "SLURM accounting storage is disabled" {
			set not_support 1
			exp_continue
		}
		-re "slurm $number.$number.$number" {
			if {$debug} {send_user "\nmatch9\n"}
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sreport not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$not_support == 1} {
		send_user "\nWARNING: can not test without accounting enabled\n"
		exit 0
	}

	if {$matches != 1} {
		send_user "\nFAILURE: sreport -$soption failed ($matches)\n"
		set exit_code 1
	}
		return $matches
	}
}

################################################################

set matches [sreport_opt h ]
if {$matches != 3} {
	send_user "\nFAILURE: sreport -h failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt -help ]
if {$matches != 3} {
	send_user "\nFAILURE: sreport --help failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt -usage ]
if {$matches != 3} {
	send_user "\nFAILURE: sreport --usage failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt n ]
if {$matches != 0} {
	send_user "\nFAILURE: sreport -n failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt -noheader ]
if {$matches != 0} {
	send_user "\nFAILURE: sreport --noheader failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt p ]
if {$matches != 2} {
	send_user "\nFAILURE: sreport -p failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt -parsable ]
if {$matches != 2} {
	send_user "\nFAILURE: sreport --parsable failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt P ]
if {$matches != 2} {
	send_user "\nFAILURE: sreport -P failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt -parsable2 ]
if {$matches != 2} {
	send_user "\nFAILURE: sreport --parsable2 failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt V ]
if {$matches != 1} {
	send_user "\nFAILURE: sreport -V failed ($matches)\n"
	set exit_code 1
}

set matches [sreport_opt -version ]
if {$matches != 1} {
	send_user "\nFAILURE: sreport --verbose failed ($matches)\n"
	set exit_code 1
}

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