/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5017.3.1 by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages).
1
# Copyright (C) 2005, 2006, 2007, 2008, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
from bzrlib import (
18
    transport,
5017.3.15 by Vincent Ladeuil
Fix missing import.
19
    urlutils,
5017.3.1 by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages).
20
    )
5017.3.19 by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server
21
from bzrlib.transport import (
5017.3.20 by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server
22
    chroot,
5017.3.19 by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server
23
    pathfilter,
24
    )
5017.3.18 by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server
25
from bzrlib.smart import server
5017.3.1 by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages).
26
27
28
class TestServer(transport.Server):
29
    """A Transport Server dedicated to tests.
30
31
    The TestServer interface provides a server for a given transport. We use
32
    these servers as loopback testing tools. For any given transport the
33
    Servers it provides must either allow writing, or serve the contents
34
    of os.getcwdu() at the time start_server is called.
35
36
    Note that these are real servers - they must implement all the things
37
    that we want bzr transports to take advantage of.
38
    """
39
40
    def get_url(self):
41
        """Return a url for this server.
42
43
        If the transport does not represent a disk directory (i.e. it is
44
        a database like svn, or a memory only transport, it should return
45
        a connection to a newly established resource for this Server.
46
        Otherwise it should return a url that will provide access to the path
47
        that was os.getcwdu() when start_server() was called.
48
49
        Subsequent calls will return the same resource.
50
        """
51
        raise NotImplementedError
52
53
    def get_bogus_url(self):
54
        """Return a url for this protocol, that will fail to connect.
55
56
        This may raise NotImplementedError to indicate that this server cannot
57
        provide bogus urls.
58
        """
59
        raise NotImplementedError
60
61
5017.3.6 by Vincent Ladeuil
Fix some fallouts of moving test servers around.
62
class LocalURLServer(TestServer):
5017.3.3 by Vincent Ladeuil
Move LocalURLServer to bzrlib.tests.test_server
63
    """A pretend server for local transports, using file:// urls.
64
65
    Of course no actual server is required to access the local filesystem, so
66
    this just exists to tell the test code how to get to it.
67
    """
68
69
    def start_server(self):
70
        pass
71
72
    def get_url(self):
73
        """See Transport.Server.get_url."""
74
        return urlutils.local_path_to_url('')
75
76
5017.3.6 by Vincent Ladeuil
Fix some fallouts of moving test servers around.
77
class MemoryServer(TestServer):
5017.3.4 by Vincent Ladeuil
Move MemoryServer to bzrlib.tests.test_server
78
    """Server for the MemoryTransport for testing with."""
79
80
    def start_server(self):
81
        self._dirs = {'/':None}
82
        self._files = {}
83
        self._locks = {}
84
        self._scheme = "memory+%s:///" % id(self)
85
        def memory_factory(url):
86
            from bzrlib.transport import memory
87
            result = memory.MemoryTransport(url)
88
            result._dirs = self._dirs
89
            result._files = self._files
90
            result._locks = self._locks
91
            return result
92
        self._memory_factory = memory_factory
93
        transport.register_transport(self._scheme, self._memory_factory)
94
95
    def stop_server(self):
96
        # unregister this server
97
        transport.unregister_transport(self._scheme, self._memory_factory)
98
99
    def get_url(self):
100
        """See bzrlib.transport.Server.get_url."""
101
        return self._scheme
102
103
5017.3.6 by Vincent Ladeuil
Fix some fallouts of moving test servers around.
104
class DecoratorServer(TestServer):
5017.3.2 by Vincent Ladeuil
Move DecoratorServer to test_server.py
105
    """Server for the TransportDecorator for testing with.
106
107
    To use this when subclassing TransportDecorator, override override the
108
    get_decorator_class method.
109
    """
110
111
    def start_server(self, server=None):
112
        """See bzrlib.transport.Server.start_server.
113
114
        :server: decorate the urls given by server. If not provided a
115
        LocalServer is created.
116
        """
117
        if server is not None:
118
            self._made_server = False
119
            self._server = server
120
        else:
121
            self._made_server = True
122
            self._server = LocalURLServer()
123
            self._server.start_server()
124
125
    def stop_server(self):
126
        if self._made_server:
127
            self._server.stop_server()
128
129
    def get_decorator_class(self):
130
        """Return the class of the decorators we should be constructing."""
131
        raise NotImplementedError(self.get_decorator_class)
132
133
    def get_url_prefix(self):
