~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_request.py

  • Committer: Ian Clatworthy
  • Date: 2009-09-09 15:30:59 UTC
  • mto: (4634.37.2 prepare-2.0)
  • mto: This revision was merged to the branch mainline in revision 4689.
  • Revision ID: ian.clatworthy@canonical.com-20090909153059-sb038agvd38ci2q8
more link fixes in the User Guide

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 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
18
18
 
19
19
import threading
20
20
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    transport,
24
 
    )
 
21
from bzrlib import errors
25
22
from bzrlib.bzrdir import BzrDir
26
23
from bzrlib.smart import request
27
24
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
 
25
from bzrlib.transport import get_transport
28
26
 
29
27
 
30
28
class NoBodyRequest(request.SmartServerRequest):
36
34
 
37
35
class DoErrorRequest(request.SmartServerRequest):
38
36
    """A request that raises an error from self.do()."""
39
 
 
 
37
    
40
38
    def do(self):
41
39
        raise errors.NoSuchFile('xyzzy')
42
40
 
43
41
 
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
42
class ChunkErrorRequest(request.SmartServerRequest):
52
43
    """A request that raises an error from self.do_chunk()."""
53
44
    
118
109
        self.assertEqual(
119
110
            [[transport]] * 3, handler._command.jail_transports_log)
120
111
 
121
 
    def test_all_registered_requests_are_safety_qualified(self):
122
 
        unclassified_requests = []
123
 
        allowed_info = ('read', 'idem', 'mutate', 'semivfs', 'semi', 'stream')
124
 
        for key in request.request_handlers.keys():
125
 
            info = request.request_handlers.get_info(key)
126
 
            if info is None or info not in allowed_info:
127
 
                unclassified_requests.append(key)
128
 
        if unclassified_requests:
129
 
            self.fail('These requests were not categorized as safe/unsafe'
130
 
                      ' to retry: %s'  % (unclassified_requests,))
131
112
 
132
113
 
133
114
class TestSmartRequestHandlerErrorTranslation(TestCase):
166
147
        handler.end_received()
167
148
        self.assertResponseIsTranslatedError(handler)
168
149
 
169
 
    def test_unexpected_error_translation(self):
170
 
        handler = request.SmartServerRequestHandler(
171
 
            None, {'foo': DoUnexpectedErrorRequest}, '/')
172
 
        handler.args_received(('foo',))
173
 
        self.assertEqual(
174
 
            request.FailedSmartServerResponse(('error', 'KeyError', "1")),
175
 
            handler.response)
176
 
 
177
150
 
178
151
class TestRequestHanderErrorTranslation(TestCase):
179
152
    """Tests for bzrlib.smart.request._translate_error."""
197
170
            ('TokenMismatch', 'some-token', 'actual-token'),
198
171
            errors.TokenMismatch('some-token', 'actual-token'))
199
172
 
200
 
    def test_MemoryError(self):
201
 
        self.assertTranslationEqual(("MemoryError",), MemoryError())
202
 
 
203
 
    def test_generic_Exception(self):
204
 
        self.assertTranslationEqual(('error', 'Exception', ""),
205
 
            Exception())
206
 
 
207
 
    def test_generic_BzrError(self):
208
 
        self.assertTranslationEqual(('error', 'BzrError', "some text"),
209
 
            errors.BzrError(msg="some text"))
210
 
 
211
 
    def test_generic_zlib_error(self):
212
 
        from zlib import error
213
 
        msg = "Error -3 while decompressing data: incorrect data check"
214
 
        self.assertTranslationEqual(('error', 'zlib.error', msg),
215
 
            error(msg))
216
 
 
217
173
 
218
174
class TestRequestJail(TestCaseWithMemoryTransport):
219
 
 
 
175
    
220
176
    def test_jail(self):
221
177
        transport = self.get_transport('blah')
222
178
        req = request.SmartServerRequest(transport)
229
185
 
230
186
class TestJailHook(TestCaseWithMemoryTransport):
231
187
 
232
 
    def setUp(self):
233
 
        super(TestJailHook, self).setUp()
234
 
        def clear_jail_info():
235
 
            request.jail_info.transports = None
236
 
        self.addCleanup(clear_jail_info)
 
188
    def tearDown(self):
 
189
        request.jail_info.transports = None
 
190
        TestCaseWithMemoryTransport.tearDown(self)
237
191
 
238
192
    def test_jail_hook(self):
239
193
        request.jail_info.transports = None
249
203
        # A parent is not allowed
250
204
        self.assertRaises(errors.JailBreak, _pre_open_hook, t.clone('..'))
251
205
        # A completely unrelated transport is not allowed
252
 
        self.assertRaises(errors.JailBreak, _pre_open_hook,
253
 
                          transport.get_transport_from_url('http://host/'))
 
206
        self.assertRaises(
 
207
            errors.JailBreak, _pre_open_hook, get_transport('http://host/'))
254
208
 
255
209
    def test_open_bzrdir_in_non_main_thread(self):
256
210
        """Opening a bzrdir in a non-main thread should work ok.