/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-07-02 11:37:38 UTC
  • mfrom: (4496.3.16 pyflakes-nits)
  • Revision ID: pqm@pqm.ubuntu.com-20090702113738-5qda6d3y80z4l3o5
(andrew) Fix some trivial bugs and unused/redundant imports reported
        by pyflakes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008, 2009 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
27
27
from cStringIO import StringIO
28
28
 
29
29
from bzrlib import (
30
 
    branch,
31
30
    bzrdir,
32
31
    config,
33
32
    errors,
34
33
    graph,
35
 
    inventory,
36
 
    inventory_delta,
37
34
    pack,
38
35
    remote,
39
36
    repository,
 
37
    smart,
40
38
    tests,
41
39
    treebuilder,
42
40
    urlutils,
43
 
    versionedfile,
44
41
    )
45
42
from bzrlib.branch import Branch
46
43
from bzrlib.bzrdir import BzrDir, BzrDirFormat
54
51
    )
55
52
from bzrlib.repofmt import groupcompress_repo, pack_repo
56
53
from bzrlib.revision import NULL_REVISION
57
 
from bzrlib.smart import medium
 
54
from bzrlib.smart import server, medium
58
55
from bzrlib.smart.client import _SmartClient
59
56
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
60
57
from bzrlib.tests import (
61
58
    condition_isinstance,
62
59
    split_suite_by_condition,
63
60
    multiply_tests,
64
 
    test_server,
 
61
    KnownFailure,
65
62
    )
66
 
from bzrlib.transport import get_transport
 
63
from bzrlib.transport import get_transport, http
67
64
from bzrlib.transport.memory import MemoryTransport
68
65
from bzrlib.transport.remote import (
69
66
    RemoteTransport,
76
73
        standard_tests, condition_isinstance(BasicRemoteObjectTests))
77
74
    smart_server_version_scenarios = [
78
75
        ('HPSS-v2',
79
 
         {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
 
76
            {'transport_server': server.SmartTCPServer_for_testing_v2_only}),
80
77
        ('HPSS-v3',
81
 
         {'transport_server': test_server.SmartTCPServer_for_testing})]
 
78
            {'transport_server': server.SmartTCPServer_for_testing})]
82
79
    return multiply_tests(to_adapt, smart_server_version_scenarios, result)
83
80
 
84
81
 
135
132
        b = BzrDir.open_from_transport(self.transport).open_branch()
136
133
        self.assertStartsWith(str(b), 'RemoteBranch(')
137
134
 
138
 
    def test_remote_bzrdir_repr(self):
139
 
        b = BzrDir.open_from_transport(self.transport)
140
 
        self.assertStartsWith(str(b), 'RemoteBzrDir(')
141
 
 
142
135
    def test_remote_branch_format_supports_stacking(self):
143
136
        t = self.transport
144
137
        self.make_branch('unstackable', format='pack-0.92')
284
277
        self.expecting_body = True
285
278
        return result[1], FakeProtocol(result[2], self)
286
279
 
287
 
    def call_with_body_bytes(self, method, args, body):
288
 
        self._check_call(method, args)
289
 
        self._calls.append(('call_with_body_bytes', method, args, body))
290
 
        result = self._get_next_response()
291
 
        return result[1], FakeProtocol(result[2], self)
292
 
 
293
280
    def call_with_body_bytes_expecting_body(self, method, args, body):
294
281
        self._check_call(method, args)
295
282
        self._calls.append(('call_with_body_bytes_expecting_body', method,
345
332
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
346
333
        return reference_bzrdir_format.repository_format
347
334
 
348
 
    def assertFinished(self, fake_client):
349
 
        """Assert that all of a FakeClient's expected calls have occurred."""
350
 
        fake_client.finished_test()
 
335
    def disable_verb(self, verb):
 
336
        """Disable a verb for one test."""
 
337
        request_handlers = smart.request.request_handlers
 
338
        orig_method = request_handlers.get(verb)
 
339
        request_handlers.remove(verb)
 
340
        def restoreVerb():
 
341
            request_handlers.register(verb, orig_method)
 
342
        self.addCleanup(restoreVerb)
351
343
 
352
344
 
353
345
class Test_ClientMedium_remote_path_from_transport(tests.TestCase):
418
410
        # Calling _remember_remote_is_before again with a lower value works.
419
411
        client_medium._remember_remote_is_before((1, 5))
420
412
        self.assertTrue(client_medium._is_remote_before((1, 5)))
421
 
        # If you call _remember_remote_is_before with a higher value it logs a
422
 
        # warning, and continues to remember the lower value.
423
 
        self.assertNotContainsRe(self.get_log(), '_remember_remote_is_before')
424
 
        client_medium._remember_remote_is_before((1, 9))
425
 
        self.assertContainsRe(self.get_log(), '_remember_remote_is_before')
426
 
        self.assertTrue(client_medium._is_remote_before((1, 5)))
 
413
        # You cannot call _remember_remote_is_before with a larger value.
 
414
        self.assertRaises(
 
415
            AssertionError, client_medium._remember_remote_is_before, (1, 9))
427
416
 
428
417
 
429
418
class TestBzrDirCloningMetaDir(TestRemote):
448
437
            'BzrDir.cloning_metadir', ('quack/', 'False'),
449
438
            'error', ('BranchReference',)),
450
439
        client.add_expected_call(
451
 
            'BzrDir.open_branchV3', ('quack/',),
 
440
            'BzrDir.open_branchV2', ('quack/',),
452
441
            'success', ('ref', self.get_url('referenced'))),
453
442
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
454
443
            _client=client)
457
446
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
458
447
        self.assertEqual(expected._repository_format, result._repository_format)
459
448
        self.assertEqual(expected._branch_format, result._branch_format)
460
 
        self.assertFinished(client)
 
449
        client.finished_test()
461
450
 
462
451
    def test_current_server(self):
463
452
        transport = self.get_transport('.')
478
467
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
479
468
        self.assertEqual(None, result._repository_format)
480
469
        self.assertEqual(None, result._branch_format)
481
 
        self.assertFinished(client)
482
 
 
483
 
 
484
 
class TestBzrDirOpen(TestRemote):
485
 
 
486
 
    def make_fake_client_and_transport(self, path='quack'):
487
 
        transport = MemoryTransport()
488
 
        transport.mkdir(path)
489
 
        transport = transport.clone(path)
490
 
        client = FakeClient(transport.base)
491
 
        return client, transport
492
 
 
493
 
    def test_absent(self):
494
 
        client, transport = self.make_fake_client_and_transport()
