/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 breezy/tests/test_smart_request.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 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
 
"""Tests for smart server request infrastructure (breezy.bzr.smart.request)."""
18
 
 
19
 
import threading
20
 
 
21
 
from breezy import (
22
 
    errors,
23
 
    transport,
24
 
    )
25
 
from breezy.bzr.bzrdir import BzrDir
26
 
from breezy.bzr.smart import request
27
 
from breezy.tests import TestCase, TestCaseWithMemoryTransport
28
 
 
29
 
 
30
 
class NoBodyRequest(request.SmartServerRequest):
31
 
    """A request that does not implement do_body."""
32
 
 
33
 
    def do(self):
34
 
        return request.SuccessfulSmartServerResponse(('ok',))
35
 
 
36
 
 
37
 
class DoErrorRequest(request.SmartServerRequest):
38
 
    """A request that raises an error from self.do()."""
39
 
 
40
 
    def do(self):
41
 
        raise errors.NoSuchFile('xyzzy')
42
 
 
43
 
 
44
 
class DoUnexpectedErrorRequest(request.SmartServerRequest):
45
 
    """A request that encounters a generic error in self.do()"""
46
 
 
47
 
    def do(self):
48
 
        dict()[1]
49
 
 
50
 
 
51
 
class ChunkErrorRequest(request.SmartServerRequest):
52
 
    """A request that raises an error from self.do_chunk()."""
53
 
 
54
 
    def do(self):
55
 
        """No-op."""
56
 
        pass
57
 
 
58
 
    def do_chunk(self, bytes):
59
 
        raise errors.NoSuchFile('xyzzy')
60
 
 
61
 
 
62
 
class EndErrorRequest(request.SmartServerRequest):
63
 
    """A request that raises an error from self.do_end()."""
64
 
 
65
 
    def do(self):
66
 
        """No-op."""
67
 
        pass
68
 
 
69
 
    def do_chunk(self, bytes):
70
 
        """No-op."""
71
 
        pass
72
 
 
73
 
    def do_end(self):
74
 
        raise errors.NoSuchFile('xyzzy')
75
 
 
76
 
 
77
 
class CheckJailRequest(request.SmartServerRequest):
78
 
 
79
 
    def __init__(self, *args):
80
 
        request.SmartServerRequest.__init__(self, *args)
81
 
        self.jail_transports_log = []
82
 
 
83
 
    def do(self):
84
 
        self.jail_transports_log.append(request.jail_info.transports)
85
 
 
86
 
    def do_chunk(self, bytes):
87
 
        self.jail_transports_log.append(request.jail_info.transports)
88
 
 
89
 
    def do_end(self):
90
 
        self.jail_transports_log.append(request.jail_info.transports)
91
 
 
92
 
 
93
 
class TestErrors(TestCase):
94
 
 
95
 
    def test_disabled_method(self):
96
 
        error = request.DisabledMethod("class name")
97
 
        self.assertEqualDiff(
98
 
            "The smart server method 'class name' is disabled.", str(error))
99
 
 
100
 
 
101
 
class TestSmartRequest(TestCase):
102
 
 
103
 
    def test_request_class_without_do_body(self):
104
 
        """If a request has no body data, and the request's implementation does
105
 
        not override do_body, then no exception is raised.
106
 
        """
107
 
        # Create a SmartServerRequestHandler with a SmartServerRequest subclass
108
 
        # that does not implement do_body.
109
 
        handler = request.SmartServerRequestHandler(
110
 
            None, {b'foo': NoBodyRequest}, '/')
111
 
        # Emulate a request with no body (i.e. just args).
112
 
        handler.args_received((b'foo',))
113
 
        handler.end_received()
114
 
        # Request done, no exception was raised.
115
 
 
116
 
    def test_only_request_code_is_jailed(self):
117
 
        transport = 'dummy transport'
118
 
        handler = request.SmartServerRequestHandler(
119
 
            transport, {b'foo': CheckJailRequest}, '/')
120
 
        handler.args_received((b'foo',))
