~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-03-11 04:33:41 UTC
  • mfrom: (4797.33.4 2.1)
  • mto: This revision was merged to the branch mainline in revision 5082.
  • Revision ID: andrew.bennetts@canonical.com-20100311043341-rzdik83fnactjsxs
Merge lp:bzr/2.1, including fixes for #496813, #526211, #526353.

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
 
    transport,
42
41
    treebuilder,
 
42
    urlutils,
43
43
    versionedfile,
44
44
    )
45
45
from bzrlib.branch import Branch
46
 
from bzrlib.bzrdir import (
47
 
    BzrDir,
48
 
    BzrDirFormat,
49
 
    RemoteBzrProber,
50
 
    )
 
46
from bzrlib.bzrdir import BzrDir, BzrDirFormat
51
47
from bzrlib.remote import (
52
48
    RemoteBranch,
53
49
    RemoteBranchFormat,
56
52
    RemoteRepository,
57
53
    RemoteRepositoryFormat,
58
54
    )
59
 
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
 
55
from bzrlib.repofmt import groupcompress_repo, pack_repo
60
56
from bzrlib.revision import NULL_REVISION
61
 
from bzrlib.smart import medium, request
 
57
from bzrlib.smart import medium
62
58
from bzrlib.smart.client import _SmartClient
63
 
from bzrlib.smart.repository import (
64
 
    SmartServerRepositoryGetParentMap,
65
 
    SmartServerRepositoryGetStream_1_19,
66
 
    )
 
59
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
67
60
from bzrlib.tests import (
 
61
    condition_isinstance,
 
62
    split_suite_by_condition,
 
63
    multiply_tests,
68
64
    test_server,
69
65
    )
70
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
66
from bzrlib.transport import get_transport
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()
93
89
        self.transport = self.get_transport()
94
90
        # make a branch that can be opened over the smart transport
95
91
        self.local_wt = BzrDir.create_standalone_workingtree('.')
96
 
        self.addCleanup(self.transport.disconnect)
 
92
 
 
93
    def tearDown(self):
 
94
        self.transport.disconnect()
 
95
        tests.TestCaseWithTransport.tearDown(self)
97
96
 
98
97
    def test_create_remote_bzrdir(self):
99
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
98
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
100
99
        self.assertIsInstance(b, BzrDir)
101
100
 
102
101
    def test_open_remote_branch(self):
103
102
        # open a standalone branch in the working directory
104
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
103
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
105
104
        branch = b.open_branch()
106
105
        self.assertIsInstance(branch, Branch)
107
106
 
123
122
    def test_find_correct_format(self):
124
123
        """Should open a RemoteBzrDir over a RemoteTransport"""
125
124
        fmt = BzrDirFormat.find_format(self.transport)
126
 
        self.assertTrue(bzrdir.RemoteBzrProber
127
 
                        in controldir.ControlDirFormat._server_probers)
128
 
        self.assertIsInstance(fmt, RemoteBzrDirFormat)
 
125
        self.assertTrue(RemoteBzrDirFormat
 
126
                        in BzrDirFormat._control_server_formats)
 
127
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
129
128
 
130
129
    def test_open_detected_smart_format(self):
131
130
        fmt = BzrDirFormat.find_format(self.transport)
360
359
        a given client_base and transport_base.
361
360
        """
362
361
        client_medium = medium.SmartClientMedium(client_base)
363
 
        t = transport.get_transport(transport_base)
364
 
        result = client_medium.remote_path_from_transport(t)
 
362
        transport = get_transport(transport_base)
 
363
        result = client_medium.remote_path_from_transport(transport)
365
364
        self.assertEqual(expected, result)
366
365
 
367
366
    def test_remote_path_from_transport(self):
378
377
        a given transport_base and relpath of that transport.  (Note that
379
378
        HttpTransportBase is a subclass of SmartClientMedium)
380
379
        """
381
 
        base_transport = transport.get_transport(transport_base)
 