495
 
        client.add_expected_call(
496
 
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
497
 
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
498
 
                remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
499
 
        self.assertFinished(client)
500
 
 
501
 
    def test_present_without_workingtree(self):
502
 
        client, transport = self.make_fake_client_and_transport()
503
 
        client.add_expected_call(
504
 
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
505
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
506
 
            _client=client, _force_probe=True)
507
 
        self.assertIsInstance(bd, RemoteBzrDir)
508
 
        self.assertFalse(bd.has_workingtree())
509
 
        self.assertRaises(errors.NoWorkingTree, bd.open_workingtree)
510
 
        self.assertFinished(client)
511
 
 
512
 
    def test_present_with_workingtree(self):
513
 
        client, transport = self.make_fake_client_and_transport()
514
 
        client.add_expected_call(
515
 
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
516
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
517
 
            _client=client, _force_probe=True)
518
 
        self.assertIsInstance(bd, RemoteBzrDir)
519
 
        self.assertTrue(bd.has_workingtree())
520
 
        self.assertRaises(errors.NotLocalUrl, bd.open_workingtree)
521
 
        self.assertFinished(client)
522
 
 
523
 
    def test_backwards_compat(self):
524
 
        client, transport = self.make_fake_client_and_transport()
525
 
        client.add_expected_call(
526
 
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
527
 
        client.add_expected_call(
528
 
            'BzrDir.open', ('quack/',), 'success', ('yes',))
529
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
530
 
            _client=client, _force_probe=True)
531
 
        self.assertIsInstance(bd, RemoteBzrDir)
532
 
        self.assertFinished(client)
533
 
 
534
 
    def test_backwards_compat_hpss_v2(self):
535
 
        client, transport = self.make_fake_client_and_transport()
536
 
        # Monkey-patch fake client to simulate real-world behaviour with v2
537
 
        # server: upon first RPC call detect the protocol version, and because
538
 
        # the version is 2 also do _remember_remote_is_before((1, 6)) before
539
 
        # continuing with the RPC.
540
 
        orig_check_call = client._check_call
541
 
        def check_call(method, args):
542
 
            client._medium._protocol_version = 2
543
 
            client._medium._remember_remote_is_before((1, 6))
544
 
            client._check_call = orig_check_call
545
 
            client._check_call(method, args)
546
 
        client._check_call = check_call
547
 
        client.add_expected_call(
548
 
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
549
 
        client.add_expected_call(
550
 
            'BzrDir.open', ('quack/',), 'success', ('yes',))
551
 
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
552
 
            _client=client, _force_probe=True)
553
 
        self.assertIsInstance(bd, RemoteBzrDir)
554
 
        self.assertFinished(client)
 
470
        client.finished_test()
555
471
 
556
472
 
557
473
class TestBzrDirOpenBranch(TestRemote):
561
477
        self.make_branch('.')
562
478
        a_dir = BzrDir.open(self.get_url('.'))
563
479
        self.reset_smart_call_log()
564
 
        verb = 'BzrDir.open_branchV3'
 
480
        verb = 'BzrDir.open_branchV2'
565
481
        self.disable_verb(verb)
566
482
        format = a_dir.open_branch()
567
483
        call_count = len([call for call in self.hpss_calls if
577
493
        transport = transport.clone('quack')
578
494
        client = FakeClient(transport.base)
579
495
        client.add_expected_call(
580
 
            'BzrDir.open_branchV3', ('quack/',),
 
496
            'BzrDir.open_branchV2', ('quack/',),
581
497
            'success', ('branch', branch_network_name))
582
498
        client.add_expected_call(
583
499
            'BzrDir.find_repositoryV3', ('quack/',),
590
506
        result = bzrdir.open_branch()
591
507
        self.assertIsInstance(result, RemoteBranch)
592
508
        self.assertEqual(bzrdir, result.bzrdir)
593
 
        self.assertFinished(client)
 
509
        client.finished_test()
594
510
 
595
511
    def test_branch_missing(self):
596
512
        transport = MemoryTransport()
602
518
            _client=client)
603
519
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
604
520
        self.assertEqual(
605
 
            [('call', 'BzrDir.open_branchV3', ('quack/',))],
 
521
            [('call', 'BzrDir.open_branchV2', ('quack/',))],
606
522
            client._calls)
607
523
 
608
524
    def test__get_tree_branch(self):
632
548
        network_name = reference_format.network_name()
633
549
        branch_network_name = self.get_branch_format().network_name()
634
550
        client.add_expected_call(
635
 
            'BzrDir.open_branchV3', ('~hello/',),
 
551
            'BzrDir.open_branchV2', ('~hello/',),
636
552
            'success', ('branch', branch_network_name))
637
553
        client.add_expected_call(
638
554
            'BzrDir.find_repositoryV3', ('~hello/',),
643
559
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
644
560
            _client=client)
645
561
        result = bzrdir.open_branch()
646
 
        self.assertFinished(client)
 
562
        client.finished_test()
647
563
 
648
564
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
649
565
        reference_format = self.get_repo_format()
747
663
        network_name = reference_format.network_name()
748
664
        client.add_expected_call(
749
665
            'BzrDir.create_repository', ('quack/',
750
 
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
751
 
                'False'),
752
 
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
 
666
                'Bazaar pack repository format 1 (needs bzr 0.92)\n', 'False'),
 
667
            'success', ('ok', 'no', 'no', 'no', network_name))
753
668
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
754
669
            _client=client)
755
670
        repo = a_bzrdir.create_repository()
757
672
        self.assertIsInstance(repo, remote.RemoteRepository)
758
673
        # its format should have the settings from the response
759
674
        format = repo._format
760
 
        self.assertTrue(format.rich_root_data)
761
 
        self.assertTrue(format.supports_tree_reference)
762
 
        self.assertTrue(format.supports_external_lookups)
 
675
        self.assertFalse(format.rich_root_data)
 
676
        self.assertFalse(format.supports_tree_reference)
 
677
        self.assertFalse(format.supports_external_lookups)
763
678
        self.assertEqual(network_name, format.network_name())
764
679
 
765
680
 
769
684
        # fallback all the way to the first version.
770
685
        reference_format = self.get_repo_format()
771
686
        network_name = reference_format.network_name()
772
 
        server_url = 'bzr://example.com/'
773
 
        self.permit_url(server_url)
774
 
        client = FakeClient(server_url)
 
687
        client = FakeClient('bzr://example.com/')
775
688
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
776
689
        client.add_unknown_method_response('BzrDir.find_repositoryV2')
777
690
        client.add_success_response('ok', '', 'no', 'no')
783
696
            reference_format.get_format_string(), 'ok')
784
697
        # PackRepository wants to do a stat
785
698
        client.add_success_response('stat', '0', '65535')
786
 
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
 
699
        remote_transport = RemoteTransport('bzr://example.com/quack/', medium=False,
787
700
            _client=client)
788
701
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
789
702
            _client=client)
803
716
        # fallback to find_repositoryV2
