~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: 2010-02-11 02:16:42 UTC
  • mfrom: (5017.1.2 initialize)
  • Revision ID: pqm@pqm.ubuntu.com-20100211021642-eitum30b2e09oalf
(mbp) Add bzrlib.initialize

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2010 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
21
22
import threading
22
23
 
23
24
from bzrlib import (
24
 
    strace,
25
25
    tests,
26
26
    )
27
27
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
28
28
 
29
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())
 
47
 
 
48
 
30
49
class TestStrace(tests.TestCaseWithTransport):
31
50
 
32
51
    _test_needs_features = [StraceFeature]
33
52
 
34
 
    def setUp(self):
35
 
        # NB: see http://pad.lv/626679 and
36
 
        # <https://code.launchpad.net/~mbp/bzr/626679-strace/+merge/34157>:
37
 
        # testing strace by connecting to ourselves has repeatedly caused
38
 
        # hangs in running the test suite; these are fixable given enough
39
 
        # determination but given that strace is not used by any other tests
40
 
        # at the moment and that it's only test-support code, we just leave it
41
 
        # untested -- mbp 20100901
42
 
        raise tests.TestSkipped("strace selftests are broken and disabled")
43
 
 
44
53
    def _check_threads(self):
45
54
        # For bug #226769, it was decided that the strace tests should not be
46
55
        # run when more than one thread is active. A lot of tests are currently
52
61
            raise tests.KnownFailure(
53
62
                '%d active threads, bug #103133 needs to be fixed.' % active)
54
63
 
55
 
    def strace_detailed_or_skip(self, *args, **kwargs):
56
 
        """Run strace, but cope if it's not allowed"""
57
 
        try:
58
 
            return strace_detailed(*args, **kwargs)
59
 
        except strace.StraceError, e:
60
 
            if e.err_messages.startswith(
61
 
                    "attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted"):
62
 
                raise tests.TestSkipped("ptrace not permitted")
63
 
            else:
64
 
                raise
65
 
 
66
64
    def test_strace_callable_is_called(self):
67
65
        self._check_threads()
68
66
 
69
67
        output = []
70
68
        def function(positional, *args, **kwargs):
71
69
            output.append((positional, args, kwargs))
72
 
        self.strace_detailed_or_skip(
73
 
            function, ["a", "b"], {"c": "c"},
74
 
            follow_children=False)
 
70
        strace_detailed(function, ["a", "b"], {"c": "c"},
 
71
                        follow_children=False)
75
72
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
76
73
 
77
74
    def test_strace_callable_result(self):
79
76
 
80
77
        def function():
81
78
            return "foo"
82
 
        result, strace_result = self.strace_detailed_or_skip(function,[], {},
 
79
        result, strace_result = strace_detailed(function,[], {},
83
80
                                                follow_children=False)
84
81
        self.assertEqual("foo", result)
85
82
        self.assertIsInstance(strace_result, StraceResult)
90
87
 
91
88
        def function():
92
89
            self.build_tree(['myfile'])
93
 
        unused, result = self.strace_detailed_or_skip(function, [], {},
 
90
        unused, result = strace_detailed(function, [], {},
94
91
                                         follow_children=False)
95
92
        self.assertContainsRe(result.raw_log, 'myfile')