~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 04:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5472.
  • Revision ID: andrew.bennetts@canonical.com-20101008042510-sg9vdhmnggilzxsk
Fix stray TAB in source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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,
56
55
    RemoteRepository,
57
56
    RemoteRepositoryFormat,
58
57
    )
59
 
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
 
58
from bzrlib.repofmt import groupcompress_repo, pack_repo
60
59
from bzrlib.revision import NULL_REVISION
61
 
from bzrlib.smart import medium, request
 
60
from bzrlib.smart import medium
62
61
from bzrlib.smart.client import _SmartClient
63
 
from bzrlib.smart.repository import (
64
 
    SmartServerRepositoryGetParentMap,
65
 
    SmartServerRepositoryGetStream_1_19,
66
 
    )
 
62
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
67
63
from bzrlib.tests import (
 
64
    condition_isinstance,
 
65
    split_suite_by_condition,
 
66
    multiply_tests,
68
67
    test_server,
69
68
    )
70
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
71
69
from bzrlib.transport.memory import MemoryTransport
72
70
from bzrlib.transport.remote import (
73
71
    RemoteTransport,
74
72
    RemoteSSHTransport,
75
73
    RemoteTCPTransport,
76
 
    )
77
 
 
78
 
 
79
 
load_tests = load_tests_apply_scenarios
80
 
 
81
 
 
82
 
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
83
 
 
84
 
    scenarios = [
 
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 = [
85
80
        ('HPSS-v2',
86
 
            {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
 
81
         {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
87
82
        ('HPSS-v3',
88
 
            {'transport_server': test_server.SmartTCPServer_for_testing})]
89
 
 
 
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):
90
88
 
91
89
    def setUp(self):
92
90
        super(BasicRemoteObjectTests, self).setUp()
96
94
        self.addCleanup(self.transport.disconnect)
97
95
 
98
96
    def test_create_remote_bzrdir(self):
99
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
97
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
100
98
        self.assertIsInstance(b, BzrDir)
101
99
 
102
100
    def test_open_remote_branch(self):
103
101
        # open a standalone branch in the working directory
104
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
102
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
105
103
        branch = b.open_branch()
106
104
        self.assertIsInstance(branch, Branch)
107
105
 
125
123
        fmt = BzrDirFormat.find_format(self.transport)
126
124
        self.assertTrue(bzrdir.RemoteBzrProber
127
125
                        in controldir.ControlDirFormat._server_probers)
128
 
        self.assertIsInstance(fmt, RemoteBzrDirFormat)
 
126
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
129
127
 
130
128
    def test_open_detected_smart_format(self):
131
129
        fmt = BzrDirFormat.find_format(self.transport)
451
449
        client.add_expected_call(
452
450
            'BzrDir.open_branchV3', ('quack/',),
453
451
            'success', ('ref', self.get_url('referenced'))),
454
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
452
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
455
453
            _client=client)
456
454
        result = a_bzrdir.cloning_metadir()
457
455
        # We should have got a control dir matching the referenced branch.
470
468
        client.add_expected_call(
471
469
            'BzrDir.cloning_metadir', ('quack/', 'False'),
472
470
            'success', (control_name, '', ('branch', ''))),
473
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
471
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
474
472
            _client=client)
475
473
        result = a_bzrdir.cloning_metadir()
476
474
        # We should have got a reference control dir with default branch and
496
494
        client.add_expected_call(
497
495
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
498
496
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
499
 
                RemoteBzrDirFormat(), _client=client, _force_probe=True)
 
497
                remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
500
498
        self.assertFinished(client)
501
499
 
502
500
    def test_present_without_workingtree(self):
503
501
        client, transport = self.make_fake_client_and_transport()
504
502
        client.add_expected_call(
505
503
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
506
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
504
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
507
505
            _client=client, _force_probe=True)
508
506
        self.assertIsInstance(bd, RemoteBzrDir)
509
507
        self.assertFalse(bd.has_workingtree())
514
512
        client, transport = self.make_fake_client_and_transport()
515
513
        client.add_expected_call(
516
514
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
517
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
515
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
518
516
            _client=client, _force_probe=True)
519
517
        self.assertIsInstance(bd, RemoteBzrDir)
520
518
        self.assertTrue(bd.has_workingtree())
527
525
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
528
526
        client.add_expected_call(
529
527
            'BzrDir.open', ('quack/',), 'success', ('yes',))
530
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
528
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
531
529
            _client=client, _force_probe=True)
532
530
        self.assertIsInstance(bd, RemoteBzrDir)
533
531
        self.assertFinished(client)