380
        base_transport = get_transport(transport_base)
382
381
        client_medium = base_transport.get_smart_medium()
383
382
        cloned_transport = base_transport.clone(relpath)
384
383
        result = client_medium.remote_path_from_transport(cloned_transport)
419
418
        # Calling _remember_remote_is_before again with a lower value works.
420
419
        client_medium._remember_remote_is_before((1, 5))
421
420
        self.assertTrue(client_medium._is_remote_before((1, 5)))
422
 
        # If you call _remember_remote_is_before with a higher value it logs a
423
 
        # warning, and continues to remember the lower value.
424
 
        self.assertNotContainsRe(self.get_log(), '_remember_remote_is_before')
425
 
        client_medium._remember_remote_is_before((1, 9))
426
 
        self.assertContainsRe(self.get_log(), '_remember_remote_is_before')
427
 
        self.assertTrue(client_medium._is_remote_before((1, 5)))
 
421
        # You cannot call _remember_remote_is_before with a larger value.
 
422
        self.assertRaises(
 
423
            AssertionError, client_medium._remember_remote_is_before, (1, 9))
428
424
 
429
425
 
430
426
class TestBzrDirCloningMetaDir(TestRemote):
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(),
531
 
            _client=client, _force_probe=True)
532
 
        self.assertIsInstance(bd, RemoteBzrDir)
533
 
        self.assertFinished(client)
534
 
 
535
 
    def test_backwards_compat_hpss_v2(self):
536
 
        client, transport = self.make_fake_client_and_transport()
537
 
        # Monkey-patch fake client to simulate real-world behaviour with v2
538
 
        # server: upon first RPC call detect the protocol version, and because
539
 
        # the version is 2 also do _remember_remote_is_before((1, 6)) before
540
 
        # continuing with the RPC.
541
 
        orig_check_call = client._check_call
542
 
        def check_call(method, args):
543
 
            client._medium._protocol_version = 2
544
 
            client._medium._remember_remote_is_before((1, 6))
545
 
            client._check_call = orig_check_call
546
 
            client._check_call(method, args)
547
 
        client._check_call = check_call
548
 
        client.add_expected_call(
549
 
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
550
 
        client.add_expected_call(
551
 
            'BzrDir.open', ('quack/',), 'success', ('yes',))
552
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
526
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
553
527
            _client=client, _force_probe=True)
554
528
        self.assertIsInstance(bd, RemoteBzrDir)
555
529
        self.assertFinished(client)
586
560
        client.add_expected_call(
587
561
            'Branch.get_stacked_on_url', ('quack/',),
588
562
            'error', ('NotStacked',))
589
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
563
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
590
564
            _client=client)
591
565
        result = bzrdir.open_branch()
592
566
        self.assertIsInstance(result, RemoteBranch)
599
573
        transport = transport.clone('quack')
600
574
        client = FakeClient(transport.base)
601
575
        client.add_error_response('nobranch')
602
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
576
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
603
577
            _client=client)
604
578
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
605
579
        self.assertEqual(
610
584
        # _get_tree_branch is a form of open_branch, but it should only ask for
611
585
        # branch opening, not any other network requests.
612
586
        calls = []
613
 
        def open_branch(name=None):
 
587
        def open_branch():
614
588
            calls.append("Called")
615
589
            return "a-branch"
616
590
        transport = MemoryTransport()
617
591
        # no requests on the network - catches other api calls being made.
618
592
        client = FakeClient(transport.base)
619
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
593
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
620
594
            _client=client)
621
595
        # patch the open_branch call to record that it was called.
622
596
        bzrdir.open_branch = open_branch
641
615
        client.add_expected_call(
642
616
            'Branch.get_stacked_on_url', ('~hello/',),
643
617
            'error', ('NotStacked',))
644
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
618
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
645
619
            _client=client)
646
620
        result = bzrdir.open_branch()
647
621
        self.assertFinished(client)
