1
# Copyright (C) 2006, 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
"""Blackbox tests for debugger breakin"""
 
 
25
from bzrlib import tests
 
 
28
class TestBreakin(tests.TestCase):
 
 
29
    # FIXME: If something is broken, these tests may just hang indefinitely in
 
 
30
    # wait() waiting for the child to exit when it's not going to.
 
 
33
        if sys.platform == 'win32':
 
 
34
            raise tests.TestSkipped('breakin signal not tested on win32')
 
 
35
        super(TestBreakin, self).setUp()
 
 
37
    def _dont_SIGQUIT_on_darwin(self):
 
 
38
        if sys.platform == 'darwin':
 
 
39
            # At least on Leopard and with python 2.6, this test will raise a
 
 
40
            # popup window asking if the python failure should be reported to
 
 
41
            # Apple... That's not the point of the test :) Marking the test as
 
 
42
            # not applicable Until we find a way to disable that intrusive
 
 
43
            # behavior... --vila20080611
 
 
44
            raise tests.TestNotApplicable(
 
 
45
                '%s raises a popup on OSX' % self.id())
 
 
47
    # port 0 means to allocate any port
 
 
48
    _test_process_args = ['serve', '--port', 'localhost:0']
 
 
50
    def test_breakin(self):
 
 
51
        # Break in to a debugger while bzr is running
 
 
52
        # we need to test against a command that will wait for 
 
 
53
        # a while -- bzr serve should do
 
 
54
        proc = self.start_bzr_subprocess(self._test_process_args,
 
 
55
                env_changes=dict(BZR_SIGQUIT_PDB=None))
 
 
56
        # wait for it to get started, and print the 'listening' line
 
 
57
        proc.stdout.readline()
 
 
58
        # first sigquit pops into debugger
 
 
59
        os.kill(proc.pid, signal.SIGQUIT)
 
 
60
        proc.stdin.write("q\n")
 
 
62
        err = proc.stderr.readline()
 
 
63
        self.assertContainsRe(err, r'entering debugger')
 
 
65
    def test_breakin_harder(self):
 
 
66
        self._dont_SIGQUIT_on_darwin()
 
 
67
        proc = self.start_bzr_subprocess(self._test_process_args,
 
 
68
                env_changes=dict(BZR_SIGQUIT_PDB=None))
 
 
69
        # wait for it to get started, and print the 'listening' line
 
 
70
        proc.stdout.readline()
 
 
71
        # break into the debugger
 
 
72
        os.kill(proc.pid, signal.SIGQUIT)
 
 
73
        # now send a second sigquit, which should cause it to exit.  That
 
 
74
        # won't happen until the original signal has been noticed by the
 
 
75
        # child and it's run its signal handler.  We don't know quite how long
 
 
76
        # this will take, but if it's more than 10s then it's probably not
 
 
80
            os.kill(proc.pid, signal.SIGQUIT)
 
 
81
            # note: waitpid is different on win32, but this test only runs on
 
 
83
            r = os.waitpid(proc.pid, os.WNOHANG)
 
 
85
                # high bit says if core was dumped; we don't care
 
 
86
                self.assertEquals(r[1] & 0x7f, signal.SIGQUIT)
 
 
89
            self.fail("subprocess wasn't terminated by repeated SIGQUIT")
 
 
91
    def test_breakin_disabled(self):
 
 
92
        self._dont_SIGQUIT_on_darwin()
 
 
93
        proc = self.start_bzr_subprocess(self._test_process_args,
 
 
94
                env_changes=dict(BZR_SIGQUIT_PDB='0'))
 
 
95
        # wait for it to get started, and print the 'listening' line
 
 
96
        proc.stdout.readline()
 
 
97
        # first hit should just kill it
 
 
98
        os.kill(proc.pid, signal.SIGQUIT)