134
        """What URL prefix does this decorator produce?"""
135
        return self.get_decorator_class()._get_url_prefix()
136
137
    def get_bogus_url(self):
138
        """See bzrlib.transport.Server.get_bogus_url."""
139
        return self.get_url_prefix() + self._server.get_bogus_url()
140
141
    def get_url(self):
142
        """See bzrlib.transport.Server.get_url."""
143
        return self.get_url_prefix() + self._server.get_url()
144
145
5017.3.8 by Vincent Ladeuil
Move BrokenRenameServer to bzrlib.tests.test_server
146
class BrokenRenameServer(DecoratorServer):
147
    """Server for the BrokenRenameTransportDecorator for testing with."""
148
149
    def get_decorator_class(self):
150
        from bzrlib.transport import brokenrename
151
        return brokenrename.BrokenRenameTransportDecorator
152
153
5017.3.7 by Vincent Ladeuil
Move FakeNFSServer to bzrlib.tests.test_server
154
class FakeNFSServer(DecoratorServer):
155
    """Server for the FakeNFSTransportDecorator for testing with."""
156
157
    def get_decorator_class(self):
158
        from bzrlib.transport import fakenfs
159
        return fakenfs.FakeNFSTransportDecorator
160
161
5017.3.9 by Vincent Ladeuil
Move FakeVFATServer to bzrlib.tests.test_server
162
class FakeVFATServer(DecoratorServer):
163
    """A server that suggests connections through FakeVFATTransportDecorator
164
165
    For use in testing.
166
    """
167
168
    def get_decorator_class(self):
169
        from bzrlib.transport import fakevfat
5017.3.14 by Vincent Ladeuil
Fix some missing prefixes.
170
        return fakevfat.FakeVFATTransportDecorator
5017.3.9 by Vincent Ladeuil
Move FakeVFATServer to bzrlib.tests.test_server
171
172
5017.3.11 by Vincent Ladeuil
Move LogDecoratorServer to bzrlib.tests.test_server
173
class LogDecoratorServer(DecoratorServer):
174
    """Server for testing."""
175
176
    def get_decorator_class(self):
177
        from bzrlib.transport import log
178
        return log.TransportLogDecorator
179
180
5017.3.12 by Vincent Ladeuil
Move NoSmartTransportServer to bzrlib.tests.test_server
181
class NoSmartTransportServer(DecoratorServer):
182
    """Server for the NoSmartTransportDecorator for testing with."""
183
184
    def get_decorator_class(self):
185
        from bzrlib.transport import nosmart
5017.3.14 by Vincent Ladeuil
Fix some missing prefixes.
186
        return nosmart.NoSmartTransportDecorator
5017.3.12 by Vincent Ladeuil
Move NoSmartTransportServer to bzrlib.tests.test_server
187
188
5017.3.5 by Vincent Ladeuil
Move ReadonlyServer to bzrlib.tests.readonly
189
class ReadonlyServer(DecoratorServer):
190
    """Server for the ReadonlyTransportDecorator for testing with."""
191
192
    def get_decorator_class(self):
193
        from bzrlib.transport import readonly
194
        return readonly.ReadonlyTransportDecorator
195
196
5017.3.10 by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server
197
class TraceServer(DecoratorServer):
198
    """Server for the TransportTraceDecorator for testing with."""
199
200
    def get_decorator_class(self):
201
        from bzrlib.transport import trace
5017.3.14 by Vincent Ladeuil
Fix some missing prefixes.
202
        return trace.TransportTraceDecorator
5017.3.10 by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server
203
204
5017.3.13 by Vincent Ladeuil
Move UnlistableServer to bzrlib.tests.test_server
205
class UnlistableServer(DecoratorServer):
206
    """Server for the UnlistableTransportDecorator for testing with."""
207
208
    def get_decorator_class(self):
209
        from bzrlib.transport import unlistable
210
        return unlistable.UnlistableTransportDecorator
211
212
5017.3.19 by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server
213
class TestingPathFilteringServer(pathfilter.PathFilteringServer):
214
215
    def __init__(self):
5017.3.20 by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server
216
        """TestingPathFilteringServer is not usable until start_server
217
        is called."""
5017.3.19 by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server
218
219
    def start_server(self, backing_server=None):
220
        """Setup the Chroot on backing_server."""
221
        if backing_server is not None:
222
            self.backing_transport = transport.get_transport(
223
                backing_server.get_url())
