1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
17
from bzrlib.hooks import Hooks
18
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
19
from bzrlib.transport import (
21
register_urlparse_netloc_protocol,
23
_unregister_urlparse_netloc_protocol,
25
from bzrlib.transport.sftp import SFTPTransport
28
class TransportHooks(Hooks):
29
"""Dict-mapping hook name to a list of callables for transport hooks"""
33
# Invoked when the transport has just created a new connection.
34
# The api signature is (transport, connection, credentials)
35
self['_set_connection'] = []
37
_hooked_scheme = 'hooked'
39
class InstrumentedTransport(SFTPTransport):
40
"""Instrumented transport class to test commands behavior"""
42
hooks = TransportHooks()
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)
51
class ConnectionHookedTransport(InstrumentedTransport):
52
"""Transport instrumented to inspect connections"""
54
def _set_connection(self, connection, credentials):
55
"""Called when a new connection is created """
56
super(ConnectionHookedTransport, self)._set_connection(connection,
58
for hook in self.hooks['_set_connection']:
59
hook(self, connection, credentials)
62
class TestCaseWithConnectionHookedTransport(TestCaseWithSFTPServer):
65
register_urlparse_netloc_protocol(_hooked_scheme)
66
register_transport(_hooked_scheme, ConnectionHookedTransport)
69
unregister_transport(_hooked_scheme, ConnectionHookedTransport)
70
_unregister_urlparse_netloc_protocol(_hooked_scheme)
72
self.addCleanup(unregister)
73
super(TestCaseWithConnectionHookedTransport, self).setUp()
74
self.reset_connections()
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'):]
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)
89
def reset_hooks(self):
90
InstrumentedTransport.hooks = TransportHooks()
92
def reset_connections(self):
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)