121
 
        self.assertEqual(None, request.jail_info.transports)
122
 
        handler.accept_body(b'bytes')
123
 
        self.assertEqual(None, request.jail_info.transports)
124
 
        handler.end_received()
125
 
        self.assertEqual(None, request.jail_info.transports)
126
 
        self.assertEqual(
127
 
            [[transport]] * 3, handler._command.jail_transports_log)
128
 
 
129
 
    def test_all_registered_requests_are_safety_qualified(self):
130
 
        unclassified_requests = []
131
 
        allowed_info = ('read', 'idem', 'mutate', 'semivfs', 'semi', 'stream')
132
 
        for key in request.request_handlers.keys():
133
 
            info = request.request_handlers.get_info(key)
134
 
            if info is None or info not in allowed_info:
135
 
                unclassified_requests.append(key)
136
 
        if unclassified_requests:
137
 
            self.fail('These requests were not categorized as safe/unsafe'
138
 
                      ' to retry: %s' % (unclassified_requests,))
139
 
 
140
 
 
141
 
class TestSmartRequestHandlerErrorTranslation(TestCase):
142
 
    """Tests that SmartServerRequestHandler will translate exceptions raised by
143
 
    a SmartServerRequest into FailedSmartServerResponses.
144
 
    """
145
 
 
146
 
    def assertNoResponse(self, handler):
147
 
        self.assertEqual(None, handler.response)
148
 
 
149
 
    def assertResponseIsTranslatedError(self, handler):
150
 
        expected_translation = (b'NoSuchFile', b'xyzzy')
151
 
        self.assertEqual(
152
 
            request.FailedSmartServerResponse(expected_translation),
153
 
            handler.response)
154
 
 
155
 
    def test_error_translation_from_args_received(self):
156
 
        handler = request.SmartServerRequestHandler(
157
 
            None, {b'foo': DoErrorRequest}, '/')
158
 
        handler.args_received((b'foo',))
159
 
        self.assertResponseIsTranslatedError(handler)
160
 
 
161
 
    def test_error_translation_from_chunk_received(self):
162
 
        handler = request.SmartServerRequestHandler(
163
 
            None, {b'foo': ChunkErrorRequest}, '/')
164
 
        handler.args_received((b'foo',))
165
 
        self.assertNoResponse(handler)
166
 
        handler.accept_body(b'bytes')
167
 
        self.assertResponseIsTranslatedError(handler)
168
 
 
169
 
    def test_error_translation_from_end_received(self):
170
 
        handler = request.SmartServerRequestHandler(
171
 
            None, {b'foo': EndErrorRequest}, '/')
172
 
        handler.args_received((b'foo',))
173
 
        self.assertNoResponse(handler)
174
 
        handler.end_received()
175
 
        self.assertResponseIsTranslatedError(handler)
176
 
 
177
 
    def test_unexpected_error_translation(self):
178
 
        handler = request.SmartServerRequestHandler(
179
 
            None, {b'foo': DoUnexpectedErrorRequest}, '/')
180
 
        handler.args_received((b'foo',))
181
 
        self.assertEqual(
182
 
            request.FailedSmartServerResponse((b'error', b'KeyError', b"1")),
183
 
            handler.response)
184
 
 
185
 
 
186
 
class TestRequestHanderErrorTranslation(TestCase):
187
 
    """Tests for breezy.bzr.smart.request._translate_error."""
188
 
 
189
 
    def assertTranslationEqual(self, expected_tuple, error):
190
 
        self.assertEqual(expected_tuple, request._translate_error(error))
191
 
 
192
 
    def test_NoSuchFile(self):
193
 
        self.assertTranslationEqual(
194
 
            (b'NoSuchFile', b'path'), errors.NoSuchFile('path'))
195
 
 
196
 
    def test_LockContention(self):
197
 
        # For now, LockContentions are always transmitted with no details.
198
 
        # Eventually they should include a relpath or url or something else to
199
 
        # identify which lock is busy.
200
 
        self.assertTranslationEqual(
201
 
            (b'LockContention',), errors.LockContention('lock', 'msg'))
