/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
1
# Copyright (C) 2009 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
16
17
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
18
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
19
import threading
20
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
21
from bzrlib import errors
4205.2.1 by Andrew Bennetts
Fix BzrDir.open in non-main (and non-server-request) thread when bzrlib.smart.request's _pre_open_hook is installed.
22
from bzrlib.bzrdir import BzrDir
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
23
from bzrlib.smart import request
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
24
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
4160.2.5 by Andrew Bennetts
Add test_jail_hook.
25
from bzrlib.transport import get_transport
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
26
27
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
28
class NoBodyRequest(request.SmartServerRequest):
29
    """A request that does not implement do_body."""
30
31
    def do(self):
32
        return request.SuccessfulSmartServerResponse(('ok',))
33
34
4144.3.1 by Andrew Bennetts
Add Repository.insert_stream_locked server-side implementation, plus tests for server-side _translate_error.
35
class DoErrorRequest(request.SmartServerRequest):
36
    """A request that raises an error from self.do()."""
37
    
38
    def do(self):
39
        raise errors.NoSuchFile('xyzzy')
40
41
42
class ChunkErrorRequest(request.SmartServerRequest):
43
    """A request that raises an error from self.do_chunk()."""
44
    
45
    def do(self):
46
        """No-op."""
47
        pass
48
49
    def do_chunk(self, bytes):
50
        raise errors.NoSuchFile('xyzzy')
51
52
53
class EndErrorRequest(request.SmartServerRequest):
54
    """A request that raises an error from self.do_end()."""
55
    
56
    def do(self):
57
        """No-op."""
58
        pass
59
60
    def do_chunk(self, bytes):
61
        """No-op."""
62
        pass
63
        
64
    def do_end(self):
65
        raise errors.NoSuchFile('xyzzy')
66
67
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
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
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
84
class TestSmartRequest(TestCase):
85
86
    def test_request_class_without_do_body(self):
87
        """If a request has no body data, and the request's implementation does
88
        not override do_body, then no exception is raised.
89
        """
90
        # Create a SmartServerRequestHandler with a SmartServerRequest subclass
91
        # that does not implement do_body.
92
        handler = request.SmartServerRequestHandler(
93
            None, {'foo': NoBodyRequest}, '/')
94
        # Emulate a request with no body (i.e. just args).
95
        handler.args_received(('foo',))
96
        handler.end_received()
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
97
        # Request done, no exception was raised.
98
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
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
4797.85.31 by John Arbash Meinel
Categorize all of the requests as safe/unsafe/semi/stream for retrying purposes.
112
    def test_all_registered_requests_are_safety_qualified(self):
113
        unclassified_requests = []
4797.85.32 by John Arbash Meinel
Per request, change
114
        allowed_info = ('read', 'idem', 'mutate', 'semivfs', 'semi', 'stream')
4797.85.31 by John Arbash Meinel
Categorize all of the requests as safe/unsafe/semi/stream for retrying purposes.
115
        for key in request.request_handlers.keys():
116
            info = request.request_handlers.get_info(key)
4797.85.32 by John Arbash Meinel
Per request, change
117
            if info is None or info not in allowed_info:
4797.85.31 by John Arbash Meinel
Categorize all of the requests as safe/unsafe/semi/stream for retrying purposes.
118
                unclassified_requests.append(key)
119
        if unclassified_requests:
120
            self.fail('These requests were not categorized as safe/unsafe'
121
                      ' to retry: %s'  % (unclassified_requests,))
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
122
4144.3.1 by Andrew Bennetts
Add Repository.insert_stream_locked server-side implementation, plus tests for server-side _translate_error.
123
124
class TestSmartRequestHandlerErrorTranslation(TestCase):
125
    """Tests that SmartServerRequestHandler will translate exceptions raised by
126
    a SmartServerRequest into FailedSmartServerResponses.
127
    """
128
129
    def assertNoResponse(self, handler):
130
        self.assertEqual(None, handler.response)
131
132
    def assertResponseIsTranslatedError(self, handler):
133
        expected_translation = ('NoSuchFile', 'xyzzy')
134
        self.assertEqual(
135
            request.FailedSmartServerResponse(expected_translation),
136
            handler.response)
137
138
    def test_error_translation_from_args_received(self):
139
        handler = request.SmartServerRequestHandler(
140
            None, {'foo': DoErrorRequest}, '/')
141
        handler.args_received(('foo',))
142
        self.assertResponseIsTranslatedError(handler)
143
144
    def test_error_translation_from_chunk_received(self):
145
        handler = request.SmartServerRequestHandler(
146
            None, {'foo': ChunkErrorRequest}, '/')