804
717
        reference_format = self.get_repo_format()
805
718
        network_name = reference_format.network_name()
806
 
        server_url = 'bzr://example.com/'
807
 
        self.permit_url(server_url)
808
 
        client = FakeClient(server_url)
 
719
        client = FakeClient('bzr://example.com/')
809
720
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
810
721
        client.add_success_response('ok', '', 'no', 'no', 'no')
811
722
        # A real repository instance will be created to determine the network
816
727
            reference_format.get_format_string(), 'ok')
817
728
        # PackRepository wants to do a stat
818
729
        client.add_success_response('stat', '0', '65535')
819
 
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
 
730
        remote_transport = RemoteTransport('bzr://example.com/quack/', medium=False,
820
731
            _client=client)
821
732
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
822
733
            _client=client)
868
779
        # transport connected to a real server.
869
780
        result = fmt._initialize_on_transport_ex_rpc(client, 'path',
870
781
            transport, False, False, False, None, None, None, None, False)
871
 
        self.assertFinished(client)
 
782
        client.finished_test()
872
783
 
873
784
    def test_error(self):
874
785
        """Error responses are translated, e.g. 'PermissionDenied' raises the
892
803
            False, False, False, None, None, None, None, False)
893
804
        self.assertEqual('path', err.path)
894
805
        self.assertEqual(': extra info', err.extra)
895
 
        self.assertFinished(client)
 
806
        client.finished_test()
896
807
 
897
808
    def test_error_from_real_server(self):
898
809
        """Integration test for error translation."""
941
852
 
942
853
class RemoteBranchTestCase(RemoteBzrDirTestCase):
943
854
 
944
 
    def lock_remote_branch(self, branch):
945
 
        """Trick a RemoteBranch into thinking it is locked."""
946
 
        branch._lock_mode = 'w'
947
 
        branch._lock_count = 2
948
 
        branch._lock_token = 'branch token'
949
 
        branch._repo_lock_token = 'repo token'
950
 
        branch.repository._lock_mode = 'w'
951
 
        branch.repository._lock_count = 2
952
 
        branch.repository._lock_token = 'repo token'
953
 
 
954
855
    def make_remote_branch(self, transport, client):
955
856
        """Make a RemoteBranch using 'client' as its _SmartClient.
956
857
 
983
884
        transport = transport.clone('quack')
984
885
        branch = self.make_remote_branch(transport, client)
985
886
        result = branch.get_parent()
986
 
        self.assertFinished(client)
 
887
        client.finished_test()
987
888
        self.assertEqual(None, result)
988
889
 
989
890
    def test_parent_relative(self):
1015
916
        branch = self.make_remote_branch(transport, client)
1016
917
        result = branch.get_parent()
1017
918
        self.assertEqual('http://foo/', result)
1018
 
        self.assertFinished(client)
 
919
        client.finished_test()
1019
920
 
1020
921
 
1021
922
class TestBranchSetParentLocation(RemoteBranchTestCase):
1036
937
        branch._lock_token = 'b'
1037
938
        branch._repo_lock_token = 'r'
1038
939
        branch._set_parent_location(None)
1039
 
        self.assertFinished(client)
 
940
        client.finished_test()
1040
941
 
1041
942
    def test_parent(self):
1042
943
        transport = MemoryTransport()
1053
954
        branch._lock_token = 'b'
1054
955
        branch._repo_lock_token = 'r'
1055
956
        branch._set_parent_location('foo')
1056
 
        self.assertFinished(client)
 
957
        client.finished_test()
1057
958
 
1058
959
    def test_backwards_compat(self):
1059
960
        self.setup_smart_server_with_call_log()
1091
992
        transport = transport.clone('quack')
1092
993
        branch = self.make_remote_branch(transport, client)
1093
994
        result = branch.tags.get_tag_dict()
1094
 
        self.assertFinished(client)
 
995
        client.finished_test()
1095
996
        self.assertEqual({}, result)
1096
997
 
1097
998
 
1098
 
class TestBranchSetTagsBytes(RemoteBranchTestCase):
1099
 
 
1100
 
    def test_trivial(self):
1101
 
        transport = MemoryTransport()
1102
 
        client = FakeClient(transport.base)
1103
 
        client.add_expected_call(
1104
 
            'Branch.get_stacked_on_url', ('quack/',),
1105
 
            'error', ('NotStacked',))
1106
 
        client.add_expected_call(
1107
 
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1108
 
            'success', ('',))
1109
 
        transport.mkdir('quack')
1110
 
        transport = transport.clone('quack')
1111
 
        branch = self.make_remote_branch(transport, client)
1112
 
        self.lock_remote_branch(branch)
1113
 
        branch._set_tags_bytes('tags bytes')
1114
 
        self.assertFinished(client)
1115
 
        self.assertEqual('tags bytes', client._calls[-1][-1])
1116
 
 
1117
 
    def test_backwards_compatible(self):
1118
 
        transport = MemoryTransport()
1119
 
        client = FakeClient(transport.base)
1120
 
        client.add_expected_call(
1121
 
            'Branch.get_stacked_on_url', ('quack/',),
1122
 
            'error', ('NotStacked',))
1123
 
        client.add_expected_call(
1124
 
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1125
 
            'unknown', ('Branch.set_tags_bytes',))
1126
 
        transport.mkdir('quack')
1127
 
        transport = transport.clone('quack')
1128
 
        branch = self.make_remote_branch(transport, client)
1129
 
        self.lock_remote_branch(branch)
1130
 
        class StubRealBranch(object):
1131
 
            def __init__(self):
1132
 
                self.calls = []
1133
 
            def _set_tags_bytes(self, bytes):
1134
 
                self.calls.append(('set_tags_bytes', bytes))
1135
 
        real_branch = StubRealBranch()
1136
 
        branch._real_branch = real_branch
1137
 
        branch._set_tags_bytes('tags bytes')
1138
 
        # Call a second time, to exercise the 'remote version already inferred'
1139
 
        # code path.
1140
 
        branch._set_tags_bytes('tags bytes')
1141
 
        self.assertFinished(client)
