~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
52
52
    RemoteBranch,
53
53
    RemoteBranchFormat,
54
54
    RemoteBzrDir,
 
55
    RemoteBzrDirFormat,
55
56
    RemoteRepository,
56
57
    RemoteRepositoryFormat,
57
58
    )
58
 
from bzrlib.repofmt import groupcompress_repo, pack_repo
 
59
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
59
60
from bzrlib.revision import NULL_REVISION
60
 
from bzrlib.smart import medium
 
61
from bzrlib.smart import medium, request
61
62
from bzrlib.smart.client import _SmartClient
62
 
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
 
63
from bzrlib.smart.repository import (
 
64
    SmartServerRepositoryGetParentMap,
 
65
    SmartServerRepositoryGetStream_1_19,
 
66
    )
63
67
from bzrlib.tests import (
64
 
    condition_isinstance,
65
 
    split_suite_by_condition,
66
 
    multiply_tests,
67
68
    test_server,
68
69
    )
 
70
from bzrlib.tests.scenarios import load_tests_apply_scenarios
69
71
from bzrlib.transport.memory import MemoryTransport
70
72
from bzrlib.transport.remote import (
71
73
    RemoteTransport,
72
74
    RemoteSSHTransport,
73
75
    RemoteTCPTransport,
74
 
)
75
 
 
76
 
def load_tests(standard_tests, module, loader):
77
 
    to_adapt, result = split_suite_by_condition(
78
 
        standard_tests, condition_isinstance(BasicRemoteObjectTests))
79
 
    smart_server_version_scenarios = [
 
76
    )
 
77
 
 
78
 
 
79
load_tests = load_tests_apply_scenarios
 
80
 
 
81
 
 
82
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
 
83
 
 
84
    scenarios = [
80
85
        ('HPSS-v2',
81
 
         {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
 
86
            {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
82
87
        ('HPSS-v3',
83
 
         {'transport_server': test_server.SmartTCPServer_for_testing})]
84
 
    return multiply_tests(to_adapt, smart_server_version_scenarios, result)
85
 
 
86
 
 
87
 
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
 
88
            {'transport_server': test_server.SmartTCPServer_for_testing})]
 
89
 
88
90
 
89
91
    def setUp(self):
90
92
        super(BasicRemoteObjectTests, self).setUp()
94
96
        self.addCleanup(self.transport.disconnect)
95
97
 
96
98
    def test_create_remote_bzrdir(self):
97
 
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
 
99
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
98
100
        self.assertIsInstance(b, BzrDir)
99
101
 
100
102
    def test_open_remote_branch(self):
101
103
        # open a standalone branch in the working directory
102
 
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
 
104
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
103
105
        branch = b.open_branch()
104
106
        self.assertIsInstance(branch, Branch)
105
107
 
123
125
        fmt = BzrDirFormat.find_format(self.transport)
124
126
        self.assertTrue(bzrdir.RemoteBzrProber
125
127
                        in controldir.ControlDirFormat._server_probers)
126
 
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
 
128
        self.assertIsInstance(fmt, RemoteBzrDirFormat)
127
129
 
128
130
    def test_open_detected_smart_format(self):
129
131
        fmt = BzrDirFormat.find_format(self.transport)
449
451
        client.add_expected_call(
450
452
            'BzrDir.open_branchV3', ('quack/',),
451
453
            'success', ('ref', self.get_url('referenced'))),
452
 
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
454
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
453
455
            _client=client)
454
456
        result = a_bzrdir.cloning_metadir()
455
457
        # We should have got a control dir matching the referenced branch.
468
470
        client.add_expected_call(
469
471
            'BzrDir.cloning_metadir', ('quack/', 'False'),
470
472
            'success', (control_name, '', ('branch', ''))),
471
 
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
473
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
472
474
            _client=client)
473
475
        result = a_bzrdir.cloning_metadir()
474
476
        # We should have got a reference control dir with default branch and
494
496
        client.add_expected_call(
495
497
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
496
498
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
497
 
                remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
 
499
                RemoteBzrDirFormat(), _client=client, _force_probe=True)
498
500
        self.assertFinished(client)
499
501
 
500
502
    def test_present_without_workingtree(self):
501
503
        client, transport = self.make_fake_client_and_transport()
502
504
        client.add_expected_call(
503
505
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
504
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
506
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
505
507
            _client=client, _force_probe=True)
