~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/transport_util.py

  • Committer: Alexander Belchenko
  • Date: 2007-08-14 06:27:51 UTC
  • mto: (2733.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2734.
  • Revision ID: bialix@ukr.net-20070814062751-tyyn1s5jraunqni9
teach windows python installer to find docs in all subdirectories

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
18
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
19
from bzrlib.transport import (
 
20
    register_transport,
 
21
    register_urlparse_netloc_protocol,
 
22
    unregister_transport,
 
23
    _unregister_urlparse_netloc_protocol,
 
24
    )
 
25
from bzrlib.transport.sftp import SFTPTransport
 
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)
 
33
        # Invoked when the transport has just created a new connection.
 
34
        # The api signature is (transport, connection, credentials)
 
35
        self['_set_connection'] = []
 
36
 
 
37
_hooked_scheme = 'hooked'
 
38
 
 
39
class InstrumentedTransport(SFTPTransport):
 
40
    """Instrumented transport class to test commands behavior"""
 
41
 
 
42
    hooks = TransportHooks()
 
43
 
 
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
 
 
50
 
 
51
class ConnectionHookedTransport(InstrumentedTransport):
 
52
    """Transport instrumented to inspect connections"""
 
53
 
 
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)
 
60
 
 
61
 
 
62
class TestCaseWithConnectionHookedTransport(TestCaseWithSFTPServer):
 
63
 
 
64
    def setUp(self):
 
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)
 
73
        super(TestCaseWithConnectionHookedTransport, self).setUp()
 
74
        self.reset_connections()
 
75
 
 
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
 
 
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)
 
88
 
 
89
    def reset_hooks(self):
 
90
        InstrumentedTransport.hooks = TransportHooks()
 
91
 
 
92
    def reset_connections(self):
 
93
        self.connections = []
 
94
 
 
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)
 
100