#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of srun attach to existing job (--attach and --join 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.
#
# Note:    This script generates and then deletes files in the working directory
#          named test1.18.prog
############################################################################
# Copyright (C) 2002 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id     "1.18"
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
spawn $srun -N1 -v -t5 $file_prog
set init_id $spawn_id
expect {
        -i $init_id
	-re "launching ($number).0 on host" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "WAITING" {
		incr matches
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job submit failure\n"
	exit 1
}
if {$matches == 0} {
	send_user "\nFAILURE: job run time failure\n"
	exit 1
}

#
# Attach to initial program via srun
#
set matches     0
set timeout     10
spawn $srun --attach=$job_id --join
set attach_id $spawn_id
expect {
        -i $attach_id
	-re "WAITING" {
		incr matches
		send -i $attach_id "exit\n"
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$matches == 0} {
	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 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