224
        else:
225
            self.backing_transport = transport.get_transport('.')
226
        self.backing_transport.clone('added-by-filter').ensure_base()
227
        self.filter_func = lambda x: 'added-by-filter/' + x
228
        super(TestingPathFilteringServer, self).start_server()
229
5017.3.20 by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server
230
    def get_bogus_url(self):
231
        raise NotImplementedError
232
233
234
class TestingChrootServer(chroot.ChrootServer):
235
236
    def __init__(self):
237
        """TestingChrootServer is not usable until start_server is called."""
238
        super(TestingChrootServer, self).__init__(None)
239
240
    def start_server(self, backing_server=None):
241
        """Setup the Chroot on backing_server."""
242
        if backing_server is not None:
243
            self.backing_transport = transport.get_transport(
244
                backing_server.get_url())
245
        else:
246
            self.backing_transport = transport.get_transport('.')
247
        super(TestingChrootServer, self).start_server()
248
249
    def get_bogus_url(self):
250
        raise NotImplementedError
251
5017.3.19 by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server
252
5017.3.18 by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server
253
class SmartTCPServer_for_testing(server.SmartTCPServer):
254
    """Server suitable for use by transport tests.
255
256
    This server is backed by the process's cwd.
257
    """
258
259
    def __init__(self, thread_name_suffix=''):
260
        super(SmartTCPServer_for_testing, self).__init__(None)
261
        self.client_path_extra = None
262
        self.thread_name_suffix = thread_name_suffix
263
264
    def get_backing_transport(self, backing_transport_server):
265
        """Get a backing transport from a server we are decorating."""
266
        return transport.get_transport(backing_transport_server.get_url())
267
268
    def start_server(self, backing_transport_server=None,
269
              client_path_extra='/extra/'):
270
        """Set up server for testing.
271
272
        :param backing_transport_server: backing server to use.  If not
273
            specified, a LocalURLServer at the current working directory will
274
            be used.
275
        :param client_path_extra: a path segment starting with '/' to append to
276
            the root URL for this server.  For instance, a value of '/foo/bar/'
277
            will mean the root of the backing transport will be published at a
278
            URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
279
            `bzr://127.0.0.1:nnnn/`.  Default value is `extra`, so that tests
280
            by default will fail unless they do the necessary path translation.
281
        """
282
        if not client_path_extra.startswith('/'):
283
            raise ValueError(client_path_extra)
284
        from bzrlib.transport.chroot import ChrootServer
285
        if backing_transport_server is None:
286
            backing_transport_server = LocalURLServer()
287
        self.chroot_server = ChrootServer(
288
            self.get_backing_transport(backing_transport_server))
289
        self.chroot_server.start_server()
290
        self.backing_transport = transport.get_transport(
291
            self.chroot_server.get_url())
292
        self.root_client_path = self.client_path_extra = client_path_extra
293
        self.start_background_thread(self.thread_name_suffix)
294
295
    def stop_server(self):
296
        self.stop_background_thread()
297
        self.chroot_server.stop_server()
298
299
    def get_url(self):
300
        url = super(SmartTCPServer_for_testing, self).get_url()
301
        return url[:-1] + self.client_path_extra
302
303
    def get_bogus_url(self):
304
        """Return a URL which will fail to connect"""
305
        return 'bzr://127.0.0.1:1/'
306
307
308
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing):
309
    """Get a readonly server for testing."""
310
311
    def get_backing_transport(self, backing_transport_server):
312
        """Get a backing transport from a server we are decorating."""
313
        url = 'readonly+' + backing_transport_server.get_url()
314
        return transport.get_transport(url)
315
316
317
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing):
318
    """A variation of SmartTCPServer_for_testing that limits the client to
319
    using RPCs in protocol v2 (i.e. bzr <= 1.5).
320
    """
321
322
    def get_url(self):
323
        url = super(SmartTCPServer_for_testing_v2_only, self).get_url()
324
        url = 'bzr-v2://' + url[len('bzr://'):]
325
        return url
326
327
328
class ReadonlySmartTCPServer_for_testing_v2_only(
329
    SmartTCPServer_for_testing_v2_only):
330
    """Get a readonly server for testing."""
331
332
    def get_backing_transport(self, backing_transport_server):
333
        """Get a backing transport from a server we are decorating."""
334
        url = 'readonly+' + backing_transport_server.get_url()
335
        return transport.get_transport(url)
336
337
338
339