506
508
        self.assertIsInstance(bd, RemoteBzrDir)
507
509
        self.assertFalse(bd.has_workingtree())
512
514
        client, transport = self.make_fake_client_and_transport()
513
515
        client.add_expected_call(
514
516
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
515
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
517
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
516
518
            _client=client, _force_probe=True)
517
519
        self.assertIsInstance(bd, RemoteBzrDir)
518
520
        self.assertTrue(bd.has_workingtree())
525
527
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
526
528
        client.add_expected_call(
527
529
            'BzrDir.open', ('quack/',), 'success', ('yes',))
528
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
530
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
529
531
            _client=client, _force_probe=True)
530
532
        self.assertIsInstance(bd, RemoteBzrDir)
531
533
        self.assertFinished(client)
547
549
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
548
550
        client.add_expected_call(
549
551
            'BzrDir.open', ('quack/',), 'success', ('yes',))
550
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
552
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
551
553
            _client=client, _force_probe=True)
552
554
        self.assertIsInstance(bd, RemoteBzrDir)
553
555
        self.assertFinished(client)
584
586
        client.add_expected_call(
585
587
            'Branch.get_stacked_on_url', ('quack/',),
586
588
            'error', ('NotStacked',))
587
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
589
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
588
590
            _client=client)
589
591
        result = bzrdir.open_branch()
590
592
        self.assertIsInstance(result, RemoteBranch)
597
599
        transport = transport.clone('quack')
598
600
        client = FakeClient(transport.base)
599
601
        client.add_error_response('nobranch')
600
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
602
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
601
603
            _client=client)
602
604
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
603
605
        self.assertEqual(
614
616
        transport = MemoryTransport()
615
617
        # no requests on the network - catches other api calls being made.
616
618
        client = FakeClient(transport.base)
617
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
619
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
618
620
            _client=client)
619
621
        # patch the open_branch call to record that it was called.
620
622
        bzrdir.open_branch = open_branch
639
641
        client.add_expected_call(
640
642
            'Branch.get_stacked_on_url', ('~hello/',),
641
643
            'error', ('NotStacked',))
642
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
644
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
643
645
            _client=client)
644
646
        result = bzrdir.open_branch()
645
647
        self.assertFinished(client)
662
664
        client.add_success_response(
663
665
            'ok', '', rich_response, subtree_response, external_lookup,
664
666
            network_name)
665
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
667
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
666
668
            _client=client)
667
669
        result = bzrdir.open_repository()
668
670
        self.assertEqual(
714
716
            'BzrDir.create_branch', ('quack/', network_name),
715
717
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
716
718
            reference_repo_name))
717
 
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
719
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
718
720
            _client=client)
719
721
        branch = a_bzrdir.create_branch()
720
722
        # We should have got a remote branch
723
725
        format = branch._format
724
726
        self.assertEqual(network_name, format.network_name())
725
727
 
 
728
    def test_already_open_repo_and_reused_medium(self):
 
729
        """Bug 726584: create_branch(..., repository=repo) should work
 
730
        regardless of what the smart medium's base URL is.
 
731
        """
 
732
        self.transport_server = test_server.SmartTCPServer_for_testing
 
733
        transport = self.get_transport('.')
 
734
        repo = self.make_repository('quack')
 
735
        # Client's medium rooted a transport root (not at the bzrdir)
 
736
        client = FakeClient(transport.base)
 
737
        transport = transport.clone('quack')
 
738
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
739
        reference_format = reference_bzrdir_format.get_branch_format()
 
740
        network_name = reference_format.network_name()
 
741
        reference_repo_fmt = reference_bzrdir_format.repository_format
 
742
        reference_repo_name = reference_repo_fmt.network_name()
 
743
        client.add_expected_call(
 
744
            'BzrDir.create_branch', ('extra/quack/', network_name),
 
745
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
 
746
            reference_repo_name))
 
747
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
748
            _client=client)
 
749
        branch = a_bzrdir.create_branch(repository=repo)
 
750
        # We should have got a remote branch
 
751
        self.assertIsInstance(branch, remote.RemoteBranch)
 
752
        # its format should have the settings from the response
 
753
        format = branch._format
 
754
        self.assertEqual(network_name, format.network_name())
 
755
 
726
756
 
