~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_request.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
18
18
 
 
19
import threading
 
20
 
19
21
from bzrlib import errors
 
22
from bzrlib.bzrdir import BzrDir
20
23
from bzrlib.smart import request
21
 
from bzrlib.tests import TestCase
 
24
from bzrlib.tests import TestCase, TestCaseWithMemoryTransport
 
25
from bzrlib.transport import get_transport
22
26
 
23
27
 
24
28
class NoBodyRequest(request.SmartServerRequest):
28
32
        return request.SuccessfulSmartServerResponse(('ok',))
29
33
 
30
34
 
 
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
 
 
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
 
31
84
class TestSmartRequest(TestCase):
32
85
 
33
86
    def test_request_class_without_do_body(self):
43
96
        handler.end_received()
44
97
        # Request done, no exception was raised.
45
98
 
46
 
    def test_unexpected_body(self):
47
 
        """If a request implementation receives an unexpected body, it
48
 
        raises an error.
 
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
 
 
114
class TestSmartRequestHandlerErrorTranslation(TestCase):
 
115
    """Tests that SmartServerRequestHandler will translate exceptions raised by
 
116
    a SmartServerRequest into FailedSmartServerResponses.
 
117
    """
 
118
 
 
119
    def assertNoResponse(self, handler):
 
120
        self.assertEqual(None, handler.response)
 
121
 
 
122
    def assertResponseIsTranslatedError(self, handler):
 
123
        expected_translation = ('NoSuchFile', 'xyzzy')
 
124
        self.assertEqual(
 
125
            request.FailedSmartServerResponse(expected_translation),
 
126
            handler.response)
 
127
 
 
128
    def test_error_translation_from_args_received(self):
 
129
        handler = request.SmartServerRequestHandler(
 
130
            None, {'foo': DoErrorRequest}, '/')
 
131
        handler.args_received(('foo',))
 
132
        self.assertResponseIsTranslatedError(handler)
 
133
 
 
134
    def test_error_translation_from_chunk_received(self):
 
135
        handler = request.SmartServerRequestHandler(
 
136
            None, {'foo': ChunkErrorRequest}, '/')
 
137
        handler.args_received(('foo',))
 
138
        self.assertNoResponse(handler)
 
139
        handler.accept_body('bytes')
 
140
        self.assertResponseIsTranslatedError(handler)
 
141
 
 
142
    def test_error_translation_from_end_received(self):
 
143
        handler = request.SmartServerRequestHandler(
 
144
            None, {'foo': EndErrorRequest}, '/')
 
145
        handler.args_received(('foo',))
 
146
        self.assertNoResponse(handler)
 
147
        handler.end_received()
 
148
        self.assertResponseIsTranslatedError(handler)
 
149
 
 
150
 
 
151
class TestRequestHanderErrorTranslation(TestCase):
 
152
    """Tests for bzrlib.smart.request._translate_error."""
 
153
 
 
154
    def assertTranslationEqual(self, expected_tuple, error):
 
155
        self.assertEqual(expected_tuple, request._translate_error(error))
 
156
 
 
157
    def test_NoSuchFile(self):
 
158
        self.assertTranslationEqual(
 
159
            ('NoSuchFile', 'path'), errors.NoSuchFile('path'))
 
160
 
 
161
    def test_LockContention(self):
 
162
        self.assertTranslationEqual(
 
163
            ('LockContention', 'lock', 'msg'),
 
164
            errors.LockContention('lock', 'msg'))
 
165
 
 
166
    def test_TokenMismatch(self):
 
167
        self.assertTranslationEqual(
 
168
            ('TokenMismatch', 'some-token', 'actual-token'),
 
169
            errors.TokenMismatch('some-token', 'actual-token'))
 
170
 
 
171
 
 
172
class TestRequestJail(TestCaseWithMemoryTransport):
 
173
    
 
174
    def test_jail(self):
 
175
        transport = self.get_transport('blah')
 
176
        req = request.SmartServerRequest(transport)
 
177
        self.assertEqual(None, request.jail_info.transports)
 
178
        req.setup_jail()
 
179
        self.assertEqual([transport], request.jail_info.transports)
 
180
        req.teardown_jail()
 
181
        self.assertEqual(None, request.jail_info.transports)
 
182
 
 
183
 
 
184
class TestJailHook(TestCaseWithMemoryTransport):
 
185
 
 
186
    def tearDown(self):
 
187
        request.jail_info.transports = None
 
188
        TestCaseWithMemoryTransport.tearDown(self)
 
189
 
 
190
    def test_jail_hook(self):
 
191
        request.jail_info.transports = None
 
192
        _pre_open_hook = request._pre_open_hook
 
193
        # Any transport is fine if jail_info.transports is None
 
194
        t = self.get_transport('foo')
 
195
        _pre_open_hook(t)
 
196
        # A transport in jail_info.transports is allowed
 
197
        request.jail_info.transports = [t]
 
198
        _pre_open_hook(t)
 
199
        # A child of a transport in jail_info is allowed
 
200
        _pre_open_hook(t.clone('child'))
 
201
        # A parent is not allowed
 
202
        self.assertRaises(errors.BzrError, _pre_open_hook, t.clone('..'))
 
203
        # A completely unrelated transport is not allowed
 
204
        self.assertRaises(
 
205
            errors.BzrError, _pre_open_hook, get_transport('http://host/'))
 
206
 
 
207
    def test_open_bzrdir_in_non_main_thread(self):
 
208
        """Opening a bzrdir in a non-main thread should work ok.
 
209
        
 
210
        This makes sure that the globally-installed
 
211
        bzrlib.smart.request._pre_open_hook, which uses a threading.local(),
 
212
        works in a newly created thread.
49
213
        """
50
 
        # Create a SmartServerRequestHandler with a SmartServerRequest subclass
51
 
        # that does not implement do_body.
52
 
        handler = request.SmartServerRequestHandler(
53
 
            None, {'foo': NoBodyRequest}, '/')
54
 
        # Emulate a request with a body
55
 
        handler.args_received(('foo',))
56
 
        handler.accept_body('some body bytes')
57
 
        # Note that the exception currently occurs at the end of the request.
58
 
        # In principle it would also be ok for it to happen earlier, during
59
 
        # accept_body.
60
 
        exc = self.assertRaises(errors.SmartProtocolError, handler.end_received)
61
 
        self.assertEquals('Request does not expect a body', exc.details)
 
214
        bzrdir = self.make_bzrdir('.')
 
215
        transport = bzrdir.root_transport
 
216
        thread_result = []
 
217
        def t():
 
218
            BzrDir.open_from_transport(transport)
 
219
            thread_result.append('ok')
 
220
        thread = threading.Thread(target=t)
 
221
        thread.start()
 
222
        thread.join()
 
223
        self.assertEqual(['ok'], thread_result)
62
224