~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Martin Pool
  • Date: 2010-08-18 07:25:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5383.
  • Revision ID: mbp@sourcefrog.net-20100818072522-uk3gsazoia3l3s0a
Start adding 'what's new in 2.3'

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
30
30
    branch,
31
31
    bzrdir,
32
32
    config,
33
 
    controldir,
34
33
    errors,
35
 
    graph as _mod_graph,
 
34
    graph,
36
35
    inventory,
37
36
    inventory_delta,
 
37
    pack,
38
38
    remote,
39
39
    repository,
40
40
    tests,
41
41
    transport,
42
42
    treebuilder,
 
43
    urlutils,
43
44
    versionedfile,
44
45
    )
45
46
from bzrlib.branch import Branch
46
 
from bzrlib.bzrdir import (
47
 
    BzrDir,
48
 
    BzrDirFormat,
49
 
    RemoteBzrProber,
50
 
    )
 
47
from bzrlib.bzrdir import BzrDir, BzrDirFormat
51
48
from bzrlib.remote import (
52
49
    RemoteBranch,
53
50
    RemoteBranchFormat,
56
53
    RemoteRepository,
57
54
    RemoteRepositoryFormat,
58
55
    )
59
 
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
 
56
from bzrlib.repofmt import groupcompress_repo, pack_repo
60
57
from bzrlib.revision import NULL_REVISION
61
 
from bzrlib.smart import medium, request
 
58
from bzrlib.smart import medium
62
59
from bzrlib.smart.client import _SmartClient
63
 
from bzrlib.smart.repository import (
64
 
    SmartServerRepositoryGetParentMap,
65
 
    SmartServerRepositoryGetStream_1_19,
66
 
    )
 
60
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
67
61
from bzrlib.tests import (
 
62
    condition_isinstance,
 
63
    split_suite_by_condition,
 
64
    multiply_tests,
68
65
    test_server,
69
66
    )
70
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
71
67
from bzrlib.transport.memory import MemoryTransport
72
68
from bzrlib.transport.remote import (
73
69
    RemoteTransport,
74
70
    RemoteSSHTransport,
75
71
    RemoteTCPTransport,
76
 
    )
77
 
 
78
 
 
79
 
load_tests = load_tests_apply_scenarios
80
 
 
81
 
 
82
 
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
83
 
 
84
 
    scenarios = [
 
72
)
 
73
 
 
74
def load_tests(standard_tests, module, loader):
 
75
    to_adapt, result = split_suite_by_condition(
 
76
        standard_tests, condition_isinstance(BasicRemoteObjectTests))
 
77
    smart_server_version_scenarios = [
85
78
        ('HPSS-v2',
86
 
            {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
 
79
         {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
87
80
        ('HPSS-v3',
88
 
            {'transport_server': test_server.SmartTCPServer_for_testing})]
89
 
 
 
81
         {'transport_server': test_server.SmartTCPServer_for_testing})]
 
82
    return multiply_tests(to_adapt, smart_server_version_scenarios, result)
 
83
 
 
84
 
 
85
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
90
86
 
91
87
    def setUp(self):
92
88
        super(BasicRemoteObjectTests, self).setUp()
96
92
        self.addCleanup(self.transport.disconnect)
97
93
 
98
94
    def test_create_remote_bzrdir(self):
99
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
95
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
100
96
        self.assertIsInstance(b, BzrDir)
101
97
 
102
98
    def test_open_remote_branch(self):
103
99
        # open a standalone branch in the working directory
104
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
100
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
105
101
        branch = b.open_branch()
106
102
        self.assertIsInstance(branch, Branch)
107
103
 
123
119
    def test_find_correct_format(self):
124
120
        """Should open a RemoteBzrDir over a RemoteTransport"""
125
121
        fmt = BzrDirFormat.find_format(self.transport)
126
 
        self.assertTrue(bzrdir.RemoteBzrProber
127
 
                        in controldir.ControlDirFormat._server_probers)
128
 
        self.assertIsInstance(fmt, RemoteBzrDirFormat)
 
122
        self.assertTrue(RemoteBzrDirFormat
 
123
                        in BzrDirFormat._control_server_formats)
 
124
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
129
125
 
130
126
    def test_open_detected_smart_format(self):
131
127
        fmt = BzrDirFormat.find_format(self.transport)
451
447
        client.add_expected_call(
452
448
            'BzrDir.open_branchV3', ('quack/',),
453
449
            'success', ('ref', self.get_url('referenced'))),
454
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
450
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
455
451
            _client=client)
