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 is about to create or reuse
33
# an ftp connection. The api signature is (transport, ftp_instance)
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"""
47
"""See FtpTransport._get_FTP.
49
This is where we can detect if the connection is reused
50
or if a new one is created. This a bit ugly, but it's the
51
easiest until transport classes are refactored.
53
instance = super(ConnectionHookedTransport, self)._get_FTP()
54
for hook in self.hooks['get_FTP']:
59
class TestCaseWithConnectionHookedTransport(TestCaseWithFTPServer):
62
super(TestCaseWithConnectionHookedTransport, self).setUp()
63
ConnectionHookedTransport.hooks.install_hook('get_FTP',
64
self.get_connection_hook)
65
# Make our instrumented transport the default ftp transport
66
register_transport('ftp://', ConnectionHookedTransport)
69
InstrumentedTransport.hooks = TransportHooks()
70
unregister_transport('ftp://', ConnectionHookedTransport)
72
self.addCleanup(cleanup)
75
def get_connection_hook(self, transport, connection):
76
if connection is not None and connection not in self.connections:
77
self.connections.append(connection)