664
638
        client.add_success_response(
665
639
            'ok', '', rich_response, subtree_response, external_lookup,
666
640
            network_name)
667
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
641
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
668
642
            _client=client)
669
643
        result = bzrdir.open_repository()
670
644
        self.assertEqual(
687
661
        old.
688
662
        """
689
663
        self.assertRaises(errors.NotBranchError,
690
 
            RemoteBzrProber.probe_transport, OldServerTransport())
 
664
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
691
665
 
692
666
 
693
667
class TestBzrDirCreateBranch(TestRemote):
716
690
            'BzrDir.create_branch', ('quack/', network_name),
717
691
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
718
692
            reference_repo_name))
719
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
693
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
720
694
            _client=client)
721
695
        branch = a_bzrdir.create_branch()
722
696
        # We should have got a remote branch
725
699
        format = branch._format
726
700
        self.assertEqual(network_name, format.network_name())
727
701
 
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
702
 
757
703
class TestBzrDirCreateRepository(TestRemote):
758
704
 
779
725
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
780
726
                'False'),
781
727
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
782
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
728
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
783
729
            _client=client)
784
730
        repo = a_bzrdir.create_repository()
785
731
        # We should have got a remote repository
814
760
        client.add_success_response('stat', '0', '65535')
815
761
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
816
762
            _client=client)
817
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
 
763
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
818
764
            _client=client)
819
765
        repo = bzrdir.open_repository()
820
766
        self.assertEqual(
847
793
        client.add_success_response('stat', '0', '65535')
848
794
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
849
795
            _client=client)
850
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
 
796
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
851
797
            _client=client)
852
798
        repo = bzrdir.open_repository()
853
799
        self.assertEqual(
868
814
        transport = transport.clone('quack')
869
815
        client = FakeClient(transport.base)
870
816
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
871
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
817
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
872
818
            _client=client)
873
819
        repo = bzrdir.open_repository()
874
820
        self.assertEqual(
881
827
 
882
828
    def test_success(self):
883
829
        """Simple test for typical successful call."""
884
 
        fmt = RemoteBzrDirFormat()
 
830
        fmt = bzrdir.RemoteBzrDirFormat()
885
831
        default_format_name = BzrDirFormat.get_default_format().network_name()
886
832
        transport = self.get_transport()
887
833
        client = FakeClient(transport.base)
903
849
        """Error responses are translated, e.g. 'PermissionDenied' raises the
904
850
        corresponding error from the client.
905
851
        """
906
 
        fmt = RemoteBzrDirFormat()
 
852
        fmt = bzrdir.RemoteBzrDirFormat()
907
853
        default_format_name = BzrDirFormat.get_default_format().network_name()
908
854
        transport = self.get_transport()
909
855
        client = FakeClient(transport.base)
927
873
        """Integration test for error translation."""
928
874
        transport = self.make_smart_server('foo')
929
875
        transport = transport.clone('no-such-path')
930
 
        fmt = RemoteBzrDirFormat()
 
876
        fmt = bzrdir.RemoteBzrDirFormat()
931
877
        err = self.assertRaises(errors.NoSuchFile,
932
878
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
933
879
 
964
910
 
965
911
    def make_remote_bzrdir(self, transport, client):
966
912
        """Make a RemotebzrDir using 'client' as the _client."""
967
 
        return RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
913
        return RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
968
914
            _client=client)
969
915
 
970
916
 
1172
1118
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1173
1119
 
1174
1120
 
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
1121
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1241
1122
 
1242
1123
    def test_empty_branch(self):
1297
1178
        client.add_expected_call(
1298
1179
            'Branch.get_stacked_on_url', ('stacked/',),
1299
1180
            'success', ('ok', vfs_url))
1300
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
1181
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1301
1182
            _client=client)
1302
1183
        repo_fmt = remote.RemoteRepositoryFormat()
1303
1184
        repo_fmt._custom_format = stacked_branch.repository._format
1330
1211
        # this will also do vfs access, but that goes direct to the transport
1331
1212
        # and isn't seen by the FakeClient.
1332
1213
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1333
 
            RemoteBzrDirFormat(), _client=client)
 
1214
            remote.RemoteBzrDirFormat(), _client=client)
1334
1215
        branch = bzrdir.open_branch()
1335
1216
        result = branch.get_stacked_on_url()
1336
1217
        self.assertEqual('../base', result)
1342
1223
            len(branch.repository._real_repository._fallback_repositories))
1343
1224
 
1344
1225
    def test_get_stacked_on_real_branch(self):
1345
 
        base_branch = self.make_branch('base')
1346
 
        stacked_branch = self.make_branch('stacked')
 
1226
        base_branch = self.make_branch('base', format='1.6')
 
1227
        stacked_branch = self.make_branch('stacked', format='1.6')
1347
1228
        stacked_branch.set_stacked_on_url('../base')
1348
1229
        reference_format = self.get_repo_format()
1349
1230
        network_name = reference_format.network_name()
1354
1235
            'success', ('branch', branch_network_name))
1355
1236
        client.add_expected_call(
1356
1237
            'BzrDir.find_repositoryV3', ('stacked/',),
1357
 
            'success', ('ok', '', 'yes', 'no', 'yes', network_name))
 
1238
            'success', ('ok', '', 'no', 'no', 'yes', network_name))
1358
1239
        # called twice, once from constructor and then again by us
1359
1240
        client.add_expected_call(
1360
1241
            'Branch.get_stacked_on_url', ('stacked/',),
1363
1244
            'Branch.get_stacked_on_url', ('stacked/',),
1364
1245
            'success', ('ok', '../base'))
1365
1246
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1366
 
            RemoteBzrDirFormat(), _client=client)
 
1247
            remote.RemoteBzrDirFormat(), _client=client)
1367
1248
        branch = bzrdir.open_branch()
1368
1249
        result = branch.get_stacked_on_url()
1369
1250
        self.assertEqual('../base', result)
1377
1258
class TestBranchSetLastRevision(RemoteBranchTestCase):
1378
1259
 
1379
1260
    def test_set_empty(self):
1380
 
        # _set_last_revision_info('null:') is translated to calling
 
1261
        # set_revision_history([]) is translated to calling
1381
1262
        # Branch.set_last_revision(path, '') on the wire.
1382
1263
        transport = MemoryTransport()
1383
1264
        transport.mkdir('branch')
1405
1286
        # unnecessarily invokes _ensure_real upon a call to lock_write.
1406
1287
        branch._ensure_real = lambda: None
1407
1288
        branch.lock_write()
1408
 
        result = branch._set_last_revision(NULL_REVISION)
 
1289
        result = branch.set_revision_history([])
1409
1290
        branch.unlock()
1410
1291
        self.assertEqual(None, result)
1411
1292
        self.assertFinished(client)
1412
1293
 
1413
1294
    def test_set_nonempty(self):
1414
 
        # set_last_revision_info(N, rev-idN) is translated to calling
 
1295
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
1415
1296
        # Branch.set_last_revision(path, rev-idN) on the wire.
1416
1297
        transport = MemoryTransport()
1417
1298
        transport.mkdir('branch')
1443
1324
        branch._ensure_real = lambda: None
1444
1325
        # Lock the branch, reset the record of remote calls.
1445
1326
        branch.lock_write()
1446
 
        result = branch._set_last_revision('rev-id2')
 
1327
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
1447
1328
        branch.unlock()
1448
1329
        self.assertEqual(None, result)
1449
1330
        self.assertFinished(client)
1479
1360
        branch = self.make_remote_branch(transport, client)
1480
1361
        branch.lock_write()
1481
1362
        self.assertRaises(
1482
 
            errors.NoSuchRevision, branch._set_last_revision, 'rev-id')
 
1363
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
1483
1364
        branch.unlock()
1484
1365
        self.assertFinished(client)
1485
1366
 
1516
1397
        branch._ensure_real = lambda: None
1517
1398
        branch.lock_write()
1518
1399
        # The 'TipChangeRejected' error response triggered by calling
1519
 
        # set_last_revision_info causes a TipChangeRejected exception.
 
1400
        # set_revision_history causes a TipChangeRejected exception.
1520
1401
        err = self.assertRaises(
1521
 
            errors.TipChangeRejected,
1522
 
            branch._set_last_revision, 'rev-id')
 
1402
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
1523
1403
        # The UTF-8 message from the response has been decoded into a unicode
1524
1404
        # object.
1525
1405
        self.assertIsInstance(err.msg, unicode)
1713
1593
    def test_get_multi_line_branch_conf(self):
1714
1594
        # Make sure that multiple-line branch.conf files are supported
1715
1595
        #
1716
 
        # https://bugs.launchpad.net/bzr/+bug/354075
 
1596
        # https://bugs.edge.launchpad.net/bzr/+bug/354075
1717
1597
        client = FakeClient()
1718
1598
        client.add_expected_call(
1719
1599
            'Branch.get_stacked_on_url', ('memory:///',),
1747
1627
        branch.unlock()
1748
1628
        self.assertFinished(client)
1749
1629
 
1750
 
    def test_set_option_with_dict(self):
1751
 
        client = FakeClient()
1752
 
        client.add_expected_call(
1753
 
            'Branch.get_stacked_on_url', ('memory:///',),
1754
 
            'error', ('NotStacked',),)
1755
 
        client.add_expected_call(
1756
 
            'Branch.lock_write', ('memory:///', '', ''),
1757
 
            'success', ('ok', 'branch token', 'repo token'))
1758
 
        encoded_dict_value = 'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde'
1759
 
        client.add_expected_call(
1760
 
            'Branch.set_config_option_dict', ('memory:///', 'branch token',
1761
 
            'repo token', encoded_dict_value, 'foo', ''),
1762
 
            'success', ())
1763
 
        client.add_expected_call(
1764
 
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1765
 
            'success', ('ok',))
1766
 
        transport = MemoryTransport()
1767
 
        branch = self.make_remote_branch(transport, client)
1768
 
        branch.lock_write()
1769
 
        config = branch._get_config()
1770
 
        config.set_option(
1771
 
            {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'},
1772
 
            'foo')
1773
 
        branch.unlock()
1774
 
        self.assertFinished(client)
1775
 
 
1776
1630
    def test_backwards_compat_set_option(self):
1777
1631
        self.setup_smart_server_with_call_log()
1778
1632
        branch = self.make_branch('.')
1785
1639
        self.assertLength(10, self.hpss_calls)
1786
1640
        self.assertEqual('value', branch._get_config().get_option('name'))
1787
1641
 
1788
 
    def test_backwards_compat_set_option_with_dict(self):
1789
 
        self.setup_smart_server_with_call_log()
1790
 
        branch = self.make_branch('.')
1791
 
        verb = 'Branch.set_config_option_dict'
1792
 
        self.disable_verb(verb)
1793
 
        branch.lock_write()
1794
 
        self.addCleanup(branch.unlock)
1795
 
        self.reset_smart_call_log()
1796
 
        config = branch._get_config()
1797
 
        value_dict = {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
1798
 
        config.set_option(value_dict, 'name')
1799
 
        self.assertLength(10, self.hpss_calls)
1800
 
        self.assertEqual(value_dict, branch._get_config().get_option('name'))
1801
 
 
1802
1642
 
1803
1643
class TestBranchLockWrite(RemoteBranchTestCase):
1804
1644
 
1940
1780
        client = FakeClient(transport.base)
1941
1781
        transport = transport.clone(transport_path)
1942
1782
        # we do not want bzrdir to make any remote calls
1943
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
 
1783
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1944
1784
            _client=False)
1945
1785
        repo = RemoteRepository(bzrdir, None, _client=client)
1946
1786
        return repo, client
1954
1794
 
1955
1795
    def test_get_format_description(self):
1956
1796
        remote_format = RemoteBranchFormat()
1957
 
        real_format = branch.format_registry.get_default()
 
1797
        real_format = branch.BranchFormat.get_default_format()
1958
1798
        remote_format._network_name = real_format.network_name()
1959
1799
        self.assertEqual(remoted_description(real_format),
1960
1800
            remote_format.get_format_description())
1963
1803
class TestRepositoryFormat(TestRemoteRepository):
1964
1804
 
1965
1805
    def test_fast_delta(self):
1966
 
        true_name = groupcompress_repo.RepositoryFormat2a().network_name()
 
1806
        true_name = groupcompress_repo.RepositoryFormatCHK1().network_name()
1967
1807
        true_format = RemoteRepositoryFormat()
1968
1808
        true_format._network_name = true_name
1969
1809
        self.assertEqual(True, true_format.fast_deltas)
1970
 
        false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
 
1810
        false_name = pack_repo.RepositoryFormatKnitPack1().network_name()
1971
1811
        false_format = RemoteRepositoryFormat()
1972
1812
        false_format._network_name = false_name
1973
1813
        self.assertEqual(False, false_format.fast_deltas)
1974
1814
 
1975
1815
    def test_get_format_description(self):
1976
1816
        remote_repo_format = RemoteRepositoryFormat()
1977
 
        real_format = repository.format_registry.get_default()
 
1817
        real_format = repository.RepositoryFormat.get_default_format()
1978
1818
        remote_repo_format._network_name = real_format.network_name()
1979
1819
        self.assertEqual(remoted_description(real_format),
1980
1820
            remote_repo_format.get_format_description())
2351
2191
        """
2352
2192
        # Make a repo with a fallback repo, both using a FakeClient.
2353
2193
        format = remote.response_tuple_to_repo_format(
2354
 
            ('yes', 'no', 'yes', self.get_repo_format().network_name()))
 
2194
            ('yes', 'no', 'yes', 'fake-network-name'))
