~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/transport_util.py

  • Committer: Patch Queue Manager
  • Date: 2015-12-17 18:39:00 UTC
  • mfrom: (6606.1.2 fix-float)
  • Revision ID: pqm@pqm.ubuntu.com-20151217183900-0719du2uv1kwu3lc
(vila) Inline testtools private method to fix an issue in xenial (the
 private implementation has changed in an backward incompatible way).
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
from bzrlib.hooks import Hooks
18
 
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
19
 
from bzrlib.transport import (
20
 
    register_transport,
21
 
    register_urlparse_netloc_protocol,
22
 
    unregister_transport,
23
 
    _unregister_urlparse_netloc_protocol,
24
 
    )
25
 
from bzrlib.transport.sftp import SFTPTransport
26
 
 
27
 
 
28
 
class TransportHooks(Hooks):
29
 
    """Dict-mapping hook name to a list of callables for transport hooks"""
30
 
 
31
 
    def __init__(self):
32
 
        Hooks.__init__(self)
33
 
        # Invoked when the transport has just created a new connection.
34
 
        # The api signature is (transport, connection, credentials)
35
 
        self['_set_connection'] = []
36
 
 
37
 
_hooked_scheme = 'hooked'
38
 
 
39
 
class InstrumentedTransport(SFTPTransport):
40
 
    """Instrumented transport class to test commands behavior"""
41
 
 
42
 
    hooks = TransportHooks()
43
 
 
44
 
    def __init__(self, base, _from_transport=None):
45
 
        assert base.startswith(_hooked_scheme + '://')
46
 
        # Avoid SFTPTransport assertion since we use a dedicated scheme
47
 
        super(SFTPTransport, self).__init__(base,
48
 
                                            _from_transport=_from_transport)
49
 
 
50
 
 
51
 
class ConnectionHookedTransport(InstrumentedTransport):
52
 
    """Transport instrumented to inspect connections"""
53
 
 
54
 
    def _set_connection(self, connection, credentials):
55
 
        """Called when a new connection is created """
56
 
        super(ConnectionHookedTransport, self)._set_connection(connection,
57
 
                                                               credentials)
58
 
        for hook in self.hooks['_set_connection']:
59
 
            hook(self, connection, credentials)
60
 
 
61
 
 
62
 
class TestCaseWithConnectionHookedTransport(TestCaseWithSFTPServer):
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from bzrlib.tests import features
 
18
 
 
19
# SFTPTransport offers better performances but relies on paramiko, if paramiko
 
20
# is not available, we fallback to FtpTransport
 
21
if features.paramiko.available():
 
22
    from bzrlib.tests import test_sftp_transport
 
23
    from bzrlib.transport import sftp, Transport
 
24
    _backing_scheme = 'sftp'
 
25
    _backing_transport_class = sftp.SFTPTransport
 
26
    _backing_test_class = test_sftp_transport.TestCaseWithSFTPServer
 
27
else:
 
28
    from bzrlib.transport import ftp, Transport
 
29
    from bzrlib.tests import test_ftp_transport
 
30
    _backing_scheme = 'ftp'
 
31
    _backing_transport_class = ftp.FtpTransport
 
32
    _backing_test_class = test_ftp_transport.TestCaseWithFTPServer
 
33
 
 
34
 
 
35
class TestCaseWithConnectionHookedTransport(_backing_test_class):
63
36
 
64
37
    def setUp(self):
65
 
        register_urlparse_netloc_protocol(_hooked_scheme)
66
 
        register_transport(_hooked_scheme, ConnectionHookedTransport)
67
 
 
68
 
        def unregister():
69
 
            unregister_transport(_hooked_scheme, ConnectionHookedTransport)
70
 
            _unregister_urlparse_netloc_protocol(_hooked_scheme)
71
 
 
72
 
        self.addCleanup(unregister)
73
38
        super(TestCaseWithConnectionHookedTransport, self).setUp()
74
39
        self.reset_connections()
75
40
 
76
 
    def get_url(self, relpath=None):
77
 
        super_self = super(TestCaseWithConnectionHookedTransport, self)
78
 
        url = super_self.get_url(relpath)
79
 
        # Replace the sftp scheme by our own
80
 
        url = _hooked_scheme + url[len('sftp'):]
81
 
        return url
82
 
 
83
 
    def install_hooks(self):
84
 
        ConnectionHookedTransport.hooks.install_hook('_set_connection',
85
 
                                                     self.set_connection_hook)
86
 
        # uninstall our hooks when we are finished
87
 
        self.addCleanup(self.reset_hooks)
88
 
 
89
 
    def reset_hooks(self):
90
 
        InstrumentedTransport.hooks = TransportHooks()
 
41
    def start_logging_connections(self):
 
42
        Transport.hooks.install_named_hook('post_connect',
 
43
            self.connections.append, None)
91
44
 
92
45
    def reset_connections(self):
93
46
        self.connections = []
94
47
 
95
 
    def set_connection_hook(self, transport, connection, credentials):
96
 
        # Note: uncomment the following line and use 'bt' under pdb, that will
97
 
        # identify all the connections made including the extraneous ones.
98
 
        # import pdb; pdb.set_trace()
99
 
        self.connections.append(connection)
100