~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_request.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-17 18:13:57 UTC
  • mfrom: (5268.7.29 transport-segments)
  • Revision ID: pqm@pqm.ubuntu.com-20110817181357-y5q5eth1hk8bl3om
(jelmer) Allow specifying the colocated branch to use in the branch URL,
 and retrieving the branch name using ControlDir._get_selected_branch.
 (Jelmer Vernooij)

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
18
18
 
19
19
import threading
20
20
 
21
 
from bzrlib import errors
 
21
from bzrlib import (
 
22
    errors,
 
23
    transport,
 
24
    )
22
25
from bzrlib.bzrdir import BzrDir
23
26
from bzrlib.smart import request
24
27
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
25
 
from bzrlib.transport import get_transport
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
    
147
156
        handler.end_received()
148
157
        self.assertResponseIsTranslatedError(handler)
149
158
 
 
159
    def test_unexpected_error_translation(self):
 
160
        handler = request.SmartServerRequestHandler(
 
161
            None, {'foo': DoUnexpectedErrorRequest}, '/')
 
162
        handler.args_received(('foo',))
 
163
        self.assertEqual(
 
164
            request.FailedSmartServerResponse(('error', 'KeyError', "1")),
 
165
            handler.response)
 
166
 
150
167
 
151
168
class TestRequestHanderErrorTranslation(TestCase):
152
169
    """Tests for bzrlib.smart.request._translate_error."""
170
187
            ('TokenMismatch', 'some-token', 'actual-token'),
171
188
            errors.TokenMismatch('some-token', 'actual-token'))
172
189
 
 
190
    def test_MemoryError(self):
 
191
        self.assertTranslationEqual(("MemoryError",), MemoryError())
 
192
 
 
193
    def test_generic_Exception(self):
 
194
        self.assertTranslationEqual(('error', 'Exception', ""),
 
195
            Exception())
 
196
 
 
197
    def test_generic_BzrError(self):
 
198
        self.assertTranslationEqual(('error', 'BzrError', "some text"),
 
199
            errors.BzrError(msg="some text"))
 
200
 
 
201
    def test_generic_zlib_error(self):
 
202
        from zlib import error
 
203
        msg = "Error -3 while decompressing data: incorrect data check"
 
204
        self.assertTranslationEqual(('error', 'zlib.error', msg),
 
205
            error(msg))
 
206
 
173
207
 
174
208
class TestRequestJail(TestCaseWithMemoryTransport):
175
 
    
 
209
 
176
210
    def test_jail(self):
177
211
        transport = self.get_transport('blah')
178
212
        req = request.SmartServerRequest(transport)
185
219
 
186
220
class TestJailHook(TestCaseWithMemoryTransport):
187
221
 
188
 
    def tearDown(self):
189
 
        request.jail_info.transports = None
190
 
        TestCaseWithMemoryTransport.tearDown(self)
 
222
    def setUp(self):
 
223
        super(TestJailHook, self).setUp()
 
224
        def clear_jail_info():
 
225
            request.jail_info.transports = None
 
226
        self.addCleanup(clear_jail_info)
191
227
 
192
228
    def test_jail_hook(self):
193
229
        request.jail_info.transports = None
203
239
        # A parent is not allowed
204
240
        self.assertRaises(errors.JailBreak, _pre_open_hook, t.clone('..'))
205
241
        # A completely unrelated transport is not allowed
206
 
        self.assertRaises(
207
 
            errors.JailBreak, _pre_open_hook, get_transport('http://host/'))
 
242
        self.assertRaises(errors.JailBreak, _pre_open_hook,
 
243
                          transport.get_transport('http://host/'))
208
244
 
209
245
    def test_open_bzrdir_in_non_main_thread(self):
210
246
        """Opening a bzrdir in a non-main thread should work ok.