#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          validates that srun exit code matches that of a test program
############################################################################
# Copyright (C) 2013 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# 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_out     "test$test_id.testfile"
set file_c       "segv.c"
set script       "test$test_id.script"
set err_num_c    0
set err_num_srun 0
set exit_code    0

# Remove any remaining files
exec $bin_rm -f $file_out $script $file_c

exec $bin_echo "
int main(char **argv, int argc)
{
	char *tmp;
	tmp\[1000000\] = 3;
}" > $file_c

exec $bin_cc -std=c99 -o $file_out $file_c
exec $bin_chmod 700 $file_out

# Set core size ulimit to zero
make_bash_script $script "
ulimit -c 0
./$file_out
echo exit_code = $?
"

# Run script alone
set error_match 0
spawn ./$script
expect {

	-re "Segmentation fault" {
		log_debug "This error is expected do not worry"
		incr error_match
		exp_continue
	}
	-re "exit_code = ($number)" {
		set err_num_c $expect_out(1,string)
		incr error_match
	 }
	timeout {
		fail "Program is not responding"
	}
	eof {
		wait
	}
}

if {$error_match != 2} {
	log_error "Program did not return a segmentation fault ($error_match != 2)"
	set exit_code 1
}

# Run script with srun
# Note the "Segmentation fault" and "exit_code" messages can come in any order
# Expect seems to have some trouble exiting if the "exit_code" comes first
set error_match 0
set timeout $max_job_delay
spawn $srun -n1 -t1 ./$script
expect {
	-re "(Segmentation|exit_code = $err_num_c)" {
		incr error_match
		if {$error_match != 2} {
			exp_continue
		}
	}
	timeout {
		log_error "srun is not responding"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$error_match != 2} {
	fail "srun did not have a segmentation error when running the program ($error_match != 2)"
} else {
	log_debug "This error is expected"
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_out $script $file_c
} else {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
