#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          strigger --set --reconfig
#
# 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) 2007 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 test_id      "19.4"
set exit_code    0
set file_in      "test$test_id.input"
set file_out     "test$test_id.output"

print_header $test_id

#
# get my uid and clear any vestigial triggers
#
set uid [stop_root_user]

exec $strigger --clear --quiet --user=$uid

#
# Build input script file
#
set cwd "[$bin_pwd]"
exec $bin_rm -f $file_in $file_out
make_bash_script $file_in "$bin_echo RECONFIG >$cwd/$file_out"

set disabled 0
set matches  0
set strigger_pid [spawn $strigger --set -v -offset=0 --reconfig --program=$cwd/$file_in]
expect {
	-re "permission denied" {
		set disabled 1
		exp_continue
	}
	-re "trigger set" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: strigger not responding\n"
		slow_kill $strigger_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$disabled == 1} {
	send_user "\nWARNING: Current configuration prevents setting triggers\n"
	send_user "         Need to run as SlurmUser or make SlurmUser=root\n"
	exec $bin_rm -f $file_in
	exit $exit_code
}
if {$matches == 0} {
	send_user "\nFAILURE: trigger creation failure\n"
	exit 1
}

set matches 0
set strigger_pid [spawn $strigger --get -v --reconfig --user=$uid]
expect {
	-re "$file_in" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: strigger not responding\n"
		slow_kill $strigger_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches == 0} {
	send_user "\nFAILURE: trigger get failure\n"
	set exit_code 1
}

#
# Try to trigger event
#
set invalid 0
spawn $scontrol reconfig
expect {
	-re "Invalid user" {
		set invalid 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$invalid == 0} {
#	Add delay for slurmctld to process triggers (every 15 secs)
	exec sleep 15

#	Plus time for cross-platform NSF delays
	if {[wait_for_file $file_out] != 0} {
		send_user "\nFAILURE: file $file_out is missing\n"
		set exit_code 1
	} else {
		set reconfig 0
		spawn $bin_cat $file_out
		expect {
			-re "RECONFIG" {
				set reconfig 1
				exp_continue
			}
			eof {
				wait
			}
		}
		if {$reconfig == 0} {
			send_user "\nFAILURE: file $file_out contents are bad\n"
			set exit_code 1
		}
	}
}

set strigger_pid [spawn $strigger --clear -v --reconfig --user=$uid]
expect {
	timeout {
		send_user "\nFAILURE: strigger not responding\n"
		slow_kill $strigger_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_in $file_out
	send_user "\nSUCCESS\n"
}
exit $exit_code

