/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_smart_request.py

  • Committer: Sabin Iacob
  • Date: 2009-03-23 14:59:43 UTC
  • mto: (4189.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4193.
  • Revision ID: iacobs@m0n5t3r.info-20090323145943-3s3p1px5q1rkh2e5
update FSF mailing address

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
18
18
 
19
 
import threading
20
 
 
21
19
from bzrlib import errors
22
 
from bzrlib.bzrdir import BzrDir
23
20
from bzrlib.smart import request
24
 
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
25
 
from bzrlib.transport import get_transport
 
21
from bzrlib.tests import TestCase
26
22
 
27
23
 
28
24
class NoBodyRequest(request.SmartServerRequest):
65
61
        raise errors.NoSuchFile('xyzzy')
66
62
 
67
63
 
68
 
class CheckJailRequest(request.SmartServerRequest):
69
 
 
70
 
    def __init__(self, *args):
71
 
        request.SmartServerRequest.__init__(self, *args)
72
 
        self.jail_transports_log = []
73
 
 
74
 
    def do(self):
75
 
        self.jail_transports_log.append(request.jail_info.transports)
76
 
 
77
 
    def do_chunk(self, bytes):
78
 
        self.jail_transports_log.append(request.jail_info.transports)
79
 
 
80
 
    def do_end(self):
81
 
        self.jail_transports_log.append(request.jail_info.transports)
82
 
 
83
 
 
84
64
class TestSmartRequest(TestCase):
85
65
 
86
66
    def test_request_class_without_do_body(self):
96
76
        handler.end_received()
97
77
        # Request done, no exception was raised.
98
78
 
99
 
    def test_only_request_code_is_jailed(self):
100
 
        transport = 'dummy transport'
101
 
        handler = request.SmartServerRequestHandler(
102
 
            transport, {'foo': CheckJailRequest}, '/')
103
 
        handler.args_received(('foo',))
104
 
        self.assertEqual(None, request.jail_info.transports)
105
 
        handler.accept_body('bytes')
106
 
        self.assertEqual(None, request.jail_info.transports)
107
 
        handler.end_received()
108
 
        self.assertEqual(None, request.jail_info.transports)
109
 
        self.assertEqual(
110
 
            [[transport]] * 3, handler._command.jail_transports_log)
111
 
 
112
 
 
113
79
 
114
80
class TestSmartRequestHandlerErrorTranslation(TestCase):
115
81
    """Tests that SmartServerRequestHandler will translate exceptions raised by
159
125
            ('NoSuchFile', 'path'), errors.NoSuchFile('path'))
160
126
 
161
127
    def test_LockContention(self):
162
 
        # For now, LockContentions are always transmitted with no details.
163
 
        # Eventually they should include a relpath or url or something else to
164
 
        # identify which lock is busy.
165
128
        self.assertTranslationEqual(
166
 
            ('LockContention',), errors.LockContention('lock', 'msg'))
 
129
            ('LockContention', 'lock', 'msg'),
 
130
            errors.LockContention('lock', 'msg'))
167
131
 
168
132
    def test_TokenMismatch(self):
169
133
        self.assertTranslationEqual(
170
134
            ('TokenMismatch', 'some-token', 'actual-token'),
171
135
            errors.TokenMismatch('some-token', 'actual-token'))
172
136
 
173
 
 
174
 
class TestRequestJail(TestCaseWithMemoryTransport):
175
 
    
176
 
    def test_jail(self):
177
 
        transport = self.get_transport('blah')
178
 
        req = request.SmartServerRequest(transport)
179
 
        self.assertEqual(None, request.jail_info.transports)
180
 
        req.setup_jail()
181
 
        self.assertEqual([transport], request.jail_info.transports)
182
 
        req.teardown_jail()
183
 
        self.assertEqual(None, request.jail_info.transports)
184
 
 
185
 
 
186
 
class TestJailHook(TestCaseWithMemoryTransport):
187
 
 
188
 
    def tearDown(self):
189
 
        request.jail_info.transports = None
190
 
        TestCaseWithMemoryTransport.tearDown(self)
191
 
 
192
 
    def test_jail_hook(self):
193
 
        request.jail_info.transports = None
194
 
        _pre_open_hook = request._pre_open_hook
195
 
        # Any transport is fine if jail_info.transports is None
196
 
        t = self.get_transport('foo')
197
 
        _pre_open_hook(t)
198
 
        # A transport in jail_info.transports is allowed
199
 
        request.jail_info.transports = [t]
200
 
        _pre_open_hook(t)
201
 
        # A child of a transport in jail_info is allowed
202
 
        _pre_open_hook(t.clone('child'))
203
 
        # A parent is not allowed
204
 
        self.assertRaises(errors.JailBreak, _pre_open_hook, t.clone('..'))
205
 
        # A completely unrelated transport is not allowed
206
 
        self.assertRaises(
207
 
            errors.JailBreak, _pre_open_hook, get_transport('http://host/'))
208
 
 
209
 
    def test_open_bzrdir_in_non_main_thread(self):
210
 
        """Opening a bzrdir in a non-main thread should work ok.
211
 
        
212
 
        This makes sure that the globally-installed
213
 
        bzrlib.smart.request._pre_open_hook, which uses a threading.local(),
214
 
        works in a newly created thread.
215
 
        """
216
 
        bzrdir = self.make_bzrdir('.')
217
 
        transport = bzrdir.root_transport
218
 
        thread_result = []
219
 
        def t():
220
 
            BzrDir.open_from_transport(transport)
221
 
            thread_result.append('ok')
222
 
        thread = threading.Thread(target=t)
223
 
        thread.start()
224
 
        thread.join()
225
 
        self.assertEqual(['ok'], thread_result)
226