~bzr-pqm/bzr/bzr.dev

2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
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
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
17
18
"""Tests for the strace-invoking support."""
19
2386.1.3 by John Arbash Meinel
import errno in test_strace.py (recommended by Ian Clatworthy)
20
import errno
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
21
import subprocess
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
22
import threading
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
23
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
24
from bzrlib import (
25
    tests,
26
    )
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
27
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
28
29
30
class TestStraceFeature(tests.TestCaseWithTransport):
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
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
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
49
class TestStrace(tests.TestCaseWithTransport):
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
50
51
    _test_needs_features = [StraceFeature]
52
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
53
    def _check_threads(self):
3406.1.3 by Vincent Ladeuil
Fixed as par John's review.
54
        # For bug #226769, it was decided that the strace tests should not be
55
        # run when more than one thread is active. A lot of tests are currently
56
        # leaking threads for good or bad reasons, once they are fixed or
57
        # strace itself is fixed (bug #103133), we can get rid of the
58
        # restriction.
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
59
        active = threading.activeCount()
60
        if active > 1: # There is always the main thread at least
61
            raise tests.KnownFailure(
62
                '%d active threads, bug #103133 needs to be fixed.' % active)
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
63
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
64
    def test_strace_callable_is_called(self):
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
65
        self._check_threads()
66
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
67
        output = []
68
        def function(positional, *args, **kwargs):
69
            output.append((positional, args, kwargs))
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
70
        strace_detailed(function, ["a", "b"], {"c": "c"},
71
                        follow_children=False)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
72
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
73
2367.1.9 by Robert Collins
Review feedback.
74
    def test_strace_callable_result(self):
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
75
        self._check_threads()
3406.1.1 by Vincent Ladeuil
Fix #226769 by disabling some strace tests.
76
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
77
        def function():
2367.1.9 by Robert Collins
Review feedback.
78
            return "foo"
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
79
        result, strace_result = strace_detailed(function,[], {},
80
                                                follow_children=False)
2367.1.9 by Robert Collins
Review feedback.
81
        self.assertEqual("foo", result)
82
        self.assertIsInstance(strace_result, StraceResult)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
83
84
    def test_strace_result_has_raw_log(self):
85
        """Checks that a reasonable raw strace log was found by strace."""
3406.1.2 by Vincent Ladeuil
Fix as per Robert's review.
86
        self._check_threads()
87
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
88
        def function():
89
            self.build_tree(['myfile'])
2566.3.4 by Vincent Ladeuil
Take Martin and Robert comments into account.
90
        unused, result = strace_detailed(function, [], {},
91
                                         follow_children=False)
2367.1.7 by Robert Collins
Added ``bzrlib.strace.strace`` which will strace a single callable and
92
        self.assertContainsRe(result.raw_log, 'myfile')