#!/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>
# CODE-OCEC-09-009. All rights reserved.
#
# 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     "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 {[test_cray]} {
	send_user "\nWARNING: This test is incompatible with Cray systems\n"
	exit $exit_code
} elseif { [test_xcpu] } {
	send_user "\nWARNING: This test is incompatible with XCPU systems\n"
	exit 0
} elseif {[test_launch_poe]} {
	send_user "\nWARNING: This test is incompatible with POE systems\n"
	exit $exit_code
}

if { [test_bluegene] } {
	if { [test_emulated] } {
		set node_cnt 1
		set task_cnt 1
	} else {
		set node_cnt 1-4
		set task_cnt 4
	}
} else {
	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
}

#
# 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
