1
# Copyright (C) 2005, 2006, 2007, 2008, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
class TestServer(transport.Server):
23
"""A Transport Server dedicated to tests.
25
The TestServer interface provides a server for a given transport. We use
26
these servers as loopback testing tools. For any given transport the
27
Servers it provides must either allow writing, or serve the contents
28
of os.getcwdu() at the time start_server is called.
30
Note that these are real servers - they must implement all the things
31
that we want bzr transports to take advantage of.
35
"""Return a url for this server.
37
If the transport does not represent a disk directory (i.e. it is
38
a database like svn, or a memory only transport, it should return
39
a connection to a newly established resource for this Server.
40
Otherwise it should return a url that will provide access to the path
41
that was os.getcwdu() when start_server() was called.
43
Subsequent calls will return the same resource.
45
raise NotImplementedError
47
def get_bogus_url(self):
48
"""Return a url for this protocol, that will fail to connect.
50
This may raise NotImplementedError to indicate that this server cannot
53
raise NotImplementedError
56
class LocalURLServer(TestServer):
57
"""A pretend server for local transports, using file:// urls.
59
Of course no actual server is required to access the local filesystem, so
60
this just exists to tell the test code how to get to it.
63
def start_server(self):
67
"""See Transport.Server.get_url."""
68
return urlutils.local_path_to_url('')
71
class MemoryServer(TestServer):
72
"""Server for the MemoryTransport for testing with."""
74
def start_server(self):
75
self._dirs = {'/':None}
78
self._scheme = "memory+%s:///" % id(self)
79
def memory_factory(url):
80
from bzrlib.transport import memory
81
result = memory.MemoryTransport(url)
82
result._dirs = self._dirs
83
result._files = self._files
84
result._locks = self._locks
86
self._memory_factory = memory_factory
87
transport.register_transport(self._scheme, self._memory_factory)
89
def stop_server(self):
90
# unregister this server
91
transport.unregister_transport(self._scheme, self._memory_factory)
94
"""See bzrlib.transport.Server.get_url."""
98
class DecoratorServer(TestServer):
99
"""Server for the TransportDecorator for testing with.
101
To use this when subclassing TransportDecorator, override override the
102
get_decorator_class method.
105
def start_server(self, server=None):
106
"""See bzrlib.transport.Server.start_server.
108
:server: decorate the urls given by server. If not provided a
109
LocalServer is created.
111
if server is not None:
112
self._made_server = False
113
self._server = server
115
self._made_server = True
116
self._server = LocalURLServer()
117
self._server.start_server()
119
def stop_server(self):
120
if self._made_server:
121
self._server.stop_server()
123
def get_decorator_class(self):
124
"""Return the class of the decorators we should be constructing."""
125
raise NotImplementedError(self.get_decorator_class)
127
def get_url_prefix(self):
128
"""What URL prefix does this decorator produce?"""
129
return self.get_decorator_class()._get_url_prefix()
131
def get_bogus_url(self):
132
"""See bzrlib.transport.Server.get_bogus_url."""
133
return self.get_url_prefix() + self._server.get_bogus_url()
136
"""See bzrlib.transport.Server.get_url."""
137
return self.get_url_prefix() + self._server.get_url()
140
class BrokenRenameServer(DecoratorServer):
141
"""Server for the BrokenRenameTransportDecorator for testing with."""
143
def get_decorator_class(self):
144
from bzrlib.transport import brokenrename
145
return brokenrename.BrokenRenameTransportDecorator
148
class FakeNFSServer(DecoratorServer):
149
"""Server for the FakeNFSTransportDecorator for testing with."""
151
def get_decorator_class(self):
152
from bzrlib.transport import fakenfs
153
return fakenfs.FakeNFSTransportDecorator
156
class ReadonlyServer(DecoratorServer):
157
"""Server for the ReadonlyTransportDecorator for testing with."""
159
def get_decorator_class(self):
160
from bzrlib.transport import readonly
161
return readonly.ReadonlyTransportDecorator