/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_strace.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

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
16
17
 
17
18
"""Tests for the strace-invoking support."""
18
19
 
 
20
import errno
 
21
import subprocess
19
22
import threading
20
23
 
21
 
from breezy import (
22
 
    strace,
 
24
from bzrlib import (
23
25
    tests,
24
26
    )
25
 
from breezy.strace import strace_detailed, StraceResult
26
 
from breezy.tests.features import (
27
 
    strace_feature,
28
 
    )
 
27
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
 
28
 
 
29
 
 
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())
29
47
 
30
48
 
31
49
class TestStrace(tests.TestCaseWithTransport):
32
50
 
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")
 
51
    _test_needs_features = [StraceFeature]
44
52
 
45
53
    def _check_threads(self):
46
54
        # For bug #226769, it was decided that the strace tests should not be
49
57
        # strace itself is fixed (bug #103133), we can get rid of the
50
58
        # restriction.
51
59
        active = threading.activeCount()
52
 
        if active > 1:  # There is always the main thread at least
53
 
            self.knownFailure(
 
60
        if active > 1: # There is always the main thread at least
 
61
            raise tests.KnownFailure(
54
62
                '%d active threads, bug #103133 needs to be fixed.' % active)
55
63
 
56
 
    def strace_detailed_or_skip(self, *args, **kwargs):
57
 
        """Run strace, but cope if it's not allowed"""
58
 
        try:
59
 
            return strace_detailed(*args, **kwargs)
60
 
        except strace.StraceError as e:
61
 
            if e.err_messages.startswith(
62
 
                    "attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted"):
63
 
                raise tests.TestSkipped("ptrace not permitted")
64
 
            else:
65
 
                raise
66
 
 
67
64
    def test_strace_callable_is_called(self):
68
65
        self._check_threads()
69
66
 
70
67
        output = []
71
 
 
72
68
        def function(positional, *args, **kwargs):
73
69
            output.append((positional, args, kwargs))
74
 
        self.strace_detailed_or_skip(
75
 
            function, ["a", "b"], {"c": "c"},
76
 
            follow_children=False)
77
 
        self.assertEqual([("a", ("b",), {"c": "c"})], output)
 
70
        strace_detailed(function, ["a", "b"], {"c": "c"},
 
71
                        follow_children=False)
 
72
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
78
73
 
79
74
    def test_strace_callable_result(self):
80
75
        self._check_threads()
81
76
 
82
77
        def function():
83
78
            return "foo"
84
 
        result, strace_result = self.strace_detailed_or_skip(function, [], {},
85
 
                                                             follow_children=False)
 
79
        result, strace_result = strace_detailed(function,[], {},
 
80
                                                follow_children=False)
86
81
        self.assertEqual("foo", result)
87
82
        self.assertIsInstance(strace_result, StraceResult)
88
83
 
92
87
 
93
88
        def function():
94
89
            self.build_tree(['myfile'])
95
 
        unused, result = self.strace_detailed_or_skip(function, [], {},
96
 
                                                      follow_children=False)
 
90
        unused, result = strace_detailed(function, [], {},
 
91
                                         follow_children=False)
97
92
        self.assertContainsRe(result.raw_log, 'myfile')