727
757
class TestBzrDirCreateRepository(TestRemote):
728
758
 
749
779
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
750
780
                'False'),
751
781
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
752
 
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
782
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
753
783
            _client=client)
754
784
        repo = a_bzrdir.create_repository()
755
785
        # We should have got a remote repository
784
814
        client.add_success_response('stat', '0', '65535')
785
815
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
786
816
            _client=client)
787
 
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
 
817
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
788
818
            _client=client)
789
819
        repo = bzrdir.open_repository()
790
820
        self.assertEqual(
817
847
        client.add_success_response('stat', '0', '65535')
818
848
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
819
849
            _client=client)
820
 
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
 
850
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
821
851
            _client=client)
822
852
        repo = bzrdir.open_repository()
823
853
        self.assertEqual(
838
868
        transport = transport.clone('quack')
839
869
        client = FakeClient(transport.base)
840
870
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
841
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
871
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
842
872
            _client=client)
843
873
        repo = bzrdir.open_repository()
844
874
        self.assertEqual(
851
881
 
852
882
    def test_success(self):
853
883
        """Simple test for typical successful call."""
854
 
        fmt = bzrdir.RemoteBzrDirFormat()
 
884
        fmt = RemoteBzrDirFormat()
855
885
        default_format_name = BzrDirFormat.get_default_format().network_name()
856
886
        transport = self.get_transport()
857
887
        client = FakeClient(transport.base)
873
903
        """Error responses are translated, e.g. 'PermissionDenied' raises the
874
904
        corresponding error from the client.
875
905
        """
876
 
        fmt = bzrdir.RemoteBzrDirFormat()
 
906
        fmt = RemoteBzrDirFormat()
877
907
        default_format_name = BzrDirFormat.get_default_format().network_name()
878
908
        transport = self.get_transport()
879
909
        client = FakeClient(transport.base)
897
927
        """Integration test for error translation."""
898
928
        transport = self.make_smart_server('foo')
899
929
        transport = transport.clone('no-such-path')
900
 
        fmt = bzrdir.RemoteBzrDirFormat()
 
930
        fmt = RemoteBzrDirFormat()
901
931
        err = self.assertRaises(errors.NoSuchFile,
902
932
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
903
933
 
934
964
 
935
965
    def make_remote_bzrdir(self, transport, client):
936
966
        """Make a RemotebzrDir using 'client' as the _client."""
937
 
        return RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
967
        return RemoteBzrDir(transport, RemoteBzrDirFormat(),
938
968
            _client=client)
939
969
 
940
970
 
1142
1172
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1143
1173
 
1144
1174
 
 
1175
class TestBranchHeadsToFetch(RemoteBranchTestCase):
 
1176
 
 
1177
    def test_uses_last_revision_info_and_tags_by_default(self):
 
1178
        transport = MemoryTransport()
 
1179
        client = FakeClient(transport.base)
 
1180
        client.add_expected_call(
 
1181
            'Branch.get_stacked_on_url', ('quack/',),
 
1182
            'error', ('NotStacked',))
 
1183
        client.add_expected_call(
 
1184
            'Branch.last_revision_info', ('quack/',),
 
1185
            'success', ('ok', '1', 'rev-tip'))
 
1186
        # XXX: this will break if the default format's serialization of tags
 
1187
        # changes, or if the RPC for fetching tags changes from get_tags_bytes.
 
1188
        client.add_expected_call(
 
1189
            'Branch.get_tags_bytes', ('quack/',),
 
1190
            'success', ('d5:tag-17:rev-foo5:tag-27:rev-bare',))
 
1191
        transport.mkdir('quack')
 
1192
        transport = transport.clone('quack')
 
1193
        branch = self.make_remote_branch(transport, client)
 
1194
        result = branch.heads_to_fetch()
 
1195
        self.assertFinished(client)
 
1196
        self.assertEqual(
 
1197
            (set(['rev-tip']), set(['rev-foo', 'rev-bar'])), result)
 
1198
 
 
1199
    def test_uses_rpc_for_formats_with_non_default_heads_to_fetch(self):
 
1200
        transport = MemoryTransport()
 
1201
        client = FakeClient(transport.base)
 
1202
        client.add_expected_call(
 
1203
            'Branch.get_stacked_on_url', ('quack/',),
 
1204
            'error', ('NotStacked',))
 
1205
        client.add_expected_call(
 
1206
            'Branch.heads_to_fetch', ('quack/',),
 
1207
            'success', (['tip'], ['tagged-1', 'tagged-2']))
 
1208
        transport.mkdir('quack')
 
1209
        transport = transport.clone('quack')
 
1210
        branch = self.make_remote_branch(transport, client)
 
1211
        branch._format._use_default_local_heads_to_fetch = lambda: False
 
1212
        result = branch.heads_to_fetch()
 
1213
        self.assertFinished(client)
 
1214
        self.assertEqual((set(['tip']), set(['tagged-1', 'tagged-2'])), result)
 
1215
 
 
1216
    def test_backwards_compatible(self):
 
1217
        self.setup_smart_server_with_call_log()
 
1218
        # Make a branch with a single revision.
 
1219
        builder = self.make_branch_builder('foo')
 
1220
        builder.start_series()
 
1221
        builder.build_snapshot('tip', None, [
 
1222
            ('add', ('', 'root-id', 'directory', ''))])
 
1223
        builder.finish_series()
 
1224
        branch = builder.get_branch()
 
1225
        # Add two tags to that branch
 
1226
        branch.tags.set_tag('tag-1', 'rev-1')
 
1227
        branch.tags.set_tag('tag-2', 'rev-2')
 
1228
        self.addCleanup(branch.lock_read().unlock)
 
1229
        # Disable the heads_to_fetch verb
 
1230
        verb = 'Branch.heads_to_fetch'
 
1231
        self.disable_verb(verb)
 
1232
        self.reset_smart_call_log()
 
1233
        result = branch.heads_to_fetch()
 
1234
        self.assertEqual((set(['tip']), set(['rev-1', 'rev-2'])), result)
 
1235
        self.assertEqual(
 
1236
            ['Branch.last_revision_info', 'Branch.get_tags_bytes'],
 
1237
            [call.call.method for call in self.hpss_calls])
 
1238
 
 
1239
 
1145
1240
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1146
1241
 
1147
1242
    def test_empty_branch(self):
1202
1297
        client.add_expected_call(
1203
1298
            'Branch.get_stacked_on_url', ('stacked/',),
1204
1299
            'success', ('ok', vfs_url))
1205
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
1300
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
1206
1301
            _client=client)
1207
1302
        repo_fmt = remote.RemoteRepositoryFormat()
1208
1303
        repo_fmt._custom_format = stacked_branch.repository._format
1235
1330
        # this will also do vfs access, but that goes direct to the transport
1236
1331
        # and isn't seen by the FakeClient.
1237
1332
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1238
 
            remote.RemoteBzrDirFormat(), _client=client)
 
