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

  • Committer: Andrew Bennetts
  • Date: 2008-03-27 06:10:18 UTC
  • mfrom: (3309 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3320.
  • Revision ID: andrew.bennetts@canonical.com-20080327061018-dxztpxyv6yoeg3am
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
These tests correspond to tests.test_smart, which exercises the server side.
24
24
"""
25
25
 
 
26
import bz2
26
27
from cStringIO import StringIO
27
28
 
28
29
from bzrlib import (
29
 
    bzrdir,
30
30
    errors,
 
31
    graph,
31
32
    pack,
32
33
    remote,
33
34
    repository,
54
55
        self.transport_server = server.SmartTCPServer_for_testing
55
56
        super(BasicRemoteObjectTests, self).setUp()
56
57
        self.transport = self.get_transport()
57
 
        self.client = self.transport.get_smart_client()
58
58
        # make a branch that can be opened over the smart transport
59
59
        self.local_wt = BzrDir.create_standalone_workingtree('.')
60
60
 
139
139
        self.responses = responses
140
140
        self._calls = []
141
141
        self.expecting_body = False
142
 
        _SmartClient.__init__(self, FakeMedium(fake_medium_base))
 
142
        _SmartClient.__init__(self, FakeMedium(fake_medium_base, self._calls))
143
143
 
144
144
    def call(self, method, *args):
145
145
        self._calls.append(('call', method, args))
151
151
        self.expecting_body = True
152
152
        return result[0], FakeProtocol(result[1], self)
153
153
 
 
154
    def call_with_body_bytes_expecting_body(self, method, args, body):
 
155
        self._calls.append(('call_with_body_bytes_expecting_body', method,
 
156
            args, body))
 
157
        result = self.responses.pop(0)
 
158
        self.expecting_body = True
 
159
        return result[0], FakeProtocol(result[1], self)
 
160
 
154
161
 
155
162
class FakeMedium(object):
156
163
 
157
 
    def __init__(self, base):
 
164
    def __init__(self, base, client_calls):
158
165
        self.base = base
 
166
        self.connection = FakeConnection(client_calls)
 
167
        self._client_calls = client_calls
 
168
 
 
169
 
 
170
class FakeConnection(object):
 
171
 
 
172
    def __init__(self, client_calls):
 
173
        self._remote_is_at_least_1_2 = True
 
174
        self._client_calls = client_calls
 
175
 
 
176
    def disconnect(self):
 
177
        self._client_calls.append(('disconnect medium',))
 
178
 
 
179
 
 
180
class TestVfsHas(tests.TestCase):
 
181
 
 
182
    def test_unicode_path(self):
 
183
        client = FakeClient([(('yes',), )], '/')
 
184
        transport = RemoteTransport('bzr://localhost/', _client=client)
 
185
        filename = u'/hell\u00d8'.encode('utf8')
 
186
        result = transport.has(filename)
 
187
        self.assertEqual(
 
188
            [('call', 'has', (filename,))],
 
189
            client._calls)
 
190
        self.assertTrue(result)
159
191
 
160
192
 
161
193
class TestBzrDirOpenBranch(tests.TestCase):
164
196
        transport = MemoryTransport()
165
197
        transport.mkdir('quack')
166
198
        transport = transport.clone('quack')
167
 
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )],
 
199
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
168
200
                            transport.base)
169
201
        bzrdir = RemoteBzrDir(transport, _client=client)
170
202
        result = bzrdir.open_branch()
171
203
        self.assertEqual(
172
204
            [('call', 'BzrDir.open_branch', ('quack/',)),
173
 
             ('call', 'BzrDir.find_repository', ('quack/',))],
 
205
             ('call', 'BzrDir.find_repositoryV2', ('quack/',))],
174
206
            client._calls)
175
207
        self.assertIsInstance(result, RemoteBranch)
176
208
        self.assertEqual(bzrdir, result.bzrdir)
186
218
            [('call', 'BzrDir.open_branch', ('quack/',))],
187
219
            client._calls)
188
220
 
189
 
    def check_open_repository(self, rich_root, subtrees):
 
221
    def test__get_tree_branch(self):
 
222
        # _get_tree_branch is a form of open_branch, but it should only ask for
 
223
        # branch opening, not any other network requests.
 
224
        calls = []
 
225
        def open_branch():
 
226
            calls.append("Called")
 
227
            return "a-branch"
 
228
        transport = MemoryTransport()
 
229
        # no requests on the network - catches other api calls being made.
 
230
        client = FakeClient([], transport.base)
 
231
        bzrdir = RemoteBzrDir(transport, _client=client)
 
232
        # patch the open_branch call to record that it was called.
 
233
        bzrdir.open_branch = open_branch
 
234
        self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
 
235
        self.assertEqual(["Called"], calls)
 
236
        self.assertEqual([], client._calls)
 
237
 
 
238
    def test_url_quoting_of_path(self):
 
239
        # Relpaths on the wire should not be URL-escaped.  So "~" should be
 
240
        # transmitted as "~", not "%7E".
 
241
        transport = RemoteTransport('bzr://localhost/~hello/')
 
242
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
 
243
                            transport.base)
 
244
        bzrdir = RemoteBzrDir(transport, _client=client)
 
245
        result = bzrdir.open_branch()
 
246
        self.assertEqual(
 
247
            [('call', 'BzrDir.open_branch', ('~hello/',)),
 
248
             ('call', 'BzrDir.find_repositoryV2', ('~hello/',))],
 
249
            client._calls)
 
250
 
 
251
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
190
252
        transport = MemoryTransport()
191
253
        transport.mkdir('quack')
192
254
        transport = transport.clone('quack')
198
260
            subtree_response = 'yes'
199
261
        else:
200
262
            subtree_response = 'no'
201
 
        client = FakeClient([(('ok', '', rich_response, subtree_response), ),],
202
 
                            transport.base)
 
263
        client = FakeClient(
 
264
            [(('ok', '', rich_response, subtree_response, external_lookup), ),],
 
265
            transport.base)
203
266
        bzrdir = RemoteBzrDir(transport, _client=client)
204
267
        result = bzrdir.open_repository()
205
268
        self.assertEqual(
206
 
            [('call', 'BzrDir.find_repository', ('quack/',))],
 
269
            [('call', 'BzrDir.find_repositoryV2', ('quack/',))],
207
270
            client._calls)
208
271
        self.assertIsInstance(result, RemoteRepository)
209
272
        self.assertEqual(bzrdir, result.bzrdir)
215
278
        self.check_open_repository(False, True)
216
279
        self.check_open_repository(True, False)
217
280
        self.check_open_repository(False, False)
 
281
        self.check_open_repository(False, False, 'yes')
218
282
 
219
283
    def test_old_server(self):
220
284
        """RemoteBzrDirFormat should fail to probe if the server version is too
236
300
            input_file, output_file)
