1
# Copyright (C) 2006, 2007, 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Blackbox tests for debugger breakin"""
38
class TestBreakin(tests.TestCase):
39
# FIXME: If something is broken, these tests may just hang indefinitely in
40
# wait() waiting for the child to exit when it's not going to.
43
super(TestBreakin, self).setUp()
44
if breakin.determine_signal() is None:
45
raise tests.TestSkipped('this platform is missing SIGQUIT'
47
if sys.platform == 'win32':
48
# Windows doesn't have os.kill, and we catch the SIGBREAK signal.
49
# We trigger SIGBREAK via a Console api so we need ctypes to access
52
raise tests.UnavailableFeature('ctypes')
53
self._send_signal = self._send_signal_win32
55
self._send_signal = self._send_signal_via_kill
57
def _send_signal_via_kill(self, pid, sig_type):
59
sig_num = signal.SIGQUIT
61
sig_num = signal.SIGKILL
63
raise ValueError("unknown signal type: %s" % (sig_type,))
66
def _send_signal_win32(self, pid, sig_type):
67
"""Send a 'signal' on Windows.
69
Windows doesn't really have signals in the same way. All it really
71
1) Sending SIGINT to the *current* process group (so self, and all
73
2) Sending SIGBREAK to a process that shares the current console,
74
which can be in its own process group.
75
So we have start_bzr_subprocess to create a new process group for the
76
spawned process, and then we map
77
SIGQUIT to GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT)
78
SIGKILL to TerminateProcess
80
if sig_type == 'break':
83
ret = ctypes.windll.kernel32.GenerateConsoleCtrlEvent(
84
CTRL_BREAK_EVENT, pid)
86
err = ctypes.FormatError()
87
raise RuntimeError('failed to send CTRL_BREAK: %s'
89
elif sig_type == 'kill':
90
# Does the exit code matter? For now we are just setting it to
91
# something other than 0
92
exit_code = breakin.determine_signal()
93
ctypes.windll.kernel32.TerminateProcess(pid, exit_code)
95
def _popen(self, *args, **kwargs):
96
if sys.platform == 'win32':
97
CREATE_NEW_PROCESS_GROUP = 512
98
# This allows us to send a signal to the child, *without* also
99
# sending it to ourselves
100
kwargs['creationflags'] = CREATE_NEW_PROCESS_GROUP
101
return super(TestBreakin, self)._popen(*args, **kwargs)
103
def _dont_SIGQUIT_on_darwin(self):
104
if sys.platform == 'darwin':
105
# At least on Leopard and with python 2.6, this test will raise a
106
# popup window asking if the python failure should be reported to
107
# Apple... That's not the point of the test :) Marking the test as
108
# not applicable Until we find a way to disable that intrusive
109
# behavior... --vila20080611
110
raise tests.TestNotApplicable(
111
'%s raises a popup on OSX' % self.id())
113
def _wait_for_process(self, pid, sig=None):
114
# We don't know quite how long waiting for the process 'pid' will take,
115
# but if it's more than 10s then it's probably not going to work.
119
self._send_signal(pid, sig)
120
# Use WNOHANG to ensure we don't get blocked, doing so, we may
121
# leave the process continue after *we* die...
122
# Win32 doesn't support WNOHANG, so we just pass 0
123
opts = getattr(os, 'WNOHANG', 0)
125
# note: waitpid is different on win32, but this test only runs
127
# TODO: waitpid doesn't work well on windows, we might consider
128
# using WaitForSingleObject(proc._handle, TIMEOUT)
129
# instead. Most notably, the WNOHANG isn't allowed, so
130
# this can hang indefinitely.
131
pid_killed, returncode = os.waitpid(pid, opts)
132
if (pid_killed, returncode) != (0, 0):
134
# high bit in low byte says if core was dumped; we
136
status, sig = (returncode >> 8, returncode & 0x7f)
139
if e.errno in (errno.ECHILD, errno.ESRCH):
140
# The process doesn't exist anymore
147
# port 0 means to allocate any port
148
_test_process_args = ['serve', '--port', 'localhost:0']
150
def test_breakin(self):
151
# Break in to a debugger while bzr is running
152
# we need to test against a command that will wait for
153
# a while -- bzr serve should do
154
proc = self.start_bzr_subprocess(self._test_process_args,
155
env_changes=dict(BZR_SIGQUIT_PDB=None))
156
# wait for it to get started, and print the 'listening' line
157
proc.stderr.readline()
158
# first sigquit pops into debugger
159
self._send_signal(proc.pid, 'break')
160
# Wait for the debugger to acknowledge the signal reception
161
# Note that it is possible for this to deadlock if the child doesn't
162
# acknowlege the signal and write to stderr. Perhaps we should try
163
# os.read(proc.stderr.fileno())?
164
err = proc.stderr.readline()
165
self.assertContainsRe(err, r'entering debugger')
166
# Now that the debugger is entered, we can ask him to quit
167
proc.stdin.write("q\n")
168
# We wait a bit to let the child process handles our query and avoid
169
# triggering deadlocks leading to hangs on multi-core hosts...
170
dead, sig = self._wait_for_process(proc.pid)
172
# The process didn't finish, let's kill it before reporting failure
173
dead, sig = self._wait_for_process(proc.pid, 'kill')
175
raise tests.KnownFailure(
176
"subprocess wasn't terminated, it had to be killed")
178
self.fail("subprocess %d wasn't terminated by repeated SIGKILL",
181
def test_breakin_harder(self):
182
"""SIGQUITting twice ends the process."""
183
self._dont_SIGQUIT_on_darwin()
184
proc = self.start_bzr_subprocess(self._test_process_args,
185
env_changes=dict(BZR_SIGQUIT_PDB=None))
186
# wait for it to get started, and print the 'listening' line
187
proc.stderr.readline()
188
# break into the debugger
189
self._send_signal(proc.pid, 'break')
190
# Wait for the debugger to acknowledge the signal reception (since we
191
# want to send a second signal, we ensure it doesn't get lost by
192
# validating the first get received and produce its effect).
193
err = proc.stderr.readline()
194
self.assertContainsRe(err, r'entering debugger')
195
dead, sig = self._wait_for_process(proc.pid, 'break')
196
self.assertTrue(dead)
197
# Either the child was dead before we could read its status, or the
198
# child was dead from the signal we sent it.
199
self.assertTrue(sig in (None, breakin.determine_signal()))
201
def test_breakin_disabled(self):
202
self._dont_SIGQUIT_on_darwin()
203
proc = self.start_bzr_subprocess(self._test_process_args,
204
env_changes=dict(BZR_SIGQUIT_PDB='0'))
205
# wait for it to get started, and print the 'listening' line
206
proc.stderr.readline()
207
# first hit should just kill it
208
self._send_signal(proc.pid, 'break')