456
452
        result = a_bzrdir.cloning_metadir()
457
453
        # We should have got a control dir matching the referenced branch.
470
466
        client.add_expected_call(
471
467
            'BzrDir.cloning_metadir', ('quack/', 'False'),
472
468
            'success', (control_name, '', ('branch', ''))),
473
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
469
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
474
470
            _client=client)
475
471
        result = a_bzrdir.cloning_metadir()
476
472
        # We should have got a reference control dir with default branch and
496
492
        client.add_expected_call(
497
493
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
498
494
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
499
 
                RemoteBzrDirFormat(), _client=client, _force_probe=True)
 
495
                remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
500
496
        self.assertFinished(client)
501
497
 
502
498
    def test_present_without_workingtree(self):
503
499
        client, transport = self.make_fake_client_and_transport()
504
500
        client.add_expected_call(
505
501
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
506
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
502
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
507
503
            _client=client, _force_probe=True)
508
504
        self.assertIsInstance(bd, RemoteBzrDir)
509
505
        self.assertFalse(bd.has_workingtree())
514
510
        client, transport = self.make_fake_client_and_transport()
515
511
        client.add_expected_call(
516
512
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
517
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
513
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
518
514
            _client=client, _force_probe=True)
519
515
        self.assertIsInstance(bd, RemoteBzrDir)
520
516
        self.assertTrue(bd.has_workingtree())
527
523
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
528
524
        client.add_expected_call(
529
525
            'BzrDir.open', ('quack/',), 'success', ('yes',))
530
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
526
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
531
527
            _client=client, _force_probe=True)
532
528
        self.assertIsInstance(bd, RemoteBzrDir)
533
529
        self.assertFinished(client)
549
545
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
550
546
        client.add_expected_call(
551
547
            'BzrDir.open', ('quack/',), 'success', ('yes',))
552
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
548
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
553
549
            _client=client, _force_probe=True)
554
550
        self.assertIsInstance(bd, RemoteBzrDir)
555
551
        self.assertFinished(client)
586
582
        client.add_expected_call(
587
583
            'Branch.get_stacked_on_url', ('quack/',),
588
584
            'error', ('NotStacked',))
589
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
585
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
590
586
            _client=client)
591
587
        result = bzrdir.open_branch()
592
588
        self.assertIsInstance(result, RemoteBranch)
599
595
        transport = transport.clone('quack')
600
596
        client = FakeClient(transport.base)
601
597
        client.add_error_response('nobranch')
602
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
598
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
603
599
            _client=client)
604
600
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
605
601
        self.assertEqual(
616
612
        transport = MemoryTransport()
617
613
        # no requests on the network - catches other api calls being made.
618
614
        client = FakeClient(transport.base)
619
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
615
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
620
616
            _client=client)
621
617
        # patch the open_branch call to record that it was called.
622
618
        bzrdir.open_branch = open_branch
641
637
        client.add_expected_call(
642
638
            'Branch.get_stacked_on_url', ('~hello/',),
643
639
            'error', ('NotStacked',))
644
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
640
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
645
641
            _client=client)
646
642
        result = bzrdir.open_branch()
647
643
        self.assertFinished(client)
664
660
        client.add_success_response(
665
661
            'ok', '', rich_response, subtree_response, external_lookup,
666
662
            network_name)
667
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
663
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
668
664
            _client=client)
669
665
        result = bzrdir.open_repository()
