/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_server.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-09 17:21:06 UTC
  • mto: (5029.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5030.
  • Revision ID: v.ladeuil+lp@free.fr-20100209172106-ayrc2am7lglvni3z
Move BrokenRenameServer to bzrlib.tests.test_server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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,
 
19
    )
 
20
 
 
21
 
 
22
class TestServer(transport.Server):
 
23
    """A Transport Server dedicated to tests.
 
24
 
 
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.
 
29
 
 
30
    Note that these are real servers - they must implement all the things
 
31
    that we want bzr transports to take advantage of.
 
32
    """
 
33
 
 
34
    def get_url(self):
 
35
        """Return a url for this server.
 
36
 
 
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.
 
42
 
 
43
        Subsequent calls will return the same resource.
 
44
        """
 
45
        raise NotImplementedError
 
46
 
 
47
    def get_bogus_url(self):
 
48
        """Return a url for this protocol, that will fail to connect.
 
49
 
 
50
        This may raise NotImplementedError to indicate that this server cannot
 
51
        provide bogus urls.
 
52
        """
 
53
        raise NotImplementedError
 
54
 
 
55
 
 
56
class LocalURLServer(TestServer):
 
57
    """A pretend server for local transports, using file:// urls.
 
58
 
 
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.
 
61
    """
 
62
 
 
63
    def start_server(self):
 
64
        pass
 
65
 
 
66
    def get_url(self):
 
67
        """See Transport.Server.get_url."""
 
68
        return urlutils.local_path_to_url('')
 
69
 
 
70
 
 
71
class MemoryServer(TestServer):
 
72
    """Server for the MemoryTransport for testing with."""
 
73
 
 
74
    def start_server(self):
 
75
        self._dirs = {'/':None}
 
76
        self._files = {}
 
77
        self._locks = {}
 
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
 
85
            return result
 
86
        self._memory_factory = memory_factory
 
87
        transport.register_transport(self._scheme, self._memory_factory)
 
88
 
 
89
    def stop_server(self):
 
90
        # unregister this server
 
91
        transport.unregister_transport(self._scheme, self._memory_factory)
 
92
 
 
93
    def get_url(self):
 
94
        """See bzrlib.transport.Server.get_url."""
 
95
        return self._scheme
 
96
 
 
97
 
 
98
class DecoratorServer(TestServer):
 
99
    """Server for the TransportDecorator for testing with.
 
100
 
 
101
    To use this when subclassing TransportDecorator, override override the
 
102
    get_decorator_class method.
 
103
    """
 
104
 
 
105
    def start_server(self, server=None):
 
106
        """See bzrlib.transport.Server.start_server.
 
107
 
 
108
        :server: decorate the urls given by server. If not provided a
 
109
        LocalServer is created.
 
110
        """
 
111
        if server is not None:
 
112
            self._made_server = False
 
113
            self._server = server
 
114
        else:
 
115
            self._made_server = True
 
116
            self._server = LocalURLServer()
 
117
            self._server.start_server()
 
118
 
 
119
    def stop_server(self):
 
120
        if self._made_server:
 
121
            self._server.stop_server()
 
122
 
 
123
    def get_decorator_class(self):
 
124
        """Return the class of the decorators we should be constructing."""
 
125
        raise NotImplementedError(self.get_decorator_class)
 
126
 
 
127
    def get_url_prefix(self):
 
128
        """What URL prefix does this decorator produce?"""
 
129
        return self.get_decorator_class()._get_url_prefix()
 
130
 
 
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()
 
134
 
 
135
    def get_url(self):
 
136
        """See bzrlib.transport.Server.get_url."""
 
137
        return self.get_url_prefix() + self._server.get_url()
 
138
 
 
139
 
 
140
class BrokenRenameServer(DecoratorServer):
 
141
    """Server for the BrokenRenameTransportDecorator for testing with."""
 
142
 
 
143
    def get_decorator_class(self):
 
144
        from bzrlib.transport import brokenrename
 
145
        return brokenrename.BrokenRenameTransportDecorator
 
146
 
 
147
 
 
148
class FakeNFSServer(DecoratorServer):
 
149
    """Server for the FakeNFSTransportDecorator for testing with."""
 
150
 
 
151
    def get_decorator_class(self):
 
152
        from bzrlib.transport import fakenfs
 
153
        return fakenfs.FakeNFSTransportDecorator
 
154
 
 
155
 
 
156
class ReadonlyServer(DecoratorServer):
 
157
    """Server for the ReadonlyTransportDecorator for testing with."""
 
158
 
 
159
    def get_decorator_class(self):
 
160
        from bzrlib.transport import readonly
 
161
        return readonly.ReadonlyTransportDecorator
 
162
 
 
163