202
 
 
203
 
    def test_TokenMismatch(self):
204
 
        self.assertTranslationEqual(
205
 
            (b'TokenMismatch', b'some-token', b'actual-token'),
206
 
            errors.TokenMismatch(b'some-token', b'actual-token'))
207
 
 
208
 
    def test_MemoryError(self):
209
 
        self.assertTranslationEqual((b"MemoryError",), MemoryError())
210
 
 
211
 
    def test_GhostRevisionsHaveNoRevno(self):
212
 
        self.assertTranslationEqual(
213
 
            (b"GhostRevisionsHaveNoRevno", b'revid1', b'revid2'),
214
 
            errors.GhostRevisionsHaveNoRevno(b'revid1', b'revid2'))
215
 
 
216
 
    def test_generic_Exception(self):
217
 
        self.assertTranslationEqual((b'error', b'Exception', b""),
218
 
                                    Exception())
219
 
 
220
 
    def test_generic_BzrError(self):
221
 
        self.assertTranslationEqual((b'error', b'BzrError', b"some text"),
222
 
                                    errors.BzrError(msg="some text"))
223
 
 
224
 
    def test_generic_zlib_error(self):
225
 
        from zlib import error
226
 
        msg = "Error -3 while decompressing data: incorrect data check"
227
 
        self.assertTranslationEqual((b'error', b'zlib.error', msg.encode('utf-8')),
228
 
                                    error(msg))
229
 
 
230
 
 
231
 
class TestRequestJail(TestCaseWithMemoryTransport):
232
 
 
233
 
    def test_jail(self):
234
 
        transport = self.get_transport('blah')
235
 
        req = request.SmartServerRequest(transport)
236
 
        self.assertEqual(None, request.jail_info.transports)
237
 
        req.setup_jail()
238
 
        self.assertEqual([transport], request.jail_info.transports)
239
 
        req.teardown_jail()
240
 
        self.assertEqual(None, request.jail_info.transports)
241
 
 
242
 
 
243
 
class TestJailHook(TestCaseWithMemoryTransport):
244
 
 
245
 
    def setUp(self):
246
 
        super(TestJailHook, self).setUp()
247
 
 
248
 
        def clear_jail_info():
249
 
            request.jail_info.transports = None
250
 
        self.addCleanup(clear_jail_info)
251
 
 
252
 
    def test_jail_hook(self):
253
 
        request.jail_info.transports = None
254
 
        _pre_open_hook = request._pre_open_hook
255
 
        # Any transport is fine if jail_info.transports is None
256
 
        t = self.get_transport('foo')
257
 
        _pre_open_hook(t)
258
 
        # A transport in jail_info.transports is allowed
259
 
        request.jail_info.transports = [t]
260
 
        _pre_open_hook(t)
261
 
        # A child of a transport in jail_info is allowed
262
 
        _pre_open_hook(t.clone('child'))
263
 
        # A parent is not allowed
264
 
        self.assertRaises(errors.JailBreak, _pre_open_hook, t.clone('..'))
265
 
        # A completely unrelated transport is not allowed
266
 
        self.assertRaises(errors.JailBreak, _pre_open_hook,
267
 
                          transport.get_transport_from_url('http://host/'))
268
 
 
269
 
    def test_open_bzrdir_in_non_main_thread(self):
270
 
        """Opening a bzrdir in a non-main thread should work ok.
271
 
 
272
 
        This makes sure that the globally-installed
273
 
        breezy.bzr.smart.request._pre_open_hook, which uses a threading.local(),
274
 
        works in a newly created thread.
275
 
        """
276
 
        bzrdir = self.make_controldir('.')
277
 
        transport = bzrdir.root_transport
278
 
        thread_result = []
279
 
 
280
 
        def t():
281
 
            BzrDir.open_from_transport(transport)
282
 
            thread_result.append('ok')
283
 
        thread = threading.Thread(target=t)
284
 
        thread.start()
285
 
        thread.join()
286
 
        self.assertEqual(['ok'], thread_result)