~bzr-pqm/bzr/bzr.dev

2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
1
# Copyright (C) 2007 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
from bzrlib.hooks import Hooks
2485.8.59 by Vincent Ladeuil
Update from review comments.
18
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
19
from bzrlib.transport import (
20
    register_transport,
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
21
    register_urlparse_netloc_protocol,
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
22
    unregister_transport,
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
23
    _unregister_urlparse_netloc_protocol,
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
24
    )
2485.8.59 by Vincent Ladeuil
Update from review comments.
25
from bzrlib.transport.sftp import SFTPTransport
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
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)
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
33
        # Invoked when the transport has just created a new connection.
34
        # The api signature is (transport, connection, credentials)
35
        self['_set_connection'] = []
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
36
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
37
_hooked_scheme = 'hooked'
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
38
2485.8.59 by Vincent Ladeuil
Update from review comments.
39
class InstrumentedTransport(SFTPTransport):
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
40
    """Instrumented transport class to test commands behavior"""
41
42
    hooks = TransportHooks()
43
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
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
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
50
51
class ConnectionHookedTransport(InstrumentedTransport):
52
    """Transport instrumented to inspect connections"""
53
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
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)
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
60
61
2485.8.59 by Vincent Ladeuil
Update from review comments.
62
class TestCaseWithConnectionHookedTransport(TestCaseWithSFTPServer):
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
63
64
    def setUp(self):
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
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)
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
73
        super(TestCaseWithConnectionHookedTransport, self).setUp()
2485.8.59 by Vincent Ladeuil
Update from review comments.
74
        self.reset_connections()
2485.8.36 by Vincent Ladeuil
Fix test suite to provide a better debugging experience.
75
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
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
2485.8.36 by Vincent Ladeuil
Fix test suite to provide a better debugging experience.
83
    def install_hooks(self):
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
84
        ConnectionHookedTransport.hooks.install_hook('_set_connection',
85
                                                     self.set_connection_hook)
2485.8.59 by Vincent Ladeuil
Update from review comments.
86
        # uninstall our hooks when we are finished
87
        self.addCleanup(self.reset_hooks)
2485.8.36 by Vincent Ladeuil
Fix test suite to provide a better debugging experience.
88
89
    def reset_hooks(self):
90
        InstrumentedTransport.hooks = TransportHooks()
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
91
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
92
    def reset_connections(self):
93
        self.connections = []
94
95
    def set_connection_hook(self, transport, connection, credentials):
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
96
        # Note: uncomment the following line and use 'bt' under pdb, that will
97
        # identify all the connections made including the extraneous ones.
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
98
        # import pdb; pdb.set_trace()
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
99
        self.connections.append(connection)
2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
100