#!/usr/bin/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>
# UCRL-CODE-217948.
# 
# This file is part of SLURM, a resource management program.
# For details, see <http://www.llnl.gov/linux/slurm/>.
#  
# 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

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $file_prog
exec $bin_make -f /dev/null $file_prog
exec $bin_chmod 700 $file_prog

#
# Spawn initial program via srun
#
set timeout $max_job_delay
set salloc_pid [spawn $salloc -N1-4 -t2 $slaunch -n4 --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 != 4} {
			exp_continue
		}
	}
	timeout {
		send_user "\nFAILURE: salloc not responding\n"
		if {$job_id == 0} {
			slow_kill $salloc_pid
		} else {
			cancel_job $job_id
		}
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job submit failure\n"
	exit 1
}
if {$matches != 4} {
	send_user "\nFAILURE: job run time failure\n"
	exit 1
}

#
# Get task layout information
#
set matches     0
spawn $sattach --layout $job_id.0
set attach_id $spawn_id
expect {
	-i $attach_id
	-re "4 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=2 $job_id.0]
set attach_id $spawn_id
expect {
        -i $attach_id
	-re "($number): WAITING" {
		if {$expect_out(1,string) != 2} {
			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 == 4} {
			send -i $attach_id "exit\n"
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sattach not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 5} {
	send_user "\nFAILURE: job run time failure\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
