/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: Jelmer Vernooij
  • Date: 2017-09-01 07:15:43 UTC
  • mfrom: (6770.3.2 py3_test_cleanup)
  • Revision ID: jelmer@jelmer.uk-20170901071543-1t83321xkog9qrxh
Merge lp:~gz/brz/py3_test_cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
 
17
"""Tests for smart server request infrastructure (breezy.bzr.smart.request)."""
18
18
 
19
19
import threading
20
20
 
21
 
from bzrlib import errors
22
 
from bzrlib.bzrdir import BzrDir
23
 
from bzrlib.smart import request
24
 
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
25
 
from bzrlib.transport import get_transport
 
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
26
28
 
27
29
 
28
30
class NoBodyRequest(request.SmartServerRequest):
34
36
 
35
37
class DoErrorRequest(request.SmartServerRequest):
36
38
    """A request that raises an error from self.do()."""
37
 
    
 
39
 
38
40
    def do(self):
39
41
        raise errors.NoSuchFile('xyzzy')
40
42
 
41
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
 
42
51
class ChunkErrorRequest(request.SmartServerRequest):
43
52
    """A request that raises an error from self.do_chunk()."""
44
53
    
81
90
        self.jail_transports_log.append(request.jail_info.transports)
82
91
 
83
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
 
84
101
class TestSmartRequest(TestCase):
85
102
 
86
103
    def test_request_class_without_do_body(self):
109
126
        self.assertEqual(
110
127
            [[transport]] * 3, handler._command.jail_transports_log)
111
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,))
112
139
 
113
140
 
114
141
class TestSmartRequestHandlerErrorTranslation(TestCase):
147
174
        handler.end_received()
148
175
        self.assertResponseIsTranslatedError(handler)
149
176
 
 
177
    def test_unexpected_error_translation(self):
 
178
        handler = request.SmartServerRequestHandler(
 
179
            None, {'foo': DoUnexpectedErrorRequest}, '/')
 
180
        handler.args_received(('foo',))
 
181
        self.assertEqual(
 
182
            request.FailedSmartServerResponse(('error', 'KeyError', "1")),
 
183
            handler.response)
 
184
 
150
185
 
151
186
class TestRequestHanderErrorTranslation(TestCase):
152
 
    """Tests for bzrlib.smart.request._translate_error."""
 
187
    """Tests for breezy.bzr.smart.request._translate_error."""
153
188
 
154
189
    def assertTranslationEqual(self, expected_tuple, error):
155
190
        self.assertEqual(expected_tuple, request._translate_error(error))
170
205
            ('TokenMismatch', 'some-token', 'actual-token'),
171
206
            errors.TokenMismatch('some-token', 'actual-token'))
172
207
 
 
208
    def test_MemoryError(self):
 
209
        self.assertTranslationEqual(("MemoryError",), MemoryError())
 
210
 
 
211
    def test_generic_Exception(self):
 
212
        self.assertTranslationEqual(('error', 'Exception', ""),
 
213
            Exception())
 
214
 
 
215
    def test_generic_BzrError(self):
 
216
        self.assertTranslationEqual(('error', 'BzrError', "some text"),
 
217
            errors.BzrError(msg="some text"))
 
218
 
 
219
    def test_generic_zlib_error(self):
 
220
        from zlib import error
 
221
        msg = "Error -3 while decompressing data: incorrect data check"
 
222
        self.assertTranslationEqual(('error', 'zlib.error', msg),
 
223
            error(msg))
 
224
 
173
225
 
174
226
class TestRequestJail(TestCaseWithMemoryTransport):
175
 
    
 
227
 
176
228
    def test_jail(self):
177
229
        transport = self.get_transport('blah')
178
230
        req = request.SmartServerRequest(transport)
185
237
 
186
238
class TestJailHook(TestCaseWithMemoryTransport):
187
239
 
188
 
    def tearDown(self):
189
 
        request.jail_info.transports = None
190
 
        TestCaseWithMemoryTransport.tearDown(self)
 
240
    def setUp(self):
 
241
        super(TestJailHook, self).setUp()
 
242
        def clear_jail_info():
 
243
            request.jail_info.transports = None
 
244
        self.addCleanup(clear_jail_info)
191
245
 
192
246
    def test_jail_hook(self):
193
247
        request.jail_info.transports = None
203
257
        # A parent is not allowed
204
258
        self.assertRaises(errors.JailBreak, _pre_open_hook, t.clone('..'))
205
259
        # A completely unrelated transport is not allowed
206
 
        self.assertRaises(
207
 
            errors.JailBreak, _pre_open_hook, get_transport('http://host/'))
 
260
        self.assertRaises(errors.JailBreak, _pre_open_hook,
 
261
                          transport.get_transport_from_url('http://host/'))
208
262
 
209
263
    def test_open_bzrdir_in_non_main_thread(self):
210
264
        """Opening a bzrdir in a non-main thread should work ok.
211
265
        
212
266
        This makes sure that the globally-installed
213
 
        bzrlib.smart.request._pre_open_hook, which uses a threading.local(),
 
267
        breezy.bzr.smart.request._pre_open_hook, which uses a threading.local(),
214
268
        works in a newly created thread.
215
269
        """
216
 
        bzrdir = self.make_bzrdir('.')
 
270
        bzrdir = self.make_controldir('.')
217
271
        transport = bzrdir.root_transport
218
272
        thread_result = []
219
273
        def t():