549
547
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
550
548
        client.add_expected_call(
551
549
            'BzrDir.open', ('quack/',), 'success', ('yes',))
552
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
550
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
553
551
            _client=client, _force_probe=True)
554
552
        self.assertIsInstance(bd, RemoteBzrDir)
555
553
        self.assertFinished(client)
586
584
        client.add_expected_call(
587
585
            'Branch.get_stacked_on_url', ('quack/',),
588
586
            'error', ('NotStacked',))
589
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
587
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
590
588
            _client=client)
591
589
        result = bzrdir.open_branch()
592
590
        self.assertIsInstance(result, RemoteBranch)
599
597
        transport = transport.clone('quack')
600
598
        client = FakeClient(transport.base)
601
599
        client.add_error_response('nobranch')
602
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
600
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
603
601
            _client=client)
604
602
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
605
603
        self.assertEqual(
616
614
        transport = MemoryTransport()
617
615
        # no requests on the network - catches other api calls being made.
618
616
        client = FakeClient(transport.base)
619
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
617
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
620
618
            _client=client)
621
619
        # patch the open_branch call to record that it was called.
622
620
        bzrdir.open_branch = open_branch
641
639
        client.add_expected_call(
642
640
            'Branch.get_stacked_on_url', ('~hello/',),
643
641
            'error', ('NotStacked',))
644
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
642
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
645
643
            _client=client)
646
644
        result = bzrdir.open_branch()
647
645
        self.assertFinished(client)
664
662
        client.add_success_response(
665
663
            'ok', '', rich_response, subtree_response, external_lookup,
666
664
            network_name)
667
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
665
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
668
666
            _client=client)
669
667
        result = bzrdir.open_repository()
670
668
        self.assertEqual(
716
714
            'BzrDir.create_branch', ('quack/', network_name),
717
715
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
718
716
            reference_repo_name))
719
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
717
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
720
718
            _client=client)
721
719
        branch = a_bzrdir.create_branch()
722
720
        # We should have got a remote branch
725
723
        format = branch._format
726
724
        self.assertEqual(network_name, format.network_name())
727
725
 
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
 
 
756
726
 
757
727
class TestBzrDirCreateRepository(TestRemote):
758
728
 
779
749
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
780
750
                'False'),
781
751
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
782
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
752
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
783
753
            _client=client)
784
754
        repo = a_bzrdir.create_repository()
785
755
        # We should have got a remote repository
814
784
        client.add_success_response('stat', '0', '65535')
815
785
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
816
786
            _client=client)
817
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
 
787
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
818
788
            _client=client)
819
789
        repo = bzrdir.open_repository()
820
790
        self.assertEqual(
847
817
        client.add_success_response('stat', '0', '65535')
848
818
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
849
819
            _client=client)
850
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
 
820
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
851
821
            _client=client)
852
822
        repo = bzrdir.open_repository()
853
823
        self.assertEqual(
868
838
        transport = transport.clone('quack')
869
839
        client = FakeClient(transport.base)
870
840
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
871
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
841
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
872
842
            _client=client)
873
843
        repo = bzrdir.open_repository()
874
844
        self.assertEqual(
881
851
 
882
852
    def test_success(self):
883
853
        """Simple test for typical successful call."""
884
 
        fmt = RemoteBzrDirFormat()
 
854
        fmt = bzrdir.RemoteBzrDirFormat()
885
855
        default_format_name = BzrDirFormat.get_default_format().network_name()
886
856
        transport = self.get_transport()
887
857
        client = FakeClient(transport.base)
903
873
        """Error responses are translated, e.g. 'PermissionDenied' raises the
904
874
        corresponding error from the client.
905
875
        """
906
 
        fmt = RemoteBzrDirFormat()
 
876
        fmt = bzrdir.RemoteBzrDirFormat()
907
877
        default_format_name = BzrDirFormat.get_default_format().network_name()
908
878
        transport = self.get_transport()
909
879
        client = FakeClient(transport.base)
927
897
        """Integration test for error translation."""
928
898
        transport = self.make_smart_server('foo')
929
899
        transport = transport.clone('no-such-path')
930
 
        fmt = RemoteBzrDirFormat()
 
900
        fmt = bzrdir.RemoteBzrDirFormat()
931
901
        err = self.assertRaises(errors.NoSuchFile,
932
902
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
933
903
 
964
934
 
965
935
    def make_remote_bzrdir(self, transport, client):
966
936
        """Make a RemotebzrDir using 'client' as the _client."""
967
 
        return RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
937
        return RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
968
938
            _client=client)
969
939
 
970
940
 
1172
1142
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1173
1143
 
1174
1144
 
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
 
 
1240
1145
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1241
1146
 
1242
1147
    def test_empty_branch(self):
1297
1202
        client.add_expected_call(
1298
1203
            'Branch.get_stacked_on_url', ('stacked/',),
1299
1204
            'success', ('ok', vfs_url))
1300
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
1205
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1301
1206
            _client=client)
