/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
1
# Copyright (C) 2006, 2007, 2009 Canonical Ltd
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
16
17
"""Blackbox tests for debugger breakin"""
18
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
19
try:
20
    import ctypes
21
    have_ctypes = True
22
except ImportError:
23
    have_ctypes = False
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
24
import errno
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
25
import os
26
import signal
27
import subprocess
28
import sys
29
import time
30
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
31
from bzrlib import (
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
32
    breakin,
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
33
    errors,
34
    tests,
35
    )
3815.2.3 by Martin Pool
merge fix for #293054, ssl on python2.6
36
37
38
class TestBreakin(tests.TestCase):
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
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.
41
42
    def setUp(self):
43
        super(TestBreakin, self).setUp()
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
44
        self.requireFeature(tests.BreakinFeature)
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
45
        if sys.platform == 'win32':
46
            self._send_signal = self._send_signal_win32
47
        else:
48
            self._send_signal = self._send_signal_via_kill
49
50
    def _send_signal_via_kill(self, pid, sig_type):
4578.1.2 by John Arbash Meinel
Fix a typo for the Linux code path.
51
        if sig_type == 'break':
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
52
            sig_num = signal.SIGQUIT
4578.1.2 by John Arbash Meinel
Fix a typo for the Linux code path.
53
        elif sig_type == 'kill':
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
54
            sig_num = signal.SIGKILL
55
        else:
56
            raise ValueError("unknown signal type: %s" % (sig_type,))
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
57
        try:
58
            os.kill(pid, sig_num)
59
        except OSError, e:
60
            if e.errno != errno.ESRCH:
61
                raise
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
62
63
    def _send_signal_win32(self, pid, sig_type):
64
        """Send a 'signal' on Windows.
65
66
        Windows doesn't really have signals in the same way. All it really
67
        supports is:
68
            1) Sending SIGINT to the *current* process group (so self, and all
69
                children of self)
70
            2) Sending SIGBREAK to a process that shares the current console,
71
                which can be in its own process group.
4578.1.4 by John Arbash Meinel
Fix some small comment bugs.
72
        So we have start_bzr_subprocess create a new process group for the
73
        spawned process (via a flag to Popen), and then we map
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
74
            SIGQUIT to GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT)
75
            SIGKILL to TerminateProcess
76
        """
77
        if sig_type == 'break':
78
            CTRL_BREAK_EVENT = 1
79
            # CTRL_C_EVENT = 0
80
            ret = ctypes.windll.kernel32.GenerateConsoleCtrlEvent(
81
                    CTRL_BREAK_EVENT, pid)
82
            if ret == 0: #error
83
                err = ctypes.FormatError()
84
                raise RuntimeError('failed to send CTRL_BREAK: %s'
85
                                   % (err,))
86
        elif sig_type == 'kill':
87
            # Does the exit code matter? For now we are just setting it to
88
            # something other than 0
89
            exit_code = breakin.determine_signal()
90
            ctypes.windll.kernel32.TerminateProcess(pid, exit_code)
91
92
    def _popen(self, *args, **kwargs):
93
        if sys.platform == 'win32':
94
            CREATE_NEW_PROCESS_GROUP = 512
95
            # This allows us to send a signal to the child, *without* also
96
            # sending it to ourselves
97
            kwargs['creationflags'] = CREATE_NEW_PROCESS_GROUP
98
        return super(TestBreakin, self)._popen(*args, **kwargs)
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
99
3815.2.3 by Martin Pool
merge fix for #293054, ssl on python2.6
100
    def _dont_SIGQUIT_on_darwin(self):
101
        if sys.platform == 'darwin':
102
            # At least on Leopard and with python 2.6, this test will raise a
103
            # popup window asking if the python failure should be reported to
104
            # Apple... That's not the point of the test :) Marking the test as
105
            # not applicable Until we find a way to disable that intrusive
106
            # behavior... --vila20080611
107
            raise tests.TestNotApplicable(
108
                '%s raises a popup on OSX' % self.id())
109
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
110
    def _wait_for_process(self, pid, sig=None, count=100):
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
111
        # We don't know quite how long waiting for the process 'pid' will take,
112
        # but if it's more than 10s then it's probably not going to work.
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
113
        for i in range(count):
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
114
            if sig is not None:
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
115
                self._send_signal(pid, sig)
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
116
            # Use WNOHANG to ensure we don't get blocked, doing so, we may
117
            # leave the process continue after *we* die...
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
118
            # Win32 doesn't support WNOHANG, so we just pass 0
119
            opts = getattr(os, 'WNOHANG', 0)
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
120
            try:
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
121
                # TODO: waitpid doesn't work well on windows, we might consider
122
                #       using WaitForSingleObject(proc._handle, TIMEOUT)
123
                #       instead. Most notably, the WNOHANG isn't allowed, so
124
                #       this can hang indefinitely.
125
                pid_killed, returncode = os.waitpid(pid, opts)
