1
# Copyright (C) 2007-2011 Canonical Ltd
1
# Copyright (C) 2007 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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
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
17
18
"""Tests for the strace-invoking support."""
21
24
from bzrlib import (
25
from bzrlib.strace import strace_detailed, StraceResult
26
from bzrlib.tests.features import (
27
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
30
class TestStraceFeature(tests.TestCaseWithTransport):
32
def test_strace_detection(self):
33
"""Strace is available if its runnable."""
35
proc = subprocess.Popen(['strace'],
36
stderr=subprocess.PIPE,
37
stdout=subprocess.PIPE)
41
if e.errno == errno.ENOENT:
42
# strace is not installed
46
self.assertEqual(found_strace, StraceFeature.available())
31
49
class TestStrace(tests.TestCaseWithTransport):
33
_test_needs_features = [strace_feature]
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]
45
53
def _check_threads(self):
46
54
# For bug #226769, it was decided that the strace tests should not be
51
59
active = threading.activeCount()
52
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)
56
def strace_detailed_or_skip(self, *args, **kwargs):
57
"""Run strace, but cope if it's not allowed"""
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")
67
64
def test_strace_callable_is_called(self):
68
65
self._check_threads()
71
68
def function(positional, *args, **kwargs):
72
69
output.append((positional, args, kwargs))
73
self.strace_detailed_or_skip(
74
function, ["a", "b"], {"c": "c"},
75
follow_children=False)
70
strace_detailed(function, ["a", "b"], {"c": "c"},
71
follow_children=False)
76
72
self.assertEqual([("a", ("b",), {"c":"c"})], output)
78
74
def test_strace_callable_result(self):
83
result, strace_result = self.strace_detailed_or_skip(function,[], {},
79
result, strace_result = strace_detailed(function,[], {},
84
80
follow_children=False)
85
81
self.assertEqual("foo", result)
86
82
self.assertIsInstance(strace_result, StraceResult)
93
89
self.build_tree(['myfile'])
94
unused, result = self.strace_detailed_or_skip(function, [], {},
90
unused, result = strace_detailed(function, [], {},
95
91
follow_children=False)
96
92
self.assertContainsRe(result.raw_log, 'myfile')