1142
 
        self.assertEqual(
1143
 
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1144
 
 
1145
 
 
1146
999
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1147
1000
 
1148
1001
    def test_empty_branch(self):
1159
1012
        transport = transport.clone('quack')
1160
1013
        branch = self.make_remote_branch(transport, client)
1161
1014
        result = branch.last_revision_info()
1162
 
        self.assertFinished(client)
 
1015
        client.finished_test()
1163
1016
        self.assertEqual((0, NULL_REVISION), result)
1164
1017
 
1165
1018
    def test_non_empty_branch(self):
1220
1073
        client = FakeClient(self.get_url())
1221
1074
        branch_network_name = self.get_branch_format().network_name()
1222
1075
        client.add_expected_call(
1223
 
            'BzrDir.open_branchV3', ('stacked/',),
 
1076
            'BzrDir.open_branchV2', ('stacked/',),
1224
1077
            'success', ('branch', branch_network_name))
1225
1078
        client.add_expected_call(
1226
1079
            'BzrDir.find_repositoryV3', ('stacked/',),
1240
1093
        branch = bzrdir.open_branch()
1241
1094
        result = branch.get_stacked_on_url()
1242
1095
        self.assertEqual('../base', result)
1243
 
        self.assertFinished(client)
 
1096
        client.finished_test()
1244
1097
        # it's in the fallback list both for the RemoteRepository and its vfs
1245
1098
        # repository
1246
1099
        self.assertEqual(1, len(branch.repository._fallback_repositories))
1248
1101
            len(branch.repository._real_repository._fallback_repositories))
1249
1102
 
1250
1103
    def test_get_stacked_on_real_branch(self):
1251
 
        base_branch = self.make_branch('base')
1252
 
        stacked_branch = self.make_branch('stacked')
 
1104
        base_branch = self.make_branch('base', format='1.6')
 
1105
        stacked_branch = self.make_branch('stacked', format='1.6')
1253
1106
        stacked_branch.set_stacked_on_url('../base')
1254
1107
        reference_format = self.get_repo_format()
1255
1108
        network_name = reference_format.network_name()
1256
1109
        client = FakeClient(self.get_url())
1257
1110
        branch_network_name = self.get_branch_format().network_name()
1258
1111
        client.add_expected_call(
1259
 
            'BzrDir.open_branchV3', ('stacked/',),
 
1112
            'BzrDir.open_branchV2', ('stacked/',),
1260
1113
            'success', ('branch', branch_network_name))
1261
1114
        client.add_expected_call(
1262
1115
            'BzrDir.find_repositoryV3', ('stacked/',),
1263
 
            'success', ('ok', '', 'yes', 'no', 'yes', network_name))
 
1116
            'success', ('ok', '', 'no', 'no', 'yes', network_name))
1264
1117
        # called twice, once from constructor and then again by us
1265
1118
        client.add_expected_call(
1266
1119
            'Branch.get_stacked_on_url', ('stacked/',),
1273
1126
        branch = bzrdir.open_branch()
1274
1127
        result = branch.get_stacked_on_url()
1275
1128
        self.assertEqual('../base', result)
1276
 
        self.assertFinished(client)
 
1129
        client.finished_test()
1277
1130
        # it's in the fallback list both for the RemoteRepository.
1278
1131
        self.assertEqual(1, len(branch.repository._fallback_repositories))
1279
1132
        # And we haven't had to construct a real repository.
1314
1167
        result = branch.set_revision_history([])
1315
1168
        branch.unlock()
1316
1169
        self.assertEqual(None, result)
1317
 
        self.assertFinished(client)
 
1170
        client.finished_test()
1318
1171
 
1319
1172
    def test_set_nonempty(self):
1320
1173
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
1352
1205
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
1353
1206
        branch.unlock()
1354
1207
        self.assertEqual(None, result)
1355
 
        self.assertFinished(client)
 
1208
        client.finished_test()
1356
1209
 
1357
1210
    def test_no_such_revision(self):
1358
1211
        transport = MemoryTransport()
1387
1240
        self.assertRaises(
1388
1241
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
1389
1242
        branch.unlock()
1390
 
        self.assertFinished(client)
 
1243
        client.finished_test()
1391
1244
 
1392
1245
    def test_tip_change_rejected(self):
1393
1246
        """TipChangeRejected responses cause a TipChangeRejected exception to
1430
1283
        self.assertIsInstance(err.msg, unicode)
1431
1284
        self.assertEqual(rejection_msg_unicode, err.msg)
1432
1285
        branch.unlock()
1433
 
        self.assertFinished(client)
 
1286
        client.finished_test()
1434
1287
 
1435
1288
 
1436
1289
class TestBranchSetLastRevisionInfo(RemoteBranchTestCase):
1490
1343
            errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
1491
1344
        branch.unlock()
1492
1345
 
 
1346
    def lock_remote_branch(self, branch):
 
1347
        """Trick a RemoteBranch into thinking it is locked."""
 
1348
        branch._lock_mode = 'w'
 
1349
        branch._lock_count = 2
 
1350
        branch._lock_token = 'branch token'
 
1351
        branch._repo_lock_token = 'repo token'
 
1352
        branch.repository._lock_mode = 'w'
 
1353
        branch.repository._lock_count = 2
 
1354
        branch.repository._lock_token = 'repo token'
 
1355
 
1493
1356
    def test_backwards_compatibility(self):
1494
1357
        """If the server does not support the Branch.set_last_revision_info
1495
1358
        verb (which is new in 1.4), then the client falls back to VFS methods.
1536
1399
        self.assertEqual(
1537
1400
            [('set_last_revision_info', 1234, 'a-revision-id')],
1538
1401
            real_branch.calls)
1539
 
        self.assertFinished(client)
 
1402
        client.finished_test()
1540
1403
 
1541
1404
    def test_unexpected_error(self):
1542
1405
        # If the server sends an error the client doesn't understand, it gets
1650
1513
        config = branch._get_config()
1651
1514
        config.set_option('foo', 'bar')
1652
1515
        branch.unlock()
1653
 
        self.assertFinished(client)
 
1516
        client.finished_test()
1654
1517
 
1655
1518
    def test_backwards_compat_set_option(self):
1656
1519
        self.setup_smart_server_with_call_log()
1680
1543
        transport = transport.clone('quack')
1681
1544
        branch = self.make_remote_branch(transport, client)
1682
1545
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
1683
 
        self.assertFinished(client)
 
1546
        client.finished_test()
1684
1547
 
1685
1548
 
1686
1549
class TestBzrDirGetSetConfig(RemoteBzrDirTestCase):
1811
1674
        return repo, client
1812
1675
 
1813
1676
 
1814
 
def remoted_description(format):
1815
 
    return 'Remote: ' + format.get_format_description()
1816
 
 
1817
 
 
1818
 
class TestBranchFormat(tests.TestCase):
1819
 
 
1820
 
    def test_get_format_description(self):
1821
 
        remote_format = RemoteBranchFormat()
1822
 
        real_format = branch.BranchFormat.get_default_format()
1823
 
        remote_format._network_name = real_format.network_name()
1824
 
        self.assertEqual(remoted_description(real_format),
1825
 
            remote_format.get_format_description())
1826
 
 
1827
 
 
1828
1677
class TestRepositoryFormat(TestRemoteRepository):
1829
1678
 
1830
1679
    def test_fast_delta(self):
1837
1686
        false_format._network_name = false_name
1838
1687
        self.assertEqual(False, false_format.fast_deltas)
1839
1688
 
1840
 
    def test_get_format_description(self):
1841
 
        remote_repo_format = RemoteRepositoryFormat()
1842
 
        real_format = repository.RepositoryFormat.get_default_format()
1843
 
        remote_repo_format._network_name = real_format.network_name()
1844
 
        self.assertEqual(remoted_description(real_format),
1845
 
            remote_repo_format.get_format_description())
1846
 
 
1847
1689
 
1848
1690
class TestRepositoryGatherStats(TestRemoteRepository):
1849
1691
 
2034
1876
        self.assertLength(1, self.hpss_calls)
2035
1877
 
2036
1878
    def disableExtraResults(self):
2037
 
        self.overrideAttr(SmartServerRepositoryGetParentMap,
2038
 
                          'no_extra_results', True)
 
1879
        old_flag = SmartServerRepositoryGetParentMap.no_extra_results
 
1880
        SmartServerRepositoryGetParentMap.no_extra_results = True
 
1881
        def reset_values():
 
1882
            SmartServerRepositoryGetParentMap.no_extra_results = old_flag
 
1883
        self.addCleanup(reset_values)
2039
1884
 
2040
1885
    def test_null_cached_missing_and_stop_key(self):
2041
1886
        self.setup_smart_server_with_call_log()
2100
1945
 
2101
1946
    def test_allows_new_revisions(self):
2102
1947
        """get_parent_map's results can be updated by commit."""
2103
 
        smart_server = test_server.SmartTCPServer_for_testing()
2104
 
        self.start_server(smart_server)
 
1948
        smart_server = server.SmartTCPServer_for_testing()
 
1949
        smart_server.setUp()
 
1950
        self.addCleanup(smart_server.tearDown)
2105
1951
        self.make_branch('branch')
2106
1952
        branch = Branch.open(smart_server.get_url() + '/branch')
2107
1953
        tree = branch.create_checkout('tree', lightweight=True)
2199
2045
            'success', ('ok', 'rev-five'))
2200
2046
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2201
2047
        self.assertEqual((True, 'rev-five'), result)
2202
 
        self.assertFinished(client)
 
2048
        client.finished_test()
2203
2049
 
2204
2050
    def test_history_incomplete(self):
2205
2051
        repo, client = self.setup_fake_client_and_repository('quack')
2208
2054
            'success', ('history-incomplete', 10, 'rev-ten'))
