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
18
from bzrlib.hooks import Hooks
19
from bzrlib.tests.test_ftp_transport import TestCaseWithFTPServer
20
from bzrlib.transport import (
24
from bzrlib.transport.ftp import FtpTransport
27
class TransportHooks(Hooks):
28
"""Dict-mapping hook name to a list of callables for transport hooks"""
32
# Invoked when the transport has just created a new connection.
33
# The api signature is (transport, connection, credentials)
34
self['_set_connection'] = []
37
class InstrumentedTransport(FtpTransport):
38
"""Instrumented transport class to test commands behavior"""
40
hooks = TransportHooks()
43
class ConnectionHookedTransport(InstrumentedTransport):
44
"""Transport instrumented to inspect connections"""
46
def _set_connection(self, connection, credentials):
47
"""Called when a new connection is created """
48
super(ConnectionHookedTransport, self)._set_connection(connection,
50
for hook in self.hooks['_set_connection']:
51
hook(self, connection, credentials)
54
class TestCaseWithConnectionHookedTransport(TestCaseWithFTPServer):
57
super(TestCaseWithConnectionHookedTransport, self).setUp()
58
self._hooks_installed = False
61
# Be nice and reset hooks if the test writer forgot about them
62
if self._hooks_installed:
65
self.addCleanup(cleanup)
68
def install_hooks(self):
69
ConnectionHookedTransport.hooks.install_hook('_set_connection',
70
self.set_connection_hook)
71
# Make our instrumented transport the default ftp transport
72
register_transport('ftp://', ConnectionHookedTransport)
73
self._hooks_installed = True
75
def reset_hooks(self):
76
InstrumentedTransport.hooks = TransportHooks()
77
unregister_transport('ftp://', ConnectionHookedTransport)
78
self._hooks_installed = False
80
def reset_connections(self):
83
def set_connection_hook(self, transport, connection, credentials):
84
# Note: uncomment the following line and use 'bt' under pdb, that will
85
# identify all the connections made including the extraneous ones.
86
# import pdb; pdb.set_trace()
87
self.connections.append(connection)