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

if {[get_config_param "LaunchType"] ne "launch/slurm"} {
	skip "This test is only compatible with systems using launch/slurm"
}

proc cleanup {} {
	global job_id file_prog

	cancel_job $job_id
	file delete $file_prog
}

set node_cnt 1-4
set task_cnt 4

#
# Delete left-over program and rebuild it
#
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
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 {
		fail "salloc not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "Job submit failure"
}
if {$matches != $task_cnt} {
	fail "Job run time failure"
}

# 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 {
		fail "sattach not responding"
	}
	eof {
		wait
	}
}
if {$matches == 0} {
	fail "Layout information not printed"
}

#
# 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]} {
			fail "Output filtering by task failed"
		} else {
			incr matches
			exec $bin_kill -KILL $attach_pid
		}
		exp_continue
	}
	timeout {
		fail "sattach not responding"
	}
	eof {
		wait
	}
}
if {$matches == 0} {
	fail "Failed to filter task output"
}

#
# 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 {
		fail "sattach not responding"
	}
	eof {
		wait
	}
}
if {$matches != [expr $task_cnt + 1]} {
	fail "Job run time failure ($matches != [expr $task_cnt + 1])"
}

#
# Make sure initial program terminates too
#
# Explicitly reset spawn_id for wait call
set spawn_id $init_id
expect {
	timeout {
		fail "srun not terminating"
	}
	eof {
		wait
	}
}
