#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol update command for front end nodes.
#
# 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) 2002-2007 The Regents of the University of California.
# Copyright (C) 2008-2011 Lawrence Livermore National Security.
# 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 test_id        "3.12"
set authorized     1
set exit_code      0
set node_list      ""
set node_name      ""
set node_old_state ""
set node_new_state ""
set read_state     ""

print_header $test_id

set matches     0
set node_name   ""
spawn $scontrol show FrontendName
expect {
	-re "FrontendName=($alpha_numeric_under) State=ALLOCATED " {
		set node_name $expect_out(1,string)
		incr matches
		exp_continue
	}
	-re "FrontendName=($alpha_numeric_under) State=IDLE " {
		set node_name $expect_out(1,string)
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches < 1} {
	send_user "\nWARNING: no suitable front end nodes found\n"
	exit $exit_code
}


#
# Change that node's state
#
spawn $scontrol update FrontendName=$node_name State=DRAIN Reason=TESTING
expect {
	-re "slurm_update error: ($alpha_numeric_under) ($alpha_numeric_under)" {
		set access_err 0
		set err_msg1 $expect_out(1,string)
		set err_msg2 $expect_out(2,string)
		if {[string compare $err_msg1 "Invalid"] == 0} {
			set access_err 1
		}
		if {[string compare $err_msg2 "user"] == 0} {
			set access_err 1
		}
		if {$access_err == 1} {
			send_user "\nWARNING: user not authorized\n"
			exit $exit_code
		} else {
			set authorized 0
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Validate node's new state
#
set read_state  0
set reason_code ""
spawn $scontrol show FrontendName $node_name
expect {
	-re "State=($alpha_cap).DRAIN" {
		set read_state 1
		exp_continue
	}
	-re "Reason=($alpha_cap)" {
		set reason_code $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$authorized == 1} {
	if {$read_state != 1} {
		send_user "\nFAILURE: scontrol state change error\n"
		set exit_code 1
	}

	set reason_set 0
	if {[string compare $reason_code "TESTING"] == 0} {
		set reason_set 1
	}

	if {$reason_set != 1} {
		send_user "\nFAILURE: scontrol reason change error\n"
		set exit_code 1
	}
}

#
# Return that front end node's state to its old value
#
spawn $scontrol update FrontendName=$node_name State=RESUME
expect {
	-re "slurm_update error: Invalid user id" {
		exp_continue
	}
	-re "slurm_update error:" {
		send_user "\nFAILURE: scontrol update error\n"
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Record that front end node's state
#
set read_state 0
spawn  $scontrol show FrontendName $node_name
expect {
	-re "State=($alpha_cap).DRAIN" {
		set read_state 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$read_state != 0} {
	send_user "\nFAILURE: scontrol state change error\n"
	set exit_code 1
}

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