2209
2055
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2210
2056
        self.assertEqual((False, (10, 'rev-ten')), result)
2211
 
        self.assertFinished(client)
 
2057
        client.finished_test()
2212
2058
 
2213
2059
    def test_history_incomplete_with_fallback(self):
2214
2060
        """A 'history-incomplete' response causes the fallback repository to be
2216
2062
        """
2217
2063
        # Make a repo with a fallback repo, both using a FakeClient.
2218
2064
        format = remote.response_tuple_to_repo_format(
2219
 
            ('yes', 'no', 'yes', self.get_repo_format().network_name()))
 
2065
            ('yes', 'no', 'yes', 'fake-network-name'))
2220
2066
        repo, client = self.setup_fake_client_and_repository('quack')
2221
2067
        repo._format = format
2222
2068
        fallback_repo, ignored = self.setup_fake_client_and_repository(
2223
2069
            'fallback')
2224
2070
        fallback_repo._client = client
2225
 
        fallback_repo._format = format
2226
2071
        repo.add_fallback_repository(fallback_repo)
2227
2072
        # First the client should ask the primary repo
2228
2073
        client.add_expected_call(
2235
2080
            'success', ('ok', 'rev-one'))
2236
2081
        result = repo.get_rev_id_for_revno(1, (42, 'rev-foo'))
2237
2082
        self.assertEqual((True, 'rev-one'), result)
2238
 
        self.assertFinished(client)
 
2083
        client.finished_test()
2239
2084
 
2240
2085
    def test_nosuchrevision(self):
2241
2086
        # 'nosuchrevision' is returned when the known-revid is not found in the
2247
2092
        self.assertRaises(
2248
2093
            errors.NoSuchRevision,
2249
2094
            repo.get_rev_id_for_revno, 5, (42, 'rev-foo'))
2250
 
        self.assertFinished(client)
2251
 
 
2252
 
    def test_branch_fallback_locking(self):
2253
 
        """RemoteBranch.get_rev_id takes a read lock, and tries to call the
2254
 
        get_rev_id_for_revno verb.  If the verb is unknown the VFS fallback
2255
 
        will be invoked, which will fail if the repo is unlocked.
2256
 
        """
2257
 
        self.setup_smart_server_with_call_log()
2258
 
        tree = self.make_branch_and_memory_tree('.')
2259
 
        tree.lock_write()
2260
 
        rev1 = tree.commit('First')
2261
 
        rev2 = tree.commit('Second')
2262
 
        tree.unlock()
2263
 
        branch = tree.branch
2264
 
        self.assertFalse(branch.is_locked())
2265
 
        self.reset_smart_call_log()
2266
 
        verb = 'Repository.get_rev_id_for_revno'
2267
 
        self.disable_verb(verb)
2268
 
        self.assertEqual(rev1, branch.get_rev_id(1))
2269
 
        self.assertLength(1, [call for call in self.hpss_calls if
2270
 
                              call.call.method == verb])
 
2095
        client.finished_test()
2271
2096
 
2272
2097
 
2273
2098
class TestRepositoryIsShared(TestRemoteRepository):
2390
2215
        self.assertEqual([], client._calls)
2391
2216
 
2392
2217
 
2393
 
class TestRepositoryInsertStreamBase(TestRemoteRepository):
2394
 
    """Base class for Repository.insert_stream and .insert_stream_1.19
2395
 
    tests.
2396
 
    """
2397
 
    
2398
 
    def checkInsertEmptyStream(self, repo, client):
2399
 
        """Insert an empty stream, checking the result.
2400
 
 
2401
 
        This checks that there are no resume_tokens or missing_keys, and that
2402
 
        the client is finished.
2403
 
        """
2404
 
        sink = repo._get_sink()
2405
 
        fmt = repository.RepositoryFormat.get_default_format()
2406
 
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2407
 
        self.assertEqual([], resume_tokens)
2408
 
        self.assertEqual(set(), missing_keys)
2409
 
        self.assertFinished(client)
2410
 
 
2411
 
 
2412
 
class TestRepositoryInsertStream(TestRepositoryInsertStreamBase):
2413
 
    """Tests for using Repository.insert_stream verb when the _1.19 variant is
2414
 
    not available.
2415
 
 
2416
 
    This test case is very similar to TestRepositoryInsertStream_1_19.
2417
 
    """
2418
 
 
2419
 
    def setUp(self):
2420
 
        TestRemoteRepository.setUp(self)