2355
2195
        repo, client = self.setup_fake_client_and_repository('quack')
2356
2196
        repo._format = format
2357
2197
        fallback_repo, ignored = self.setup_fake_client_and_repository(
2358
2198
            'fallback')
2359
2199
        fallback_repo._client = client
2360
 
        fallback_repo._format = format
2361
2200
        repo.add_fallback_repository(fallback_repo)
2362
2201
        # First the client should ask the primary repo
2363
2202
        client.add_expected_call(
2392
2231
        self.setup_smart_server_with_call_log()
2393
2232
        tree = self.make_branch_and_memory_tree('.')
2394
2233
        tree.lock_write()
2395
 
        tree.add('')
2396
2234
        rev1 = tree.commit('First')
2397
2235
        rev2 = tree.commit('Second')
2398
2236
        tree.unlock()
2437
2275
        transport_path = 'quack'
2438
2276
        repo, client = self.setup_fake_client_and_repository(transport_path)
2439
2277
        client.add_success_response('ok', 'a token')
2440
 
        token = repo.lock_write().repository_token
 
2278
        result = repo.lock_write()
2441
2279
        self.assertEqual(
2442
2280
            [('call', 'Repository.lock_write', ('quack/', ''))],
2443
2281
            client._calls)
2444
 
        self.assertEqual('a token', token)
 
2282
        self.assertEqual('a token', result)
2445
2283
 
2446
2284
    def test_lock_write_already_locked(self):
2447
2285
        transport_path = 'quack'
2538
2376
        the client is finished.
2539
2377
        """
2540
2378
        sink = repo._get_sink()
2541
 
        fmt = repository.format_registry.get_default()
 
2379
        fmt = repository.RepositoryFormat.get_default_format()
2542
2380
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2543
2381
        self.assertEqual([], resume_tokens)
2544
2382
        self.assertEqual(set(), missing_keys)
2644
2482
                return True
2645
2483
        repo._real_repository = FakeRealRepository()
2646
2484
        sink = repo._get_sink()
2647
 
        fmt = repository.format_registry.get_default()
 
2485
        fmt = repository.RepositoryFormat.get_default_format()
2648
2486
        stream = self.make_stream_with_inv_deltas(fmt)
2649
2487
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2650
2488
        # Every record from the first inventory delta should have been sent to
2870
2708
             ('pack collection autopack',)],
2871
2709
            client._calls)
2872
2710
 
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
2711
 
2884
2712
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2885
2713
    """Base class for unit tests for bzrlib.remote._translate_error."""
2958
2786
            detail='extra detail')
2959
2787
        self.assertEqual(expected_error, translated_error)
2960
2788
 
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
2789
    def test_LockContention(self):
2969
2790
        translated_error = self.translateTuple(('LockContention',))
2970
2791
        expected_error = errors.LockContention('(remote lock)')
2998
2819
        expected_error = errors.DivergedBranches(branch, other_branch)
2999
2820
        self.assertEqual(expected_error, translated_error)
3000
2821
 
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
2822
    def test_ReadError_no_args(self):
3008
2823
        path = 'a path'
3009
2824
        translated_error = self.translateTuple(('ReadError',), path=path)
3025
2840
 
3026
2841
    def test_PermissionDenied_no_args(self):
3027
2842
        path = 'a path'
3028
 
        translated_error = self.translateTuple(('PermissionDenied',),
3029
 
            path=path)
 
2843
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
3030
2844
        expected_error = errors.PermissionDenied(path)
3031
2845
        self.assertEqual(expected_error, translated_error)
3032
2846
 
3055
2869
        expected_error = errors.PermissionDenied(path, extra)
3056
2870
        self.assertEqual(expected_error, translated_error)
3057
2871
 
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
2872
 
3098
2873
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3099
2874
    """Unit tests for bzrlib.remote._translate_error's robustness.
3244
3019
        _, stacked = branch_factory()
3245
3020
        source = stacked.repository._get_source(target_repository_format)
3246
3021
        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)
 