1333
            RemoteBzrDirFormat(), _client=client)
1239
1334
        branch = bzrdir.open_branch()
1240
1335
        result = branch.get_stacked_on_url()
1241
1336
        self.assertEqual('../base', result)
1268
1363
            'Branch.get_stacked_on_url', ('stacked/',),
1269
1364
            'success', ('ok', '../base'))
1270
1365
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1271
 
            remote.RemoteBzrDirFormat(), _client=client)
 
1366
            RemoteBzrDirFormat(), _client=client)
1272
1367
        branch = bzrdir.open_branch()
1273
1368
        result = branch.get_stacked_on_url()
1274
1369
        self.assertEqual('../base', result)
1282
1377
class TestBranchSetLastRevision(RemoteBranchTestCase):
1283
1378
 
1284
1379
    def test_set_empty(self):
1285
 
        # set_revision_history([]) is translated to calling
 
1380
        # _set_last_revision_info('null:') is translated to calling
1286
1381
        # Branch.set_last_revision(path, '') on the wire.
1287
1382
        transport = MemoryTransport()
1288
1383
        transport.mkdir('branch')
1310
1405
        # unnecessarily invokes _ensure_real upon a call to lock_write.
1311
1406
        branch._ensure_real = lambda: None
1312
1407
        branch.lock_write()
1313
 
        result = branch.set_revision_history([])
 
1408
        result = branch._set_last_revision(NULL_REVISION)
1314
1409
        branch.unlock()
1315
1410
        self.assertEqual(None, result)
1316
1411
        self.assertFinished(client)
1317
1412
 
1318
1413
    def test_set_nonempty(self):
1319
 
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
 
