~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Andrew Bennetts
  • Date: 2008-04-07 08:17:38 UTC
  • mto: This revision was merged to the branch mainline in revision 3349.
  • Revision ID: andrew.bennetts@canonical.com-20080407081738-yx4c53pvqfif313d
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod.  Callers no longer need to do their own ad hoc unknown smart method error detection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
        """Create a FakeClient.
136
136
 
137
137
        :param responses: A list of response-tuple, body-data pairs to be sent
138
 
            back to callers.
 
138
            back to callers.  A special case is if the response-tuple is
 
139
            'unknown verb', then a UnknownSmartMethod will be raised for that
 
140
            call, using the second element of the tuple as the verb in the
 
141
            exception.
139
142
        """
140
143
        self.responses = responses
141
144
        self._calls = []
142
145
        self.expecting_body = False
143
146
        _SmartClient.__init__(self, FakeMedium(fake_medium_base, self._calls))
144
147
 
 
148
    def _get_next_response(self):
 
149
        response_tuple = self.responses.pop(0)
 
150
        if response_tuple[0][0] == 'unknown verb':
 
151
            raise errors.UnknownSmartMethod(response_tuple[0][1])
 
152
        return response_tuple
 
153
 
145
154
    def call(self, method, *args):
146
155
        self._calls.append(('call', method, args))
147
 
        return self.responses.pop(0)[0]
 
156
        return self._get_next_response()[0]
148
157
 
149
158
    def call_expecting_body(self, method, *args):
150
159
        self._calls.append(('call_expecting_body', method, args))
151
 
        result = self.responses.pop(0)
 
160
        result = self._get_next_response()
152
161
        self.expecting_body = True
153
162
        return result[0], FakeProtocol(result[1], self)
154
163
 
155
164
    def call_with_body_bytes_expecting_body(self, method, args, body):
156
165
        self._calls.append(('call_with_body_bytes_expecting_body', method,
157
166
            args, body))
158
 
        result = self.responses.pop(0)
 
167
        result = self._get_next_response()
159
168
        self.expecting_body = True
160
169
        return result[0], FakeProtocol(result[1], self)
161
170
 
289
298
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
290
299
 
291
300
 
 
301
class TestBzrDirOpenRepository(tests.TestCase):
 
302
 
 
303
    def test_backwards_compat_1_2(self):
 
304
        transport = MemoryTransport()
 
305
        transport.mkdir('quack')
 
306
        transport = transport.clone('quack')
 
307
        client = FakeClient([
 
308
            (('unknown verb', 'RemoteRepository.find_repositoryV2'), ''),
 
309
            (('ok', '', 'no', 'no'), ''),],
 
310
            transport.base)
 
311
        bzrdir = RemoteBzrDir(transport, _client=client)
 
312
        repo = bzrdir.open_repository()
 
313
        self.assertEqual(
 
314
            [('call', 'BzrDir.find_repositoryV2', ('quack/',)),
 
315
             ('call', 'BzrDir.find_repository', ('quack/',))],
 
316
            client._calls)
 
317
 
 
318
 
292
319
class OldSmartClient(object):
293
320
    """A fake smart client for test_old_version that just returns a version one
294
321
    response to the 'hello' (query version) command.
507
534
        advisory anyway (a transport could be read-write, but then the
508
535
        underlying filesystem could be readonly anyway).
509
536
        """
510
 
        client = FakeClient([(
511
 
            ('error', "Generic bzr smart protocol error: "
512
 
                      "bad request 'Transport.is_readonly'"), '')])
513
 
        transport = RemoteTransport('bzr://example.com/', medium=False,
514
 
                                    _client=client)
515
 
        self.assertEqual(False, transport.is_readonly())
516
 
        self.assertEqual(
517
 
            [('call', 'Transport.is_readonly', ())],
518
 
            client._calls)
519
 
 
520
 
    def test_error_from_old_0_11_server(self):
521
 
        """Same as test_error_from_old_server, but with the slightly different
522
 
        error message from bzr 0.11 servers.
523
 
        """
524
 
        client = FakeClient([(
525
 
            ('error', "Generic bzr smart protocol error: "
526
 
                      "bad request u'Transport.is_readonly'"), '')])
 
537
        client = FakeClient([(('unknown verb', 'Transport.is_readonly'), '')])
527
538
        transport = RemoteTransport('bzr://example.com/', medium=False,
528
539
                                    _client=client)
529
540
        self.assertEqual(False, transport.is_readonly())
675
686
        repo.unlock()
676
687
 
677
688
    def test_get_parent_map_reconnects_if_unknown_method(self):
678
 
        error_msg = (
679
 
            "Generic bzr smart protocol error: "
680
 
            "bad request 'Repository.get_parent_map'")
681
689
        responses = [
682
 
            (('error', error_msg), ''),
 
690
            (('unknown verb', 'Repository.get_parent_map'), ''),
683
691
            (('ok',), '')]
684
692
        transport_path = 'quack'
685
693
        repo, client = self.setup_fake_client_and_repository(
959
967
    
960
968
    def test_backwards_compatibility(self):
961
969
        """If the server doesn't recognise this request, fallback to VFS."""
962
 
        error_msg = (
963
 
            "Generic bzr smart protocol error: "
964
 
            "bad request 'Repository.stream_revisions_chunked'")
965
970
        responses = [
966
 
            (('error', error_msg), '')]
 
971
            (('unknown verb', 'Repository.stream_revisions_chunked'), '')]
967
972
        repo, client = self.setup_fake_client_and_repository(
968
973
            responses, 'path')
969
974
        self.mock_called = False