~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_strace.py

  • Committer: Ian Clatworthy
  • Date: 2007-12-11 02:07:30 UTC
  • mto: (3119.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3120.
  • Revision ID: ian.clatworthy@internode.on.net-20071211020730-sdj4kj794dw0628e
make help topics more discoverable

Show diffs side-by-side

added added

removed removed

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