2421
 
        self.disable_verb('Repository.insert_stream_1.19')
2422
 
 
2423
 
    def test_unlocked_repo(self):
2424
 
        transport_path = 'quack'
2425
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2426
 
        client.add_expected_call(
2427
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2428
 
            'unknown', ('Repository.insert_stream_1.19',))
2429
 
        client.add_expected_call(
2430
 
            'Repository.insert_stream', ('quack/', ''),
2431
 
            'success', ('ok',))
2432
 
        client.add_expected_call(
2433
 
            'Repository.insert_stream', ('quack/', ''),
2434
 
            'success', ('ok',))
2435
 
        self.checkInsertEmptyStream(repo, client)
2436
 
 
2437
 
    def test_locked_repo_with_no_lock_token(self):
2438
 
        transport_path = 'quack'
2439
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2440
 
        client.add_expected_call(
2441
 
            'Repository.lock_write', ('quack/', ''),
2442
 
            'success', ('ok', ''))
2443
 
        client.add_expected_call(
2444
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2445
 
            'unknown', ('Repository.insert_stream_1.19',))
2446
 
        client.add_expected_call(
2447
 
            'Repository.insert_stream', ('quack/', ''),
2448
 
            'success', ('ok',))
2449
 
        client.add_expected_call(
2450
 
            'Repository.insert_stream', ('quack/', ''),
2451
 
            'success', ('ok',))
2452
 
        repo.lock_write()
2453
 
        self.checkInsertEmptyStream(repo, client)
2454
 
 
2455
 
    def test_locked_repo_with_lock_token(self):
2456
 
        transport_path = 'quack'
2457
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2458
 
        client.add_expected_call(
2459
 
            'Repository.lock_write', ('quack/', ''),
2460
 
            'success', ('ok', 'a token'))
2461
 
        client.add_expected_call(
2462
 
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2463
 
            'unknown', ('Repository.insert_stream_1.19',))
2464
 
        client.add_expected_call(
2465
 
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
2466
 
            'success', ('ok',))
2467
 
        client.add_expected_call(
2468
 
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
2469
 
            'success', ('ok',))
2470
 
        repo.lock_write()
2471
 
        self.checkInsertEmptyStream(repo, client)
2472
 
 
2473
 
    def test_stream_with_inventory_deltas(self):
2474
 
        """'inventory-deltas' substreams cannot be sent to the
2475
 
        Repository.insert_stream verb, because not all servers that implement
2476
 
        that verb will accept them.  So when one is encountered the RemoteSink
2477
 
        immediately stops using that verb and falls back to VFS insert_stream.
2478
 
        """
2479
 
        transport_path = 'quack'
2480
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2481
 
        client.add_expected_call(
2482
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2483
 
            'unknown', ('Repository.insert_stream_1.19',))
2484
 
        client.add_expected_call(
2485
 
            'Repository.insert_stream', ('quack/', ''),
2486
 
            'success', ('ok',))
2487
 
        client.add_expected_call(
2488
 
            'Repository.insert_stream', ('quack/', ''),
2489
 
            'success', ('ok',))
2490
 
        # Create a fake real repository for insert_stream to fall back on, so
2491
 
        # that we can directly see the records the RemoteSink passes to the
2492
 
        # real sink.
2493
 
        class FakeRealSink:
2494
 
            def __init__(self):
2495
 
                self.records = []
2496
 
            def insert_stream(self, stream, src_format, resume_tokens):
2497
 
                for substream_kind, substream in stream:
2498
 
                    self.records.append(
2499
 
                        (substream_kind, [record.key for record in substream]))
2500
 
                return ['fake tokens'], ['fake missing keys']
2501
 
        fake_real_sink = FakeRealSink()
2502
 
        class FakeRealRepository:
2503
 
            def _get_sink(self):
2504
 
                return fake_real_sink
2505
 
            def is_in_write_group(self):
2506
 
                return False
2507
 
            def refresh_data(self):
2508
 
                return True
2509
 
        repo._real_repository = FakeRealRepository()
2510
 
        sink = repo._get_sink()
2511
 
        fmt = repository.RepositoryFormat.get_default_format()
2512
 
        stream = self.make_stream_with_inv_deltas(fmt)
2513
 
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2514
 
        # Every record from the first inventory delta should have been sent to
2515
 
        # the VFS sink.
2516
 
        expected_records = [
2517
 
            ('inventory-deltas', [('rev2',), ('rev3',)]),
2518
 
            ('texts', [('some-rev', 'some-file')])]
2519
 
        self.assertEqual(expected_records, fake_real_sink.records)
2520
 
        # The return values from the real sink's insert_stream are propagated
2521
 
        # back to the original caller.
2522
 
        self.assertEqual(['fake tokens'], resume_tokens)
2523
 
        self.assertEqual(['fake missing keys'], missing_keys)
2524
 
        self.assertFinished(client)
2525
 
 
2526
 
    def make_stream_with_inv_deltas(self, fmt):
2527
 
        """Make a simple stream with an inventory delta followed by more
2528
 
        records and more substreams to test that all records and substreams
2529
 
        from that point on are used.
2530
 
 
2531
 
        This sends, in order:
2532
 
           * inventories substream: rev1, rev2, rev3.  rev2 and rev3 are
2533
 
             inventory-deltas.
2534
 
           * texts substream: (some-rev, some-file)
2535
 
        """
2536
 
        # Define a stream using generators so that it isn't rewindable.
2537
 
        inv = inventory.Inventory(revision_id='rev1')
2538
 
        inv.root.revision = 'rev1'
2539
 
        def stream_with_inv_delta():
2540
 
            yield ('inventories', inventories_substream())
2541
 
            yield ('inventory-deltas', inventory_delta_substream())
2542
 
            yield ('texts', [
2543
 
                versionedfile.FulltextContentFactory(
2544
 
                    ('some-rev', 'some-file'), (), None, 'content')])
2545
 
        def inventories_substream():
2546
 
            # An empty inventory fulltext.  This will be streamed normally.
2547
 
            text = fmt._serializer.write_inventory_to_string(inv)
2548
 
            yield versionedfile.FulltextContentFactory(
2549
 
                ('rev1',), (), None, text)
2550
 
        def inventory_delta_substream():
2551
 
            # An inventory delta.  This can't be streamed via this verb, so it
2552
 
            # will trigger a fallback to VFS insert_stream.
2553
 
            entry = inv.make_entry(
2554
 
                'directory', 'newdir', inv.root.file_id, 'newdir-id')
2555
 
            entry.revision = 'ghost'
2556
 
            delta = [(None, 'newdir', 'newdir-id', entry)]
2557
 
            serializer = inventory_delta.InventoryDeltaSerializer(
2558
 
                versioned_root=True, tree_references=False)
