~bzr-pqm/bzr/bzr.dev

5387.2.7 by John Arbash Meinel
Merge bzr.dev 5444 to resolve some small text conflicts.
1
# Copyright (C) 2007-2010 Canonical Ltd
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
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
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
16
17
"""Tests for the strace-invoking support."""
18
2386.1.3 by John Arbash Meinel
import errno in test_strace.py (recommended by Ian Clatworthy)
19
import errno
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
20
import subprocess
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
21
import threading
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
22
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
23
from bzrlib import (
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
24
    strace,
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
25
    tests,
26
    )
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
27
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
28
29
30
class TestStrace(tests.TestCaseWithTransport):
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
31
32
    _test_needs_features = [StraceFeature]
33
5398.2.3 by Martin Pool
Just skip the strace tests
34
    def setUp(self):
35
        # NB: see http://pad.lv/626679 and
36
        # <https://code.launchpad.net/~mbp/bzr/626679-strace/+merge/34157>:
37
        # testing strace by connecting to ourselves has repeatedly caused
38
        # hangs in running the test suite; these are fixable given enough
39
        # determination but given that strace is not used by any other tests
40
        # at the moment and that it's only test-support code, we just leave it
41
        # untested -- mbp 20100901
42
        raise tests.TestSkipped("strace selftests are broken and disabled")
43
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
44
    def _check_threads(self):
3406.1.3 by Vincent Ladeuil
Fixed as par John's review.
45
        # For bug #226769, it was decided that the strace tests should not be
46
        # run when more than one thread is active. A lot of tests are currently
47
        # leaking threads for good or bad reasons, once they are fixed or
48
        # strace itself is fixed (bug #103133), we can get rid of the
49
        # restriction.
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
50
        active = threading.activeCount()
51
        if active > 1: # There is always the main thread at least
52
            raise tests.KnownFailure(
53
                '%d active threads, bug #103133 needs to be fixed.' % active)
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
54
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
55
    def strace_detailed_or_skip(self, *args, **kwargs):
56
        """Run strace, but cope if it's not allowed"""
57
        try:
58
            return strace_detailed(*args, **kwargs)
59
        except strace.StraceError, e:
60
            if e.err_messages.startswith(
61
                    "attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted"):
62
                raise tests.TestSkipped("ptrace not permitted")
63
            else:
64
                raise
65
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
66
    def test_strace_callable_is_called(self):
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
67
        self._check_threads()
68
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
69
        output = []
70
        def function(positional, *args, **kwargs):
71
            output.append((positional, args, kwargs))
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
72
        self.strace_detailed_or_skip(
73
            function, ["a", "b"], {"c": "c"},
74
            follow_children=False)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
75
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
76
2367.1.9 by Robert Collins
Review feedback.
77
    def test_strace_callable_result(self):
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
78
        self._check_threads()
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
79
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
80
        def function():
2367.1.9 by Robert Collins
Review feedback.
81
            return "foo"
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
82
        result, strace_result = self.strace_detailed_or_skip(function,[], {},
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
83
                                                follow_children=False)
2367.1.9 by Robert Collins
Review feedback.
84
        self.assertEqual("foo", result)
85
        self.assertIsInstance(strace_result, StraceResult)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
86
87
    def test_strace_result_has_raw_log(self):
88
        """Checks that a reasonable raw strace log was found by strace."""
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
89
        self._check_threads()
90
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
91
        def function():
92
            self.build_tree(['myfile'])
5398.2.1 by Martin Pool
Cope with strace not allowing you to attach to your own processes
93
        unused, result = self.strace_detailed_or_skip(function, [], {},
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
94
                                         follow_children=False)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
95
        self.assertContainsRe(result.raw_log, 'myfile')