#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Basic sattach functionality test (--layout, --verbose, --label
#          and --output-filter options).
#
# 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-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://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     "16.4"
set exit_code   0
set file_prog   "test$test_id.prog"
set job_id      0
set matches     0

print_header $test_id

if { [string compare [test_launch_type] "slurm"] } {
	send_user "\nWARNING: This test is only compatible with systems using launch/slurm\n"
	exit $exit_code
}

set node_cnt 1-4
set task_cnt 4

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $file_prog
exec $bin_cc -O -o $file_prog ${file_prog}.c
exec $bin_chmod 700 $file_prog

#
# Spawn initial program via srun
#
set timeout $max_job_delay
set salloc_pid [spawn $salloc -N $node_cnt -t2 $srun -n $task_cnt --overcommit ./$file_prog]
set init_id $spawn_id
expect {
        -i $init_id
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "WAITING" {
		incr matches
		if {$matches != $task_cnt} {
			exp_continue
		}
	}
	timeout {
		send_user "\nFAILURE: salloc not responding\n"
		if {$job_id != 0} {
			cancel_job $job_id
		}
		slow_kill [expr 0 - $salloc_pid]
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job submit failure\n"
	exit 1
}
if {$matches != $task_cnt} {
	send_user "\nFAILURE: job run time failure\n"
	exit 1
}

# Wait for startup to complete (including RESPONSE_LAUNCH_TASKS message to srun)
# before attempting to attach
sleep 0.1

#
# Get task layout information
#
set matches     0
spawn $sattach --layout $job_id.0
set attach_id $spawn_id
expect {
	-i $attach_id
	-re "($number) tasks, ($number) nodes" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sattach not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches == 0} {
	send_user "\nFAILURE: layout information not printed\n"
	set exit_code 1
}

#
# Attach to initial program, just get one tasks output
#
set matches     0
set timeout     10
set attach_pid [spawn $sattach -l --output-filter=[expr $task_cnt - 1] $job_id.0]
set attach_id $spawn_id
expect {
        -i $attach_id
	-re "($number): WAITING" {
		if {$expect_out(1,string) != [expr $task_cnt - 1]} {
			send_user "\nFAILURE: output filtering by task failed\n"
			set exit_code 1
		} else {
			incr matches
			exec $bin_kill -KILL $attach_pid
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sattach not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches == 0} {
	send_user "\nFAILURE: failed to filter task output\n"
	set exit_code 1
}

#
# Attach to initial program
#
set matches     0
set timeout     10
spawn $sattach -vv -l $job_id.0
set attach_id $spawn_id
expect {
        -i $attach_id
	-re "verbose *: 2" {
		incr matches
		exp_continue
	}
	-re "($number): WAITING" {
		incr matches
		if {$matches == [expr $task_cnt + 1]} {
			send -i $attach_id "exit\r"
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sattach not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != [expr $task_cnt + 1]} {
	send_user "\nFAILURE: job run time failure ($matches != [expr $task_cnt + 1])\n"
	set exit_code 1
}

#
# Make sure initial program terminates too
#
# Explicitly reset spawn_id for wait call
set spawn_id $init_id
expect {
	timeout {
		send_user "\nFAILURE: srun (terminate) not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_prog
	send_user "\nSUCCESS\n"
} else {
	cancel_job $job_id
}
exit $exit_code