670
666
        self.assertEqual(
687
683
        old.
688
684
        """
689
685
        self.assertRaises(errors.NotBranchError,
690
 
            RemoteBzrProber.probe_transport, OldServerTransport())
 
686
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
691
687
 
692
688
 
693
689
class TestBzrDirCreateBranch(TestRemote):
716
712
            'BzrDir.create_branch', ('quack/', network_name),
717
713
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
718
714
            reference_repo_name))
719
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
715
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
720
716
            _client=client)
721
717
        branch = a_bzrdir.create_branch()
722
718
        # We should have got a remote branch
725
721
        format = branch._format
726
722
        self.assertEqual(network_name, format.network_name())
727
723
 
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
724
 
757
725
class TestBzrDirCreateRepository(TestRemote):
758
726
 
779
747
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
780
748
                'False'),
781
749
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
782
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
750
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
783
751
            _client=client)
784
752
        repo = a_bzrdir.create_repository()
785
753
        # We should have got a remote repository
814
782
        client.add_success_response('stat', '0', '65535')
815
783
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
816
784
            _client=client)
817
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
 
785
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
818
786
            _client=client)
819
787
        repo = bzrdir.open_repository()
820
788
        self.assertEqual(
847
815
        client.add_success_response('stat', '0', '65535')
848
816
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
849
817
            _client=client)
850
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
 
818
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
851
819
            _client=client)
852
820
        repo = bzrdir.open_repository()
853
821
        self.assertEqual(
868
836
        transport = transport.clone('quack')
869
837
        client = FakeClient(transport.base)
870
838
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
871
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
839
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
872
840
            _client=client)
873
841
        repo = bzrdir.open_repository()
874
842
        self.assertEqual(
881
849
 
882
850
    def test_success(self):
883
851
        """Simple test for typical successful call."""
884
 
        fmt = RemoteBzrDirFormat()
 
852
        fmt = bzrdir.RemoteBzrDirFormat()
885
853
        default_format_name = BzrDirFormat.get_default_format().network_name()
886
854
        transport = self.get_transport()
887
855
        client = FakeClient(transport.base)
903
871
        """Error responses are translated, e.g. 'PermissionDenied' raises the
904
872
        corresponding error from the client.
905
873
        """
906
 
        fmt = RemoteBzrDirFormat()
 
874
        fmt = bzrdir.RemoteBzrDirFormat()
907
875
        default_format_name = BzrDirFormat.get_default_format().network_name()
908
876
        transport = self.get_transport()
909
877
        client = FakeClient(transport.base)
927
895
        """Integration test for error translation."""
928
896
        transport = self.make_smart_server('foo')
929
897
        transport = transport.clone('no-such-path')
930
 
        fmt = RemoteBzrDirFormat()
 
898
        fmt = bzrdir.RemoteBzrDirFormat()
931
899
        err = self.assertRaises(errors.NoSuchFile,
932
900
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
933
901
 
964
932
 
965
933
    def make_remote_bzrdir(self, transport, client):
966
934
        """Make a RemotebzrDir using 'client' as the _client."""
967
 
        return RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
935
        return RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
968
936
            _client=client)
969
937
 
970
938
 
1172
1140
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1173
1141
 
1174
1142
 
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
1143
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1241
1144
 
1242
1145
    def test_empty_branch(self):
1297
1200
        client.add_expected_call(
1298
1201
            'Branch.get_stacked_on_url', ('stacked/',),
1299
1202
            'success', ('ok', vfs_url))
1300
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
1203
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1301
1204
            _client=client)
1302
1205
        repo_fmt = remote.RemoteRepositoryFormat()
1303
1206
        repo_fmt._custom_format = stacked_branch.repository._format
1330
1233
        # this will also do vfs access, but that goes direct to the transport
1331
1234
        # and isn't seen by the FakeClient.
1332
1235
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1333
 
            RemoteBzrDirFormat(), _client=client)
 
1236
            remote.RemoteBzrDirFormat(), _client=client)
1334
1237
        branch = bzrdir.open_branch()
1335
1238
        result = branch.get_stacked_on_url()
1336
1239
        self.assertEqual('../base', result)
1363
1266
            'Branch.get_stacked_on_url', ('stacked/',),
1364
1267
            'success', ('ok', '../base'))
1365
1268
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1366
 
            RemoteBzrDirFormat(), _client=client)
 
1269
            remote.RemoteBzrDirFormat(), _client=client)
1367
1270
        branch = bzrdir.open_branch()
1368
1271
        result = branch.get_stacked_on_url()
1369
1272
        self.assertEqual('../base', result)
1377
1280
class TestBranchSetLastRevision(RemoteBranchTestCase):
1378
1281
 
1379
1282
    def test_set_empty(self):
1380
 
        # _set_last_revision_info('null:') is translated to calling
 
1283
        # set_revision_history([]) is translated to calling
1381
1284
        # Branch.set_last_revision(path, '') on the wire.
1382
1285
        transport = MemoryTransport()
1383
1286
        transport.mkdir('branch')
1405
1308
        # unnecessarily invokes _ensure_real upon a call to lock_write.
1406
1309
        branch._ensure_real = lambda: None
1407
1310
        branch.lock_write()
1408
 
        result = branch._set_last_revision(NULL_REVISION)
 
1311
        result = branch.set_revision_history([])
1409
1312
        branch.unlock()
1410
1313
        self.assertEqual(None, result)
1411
1314
        self.assertFinished(client)
1412
1315
 
1413
1316
    def test_set_nonempty(self):
1414
 
        # set_last_revision_info(N, rev-idN) is translated to calling
 
1317
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
1415
1318
        # Branch.set_last_revision(path, rev-idN) on the wire.
1416
1319
        transport = MemoryTransport()
1417
1320
        transport.mkdir('branch')
1443
1346
        branch._ensure_real = lambda: None
1444
1347
        # Lock the branch, reset the record of remote calls.
1445
1348
        branch.lock_write()
1446
 
        result = branch._set_last_revision('rev-id2')
 
1349
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
1447
1350
        branch.unlock()
1448
1351
        self.assertEqual(None, result)
1449
1352
        self.assertFinished(client)
1479
1382
        branch = self.make_remote_branch(transport, client)
1480
1383
        branch.lock_write()
1481
1384
        self.assertRaises(
1482
 
            errors.NoSuchRevision, branch._set_last_revision, 'rev-id')
 
1385
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
1483
1386
        branch.unlock()
1484
1387
        self.assertFinished(client)
1485
1388
 
1516
1419
        branch._ensure_real = lambda: None
1517
1420
        branch.lock_write()
1518
1421
        # The 'TipChangeRejected' error response triggered by calling
1519
 
        # set_last_revision_info causes a TipChangeRejected exception.
 
1422
        # set_revision_history causes a TipChangeRejected exception.
1520
1423
        err = self.assertRaises(
1521
 
            errors.TipChangeRejected,
1522
 
            branch._set_last_revision, 'rev-id')
 
1424
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
1523
1425
        # The UTF-8 message from the response has been decoded into a unicode
1524
1426
        # object.
1525
1427
        self.assertIsInstance(err.msg, unicode)
1940
1842
        client = FakeClient(transport.base)
1941
1843
        transport = transport.clone(transport_path)
1942
1844
        # we do not want bzrdir to make any remote calls
1943
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
1845
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1944
1846
            _client=False)
1945
1847
        repo = RemoteRepository(bzrdir, None, _client=client)
1946
1848
        return repo, client
1954
1856
 
1955
1857
    def test_get_format_description(self):
1956
1858
        remote_format = RemoteBranchFormat()
1957
 
        real_format = branch.format_registry.get_default()
 
1859
        real_format = branch.BranchFormat.get_default_format()
1958
1860
        remote_format._network_name = real_format.network_name()
1959
1861
        self.assertEqual(remoted_description(real_format),
1960
1862
            remote_format.get_format_description())
1963
1865
class TestRepositoryFormat(TestRemoteRepository):
1964
1866
 
1965
1867
    def test_fast_delta(self):
1966
 
        true_name = groupcompress_repo.RepositoryFormat2a().network_name()
 
1868
        true_name = groupcompress_repo.RepositoryFormatCHK1().network_name()
1967
1869
        true_format = RemoteRepositoryFormat()
1968
1870
        true_format._network_name = true_name
1969
1871
        self.assertEqual(True, true_format.fast_deltas)
1970
 
        false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
 
1872
        false_name = pack_repo.RepositoryFormatKnitPack1().network_name()
1971
1873
        false_format = RemoteRepositoryFormat()
1972
1874
        false_format._network_name = false_name
1973
1875
        self.assertEqual(False, false_format.fast_deltas)
1974
1876
 
1975
1877
    def test_get_format_description(self):
1976
1878
        remote_repo_format = RemoteRepositoryFormat()
1977
 
        real_format = repository.format_registry.get_default()
 
1879
        real_format = repository.RepositoryFormat.get_default_format()
1978
1880
        remote_repo_format._network_name = real_format.network_name()
1979
1881
        self.assertEqual(remoted_description(real_format),
1980
1882
            remote_repo_format.get_format_description())
2538
2440
        the client is finished.
2539
2441
        """
2540
2442
        sink = repo._get_sink()
2541
 
        fmt = repository.format_registry.get_default()
 
2443
        fmt = repository.RepositoryFormat.get_default_format()
2542
2444
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2543
2445
        self.assertEqual([], resume_tokens)
2544
2446
        self.assertEqual(set(), missing_keys)
2644
2546
                return True
2645
2547
        repo._real_repository = FakeRealRepository()
2646
2548
        sink = repo._get_sink()
2647
 
        fmt = repository.format_registry.get_default()
 
2549
        fmt = repository.RepositoryFormat.get_default_format()
2648
2550
        stream = self.make_stream_with_inv_deltas(fmt)
2649
2551
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2650
2552
        # Every record from the first inventory delta should have been sent to
2870
2772
             ('pack collection autopack',)],