1414
        # set_last_revision_info(N, rev-idN) is translated to calling
1320
1415
        # Branch.set_last_revision(path, rev-idN) on the wire.
1321
1416
        transport = MemoryTransport()
1322
1417
        transport.mkdir('branch')
1348
1443
        branch._ensure_real = lambda: None
1349
1444
        # Lock the branch, reset the record of remote calls.
1350
1445
        branch.lock_write()
1351
 
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
 
1446
        result = branch._set_last_revision('rev-id2')
1352
1447
        branch.unlock()
1353
1448
        self.assertEqual(None, result)
1354
1449
        self.assertFinished(client)
1384
1479
        branch = self.make_remote_branch(transport, client)
1385
1480
        branch.lock_write()
1386
1481
        self.assertRaises(
1387
 
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
 
1482
            errors.NoSuchRevision, branch._set_last_revision, 'rev-id')
1388
1483
        branch.unlock()
1389
1484
        self.assertFinished(client)
1390
1485
 
1421
1516
        branch._ensure_real = lambda: None
1422
1517
        branch.lock_write()
1423
1518
        # The 'TipChangeRejected' error response triggered by calling
1424
 
        # set_revision_history causes a TipChangeRejected exception.
 
1519
        # set_last_revision_info causes a TipChangeRejected exception.
1425
1520
        err = self.assertRaises(
1426
 
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
 
1521
            errors.TipChangeRejected,
 
1522
            branch._set_last_revision, 'rev-id')
1427
1523
        # The UTF-8 message from the response has been decoded into a unicode
1428
1524
        # object.
1429
1525
        self.assertIsInstance(err.msg, unicode)
1844
1940
        client = FakeClient(transport.base)
1845
1941
        transport = transport.clone(transport_path)
1846
1942
        # we do not want bzrdir to make any remote calls
1847
 
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
1943
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
1848
1944
            _client=False)
1849
1945
        repo = RemoteRepository(bzrdir, None, _client=client)
1850
1946
        return repo, client
1858
1954
 
1859
1955
    def test_get_format_description(self):
1860
1956
        remote_format = RemoteBranchFormat()
1861
 
        real_format = branch.BranchFormat.get_default_format()
 
1957
        real_format = branch.format_registry.get_default()
1862
1958
        remote_format._network_name = real_format.network_name()
1863
1959
        self.assertEqual(remoted_description(real_format),
1864
1960
            remote_format.get_format_description())
1867
1963
class TestRepositoryFormat(TestRemoteRepository):
1868
1964
 
1869
1965
    def test_fast_delta(self):
1870
 
        true_name = groupcompress_repo.RepositoryFormatCHK1().network_name()
 
1966
        true_name = groupcompress_repo.RepositoryFormat2a().network_name()
1871
1967
        true_format = RemoteRepositoryFormat()
1872
1968
        true_format._network_name = true_name
1873
1969
        self.assertEqual(True, true_format.fast_deltas)
1874
 
        false_name = pack_repo.RepositoryFormatKnitPack1().network_name()
 
1970
        false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
1875
1971
        false_format = RemoteRepositoryFormat()
1876
1972
        false_format._network_name = false_name
1877
1973
        self.assertEqual(False, false_format.fast_deltas)
1878
1974
 
1879
1975
    def test_get_format_description(self):
1880
1976
        remote_repo_format = RemoteRepositoryFormat()
1881
 
        real_format = repository.RepositoryFormat.get_default_format()
 
1977
        real_format = repository.format_registry.get_default()
1882
1978
        remote_repo_format._network_name = real_format.network_name()
1883
1979
        self.assertEqual(remoted_description(real_format),
1884
1980
            remote_repo_format.get_format_description())
2442
2538
        the client is finished.
