#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of srun exit code reporting
############################################################################
# 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 exit_script    "$test_dir/script.exit.bash"
set test_script    "$test_dir/script.bash"
set task_cnt       2

#
# Delete left-over scripts and build new ones
#
# POE BUG: If the application exits immediately then pmdv12 hangs until
# slurm kills it with a timeout, thus the sleep below is required:
# F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN TTY   TIME CMD
# 4 R  1001 17141 17137 99  80   0 - 33410 ?    ?  00:00:02 pmdv12
# 0 Z  1001 17152 17141  0  80   0 -     0 exit ?  00:00:00 test1.33.exit.b <defunct>
#
make_bash_script $exit_script "
  $bin_sleep 2
  RC=`expr \$SLURM_PROCID + 10`
  exit \$RC
"

make_bash_script $test_script "
  $srun -N1 -n $task_cnt -O -t1 $exit_script
  echo srun_exit_code_\$?
"

#
# Spawn program and check for exit code messages from srun
#
set sum 0
set myexit 0
set timeout $max_job_delay
spawn $srun -N1 -n $task_cnt -O -t1 $exit_script
expect {
	-re "exit code ($number)" {
		log_debug "This error is expected, no worries"
		incr sum $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}

if {$sum != 21} {
	fail "srun failed to report exit code ($sum != 21) $myexit"
}

#
# Spawn program to check the exit code of srun itself
#
set matches 0
spawn $test_script
expect {
	-re "srun_exit_code_11" {
		log_debug "Above error are expected, no worries"
		incr matches
		exp_continue
	}
	eof {
		wait
	}
}

subtest {$matches == 1} "Verify correct exit code from srun"
