#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of sched/wiki plugin. This is intended to execute in the
#          place of Maui and emulate its actions to confirm proper
#          operation of the plugin.
#
# 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) 2006 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://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     "7.8"
set exit_code   0
set file_in     "test$test_id.in"
set file_out    "test$test_id.out"
set test_prog   "test$test_id.prog"

print_header $test_id

#
# Check if we have sched/wiki configured
#
log_user 0
set conf_dir     ""
set control_addr ""
set sched_port   0
set sched_wiki   0
exec $bin_rm -f $file_in $file_out $test_prog
spawn $scontrol show config
expect {
	-re "ControlAddr *= ($alpha_numeric_under)" {
		set control_addr $expect_out(1,string)
		exp_continue
	}
	-re "SchedulerPort *= ($number)" {
		set sched_port $expect_out(1,string)
		exp_continue
	}
	-re "SchedulerType *= sched/wiki2" {
		set sched_wiki 0
		exp_continue
	}
	-re "SchedulerType *= sched/wiki" {
		set sched_wiki 1
		exp_continue
	}
	-re "SLURM_CONF *= (.*)/slurm.conf.*SLURM_VERSION" {
		set conf_dir $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
log_user 1
if {$sched_wiki == 0} {
	send_user "\nWARNING: not running sched/wiki, test is not applicable\n"
	exit $exit_code
}
if {[string compare $control_addr ""] == 0} {
	send_user "\nFAILURE: ControlAddr not set\n"
	exit 1
}
if {$sched_port == 0} {
	send_user "\nFAILURE: SchedulerPort = 0\n"
	exit 1
}

if { [test_bluegene] } {
	set is_bluegene 1
	set node_cnt "1-1024"
} else {
	set is_bluegene 0
	set node_cnt "1-2"
}

#
# Submit two jobs to work with
#
exec $bin_rm -f $file_out
make_bash_script $file_in "
  echo BEGIN
  $bin_sleep 20
  echo FINI
  exit 0"

set job_id1 0
set job_id2 0
set sbatch_pid [spawn $sbatch -N $node_cnt --output=$file_out --comment=test -t1 $file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id1 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		catch {exec $bin_kill -KILL $sbatch_pid}
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id1 == 0} {
	send_user "\nFAILURE: sbatch job submit failed\n"
	exit 1
}

set sbatch_pid [spawn $sbatch -N $node_cnt --output=$file_out --comment=test -t1 $file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id2 $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		catch {exec $bin_kill -KILL $sbatch_pid}
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id2 == 0} {
        send_user "\nFAILURE: srun job submit failed\n"
	cancel_job $job_id1
        exit 1
}


#
# Start our Moab emulator ($test_prog) to manage the job
#
exec $bin_rm -f $test_prog
exec $bin_make -f /dev/null $test_prog
set success 0
set moab_pid [spawn $test_prog $control_addr $job_id1 $job_id2 $sched_port $is_bluegene]
expect {
	-re "SUCCESS" {
		set success 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: Moab emulator not responding\n"
		catch {exec $bin_kill -KILL $moab_pid}
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$success == 0} {
	send_user "\nFAILURE: Moab emulator failed\n"
	set exit_code 1
}

if {[wait_for_job $job_id1 "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	set exit_code 1
}

cancel_job $job_id1
cancel_job $job_id2

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

