#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Verify that auth/munge credential is properly validated.
#
# 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) 2012 SchedMD LLC.
# Written by Morris Jette <jette@schedmd.com>
#
# This file is part of SLURM, a resource management program.
# For details, see <http://www.schedmd.com/slurmdocs/>.
# 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.16"
set exit_code   0

print_header $test_id


#
# Check if we have auth/munge configured
#
log_user 0
set auth_munge 0
spawn $scontrol show config
expect {
	-re "AuthType *= auth/munge" {
		set auth_munge 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
log_user 1
if {$auth_munge == 0} {
	send_user "\nWARNING: not running auth/munge, test is not applicable\n"
	exit $exit_code
}

#
# Try to get allocation with bad credential
#
set env(SLURM_MUNGE_AUTH_FAIL_TEST) 1
set job_id 0
set salloc_pid [spawn $salloc -N1 $bin_bash]
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
		send "exit\r"
		exp_continue
	}
	-re "error" {
		send_user "\nError is expected, no worries.\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: salloc not responding\n"
		if {$job_id != 0} {
			cancel_job $job_id
		}
		slow_kill [expr 0 - $salloc_pid]
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id != 0} {
	send_user "\FAILURE: allocation granted with bad Munge credential\n"
	exit 1
}

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