2871
2773
            client._calls)
2872
2774
 
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
2775
 
2884
2776
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2885
2777
    """Base class for unit tests for bzrlib.remote._translate_error."""
2958
2850
            detail='extra detail')
2959
2851
        self.assertEqual(expected_error, translated_error)
2960
2852
 
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
2853
    def test_LockContention(self):
2969
2854
        translated_error = self.translateTuple(('LockContention',))
2970
2855
        expected_error = errors.LockContention('(remote lock)')
2998
2883
        expected_error = errors.DivergedBranches(branch, other_branch)
2999
2884
        self.assertEqual(expected_error, translated_error)
3000
2885
 
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
2886
    def test_ReadError_no_args(self):
3008
2887
        path = 'a path'
3009
2888
        translated_error = self.translateTuple(('ReadError',), path=path)
3025
2904
 
3026
2905
    def test_PermissionDenied_no_args(self):
3027
2906
        path = 'a path'
3028
 
        translated_error = self.translateTuple(('PermissionDenied',),
3029
 
            path=path)
 
2907
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
3030
2908
        expected_error = errors.PermissionDenied(path)
3031
2909
        self.assertEqual(expected_error, translated_error)
3032
2910
 
3055
2933
        expected_error = errors.PermissionDenied(path, extra)
3056
2934
        self.assertEqual(expected_error, translated_error)
