~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/transport_util.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 16:58:03 UTC
  • mfrom: (3224.3.1 news-typo)
  • Revision ID: pqm@pqm.ubuntu.com-20080316165803-tisoc9mpob9z544o
(Matt Nordhoff) Trivial NEWS typo fix

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
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import bzrlib.hooks
18
 
from bzrlib import transport
19
 
from bzrlib.tests import features
20
18
 
21
19
# SFTPTransport offers better performances but relies on paramiko, if paramiko
22
20
# is not available, we fallback to FtpTransport
23
 
if features.paramiko.available():
24
 
    from bzrlib.tests import test_sftp_transport
 
21
from bzrlib.tests import test_sftp_transport
 
22
if test_sftp_transport.paramiko_loaded:
25
23
    from bzrlib.transport import sftp
26
24
    _backing_scheme = 'sftp'
27
25
    _backing_transport_class = sftp.SFTPTransport
47
45
    """Dict-mapping hook name to a list of callables for transport hooks"""
48
46
 
49
47
    def __init__(self):
50
 
        super(TransportHooks, self).__init__("bzrlib.tests.transport_util",
51
 
            "InstrumentedTransport.hooks")
 
48
        super(TransportHooks, self).__init__()
52
49
        # Invoked when the transport has just created a new connection.
53
50
        # The api signature is (transport, connection, credentials)
54
51
        self['_set_connection'] = []
68
65
    hooks = TransportHooks()
69
66
 
70
67
    def __init__(self, base, _from_transport=None):
71
 
        if not base.startswith(_hooked_scheme + '://'):
72
 
            raise ValueError(base)
 
68
        assert base.startswith(_hooked_scheme + '://')
73
69
        # We need to trick the backing transport class about the scheme used
74
70
        # We'll do the reverse when we need to talk to the backing server
75
71
        fake_base = _change_scheme_in(base, _hooked_scheme, _backing_scheme)
77
73
            fake_base, _from_transport=_from_transport)
78
74
        # The following is needed to minimize the effects of our trick above
79
75
        # while retaining the best compatibility.
80
 
        self._parsed_url.scheme = _hooked_scheme
81
 
        super(ConnectedTransport, self).__init__(str(self._parsed_url))
 
76
        self._scheme = _hooked_scheme
 
77
        base = self._unsplit_url(self._scheme,
 
78
                                 self._user, self._password,
 
79
                                 self._host, self._port,
 
80
                                 self._path)
 
81
        super(ConnectedTransport, self).__init__(base)
82
82
 
83
83
 
84
84
class ConnectionHookedTransport(InstrumentedTransport):
97
97
    def setUp(self):
98
98
        register_urlparse_netloc_protocol(_hooked_scheme)
99
99
        register_transport(_hooked_scheme, ConnectionHookedTransport)
100
 
        self.addCleanup(unregister_transport, _hooked_scheme,
101
 
                        ConnectionHookedTransport)
102
 
        self.addCleanup(_unregister_urlparse_netloc_protocol, _hooked_scheme)
 
100
 
 
101
        def unregister():
 
102
            unregister_transport(_hooked_scheme, ConnectionHookedTransport)
 
103
            _unregister_urlparse_netloc_protocol(_hooked_scheme)
 
104
 
 
105
        self.addCleanup(unregister)
103
106
        super(TestCaseWithConnectionHookedTransport, self).setUp()
104
107
        self.reset_connections()
105
 
        # Add the 'hooked' url to the permitted url list.
106
 
        # XXX: See TestCase.start_server. This whole module shouldn't need to
107
 
        # exist - a bug has been filed on that. once its cleanedup/removed, the
108
 
        # standard test support code will work and permit the server url
109
 
        # correctly.
110
 
        url = self.get_url()
111
 
        t = transport.get_transport_from_url(url)
112
 
        if t.base.endswith('work/'):
113
 
            t = t.clone('../..')
114
 
        self.permit_url(t.base)
115
108
 
116
109
    def get_url(self, relpath=None):
117
110
        super_self = super(TestCaseWithConnectionHookedTransport, self)
122
115
        return url
123
116
 
124
117
    def start_logging_connections(self):
125
 
        self.overrideAttr(InstrumentedTransport, 'hooks', TransportHooks())
126
 
        # We preserved the hooks class attribute. Now we install our hook.
127
 
        ConnectionHookedTransport.hooks.install_named_hook(
128
 
            '_set_connection', self._collect_connection, None)
 
118
        ConnectionHookedTransport.hooks.install_hook('_set_connection',
 
119
                                                     self._collect_connection)
 
120
        # uninstall our hooks when we are finished
 
121
        self.addCleanup(self.reset_hooks)
 
122
 
 
123
    def reset_hooks(self):
 
124
        InstrumentedTransport.hooks = TransportHooks()
129
125
 
130
126
    def reset_connections(self):
131
127
        self.connections = []