~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_strace.py

  • Committer: Martin Packman
  • Date: 2011-12-23 19:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 6405.
  • Revision ID: martin.packman@canonical.com-20111223193822-hesheea4o8aqwexv
Accept and document passing the medium rather than transport for smart connections

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
1
# Copyright (C) 2007-2011 Canonical Ltd
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
13
12
#
14
13
# You should have received a copy of the GNU General Public License
15
14
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
16
 
18
17
"""Tests for the strace-invoking support."""
19
18
 
20
 
import errno
21
 
import subprocess
22
 
 
23
 
from bzrlib.strace import StraceFeature, strace, StraceResult
24
 
from bzrlib.tests import TestCaseWithTransport
25
 
 
26
 
 
27
 
class TestStraceFeature(TestCaseWithTransport):
28
 
 
29
 
    def test_strace_detection(self):
30
 
        """Strace is available if its runnable."""
 
19
import threading
 
20
 
 
21
from bzrlib import (
 
22
    strace,
 
23
    tests,
 
24
    )
 
25
from bzrlib.strace import strace_detailed, StraceResult
 
26
from bzrlib.tests.features import (
 
27
    strace_feature,
 
28
    )
 
29
 
 
30
 
 
31
class TestStrace(tests.TestCaseWithTransport):
 
32
 
 
33
    _test_needs_features = [strace_feature]
 
34
 
 
35
    def setUp(self):
 
36
        # NB: see http://pad.lv/626679 and
 
37
        # <https://code.launchpad.net/~mbp/bzr/626679-strace/+merge/34157>:
 
38
        # testing strace by connecting to ourselves has repeatedly caused
 
39
        # hangs in running the test suite; these are fixable given enough
 
40
        # determination but given that strace is not used by any other tests
 
41
        # at the moment and that it's only test-support code, we just leave it
 
42
        # untested -- mbp 20100901
 
43
        raise tests.TestSkipped("strace selftests are broken and disabled")
 
44
 
 
45
    def _check_threads(self):
 
46
        # For bug #226769, it was decided that the strace tests should not be
 
47
        # run when more than one thread is active. A lot of tests are currently
 
48
        # leaking threads for good or bad reasons, once they are fixed or
 
49
        # strace itself is fixed (bug #103133), we can get rid of the
 
50
        # restriction.
 
51
        active = threading.activeCount()
 
52
        if active > 1: # There is always the main thread at least
 
53
            self.knownFailure(
 
54
                '%d active threads, bug #103133 needs to be fixed.' % active)
 
55
 
 
56
    def strace_detailed_or_skip(self, *args, **kwargs):
 
57
        """Run strace, but cope if it's not allowed"""
31
58
        try:
32
 
            proc = subprocess.Popen(['strace'],
33
 
                stderr=subprocess.PIPE,
34
 
                stdout=subprocess.PIPE)
35
 
            proc.communicate()
36
 
            found_strace = True
37
 
        except OSError, e:
38
 
            if e.errno == errno.ENOENT:
39
 
                # strace is not installed
40
 
                found_strace = False
 
59
            return strace_detailed(*args, **kwargs)
 
60
        except strace.StraceError, e:
 
61
            if e.err_messages.startswith(
 
62
                    "attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted"):
 
63
                raise tests.TestSkipped("ptrace not permitted")
41
64
            else:
42
65
                raise
43
 
        self.assertEqual(found_strace, StraceFeature.available())
44
 
 
45
 
 
46
 
class TestStrace(TestCaseWithTransport):
47
 
 
48
 
    _test_needs_features = [StraceFeature]
49
66
 
50
67
    def test_strace_callable_is_called(self):
 
68
        self._check_threads()
 
69
 
51
70
        output = []
52
71
        def function(positional, *args, **kwargs):
53
72
            output.append((positional, args, kwargs))
54
 
        strace(function, "a", "b", c="c")
 
73
        self.strace_detailed_or_skip(
 
74
            function, ["a", "b"], {"c": "c"},
 
75
            follow_children=False)
55
76
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
56
77
 
57
78
    def test_strace_callable_result(self):
 
79
        self._check_threads()
 
80
 
58
81
        def function():
59
82
            return "foo"
60
 
        result, strace_result = strace(function)
 
83
        result, strace_result = self.strace_detailed_or_skip(function,[], {},
 
84
                                                follow_children=False)
61
85
        self.assertEqual("foo", result)
62
86
        self.assertIsInstance(strace_result, StraceResult)
63
87
 
64
88
    def test_strace_result_has_raw_log(self):
65
89
        """Checks that a reasonable raw strace log was found by strace."""
 
90
        self._check_threads()
 
91
 
66
92
        def function():
67
93
            self.build_tree(['myfile'])
68
 
        _, result = strace(function)
 
94
        unused, result = self.strace_detailed_or_skip(function, [], {},
 
95
                                         follow_children=False)
69
96
        self.assertContainsRe(result.raw_log, 'myfile')