3057
2935
 
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
2936
 
3098
2937
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3099
2938
    """Unit tests for bzrlib.remote._translate_error's robustness.
3244
3083
        _, stacked = branch_factory()
3245
3084
        source = stacked.repository._get_source(target_repository_format)
3246
3085
        tip = stacked.last_revision()
3247
 
        stacked.repository._ensure_real()
3248
 
        graph = stacked.repository.get_graph()
3249
 
        revs = [r for (r,ps) in graph.iter_ancestry([tip])
3250
 
                if r != NULL_REVISION]
3251
 
        revs.reverse()
3252
 
        search = _mod_graph.PendingAncestryResult([tip], stacked.repository)
 
3086
        revs = stacked.repository.get_ancestry(tip)
 
3087
        search = graph.PendingAncestryResult([tip], stacked.repository)
3253
3088
        self.reset_smart_call_log()
3254
3089
        stream = source.get_stream(search)
 
3090
        if None in revs:
 
3091
            revs.remove(None)
3255
3092
        # We trust that if a revision is in the stream the rest of the new
3256
3093
        # content for it is too, as per our main fetch tests; here we are
3257
3094
        # checking that the revisions are actually included at all, and their
3296
3133
        self.assertEqual(expected_revs, rev_ord)
3297
3134
        # Getting topological sort requires VFS calls still - one of which is
3298
3135
        # pushing up from the bound branch.
3299
 
        self.assertLength(14, self.hpss_calls)
 
3136
        self.assertLength(13, self.hpss_calls)
3300
3137
 
3301
3138
    def test_stacked_get_stream_groupcompress(self):
3302
3139
        # Repository._get_source.get_stream() from a stacked repository with
3343
3180
 
3344
3181
    def test_copy_content_into_avoids_revision_history(self):
3345
3182
        local = self.make_branch('local')
3346
 
        builder = self.make_branch_builder('remote')
3347
 
        builder.build_commit(message="Commit.")
 
3183
        remote_backing_tree = self.make_branch_and_tree('remote')
 
3184
        remote_backing_tree.commit("Commit.")
3348
3185
        remote_branch_url = self.smart_server.get_url() + 'remote'
3349
3186
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3350
3187
        local.repository.fetch(remote_branch.repository)
3351
3188
        self.hpss_calls = []
3352
3189
        remote_branch.copy_content_into(local)
3353
3190
        self.assertFalse('Branch.revision_history' in self.hpss_calls)
3354
 
 
3355
 
    def test_fetch_everything_needs_just_one_call(self):
3356
 
        local = self.make_branch('local')
3357
 
        builder = self.make_branch_builder('remote')
3358
 
        builder.build_commit(message="Commit.")
3359
 
        remote_branch_url = self.smart_server.get_url() + 'remote'
3360
 
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3361
 
        self.hpss_calls = []
3362
 
        local.repository.fetch(remote_branch.repository,
3363
 
                fetch_spec=_mod_graph.EverythingResult(remote_branch.repository))
3364
 
        self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
3365
 
 
3366
 
    def override_verb(self, verb_name, verb):
3367
 
        request_handlers = request.request_handlers
3368
 
        orig_verb = request_handlers.get(verb_name)
3369
 
        request_handlers.register(verb_name, verb, override_existing=True)
3370
 
        self.addCleanup(request_handlers.register, verb_name, orig_verb,
3371
 
                override_existing=True)
3372
 
 
3373
 
    def test_fetch_everything_backwards_compat(self):
3374
 
        """Can fetch with EverythingResult even with pre 2.4 servers.