1302
1207
        repo_fmt = remote.RemoteRepositoryFormat()
1303
1208
        repo_fmt._custom_format = stacked_branch.repository._format
1330
1235
        # this will also do vfs access, but that goes direct to the transport
1331
1236
        # and isn't seen by the FakeClient.
1332
1237
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1333
 
            RemoteBzrDirFormat(), _client=client)
 
1238
            remote.RemoteBzrDirFormat(), _client=client)
1334
1239
        branch = bzrdir.open_branch()
1335
1240
        result = branch.get_stacked_on_url()
1336
1241
        self.assertEqual('../base', result)
1363
1268
            'Branch.get_stacked_on_url', ('stacked/',),
1364
1269
            'success', ('ok', '../base'))
1365
1270
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1366
 
            RemoteBzrDirFormat(), _client=client)
 
1271
            remote.RemoteBzrDirFormat(), _client=client)
1367
1272
        branch = bzrdir.open_branch()
1368
1273
        result = branch.get_stacked_on_url()
1369
1274
        self.assertEqual('../base', result)
1377
1282
class TestBranchSetLastRevision(RemoteBranchTestCase):
1378
1283
 
1379
1284
    def test_set_empty(self):
1380
 
        # _set_last_revision_info('null:') is translated to calling
 
1285
        # set_revision_history([]) is translated to calling
1381
1286
        # Branch.set_last_revision(path, '') on the wire.
1382
1287
        transport = MemoryTransport()
1383
1288
        transport.mkdir('branch')
1405
1310
        # unnecessarily invokes _ensure_real upon a call to lock_write.
1406
1311
        branch._ensure_real = lambda: None
1407
1312
        branch.lock_write()
1408
 
        result = branch._set_last_revision(NULL_REVISION)
 
1313
        result = branch.set_revision_history([])
1409
1314
        branch.unlock()
1410
1315
        self.assertEqual(None, result)
1411
1316
        self.assertFinished(client)
1412
1317
 
1413
1318
    def test_set_nonempty(self):
1414
 
        # set_last_revision_info(N, rev-idN) is translated to calling
 
1319
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
1415
1320
        # Branch.set_last_revision(path, rev-idN) on the wire.
1416
1321
        transport = MemoryTransport()
1417
1322
        transport.mkdir('branch')
1443
1348
        branch._ensure_real = lambda: None
1444
1349
        # Lock the branch, reset the record of remote calls.
1445
1350
        branch.lock_write()
1446
 
        result = branch._set_last_revision('rev-id2')
 
1351
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
1447
1352
        branch.unlock()
1448
1353
        self.assertEqual(None, result)
1449
1354
        self.assertFinished(client)
1479
1384
        branch = self.make_remote_branch(transport, client)
1480
1385
        branch.lock_write()
1481
1386
        self.assertRaises(
1482
 
            errors.NoSuchRevision, branch._set_last_revision, 'rev-id')
 
1387
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
1483
1388
        branch.unlock()
1484
1389
        self.assertFinished(client)
1485
1390
 
1516
1421
        branch._ensure_real = lambda: None
1517
1422
        branch.lock_write()
1518
1423
        # The 'TipChangeRejected' error response triggered by calling
1519
 
        # set_last_revision_info causes a TipChangeRejected exception.
 
1424
        # set_revision_history causes a TipChangeRejected exception.
1520
1425
        err = self.assertRaises(
1521
 
            errors.TipChangeRejected,
1522
 
            branch._set_last_revision, 'rev-id')
 
1426
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
1523
1427
        # The UTF-8 message from the response has been decoded into a unicode
1524
1428
        # object.
1525
1429
        self.assertIsInstance(err.msg, unicode)
1940
1844
        client = FakeClient(transport.base)
1941
1845
        transport = transport.clone(transport_path)
1942
1846
        # we do not want bzrdir to make any remote calls
1943
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
1847
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1944
1848
            _client=False)
1945
1849
        repo = RemoteRepository(bzrdir, None, _client=client)
1946
1850
        return repo, client
1954
1858
 
1955
1859
    def test_get_format_description(self):
1956
1860
        remote_format = RemoteBranchFormat()
1957
 
        real_format = branch.format_registry.get_default()
 