2559
 
            lines = serializer.delta_to_lines('rev1', 'rev2', delta)
2560
 
            yield versionedfile.ChunkedContentFactory(
2561
 
                ('rev2',), (('rev1',)), None, lines)
2562
 
            # Another delta.
2563
 
            lines = serializer.delta_to_lines('rev1', 'rev3', delta)
2564
 
            yield versionedfile.ChunkedContentFactory(
2565
 
                ('rev3',), (('rev1',)), None, lines)
2566
 
        return stream_with_inv_delta()
2567
 
 
2568
 
 
2569
 
class TestRepositoryInsertStream_1_19(TestRepositoryInsertStreamBase):
2570
 
 
2571
 
    def test_unlocked_repo(self):
2572
 
        transport_path = 'quack'
2573
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2574
 
        client.add_expected_call(
2575
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2576
 
            'success', ('ok',))
2577
 
        client.add_expected_call(
2578
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2579
 
            'success', ('ok',))
2580
 
        self.checkInsertEmptyStream(repo, client)
2581
 
 
2582
 
    def test_locked_repo_with_no_lock_token(self):
2583
 
        transport_path = 'quack'
2584
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2585
 
        client.add_expected_call(
2586
 
            'Repository.lock_write', ('quack/', ''),
2587
 
            'success', ('ok', ''))
2588
 
        client.add_expected_call(
2589
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2590
 
            'success', ('ok',))
2591
 
        client.add_expected_call(
2592
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2593
 
            'success', ('ok',))
2594
 
        repo.lock_write()
2595
 
        self.checkInsertEmptyStream(repo, client)
2596
 
 
2597
 
    def test_locked_repo_with_lock_token(self):
2598
 
        transport_path = 'quack'
2599
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2600
 
        client.add_expected_call(
2601
 
            'Repository.lock_write', ('quack/', ''),
2602
 
            'success', ('ok', 'a token'))
2603
 
        client.add_expected_call(
2604
 
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2605
 
            'success', ('ok',))
2606
 
        client.add_expected_call(
2607
 
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2608
 
            'success', ('ok',))
2609
 
        repo.lock_write()
2610
 
        self.checkInsertEmptyStream(repo, client)
 
2218
class TestRepositoryInsertStream(TestRemoteRepository):
 
2219
 
 
2220
    def test_unlocked_repo(self):
 
2221
        transport_path = 'quack'
 
2222
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2223
        client.add_expected_call(
 
2224
            'Repository.insert_stream', ('quack/', ''),
 
2225
            'success', ('ok',))
 
2226
        client.add_expected_call(
 
2227
            'Repository.insert_stream', ('quack/', ''),
 
2228
            'success', ('ok',))
 
2229
        sink = repo._get_sink()
 
2230
        fmt = repository.RepositoryFormat.get_default_format()
 
2231
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
 
2232
        self.assertEqual([], resume_tokens)
 
2233
        self.assertEqual(set(), missing_keys)
 
2234
        client.finished_test()
 
2235
 
 
2236
    def test_locked_repo_with_no_lock_token(self):
 
2237
        transport_path = 'quack'
 
2238
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2239
        client.add_expected_call(
 
2240
            'Repository.lock_write', ('quack/', ''),
 
2241
            'success', ('ok', ''))
 
2242
        client.add_expected_call(
 
2243
            'Repository.insert_stream', ('quack/', ''),
 
2244
            'success', ('ok',))
 
2245
        client.add_expected_call(
 
2246
            'Repository.insert_stream', ('quack/', ''),
 
2247
            'success', ('ok',))
 
2248
        repo.lock_write()
 
2249
        sink = repo._get_sink()
 
2250
        fmt = repository.RepositoryFormat.get_default_format()
 
2251
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
 
2252
        self.assertEqual([], resume_tokens)
 
2253
        self.assertEqual(set(), missing_keys)
 
2254
        client.finished_test()
 
2255
 
 
2256
    def test_locked_repo_with_lock_token(self):
 
2257
        transport_path = 'quack'
 
2258
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2259
        client.add_expected_call(
 
2260
            'Repository.lock_write', ('quack/', ''),
 
2261
            'success', ('ok', 'a token'))
 
2262
        client.add_expected_call(
 
2263
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
 
2264
            'success', ('ok',))
 
2265
        client.add_expected_call(
 
2266
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
 
2267
            'success', ('ok',))
 
2268
        repo.lock_write()
 
2269
        sink = repo._get_sink()
 
2270
        fmt = repository.RepositoryFormat.get_default_format()
 
2271
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
 
2272
        self.assertEqual([], resume_tokens)
 
2273
        self.assertEqual(set(), missing_keys)
 
2274
        client.finished_test()
2611
2275
 
2612
2276
 
2613
2277
class TestRepositoryTarball(TestRemoteRepository):
2649
2313
    """RemoteRepository.copy_content_into optimizations"""
2650
2314
 
2651
2315
    def test_copy_content_remote_to_local(self):
2652
 
        self.transport_server = test_server.SmartTCPServer_for_testing
 
2316
        self.transport_server = server.SmartTCPServer_for_testing
2653
2317
        src_repo = self.make_repository('repo1')
2654
2318
        src_repo = repository.Repository.open(self.get_url('repo1'))
2655
2319
        # At the moment the tarball-based copy_content_into can't write back
2698
2362
        client.add_expected_call(
2699
2363
            'PackRepository.autopack', ('quack/',), 'success', ('ok',))
2700
2364
        repo.autopack()
2701
 
        self.assertFinished(client)
 
2365
        client.finished_test()
2702
2366
 
2703
2367
    def test_ok_with_real_repo(self):