3375
 
        
3376
 
        Pre-2.4 do not support 'everything' searches with the
3377
 
        Repository.get_stream_1.19 verb.
3378
 
        """
3379
 
        verb_log = []
3380
 
        class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
3381
 
            """A version of the Repository.get_stream_1.19 verb patched to
3382
 
            reject 'everything' searches the way 2.3 and earlier do.
3383
 
            """
3384
 
            def recreate_search(self, repository, search_bytes, discard_excess=False):
3385
 
                verb_log.append(search_bytes.split('\n', 1)[0])
3386
 
                if search_bytes == 'everything':
3387
 
                    return (None, request.FailedSmartServerResponse(('BadSearch',)))
3388
 
                return super(OldGetStreamVerb,
3389
 
                        self).recreate_search(repository, search_bytes,
3390
 
                            discard_excess=discard_excess)
3391
 
        self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
3392
 
        local = self.make_branch('local')
3393
 
        builder = self.make_branch_builder('remote')
3394
 
        builder.build_commit(message="Commit.")
3395
 
        remote_branch_url = self.smart_server.get_url() + 'remote'
3396
 
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3397
 
        self.hpss_calls = []
3398
 
        local.repository.fetch(remote_branch.repository,
3399
 
                fetch_spec=_mod_graph.EverythingResult(remote_branch.repository))
3400
 
        # make sure the overridden verb was used
3401
 
        self.assertLength(1, verb_log)
3402
 
        # more than one HPSS call is needed, but because it's a VFS callback
3403
 
        # its hard to predict exactly how many.
3404
 
        self.assertTrue(len(self.hpss_calls) > 1)
3405