1861
        real_format = branch.BranchFormat.get_default_format()
1958
1862
        remote_format._network_name = real_format.network_name()
1959
1863
        self.assertEqual(remoted_description(real_format),
1960
1864
            remote_format.get_format_description())
1963
1867
class TestRepositoryFormat(TestRemoteRepository):
1964
1868
 
1965
1869
    def test_fast_delta(self):
1966
 
        true_name = groupcompress_repo.RepositoryFormat2a().network_name()
 
1870
        true_name = groupcompress_repo.RepositoryFormatCHK1().network_name()
1967
1871
        true_format = RemoteRepositoryFormat()
1968
1872
        true_format._network_name = true_name
1969
1873
        self.assertEqual(True, true_format.fast_deltas)
1970
 
        false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
 
1874
        false_name = pack_repo.RepositoryFormatKnitPack1().network_name()
1971
1875
        false_format = RemoteRepositoryFormat()
1972
1876
        false_format._network_name = false_name
1973
1877
        self.assertEqual(False, false_format.fast_deltas)
1974
1878
 
1975
1879
    def test_get_format_description(self):
1976
1880
        remote_repo_format = RemoteRepositoryFormat()
1977
 
        real_format = repository.format_registry.get_default()
 
1881
        real_format = repository.RepositoryFormat.get_default_format()
1978
1882
        remote_repo_format._network_name = real_format.network_name()
1979
1883
        self.assertEqual(remoted_description(real_format),
1980
1884
            remote_repo_format.get_format_description())
2538
2442
        the client is finished.
2539
2443
        """
2540
2444
        sink = repo._get_sink()
2541
 
        fmt = repository.format_registry.get_default()
 
2445
        fmt = repository.RepositoryFormat.get_default_format()
2542
2446
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2543
2447
        self.assertEqual([], resume_tokens)
2544
2448
        self.assertEqual(set(), missing_keys)
2644
2548
                return True
2645
2549
        repo._real_repository = FakeRealRepository()
2646
2550
        sink = repo._get_sink()
2647
 
        fmt = repository.format_registry.get_default()
 
2551
        fmt = repository.RepositoryFormat.get_default_format()
2648
2552
        stream = self.make_stream_with_inv_deltas(fmt)
2649
2553
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2650
2554
        # Every record from the first inventory delta should have been sent to
2870
2774
             ('pack collection autopack',)],
2871
2775
            client._calls)
2872
2776
 
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
 
 
2883
2777
 
2884
2778
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2885
2779
    """Base class for unit tests for bzrlib.remote._translate_error."""
2958
2852
            detail='extra detail')
2959
2853
        self.assertEqual(expected_error, translated_error)
2960
2854
 
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
 
 
2968
2855
    def test_LockContention(self):
2969
2856
        translated_error = self.translateTuple(('LockContention',))
2970
2857
        expected_error = errors.LockContention('(remote lock)')
2998
2885
        expected_error = errors.DivergedBranches(branch, other_branch)
2999
2886
        self.assertEqual(expected_error, translated_error)
3000
2887
 
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
 
 
3007
2888
    def test_ReadError_no_args(self):
3008
2889
        path = 'a path'
3009
2890
        translated_error = self.translateTuple(('ReadError',), path=path)
3025
2906
 
3026
2907
    def test_PermissionDenied_no_args(self):
3027
2908
        path = 'a path'
3028
 
        translated_error = self.translateTuple(('PermissionDenied',),
3029
 
            path=path)
 
2909
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
3030
2910
        expected_error = errors.PermissionDenied(path)
3031
2911
        self.assertEqual(expected_error, translated_error)
3032
2912
 
3055
2935
        expected_error = errors.PermissionDenied(path, extra)
3056
2936
        self.assertEqual(expected_error, translated_error)
3057
2937
 
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
 
 
3097
2938
 
3098
2939
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3099
2940
    """Unit tests for bzrlib.remote._translate_error's robustness.
3341
3182
 
3342
3183
    def test_copy_content_into_avoids_revision_history(self):
3343
3184
        local = self.make_branch('local')
3344
 
        builder = self.make_branch_builder('remote')
3345
 
        builder.build_commit(message="Commit.")
 
3185
        remote_backing_tree = self.make_branch_and_tree('remote')
 
3186
        remote_backing_tree.commit("Commit.")
3346
3187
        remote_branch_url = self.smart_server.get_url() + 'remote'
3347
3188
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3348
3189
        local.repository.fetch(remote_branch.repository)
3349
3190
        self.hpss_calls = []
3350
3191
        remote_branch.copy_content_into(local)
3351
3192
        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