#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test configuration file test logic ("slurmctld -t").
#
# 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) 2018 SchedMD LLC
#
# 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		"7.8"
set error_cnt		0
set exit_code		0
set result_code		-1
set result_msg		-1

spawn $slurmctld -t
expect {
	-re "error" {
		incr error_cnt
		exp_continue
	}
	-re "Succeeded configuration test" {
		set result_msg 0
		exp_continue
	}
	-re "FAILED configuration test" {
		set result_msg 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: slurmctld not responding\n"
		set exit_code 1
	}
	eof {
		lassign [wait] pid spawnid os_error_flag result_code
	}
}
if {$result_code == 0} {
	if {$result_msg != 0} {
		send_user "\nFAILURE: slurmctld exit code and message do not match\n"
		set exit_code 1
	}	
	if {$error_cnt != 0} {
		send_user "\nFAILURE: slurmctld failed to detect some configuration error\n"
		set exit_code 1
	}
}
if {$result_code != 0} {
	if {$result_msg != 1} {
		send_user "\nFAILURE: slurmctld exit code and message do not match\n"
		set exit_code 1
	}	
	if {$error_cnt == 0} {
		send_user "\nFAILURE: slurmctld appears to have detected some configuration error without error message reporting it\n"
		set exit_code 1
	} else {
		send_user "\nFAILURE: Does your configuration file have some error?\n"
		set exit_code 1
	}
}

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