2704
2368
        """When the server returns 'ok' and there is a _real_repository, then
2803
2467
        expected_error = errors.NotBranchError(path=bzrdir.root_transport.base)
2804
2468
        self.assertEqual(expected_error, translated_error)
2805
2469
 
2806
 
    def test_nobranch_one_arg(self):
2807
 
        bzrdir = self.make_bzrdir('')
2808
 
        translated_error = self.translateTuple(
2809
 
            ('nobranch', 'extra detail'), bzrdir=bzrdir)
2810
 
        expected_error = errors.NotBranchError(
2811
 
            path=bzrdir.root_transport.base,
2812
 
            detail='extra detail')
2813
 
        self.assertEqual(expected_error, translated_error)
2814
 
 
2815
2470
    def test_LockContention(self):
2816
2471
        translated_error = self.translateTuple(('LockContention',))
2817
2472
        expected_error = errors.LockContention('(remote lock)')
2857
2512
        expected_error = errors.ReadError(path)
2858
2513
        self.assertEqual(expected_error, translated_error)
2859
2514
 
2860
 
    def test_IncompatibleRepositories(self):
2861
 
        translated_error = self.translateTuple(('IncompatibleRepositories',
2862
 
            "repo1", "repo2", "details here"))
2863
 
        expected_error = errors.IncompatibleRepositories("repo1", "repo2",
2864
 
            "details here")
2865
 
        self.assertEqual(expected_error, translated_error)
2866
 
 
2867
2515
    def test_PermissionDenied_no_args(self):
2868
2516
        path = 'a path'
2869
2517
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
2930
2578
        # In addition to re-raising ErrorFromSmartServer, some debug info has
2931
2579
        # been muttered to the log file for developer to look at.
2932
2580
        self.assertContainsRe(
2933
 
            self.get_log(),
 
2581
            self._get_log(keep_log_file=True),
2934
2582
            "Missing key 'branch' in context")
2935
2583
 
2936
2584
    def test_path_missing(self):
2944
2592
        self.assertEqual(server_error, translated_error)
2945
2593
        # In addition to re-raising ErrorFromSmartServer, some debug info has
2946
2594
        # been muttered to the log file for developer to look at.
2947
 
        self.assertContainsRe(self.get_log(), "Missing key 'path' in context")
 
2595
        self.assertContainsRe(
 
2596
            self._get_log(keep_log_file=True), "Missing key 'path' in context")
2948
2597
 
2949
2598
 
2950
2599
class TestStacking(tests.TestCaseWithTransport):
2968
2617
        stacked_branch = self.make_branch('stacked', format='1.9')
2969
2618
        stacked_branch.set_stacked_on_url('../base')
2970
2619
        # start a server looking at this
2971
 
        smart_server = test_server.SmartTCPServer_for_testing()
2972
 
        self.start_server(smart_server)
 
2620
        smart_server = server.SmartTCPServer_for_testing()
 
2621
        smart_server.setUp()
 
2622
        self.addCleanup(smart_server.tearDown)
2973
2623
        remote_bzrdir = BzrDir.open(smart_server.get_url() + '/stacked')
2974
2624
        # can get its branch and repository
2975
2625
        remote_branch = remote_bzrdir.open_branch()
2997
2647
        tree1.commit('rev1', rev_id='rev1')
2998
2648
        tree2 = tree1.branch.bzrdir.sprout('tree2', stacked=True
2999
2649
            ).open_workingtree()
3000
 
        local_tree = tree2.branch.create_checkout('local')
3001
 
        local_tree.commit('local changes make me feel good.')
 
2650
        tree2.commit('local changes make me feel good.')
3002
2651
        branch2 = Branch.open(self.get_url('tree2'))
3003
2652
        branch2.lock_read()
3004
2653
        self.addCleanup(branch2.unlock)
3026
2675
                    result.append(content.key[-1])
3027
2676
        return result
3028
2677
 
3029
 
    def get_ordered_revs(self, format, order, branch_factory=None):
 
2678
    def get_ordered_revs(self, format, order):
3030
2679
        """Get a list of the revisions in a stream to format format.
3031
2680
 
3032
2681
        :param format: The format of the target.
3033
2682
        :param order: the order that target should have requested.
3034
 
        :param branch_factory: A callable to create a trunk and stacked branch
3035
 
            to fetch from. If none, self.prepare_stacked_remote_branch is used.
3036
2683
        :result: The revision ids in the stream, in the order seen,
3037
2684
            the topological order of revisions in the source.
3038
2685
        """
3040
2687
        target_repository_format = unordered_format.repository_format
3041
2688
        # Cross check
3042
2689
        self.assertEqual(order, target_repository_format._fetch_order)
3043
 
        if branch_factory is None:
3044
 
            branch_factory = self.prepare_stacked_remote_branch
3045
 
        _, stacked = branch_factory()
 
2690
        trunk, stacked = self.prepare_stacked_remote_branch()
3046
2691
        source = stacked.repository._get_source(target_repository_format)
3047
2692
        tip = stacked.last_revision()
3048
2693
        revs = stacked.repository.get_ancestry(tip)
3067
2712
        # from the server, then one from the backing branch.
3068
2713
        self.assertLength(2, self.hpss_calls)
3069
2714
 
3070
 
    def test_stacked_on_stacked_get_stream_unordered(self):
3071
 
        # Repository._get_source.get_stream() from a stacked repository which
3072
 
        # is itself stacked yields the full data from all three sources.
3073
 
        def make_stacked_stacked():
3074
 
            _, stacked = self.prepare_stacked_remote_branch()
3075
 
            tree = stacked.bzrdir.sprout('tree3', stacked=True
3076
 
                ).open_workingtree()
3077
 
            local_tree = tree.branch.create_checkout('local-tree3')
3078
 
            local_tree.commit('more local changes are better')
3079
 
            branch = Branch.open(self.get_url('tree3'))
3080
 
            branch.lock_read()
3081
 
            self.addCleanup(branch.unlock)
3082
 
            return None, branch
3083
 
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered',
3084
 
            branch_factory=make_stacked_stacked)
3085
 
        self.assertEqual(set(expected_revs), set(rev_ord))
3086
 
        # Getting unordered results should have made a streaming data request
3087
 
        # from the server, and one from each backing repo
3088
 
        self.assertLength(3, self.hpss_calls)
3089
 
 
3090
2715
    def test_stacked_get_stream_topological(self):
3091
2716
        # Repository._get_source.get_stream() from a stacked repository with
3092
2717
        # topological sorting yields the full data from both stacked and
3093
2718
        # stacked upon sources in topological order.
3094
2719
        rev_ord, expected_revs = self.get_ordered_revs('knit', 'topological')
3095
2720
        self.assertEqual(expected_revs, rev_ord)
3096
 
        # Getting topological sort requires VFS calls still - one of which is
3097
 
        # pushing up from the bound branch.
3098
 
        self.assertLength(13, self.hpss_calls)
 
2721
        # Getting topological sort requires VFS calls still
 
2722
        self.assertLength(12, self.hpss_calls)
3099
2723
 
3100
2724
    def test_stacked_get_stream_groupcompress(self):
3101
2725
        # Repository._get_source.get_stream() from a stacked repository with
3130
2754
        super(TestRemoteBranchEffort, self).setUp()
3131
2755
        # Create a smart server that publishes whatever the backing VFS server
3132
2756
        # does.
3133
 
        self.smart_server = test_server.SmartTCPServer_for_testing()
3134
 
        self.start_server(self.smart_server, self.get_server())
 
2757
        self.smart_server = server.SmartTCPServer_for_testing()
 
2758
        self.smart_server.setUp(self.get_server())
 
2759
        self.addCleanup(self.smart_server.tearDown)
3135
2760
        # Log all HPSS calls into self.hpss_calls.
3136
2761
        _SmartClient.hooks.install_named_hook(
3137
2762
            'call', self.capture_hpss_call, None)