4676.2.1 by Vincent Ladeuil
Fix test_breakin failure.
126
                if pid_killed != 0 and returncode != 0:
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
127
                    if sig is not None:
128
                        # high bit in low byte says if core was dumped; we
129
                        # don't care
130
                        status, sig = (returncode >> 8, returncode & 0x7f)
131
                        return True, sig
132
            except OSError, e:
133
                if e.errno in (errno.ECHILD, errno.ESRCH):
134
                    # The process doesn't exist anymore
135
                    return True, None
136
                else:
137
                    raise
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
138
            if i + 1 != count:
139
                time.sleep(0.1)
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
140
141
        return False, None
142
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
143
    # port 0 means to allocate any port
144
    _test_process_args = ['serve', '--port', 'localhost:0']
145
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
146
    def test_breakin(self):
147
        # Break in to a debugger while bzr is running
148
        # we need to test against a command that will wait for
149
        # a while -- bzr serve should do
150
        proc = self.start_bzr_subprocess(self._test_process_args,
151
                env_changes=dict(BZR_SIGQUIT_PDB=None))
152
        # wait for it to get started, and print the 'listening' line
153
        proc.stderr.readline()
154
        # first sigquit pops into debugger
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
155
        self._send_signal(proc.pid, 'break')
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
156
        # Wait for the debugger to acknowledge the signal reception
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
157
        # Note that it is possible for this to deadlock if the child doesn't
158
        # acknowlege the signal and write to stderr. Perhaps we should try
159
        # os.read(proc.stderr.fileno())?
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
160
        err = proc.stderr.readline()
161
        self.assertContainsRe(err, r'entering debugger')
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
162
        # Try to shutdown cleanly;
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
163
        # Now that the debugger is entered, we can ask him to quit
164
        proc.stdin.write("q\n")
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
165
        # But we don't really care if it doesn't.
166
        dead, sig = self._wait_for_process(proc.pid, count=3)
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
167
        if not dead:
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
168
            # The process didn't finish, let's kill it.
4715.2.2 by Robert Collins
Up the count slightly to allow for PQM slowness.
169
            dead, sig = self._wait_for_process(proc.pid, 'kill', count=10)
4715.2.1 by Robert Collins
Stop caring about a clean shutdown of the process in tests for the interactive debugger break-in facility.
170
            if not dead:
171
                # process isn't gone, user will have to hunt it down and kill
172
                # it.
173
                self.fail("subprocess %d wasn't terminated by repeated SIGKILL" %
4168.1.4 by Vincent Ladeuil
More robust handling of test_breakin.
174
                          proc.pid)
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
175
4168.1.1 by Vincent Ladeuil
Commit fix in a new thread to handle review comments
176
    def test_breakin_harder(self):
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
177
        """SIGQUITting twice ends the process."""
4168.1.1 by Vincent Ladeuil
Commit fix in a new thread to handle review comments
178
        self._dont_SIGQUIT_on_darwin()
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
179
        proc = self.start_bzr_subprocess(self._test_process_args,
180
                env_changes=dict(BZR_SIGQUIT_PDB=None))
181
        # wait for it to get started, and print the 'listening' line
3955.1.7 by Jonathan Lange
Fix some tests that assumed the port was on stderr rather than stdout.
182
        proc.stderr.readline()
4168.1.1 by Vincent Ladeuil
Commit fix in a new thread to handle review comments
183
        # break into the debugger
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
184
        self._send_signal(proc.pid, 'break')
4168.1.2 by Vincent Ladeuil
Take Martin and John review comments into account and tighten the tests.
185
        # Wait for the debugger to acknowledge the signal reception (since we
186
        # want to send a second signal, we ensure it doesn't get lost by
187
        # validating the first get received and produce its effect).
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
188
        err = proc.stderr.readline()
189
        self.assertContainsRe(err, r'entering debugger')
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
190
        dead, sig = self._wait_for_process(proc.pid, 'break')
191
        self.assertTrue(dead)
192
        # Either the child was dead before we could read its status, or the
193
        # child was dead from the signal we sent it.
194
        self.assertTrue(sig in (None, breakin.determine_signal()))
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
195
196
    def test_breakin_disabled(self):
3815.2.3 by Martin Pool
merge fix for #293054, ssl on python2.6
197
        self._dont_SIGQUIT_on_darwin()
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
198
        proc = self.start_bzr_subprocess(self._test_process_args,
199
                env_changes=dict(BZR_SIGQUIT_PDB='0'))
200
        # wait for it to get started, and print the 'listening' line
3955.1.7 by Jonathan Lange
Fix some tests that assumed the port was on stderr rather than stdout.
201
        proc.stderr.readline()
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
202
        # first hit should just kill it
4578.1.1 by John Arbash Meinel
Update the breakin support to support CTRL-BREAK on Windows.
203
        self._send_signal(proc.pid, 'break')
2423.3.7 by Martin Pool
Add BZR_SIGQUIT_PDB=0 option to disable breakin.
204
        proc.wait()