237
301
        return medium.SmartClientStreamMediumRequest(client_medium)
238
302
 
 
303
    def protocol_version(self):
 
304
        return 1
 
305
 
239
306
 
240
307
class OldServerTransport(object):
241
308
    """A fake transport for test_old_server that reports it's smart server
554
621
                         result)
555
622
 
556
623
 
 
624
class TestRepositoryGetGraph(TestRemoteRepository):
 
625
 
 
626
    def test_get_graph(self):
 
627
        # get_graph returns a graph with the repository as the
 
628
        # parents_provider.
 
629
        responses = []
 
630
        transport_path = 'quack'
 
631
        repo, client = self.setup_fake_client_and_repository(
 
632
            responses, transport_path)
 
633
        graph = repo.get_graph()
 
634
        self.assertEqual(graph._parents_provider, repo)
 
635
 
 
636
 
 
637
class TestRepositoryGetParentMap(TestRemoteRepository):
 
638
 
 
639
    def test_get_parent_map_caching(self):
 
640
        # get_parent_map returns from cache until unlock()
 
641
        # setup a reponse with two revisions
 
642
        r1 = u'\u0e33'.encode('utf8')
 
643
        r2 = u'\u0dab'.encode('utf8')
 
644
        lines = [' '.join([r2, r1]), r1]
 
645
        encoded_body = bz2.compress('\n'.join(lines))
 
646
        responses = [(('ok', ), encoded_body), (('ok', ), encoded_body)]
 
647
 
 
648
        transport_path = 'quack'
 
649
        repo, client = self.setup_fake_client_and_repository(
 
650
            responses, transport_path)
 
651
        repo.lock_read()
 
652
        graph = repo.get_graph()
 
653
        parents = graph.get_parent_map([r2])
 
654
        self.assertEqual({r2: (r1,)}, parents)
 
655
        # locking and unlocking deeper should not reset
 
656
        repo.lock_read()
 
657
        repo.unlock()
 
658
        parents = graph.get_parent_map([r1])
 
659
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
 
660
        self.assertEqual(
 
661
            [('call_with_body_bytes_expecting_body',
 
662
              'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
 
663
            client._calls)
 
664
        repo.unlock()
 
665
        # now we call again, and it should use the second response.
 
666
        repo.lock_read()
 
667
        graph = repo.get_graph()
 
668
        parents = graph.get_parent_map([r1])
 
669
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
 
670
        self.assertEqual(
 
671
            [('call_with_body_bytes_expecting_body',
 
672
              'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
 
673
             ('call_with_body_bytes_expecting_body',
 
674
              'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
 
675
            ],
 
676
            client._calls)
 
677
        repo.unlock()
 
678
 
 
679
    def test_get_parent_map_reconnects_if_unknown_method(self):
 
680
        error_msg = (
 
681
            "Generic bzr smart protocol error: "
 
682
            "bad request 'Repository.get_parent_map'")
 
683
        responses = [
 
684
            (('error', error_msg), ''),
 
685
            (('ok',), '')]
 
686
        transport_path = 'quack'
 
687
        repo, client = self.setup_fake_client_and_repository(
 
688
            responses, transport_path)
 
689
        rev_id = 'revision-id'
 
690
        parents = repo.get_parent_map([rev_id])
 
691
        self.assertEqual(
 
692
            [('call_with_body_bytes_expecting_body',
 
693
              'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
 
694
             ('disconnect medium',),
 
695
             ('call_expecting_body', 'Repository.get_revision_graph',
 
696
              ('quack/', ''))],
 
697
            client._calls)
 
698
 
 
699
 
 
700
 
557
701
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
558
702
    
559
703
    def test_null_revision(self):
811
955
        transport_path = 'quack'
812
956
        repo, client = self.setup_fake_client_and_repository(
813
957
            responses, transport_path)
814
 
        stream = repo.get_data_stream(['revid'])
 
958
        search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
 
959
        stream = repo.get_data_stream_for_search(search)
815
960
        self.assertRaises(errors.SmartProtocolError, list, stream)
816
961
    
817
962
    def test_backwards_compatibility(self):
825
970
            responses, 'path')
826
971
        self.mock_called = False
827
972
        repo._real_repository = MockRealRepository(self)
828
 
        repo.get_data_stream(['revid'])
 
973
        search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
 
974
        repo.get_data_stream_for_search(search)
829
975
        self.assertTrue(self.mock_called)
830
976
        self.failIf(client.expecting_body,
831
977
            "The protocol has been left in an unclean state that will cause "
838
984
    def __init__(self, test):
839
985
        self.test = test
840
986
 
841
 
    def get_data_stream(self, revision_ids):
842
 
        self.test.assertEqual(['revid'], revision_ids)
 
987
    def get_data_stream_for_search(self, search):
 
988
        self.test.assertEqual(set(['revid']), search.get_keys())
843
989
        self.test.mock_called = True
844
990
 
845
991