2443
2539
        """
2444
2540
        sink = repo._get_sink()
2445
 
        fmt = repository.RepositoryFormat.get_default_format()
 
2541
        fmt = repository.format_registry.get_default()
2446
2542
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2447
2543
        self.assertEqual([], resume_tokens)
2448
2544
        self.assertEqual(set(), missing_keys)
2548
2644
                return True
2549
2645
        repo._real_repository = FakeRealRepository()
2550
2646
        sink = repo._get_sink()
2551
 
        fmt = repository.RepositoryFormat.get_default_format()
 
2647
        fmt = repository.format_registry.get_default()
2552
2648
        stream = self.make_stream_with_inv_deltas(fmt)
2553
2649
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2554
2650
        # Every record from the first inventory delta should have been sent to
2774
2870
             ('pack collection autopack',)],
2775
2871
            client._calls)
2776
2872
 
 
2873
    def test_oom_error_reporting(self):
 
2874
        """An out-of-memory condition on the server is reported clearly"""
 
2875
        transport_path = 'quack'
 
2876
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2877
        client.add_expected_call(
 
2878
            'PackRepository.autopack', ('quack/',),
 
2879
            'error', ('MemoryError',))
 
2880
        err = self.assertRaises(errors.BzrError, repo.autopack)
 
2881
        self.assertContainsRe(str(err), "^remote server out of mem")
 
2882
 
2777
2883
 
2778
2884
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2779
2885
    """Base class for unit tests for bzrlib.remote._translate_error."""
2852
2958
            detail='extra detail')
2853
2959
        self.assertEqual(expected_error, translated_error)
2854
2960
 
 
2961
    def test_norepository(self):
 
2962
        bzrdir = self.make_bzrdir('')
 
2963
        translated_error = self.translateTuple(('norepository',),
 
2964
            bzrdir=bzrdir)
 
2965
        expected_error = errors.NoRepositoryPresent(bzrdir)
 
2966
        self.assertEqual(expected_error, translated_error)
 
2967
 
2855
2968
    def test_LockContention(self):
2856
2969
        translated_error = self.translateTuple(('LockContention',))
2857
2970
        expected_error = errors.LockContention('(remote lock)')
2885
2998
        expected_error = errors.DivergedBranches(branch, other_branch)
2886
2999
        self.assertEqual(expected_error, translated_error)
2887
3000
 
 
3001
    def test_NotStacked(self):
 
3002
        branch = self.make_branch('')
 
3003
        translated_error = self.translateTuple(('NotStacked',), branch=branch)
 
3004
        expected_error = errors.NotStacked(branch)
 
3005
        self.assertEqual(expected_error, translated_error)
 
3006
 
2888
3007
    def test_ReadError_no_args(self):
2889
3008
        path = 'a path'
2890
3009
        translated_error = self.translateTuple(('ReadError',), path=path)
2906
3025
 
2907
3026
    def test_PermissionDenied_no_args(self):
2908
3027
        path = 'a path'
2909
 
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
 
3028
        translated_error = self.translateTuple(('PermissionDenied',),
 
3029
            path=path)
2910
3030
        expected_error = errors.PermissionDenied(path)
2911
3031
        self.assertEqual(expected_error, translated_error)
2912
3032
 
2935
3055
        expected_error = errors.PermissionDenied(path, extra)
2936
3056
        self.assertEqual(expected_error, translated_error)
2937
3057
 
 
3058
    # GZ 2011-03-02: TODO test for PermissionDenied with non-ascii 'extra'
 
3059
 
 
3060
    def test_NoSuchFile_context_path(self):
 
3061
        local_path = "local path"
 
3062
        translated_error = self.translateTuple(('ReadError', "remote path"),
 
3063
            path=local_path)
 
3064
        expected_error = errors.ReadError(local_path)
 
3065
        self.assertEqual(expected_error, translated_error)
 
3066
 
 
3067
    def test_NoSuchFile_without_context(self):
 
3068
        remote_path = "remote path"
 
3069
        translated_error = self.translateTuple(('ReadError', remote_path))
 
3070
        expected_error = errors.ReadError(remote_path)
 
3071
        self.assertEqual(expected_error, translated_error)
 
3072
 
 
3073
    def test_ReadOnlyError(self):
 
3074
        translated_error = self.translateTuple(('ReadOnlyError',))
 
3075
        expected_error = errors.TransportNotPossible("readonly transport")
 
3076
        self.assertEqual(expected_error, translated_error)
 
3077
 
 
3078
    def test_MemoryError(self):
 
3079
        translated_error = self.translateTuple(('MemoryError',))
 
3080
        self.assertStartsWith(str(translated_error),
 
3081
            "remote server out of memory")
 
3082
 
 
3083
    def test_generic_IndexError_no_classname(self):
 
3084
        err = errors.ErrorFromSmartServer(('error', "list index out of range"))
 
3085
        translated_error = self.translateErrorFromSmartServer(err)
 
3086
        expected_error = errors.UnknownErrorFromSmartServer(err)
 
3087
        self.assertEqual(expected_error, translated_error)
 
3088
 
 
3089
    # GZ 2011-03-02: TODO test generic non-ascii error string
 
3090
 
 
3091
    def test_generic_KeyError(self):
 
3092
        err = errors.ErrorFromSmartServer(('error', 'KeyError', "1"))
 
3093
        translated_error = self.translateErrorFromSmartServer(err)
 
3094
        expected_error = errors.UnknownErrorFromSmartServer(err)
 
3095
        self.assertEqual(expected_error, translated_error)
 
3096
 
2938
3097
 
2939
3098
class TestErrorTranslationRobustness(TestErrorTranslationBase):
2940
3099
    """Unit tests for bzrlib.remote._translate_error's robustness.
