~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_strace.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-02-07 04:51:19 UTC
  • mfrom: (5641.2.1 bzr-dev-overview-tweaks)
  • Revision ID: pqm@pqm.ubuntu.com-20110207045119-fn3s8bol0zscqr1p
(spiv) Expand doc/developers/overview.txt a little,
 and fix some formatting nits. (Andrew Bennetts)

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
17
16
 
18
17
"""Tests for the strace-invoking support."""
19
18
 
20
 
import errno
21
 
import subprocess
22
19
import threading
23
20
 
24
21
from bzrlib import (
 
22
    strace,
25
23
    tests,
26
24
    )
27
25
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
28
26
 
29
27
 
30
 
class TestStraceFeature(tests.TestCaseWithTransport):
31
 
 
32
 
    def test_strace_detection(self):
33
 
        """Strace is available if its runnable."""
34
 
        try:
35
 
            proc = subprocess.Popen(['strace'],
36
 
                stderr=subprocess.PIPE,
37
 
                stdout=subprocess.PIPE)
38
 
            proc.communicate()
39
 
            found_strace = True
40
 
        except OSError, e:
41
 
            if e.errno == errno.ENOENT:
42
 
                # strace is not installed
43
 
                found_strace = False
44
 
            else:
45
 
                raise
46
 
        self.assertEqual(found_strace, StraceFeature.available())
47
 
 
48
 
 
49
28
class TestStrace(tests.TestCaseWithTransport):
50
29
 
51
30
    _test_needs_features = [StraceFeature]
52
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
 
53
42
    def _check_threads(self):
54
43
        # For bug #226769, it was decided that the strace tests should not be
55
44
        # run when more than one thread is active. A lot of tests are currently
61
50
            raise tests.KnownFailure(
62
51
                '%d active threads, bug #103133 needs to be fixed.' % active)
63
52
 
 
53
    def strace_detailed_or_skip(self, *args, **kwargs):
 
54
        """Run strace, but cope if it's not allowed"""
 
55
        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")
 
61
            else:
 
62
                raise
 
63
 
64
64
    def test_strace_callable_is_called(self):
65
65
        self._check_threads()
66
66
 
67
67
        output = []
68
68
        def function(positional, *args, **kwargs):
69
69
            output.append((positional, args, kwargs))
70
 
        strace_detailed(function, ["a", "b"], {"c": "c"},
71
 
                        follow_children=False)
 
70
        self.strace_detailed_or_skip(
 
71
            function, ["a", "b"], {"c": "c"},
 
72
            follow_children=False)
72
73
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
73
74
 
74
75
    def test_strace_callable_result(self):
76
77
 
77
78
        def function():
78
79
            return "foo"
79
 
        result, strace_result = strace_detailed(function,[], {},
 
80
        result, strace_result = self.strace_detailed_or_skip(function,[], {},
80
81
                                                follow_children=False)
81
82
        self.assertEqual("foo", result)
82
83
        self.assertIsInstance(strace_result, StraceResult)
87
88
 
88
89
        def function():
89
90
            self.build_tree(['myfile'])
90
 
        unused, result = strace_detailed(function, [], {},
 
91
        unused, result = self.strace_detailed_or_skip(function, [], {},
91
92
                                         follow_children=False)
92
93
        self.assertContainsRe(result.raw_log, 'myfile')