1#!/usr/bin/env expect
2############################################################################
3# Purpose: Test of Slurm functionality
4#          Test srun handling of SIGINT to get task status or kill the job
5#          (--quit-on-interrupt option).
6############################################################################
7# Copyright (C) 2002-2007 The Regents of the University of California.
8# Copyright (C) 2008 Lawrence Livermore National Security.
9# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
10# Written by Morris Jette <jette1@llnl.gov>
11# CODE-OCEC-09-009. All rights reserved.
12#
13# This file is part of Slurm, a resource management program.
14# For details, see <https://slurm.schedmd.com/>.
15# Please also read the included file: DISCLAIMER.
16#
17# Slurm is free software; you can redistribute it and/or modify it under
18# the terms of the GNU General Public License as published by the Free
19# Software Foundation; either version 2 of the License, or (at your option)
20# any later version.
21#
22# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
23# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
25# details.
26#
27# You should have received a copy of the GNU General Public License along
28# with Slurm; if not, write to the Free Software Foundation, Inc.,
29# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
30############################################################################
31source ./globals
32
33set exit_code   0
34set file_in     "test$test_id.input"
35set step_id	0
36
37#
38# Build input script file
39#
40make_bash_script $file_in "
41  trap \"\" INT
42  $bin_echo WAITING
43  $bin_sleep 1000
44"
45
46#
47# Get uid
48#
49set uid [get_my_uid]
50
51#
52# Spawn initial program via srun and use SIGINT to status
53# Note: For systems supporting proper pthreads use
54#       exec $bin_kill -INT $srun_pid
55#	otherwise use
56#	exec $bin_pkill -INT -n -u $uid srun
57#
58set timeout $max_job_delay
59set match_int  0
60set match_term 0
61set match_wait 0
62set job_id  0
63set srun_pid [spawn $srun -v -N1 -t1 --unbuffered ./$file_in]
64expect {
65	-re "launching ($number)\\.$step_id" {
66		set job_id $expect_out(1,string)
67		exp_continue
68	}
69	-re "WAITING" {
70		set match_wait 1
71		# make sure the task gets started
72		sleep 1
73		exec $bin_kill -INT $srun_pid
74		exp_continue
75	}
76	-re "srun: interrupt" {
77		set match_int 1
78		cancel_job $job_id
79		exp_continue
80	}
81	-re "Force Terminated job" {
82		set match_term 1
83		exp_continue
84	}
85	timeout {
86		slow_kill $srun_pid
87		fail "srun not responding"
88	}
89	eof {
90		wait
91	}
92}
93if {[expr $match_wait + $match_int + $match_term] != 3} {
94	log_error "srun failed to properly process SIGINT, ($match_wait + $match_int + $match_term != 3)"
95	set exit_code 1
96}
97if {$exit_code == 0} {
98	log_debug "So far, so good"
99} else {
100	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
101}
102
103#
104# Spawn initial program via srun and use SIGINT to kill
105# Note: For systems supporting proper pthreads, instead use
106#       exec $bin_kill -INT $srun_pid
107#
108set match_int  0
109set match_term 0
110set match_wait 0
111set job_id  0
112set srun_pid [spawn $srun -v -N1 -t1 --unbuffered --quit-on-interrupt ./$file_in]
113expect {
114	-re "launching ($number)\\.$step_id" {
115		set job_id $expect_out(1,string)
116		exp_continue
117	}
118	-re "WAITING" {
119		set match_wait 1
120		exec $bin_kill -INT $srun_pid
121		exp_continue
122	}
123	-re "srun: interrupt" {
124		set match_int 1
125		exp_continue
126	}
127	-re "srun: task 0: running" {
128		set match_int 1
129		exp_continue
130	}
131	-re "srun: sending Ctrl-C" {
132		set match_term 1
133		exp_continue
134	}
135	timeout {
136		slow_kill $srun_pid
137		fail "srun not responding"
138	}
139	eof {
140		wait
141	}
142}
143cancel_job $job_id
144if {[expr $match_wait + $match_int + $match_term] != 2} {
145	log_error "srun failed to properly process SIGINT, ($match_wait + $match_int + $match_term != 2)"
146	set exit_code 1
147} elseif {$match_int != 0} {
148	log_error "srun failed to properly process SIGINT, ($match_int != 0)"
149	set exit_code 1
150}
151
152#
153# Post-processing
154#
155if {$exit_code == 0} {
156	exec $bin_rm -f $file_in
157} else {
158	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
159}
160