3182
3341
 
3183
3342
    def test_copy_content_into_avoids_revision_history(self):
3184
3343
        local = self.make_branch('local')
3185
 
        remote_backing_tree = self.make_branch_and_tree('remote')
3186
 
        remote_backing_tree.commit("Commit.")
 
3344
        builder = self.make_branch_builder('remote')
 
3345
        builder.build_commit(message="Commit.")
3187
3346
        remote_branch_url = self.smart_server.get_url() + 'remote'
3188
3347
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3189
3348
        local.repository.fetch(remote_branch.repository)
3190
3349
        self.hpss_calls = []
3191
3350
        remote_branch.copy_content_into(local)
3192
3351
        self.assertFalse('Branch.revision_history' in self.hpss_calls)
 
3352
 
 
3353
    def test_fetch_everything_needs_just_one_call(self):
 
3354
        local = self.make_branch('local')
 
3355
        builder = self.make_branch_builder('remote')
 
3356
        builder.build_commit(message="Commit.")
 
3357
        remote_branch_url = self.smart_server.get_url() + 'remote'
 
3358
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
 
3359
        self.hpss_calls = []
 
3360
        local.repository.fetch(remote_branch.repository,
 
3361
                fetch_spec=graph.EverythingResult(remote_branch.repository))
 
3362
        self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
 
3363
 
 
3364
    def override_verb(self, verb_name, verb):
 
3365
        request_handlers = request.request_handlers
 
3366
        orig_verb = request_handlers.get(verb_name)
 
3367
        request_handlers.register(verb_name, verb, override_existing=True)
 
3368
        self.addCleanup(request_handlers.register, verb_name, orig_verb,
 
3369
                override_existing=True)
 
3370
 
 
3371
    def test_fetch_everything_backwards_compat(self):
 
3372
        """Can fetch with EverythingResult even with pre 2.4 servers.
 
3373
        
 
3374
        Pre-2.4 do not support 'everything' searches with the
 
3375
        Repository.get_stream_1.19 verb.
 
3376
        """
 
3377
        verb_log = []
 
3378
        class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
 
3379
            """A version of the Repository.get_stream_1.19 verb patched to
 
3380
            reject 'everything' searches the way 2.3 and earlier do.
 
3381
            """
 
3382
            def recreate_search(self, repository, search_bytes, discard_excess=False):
 
3383
                verb_log.append(search_bytes.split('\n', 1)[0])
 
3384
                if search_bytes == 'everything':
 
3385
                    return (None, request.FailedSmartServerResponse(('BadSearch',)))
 
3386
                return super(OldGetStreamVerb,
 
3387
                        self).recreate_search(repository, search_bytes,
 
3388
                            discard_excess=discard_excess)
 
3389
        self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
 
3390
        local = self.make_branch('local')
 
3391
        builder = self.make_branch_builder('remote')
 
3392
        builder.build_commit(message="Commit.")
 
3393
        remote_branch_url = self.smart_server.get_url() + 'remote'
 
3394
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
 
3395
        self.hpss_calls = []
 
3396
        local.repository.fetch(remote_branch.repository,
 
3397
                fetch_spec=graph.EverythingResult(remote_branch.repository))
 
3398
        # make sure the overridden verb was used
 
3399
        self.assertLength(1, verb_log)
 
3400
        # more than one HPSS call is needed, but because it's a VFS callback
 
3401
        # its hard to predict exactly how many.
 
3402
        self.assertTrue(len(self.hpss_calls) > 1)
 
3403