147
        handler.args_received(('foo',))
148
        self.assertNoResponse(handler)
149
        handler.accept_body('bytes')
150
        self.assertResponseIsTranslatedError(handler)
151
152
    def test_error_translation_from_end_received(self):
153
        handler = request.SmartServerRequestHandler(
154
            None, {'foo': EndErrorRequest}, '/')
155
        handler.args_received(('foo',))
156
        self.assertNoResponse(handler)
157
        handler.end_received()
158
        self.assertResponseIsTranslatedError(handler)
159
160
161
class TestRequestHanderErrorTranslation(TestCase):
162
    """Tests for bzrlib.smart.request._translate_error."""
163
164
    def assertTranslationEqual(self, expected_tuple, error):
165
        self.assertEqual(expected_tuple, request._translate_error(error))
166
167
    def test_NoSuchFile(self):
168
        self.assertTranslationEqual(
169
            ('NoSuchFile', 'path'), errors.NoSuchFile('path'))
170
171
    def test_LockContention(self):
4556.2.7 by Andrew Bennetts
Update test.
172
        # For now, LockContentions are always transmitted with no details.
173
        # Eventually they should include a relpath or url or something else to
174
        # identify which lock is busy.
4144.3.1 by Andrew Bennetts
Add Repository.insert_stream_locked server-side implementation, plus tests for server-side _translate_error.
175
        self.assertTranslationEqual(
4556.2.7 by Andrew Bennetts
Update test.
176
            ('LockContention',), errors.LockContention('lock', 'msg'))
4144.3.1 by Andrew Bennetts
Add Repository.insert_stream_locked server-side implementation, plus tests for server-side _translate_error.
177
178
    def test_TokenMismatch(self):
179
        self.assertTranslationEqual(
180
            ('TokenMismatch', 'some-token', 'actual-token'),
181
            errors.TokenMismatch('some-token', 'actual-token'))
182
4160.2.2 by Andrew Bennetts
Add setup_jail and teardown_jail to SmartServerRequest.
183
184
class TestRequestJail(TestCaseWithMemoryTransport):
185
    
186
    def test_jail(self):
187
        transport = self.get_transport('blah')
188
        req = request.SmartServerRequest(transport)
189
        self.assertEqual(None, request.jail_info.transports)
190
        req.setup_jail()
191
        self.assertEqual([transport], request.jail_info.transports)
192
        req.teardown_jail()
193
        self.assertEqual(None, request.jail_info.transports)
194
4160.2.5 by Andrew Bennetts
Add test_jail_hook.
195
196
class TestJailHook(TestCaseWithMemoryTransport):
197
198
    def tearDown(self):
199
        request.jail_info.transports = None
200
        TestCaseWithMemoryTransport.tearDown(self)
201
202
    def test_jail_hook(self):
203
        request.jail_info.transports = None
204
        _pre_open_hook = request._pre_open_hook
205
        # Any transport is fine if jail_info.transports is None
206
        t = self.get_transport('foo')
207
        _pre_open_hook(t)
208
        # A transport in jail_info.transports is allowed
209
        request.jail_info.transports = [t]
210
        _pre_open_hook(t)
211
        # A child of a transport in jail_info is allowed
212
        _pre_open_hook(t.clone('child'))
213
        # A parent is not allowed
4294.2.10 by Robert Collins
Review feedback.
214
        self.assertRaises(errors.JailBreak, _pre_open_hook, t.clone('..'))
4160.2.5 by Andrew Bennetts
Add test_jail_hook.
215
        # A completely unrelated transport is not allowed
216
        self.assertRaises(
4294.2.10 by Robert Collins
Review feedback.
217
            errors.JailBreak, _pre_open_hook, get_transport('http://host/'))
4160.2.5 by Andrew Bennetts
Add test_jail_hook.
218
4205.2.1 by Andrew Bennetts
Fix BzrDir.open in non-main (and non-server-request) thread when bzrlib.smart.request's _pre_open_hook is installed.
219
    def test_open_bzrdir_in_non_main_thread(self):
220
        """Opening a bzrdir in a non-main thread should work ok.
221
        
222
        This makes sure that the globally-installed
223
        bzrlib.smart.request._pre_open_hook, which uses a threading.local(),
224
        works in a newly created thread.
225
        """
226
        bzrdir = self.make_bzrdir('.')
227
        transport = bzrdir.root_transport
228
        thread_result = []
229
        def t():
230
            BzrDir.open_from_transport(transport)
231
            thread_result.append('ok')
232
        thread = threading.Thread(target=t)
233
        thread.start()
234
        thread.join()
235
        self.assertEqual(['ok'], thread_result)
236