3022
        revs = stacked.repository.get_ancestry(tip)
 
3023
        search = graph.PendingAncestryResult([tip], stacked.repository)
3253
3024
        self.reset_smart_call_log()
3254
3025
        stream = source.get_stream(search)
 
3026
        if None in revs:
 
3027
            revs.remove(None)
3255
3028
        # We trust that if a revision is in the stream the rest of the new
3256
3029
        # content for it is too, as per our main fetch tests; here we are
3257
3030
        # checking that the revisions are actually included at all, and their
3296
3069
        self.assertEqual(expected_revs, rev_ord)
3297
3070
        # Getting topological sort requires VFS calls still - one of which is
3298
3071
        # pushing up from the bound branch.
3299
 
        self.assertLength(14, self.hpss_calls)
 
3072
        self.assertLength(13, self.hpss_calls)
3300
3073
 
3301
3074
    def test_stacked_get_stream_groupcompress(self):
3302
3075
        # Repository._get_source.get_stream() from a stacked repository with
3343
3116
 
3344
3117
    def test_copy_content_into_avoids_revision_history(self):
3345
3118
        local = self.make_branch('local')
3346
 
        builder = self.make_branch_builder('remote')
3347
 
        builder.build_commit(message="Commit.")
 
3119
        remote_backing_tree = self.make_branch_and_tree('remote')
 
3120
        remote_backing_tree.commit("Commit.")
3348
3121
        remote_branch_url = self.smart_server.get_url() + 'remote'
3349
3122
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3350
3123
        local.repository.fetch(remote_branch.repository)
3351
3124
        self.hpss_calls = []
3352
3125
        remote_branch.copy_content_into(local)
3353
3126
        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