#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of TotalView operation with srun, with and without bulk
#          transfer.
############################################################################
# Copyright (C) 2004-2007 The Regents of the University of California.
# Copyright (C) 2008-2011 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov> and Dong Ang <dahn@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.
#
# In addition, as a special exception, the copyright holders give permission
# to link the code of portions of this program with the OpenSSL library under
# certain conditions as described in each individual source file, and
# distribute linked combinations including the two. You must obey the GNU
# General Public License in all respects for all of the code used other than
# OpenSSL. If you modify file(s) with this exception, you may extend this
# exception to your version of the file(s), but you are not obligated to do
# so. If you do not wish to do so, delete this exception statement from your
# version.  If you delete this exception statement from all source files in
# the program, then also delete it here.
#
# 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_prog     "$test_name.prog"

# Note this appears as a single argv value to totalviewcli
set bulk "dset TV::bulk_launch_enabled true; dset TV::bulk_launch_tmpfile1_host_lines %R; dset TV::bulk_launch_string {$srun --jobid=%J -N%N -n%N -w`awk -F. \'BEGIN {ORS=\",\"} {if (NR==%N) ORS=\"\"; print \$1}\' %t1` -l --input=none %B/tvdsvr%K -callback_host %H -callback_ports %L -set_pws %P -verbosity %V -working_directory %D %F}"
set no_bulk       "dset TV::bulk_launch_enabled false"

#
# Put desired Slurm install directory at head of search path for bulk launch
# command to work (runs "srun" without path)
#
set env(PATH) "$slurm_dir/bin:$env(PATH)"
log_debug "$env(PATH)"

#
# Test for existence of mpi compiler and totalview
#
if {[info exists mpicc] == 0} {
	skip "mpicc not defined, can't perform mpi testing"
}
if {[file executable $mpicc] == 0} {
	skip "$mpicc does not exist"
}
if {[info exists totalviewcli] == 0} {
	skip "totalviewcli not defined, can't perform mpi testing"
}
if {[file executable $totalviewcli] == 0} {
	skip "$totalviewcli does not exist"
}
if {[get_config_param "FrontendName"] ne "MISSING"} {
    skip "This test is incompatible with front-end systems"
}

proc cleanup {} {
	global bin_rm test_prog

	exec $bin_rm -f $test_prog ${test_prog}.o ${test_prog}.TVD.v3breakpoints ${test_prog}.TVD.v3breakpoints~
}

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $test_prog ${test_prog}.o ${test_prog}.TVD.v3breakpoints
exec $mpicc -o $test_prog ${test_prog}.c

#
# Now run totalviewcli (command line interface to TotalView)
#
set matches 0
set no_capability 0
set timeout $max_job_delay
log_info "======================================================================"
log_info "=======================  Run without bulk transfer ==================="
log_info "======================================================================"
spawn $totalviewcli -verbosity info -e $no_bulk $srun -a --input=none -n4 -N1-2 -O -t1 $test_prog
expect {
	-re "All licenses are currently in use" {
		log_warn "We can not run this test now"
		incr no_capability
		exp_continue
	}
	-re "d1.<>" {
		if {$matches == 0} {
			incr matches
			send "G\r"
		}
		if {$no_capability != 0} {
			send "quit\r"
		}
		exp_continue
	}
	-re "cannot open shared object" {
		log_warn "Set LD_LIBRARY_PATH environment variable to include this object's directory"
		exp_continue
	}
	-re "Do you want to stop the job now?.*:" {
		incr matches
		send "yes\r"
		exp_continue
	}
	-re "Attached to parallel task ($number)" {
		incr matches
		exp_continue
	}
	-re "Loaded MPI support.*" {
		exec sleep 2
		send "G\r"
		exp_continue
	}
	-re "I just received msg from Rank" {
		incr matches
		exp_continue
	}
	-re "Process 1 has exited.*" {
		incr matches
		exec sleep 2
		send "quit\r"
		exp_continue
	}
	-re "Do you really wish to exit TotalView?" {
		incr matches
		send "yes\r"
		exp_continue
	}
	-re "Could not open breakpoint file" {
		log_debug "NOTE: Breakpoint file error is expected due to known Totalview bug"
		exp_continue
	}
	timeout {
		fail "totalviewcli not responding"
	}
	eof {
		wait
	}
}
if {$no_capability != 0} {
	skip "Unable to run test with present configuration"
}
if {$matches != 12} {
	fail "totalviewcli operation matches $matches of 12. Remove your ~/.totalview directory and try again"
}

#
# Now run totalviewcli (command line interface to TotalView)
#
set matches 0
set no_capability 0
log_info "======================================================================"
log_info "=====================  Run with bulk transfer ========================"
log_info "======================================================================"
spawn $totalviewcli -verbosity info -e $bulk $srun -a --input=none -n4 -N1-2 -O -t1 $test_prog
expect {
	-re "All licenses are currently in use" {
		log_warn "We can not run this test now"
		incr no_capability
		exp_continue
	}
	-re "d1.<>" {
		if {$matches == 0} {
			incr matches
			send "G\r"
		}
		if {$no_capability != 0} {
			send "quit\r"
		}
		exp_continue
	}
	-re "cannot open shared object" {
		log_warn "Set LD_LIBRARY_PATH environment variable to include this object's directory"
		exp_continue
	}
	-re "Do you want to stop the job now?.*:" {
		incr matches
		send "yes\r"
		exp_continue
	}
	-re "Attached to parallel task ($number)" {
		incr matches
		exp_continue
	}
	-re "Loaded MPI support.*" {
		exec sleep 2
		send "G\r"
		exp_continue
	}
	-re "I just received msg from Rank" {
		incr matches
		exp_continue
	}
	-re "Process 1 has exited.*" {
		incr matches
		exec sleep 2
		send "quit\r"
		exp_continue
	}
	-re "Do you really wish to exit TotalView?" {
		incr matches
		send "yes\r"
		exp_continue
	}
	-re "Could not open breakpoint file" {
		log_debug "Breakpoint file error is expected due to known Totalview bug"
		exp_continue
	}
	timeout {
		fail "totalviewcli not responding"
	}
	eof {
		wait
	}
}
if {$no_capability != 0} {
	skip "Unable to run test with present configuration"
}
if {$matches != 12} {
	fail "totalviewcli operation matches $matches of 12. Remove your ~/.totalview directory and try again"
}
