~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

  • Committer: Andrew Bennetts
  • Date: 2009-07-27 05:35:00 UTC
  • mfrom: (4570 +trunk)
  • mto: (4634.6.29 2.0)
  • mto: This revision was merged to the branch mainline in revision 4680.
  • Revision ID: andrew.bennetts@canonical.com-20090727053500-q76zsn2dx33jhmj5
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the Repository facility that are not interface tests.
18
18
 
31
31
                           UnknownFormatError,
32
32
                           UnsupportedFormatError,
33
33
                           )
34
 
from bzrlib import graph
 
34
from bzrlib import (
 
35
    graph,
 
36
    tests,
 
37
    )
 
38
from bzrlib.branchbuilder import BranchBuilder
35
39
from bzrlib.btree_index import BTreeBuilder, BTreeGraphIndex
36
40
from bzrlib.index import GraphIndex, InMemoryGraphIndex
37
41
from bzrlib.repository import RepositoryFormat
47
51
    get_transport,
48
52
    )
49
53
from bzrlib.transport.memory import MemoryServer
50
 
from bzrlib.util import bencode
51
54
from bzrlib import (
 
55
    bencode,
52
56
    bzrdir,
53
57
    errors,
54
58
    inventory,
60
64
    upgrade,
61
65
    workingtree,
62
66
    )
63
 
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
 
67
from bzrlib.repofmt import (
 
68
    groupcompress_repo,
 
69
    knitrepo,
 
70
    pack_repo,
 
71
    weaverepo,
 
72
    )
64
73
 
65
74
 
66
75
class TestDefaultFormat(TestCase):
664
673
        self.assertFalse(repo._format.supports_external_lookups)
665
674
 
666
675
 
 
676
class Test2a(TestCaseWithTransport):
 
677
 
 
678
    def test_format_pack_compresses_True(self):
 
679
        repo = self.make_repository('repo', format='2a')
 
680
        self.assertTrue(repo._format.pack_compresses)
 
681
 
 
682
    def test_inventories_use_chk_map_with_parent_base_dict(self):
 
683
        tree = self.make_branch_and_tree('repo', format="2a")
 
684
        revid = tree.commit("foo")
 
685
        tree.lock_read()
 
686
        self.addCleanup(tree.unlock)
 
687
        inv = tree.branch.repository.get_inventory(revid)
 
688
        self.assertNotEqual(None, inv.parent_id_basename_to_file_id)
 
689
        inv.parent_id_basename_to_file_id._ensure_root()
 
690
        inv.id_to_entry._ensure_root()
 
691
        self.assertEqual(65536, inv.id_to_entry._root_node.maximum_size)
 
692
        self.assertEqual(65536,
 
693
            inv.parent_id_basename_to_file_id._root_node.maximum_size)
 
694
 
 
695
    def test_autopack_unchanged_chk_nodes(self):
 
696
        # at 20 unchanged commits, chk pages are packed that are split into
 
697
        # two groups such that the new pack being made doesn't have all its
 
698
        # pages in the source packs (though they are in the repository).
 
699
        tree = self.make_branch_and_tree('tree', format='2a')
 
700
        for pos in range(20):
 
701
            tree.commit(str(pos))
 
702
 
 
703
    def test_pack_with_hint(self):
 
704
        tree = self.make_branch_and_tree('tree', format='2a')
 
705
        # 1 commit to leave untouched
 
706
        tree.commit('1')
 
707
        to_keep = tree.branch.repository._pack_collection.names()
 
708
        # 2 to combine
 
709
        tree.commit('2')
 
710
        tree.commit('3')
 
711
        all = tree.branch.repository._pack_collection.names()
 
712
        combine = list(set(all) - set(to_keep))
 
713
        self.assertLength(3, all)
 
714
        self.assertLength(2, combine)
 
715
        tree.branch.repository.pack(hint=combine)
 
716
        final = tree.branch.repository._pack_collection.names()
 
717
        self.assertLength(2, final)
 
718
        self.assertFalse(combine[0] in final)
 
719
        self.assertFalse(combine[1] in final)
 
720
        self.assertSubset(to_keep, final)
 
721
 
 
722
    def test_stream_source_to_gc(self):
 
723
        source = self.make_repository('source', format='2a')
 
724
        target = self.make_repository('target', format='2a')
 
725
        stream = source._get_source(target._format)
 
726
        self.assertIsInstance(stream, groupcompress_repo.GroupCHKStreamSource)
 
727
 
 
728
    def test_stream_source_to_non_gc(self):
 
729
        source = self.make_repository('source', format='2a')
 
730
        target = self.make_repository('target', format='rich-root-pack')
 
731
        stream = source._get_source(target._format)
 
732
        # We don't want the child GroupCHKStreamSource
 
733
        self.assertIs(type(stream), repository.StreamSource)
 
734
 
 
735
    def test_get_stream_for_missing_keys_includes_all_chk_refs(self):
 
736
        source_builder = self.make_branch_builder('source',
 
737
                            format='2a')
 
738
        # We have to build a fairly large tree, so that we are sure the chk
 
739
        # pages will have split into multiple pages.
 
740
        entries = [('add', ('', 'a-root-id', 'directory', None))]
 
741
        for i in 'abcdefghijklmnopqrstuvwxyz123456789':
 
742
            for j in 'abcdefghijklmnopqrstuvwxyz123456789':
 
743
                fname = i + j
 
744
                fid = fname + '-id'
 
745
                content = 'content for %s\n' % (fname,)
 
746
                entries.append(('add', (fname, fid, 'file', content)))
 
747
        source_builder.start_series()
 
748
        source_builder.build_snapshot('rev-1', None, entries)
 
749
        # Now change a few of them, so we get a few new pages for the second
 
750
        # revision
 
751
        source_builder.build_snapshot('rev-2', ['rev-1'], [
 
752
            ('modify', ('aa-id', 'new content for aa-id\n')),
 
753
            ('modify', ('cc-id', 'new content for cc-id\n')),
 
754
            ('modify', ('zz-id', 'new content for zz-id\n')),
 
755
            ])
 
756
        source_builder.finish_series()
 
757
        source_branch = source_builder.get_branch()
 
758
        source_branch.lock_read()
 
759
        self.addCleanup(source_branch.unlock)
 
760
        target = self.make_repository('target', format='2a')
 
761
        source = source_branch.repository._get_source(target._format)
 
762
        self.assertIsInstance(source, groupcompress_repo.GroupCHKStreamSource)
 
763
 
 
764
        # On a regular pass, getting the inventories and chk pages for rev-2
 
765
        # would only get the newly created chk pages
 
766
        search = graph.SearchResult(set(['rev-2']), set(['rev-1']), 1,
 
767
                                    set(['rev-2']))
 
768
        simple_chk_records = []
 
769
        for vf_name, substream in source.get_stream(search):
 
770
            if vf_name == 'chk_bytes':
 
771
                for record in substream:
 
772
                    simple_chk_records.append(record.key)
 
773
            else:
 
774
                for _ in substream:
 
775
                    continue
 
776
        # 3 pages, the root (InternalNode), + 2 pages which actually changed
 
777
        self.assertEqual([('sha1:91481f539e802c76542ea5e4c83ad416bf219f73',),
 
778
                          ('sha1:4ff91971043668583985aec83f4f0ab10a907d3f',),
 
779
                          ('sha1:81e7324507c5ca132eedaf2d8414ee4bb2226187',),
 
780
                          ('sha1:b101b7da280596c71a4540e9a1eeba8045985ee0',)],
 
781
                         simple_chk_records)
 
782
        # Now, when we do a similar call using 'get_stream_for_missing_keys'
 
783
        # we should get a much larger set of pages.
 
784
        missing = [('inventories', 'rev-2')]
 
785
        full_chk_records = []
 
786
        for vf_name, substream in source.get_stream_for_missing_keys(missing):
 
787
            if vf_name == 'inventories':
 
788
                for record in substream:
 
789
                    self.assertEqual(('rev-2',), record.key)
 
790
            elif vf_name == 'chk_bytes':
 
791
                for record in substream:
 
792
                    full_chk_records.append(record.key)
 
793
            else:
 
794
                self.fail('Should not be getting a stream of %s' % (vf_name,))
 
795
        # We have 257 records now. This is because we have 1 root page, and 256
 
796
        # leaf pages in a complete listing.
 
797
        self.assertEqual(257, len(full_chk_records))
 
798
        self.assertSubset(simple_chk_records, full_chk_records)
 
799
 
 
800
    def test_inconsistency_fatal(self):
 
801
        repo = self.make_repository('repo', format='2a')
 
802
        self.assertTrue(repo.revisions._index._inconsistency_fatal)
 
803
        self.assertFalse(repo.texts._index._inconsistency_fatal)
 
804
        self.assertFalse(repo.inventories._index._inconsistency_fatal)
 
805
        self.assertFalse(repo.signatures._index._inconsistency_fatal)
 
806
        self.assertFalse(repo.chk_bytes._index._inconsistency_fatal)
 
807
 
 
808
 
 
809
class TestKnitPackStreamSource(tests.TestCaseWithMemoryTransport):
 
810
 
 
811
    def test_source_to_exact_pack_092(self):
 
812
        source = self.make_repository('source', format='pack-0.92')
 
813
        target = self.make_repository('target', format='pack-0.92')
 
814
        stream_source = source._get_source(target._format)
 
815
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
816
 
 
817
    def test_source_to_exact_pack_rich_root_pack(self):
 
818
        source = self.make_repository('source', format='rich-root-pack')
 
819
        target = self.make_repository('target', format='rich-root-pack')
 
820
        stream_source = source._get_source(target._format)
 
821
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
822
 
 
823
    def test_source_to_exact_pack_19(self):
 
824
        source = self.make_repository('source', format='1.9')
 
825
        target = self.make_repository('target', format='1.9')
 
826
        stream_source = source._get_source(target._format)
 
827
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
828
 
 
829
    def test_source_to_exact_pack_19_rich_root(self):
 
830
        source = self.make_repository('source', format='1.9-rich-root')
 
831
        target = self.make_repository('target', format='1.9-rich-root')
 
832
        stream_source = source._get_source(target._format)
 
833
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
834
 
 
835
    def test_source_to_remote_exact_pack_19(self):
 
836
        trans = self.make_smart_server('target')
 
837
        trans.ensure_base()
 
838
        source = self.make_repository('source', format='1.9')
 
839
        target = self.make_repository('target', format='1.9')
 
840
        target = repository.Repository.open(trans.base)
 
841
        stream_source = source._get_source(target._format)
 
842
        self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
 
843
 
 
844
    def test_stream_source_to_non_exact(self):
 
845
        source = self.make_repository('source', format='pack-0.92')
 
846
        target = self.make_repository('target', format='1.9')
 
847
        stream = source._get_source(target._format)
 
848
        self.assertIs(type(stream), repository.StreamSource)
 
849
 
 
850
    def test_stream_source_to_non_exact_rich_root(self):
 
851
        source = self.make_repository('source', format='1.9')
 
852
        target = self.make_repository('target', format='1.9-rich-root')
 
853
        stream = source._get_source(target._format)
 
854
        self.assertIs(type(stream), repository.StreamSource)
 
855
 
 
856
    def test_source_to_remote_non_exact_pack_19(self):
 
857
        trans = self.make_smart_server('target')
 
858
        trans.ensure_base()
 
859
        source = self.make_repository('source', format='1.9')
 
860
        target = self.make_repository('target', format='1.6')
 
861
        target = repository.Repository.open(trans.base)
 
862
        stream_source = source._get_source(target._format)
 
863
        self.assertIs(type(stream_source), repository.StreamSource)
 
864
 
 
865
    def test_stream_source_to_knit(self):
 
866
        source = self.make_repository('source', format='pack-0.92')
 
867
        target = self.make_repository('target', format='dirstate')
 
868
        stream = source._get_source(target._format)
 
869
        self.assertIs(type(stream), repository.StreamSource)
 
870
 
 
871
 
 
872
class TestDevelopment6FindParentIdsOfRevisions(TestCaseWithTransport):
 
873
    """Tests for _find_parent_ids_of_revisions."""
 
874
 
 
875
    def setUp(self):
 
876
        super(TestDevelopment6FindParentIdsOfRevisions, self).setUp()
 
877
        self.builder = self.make_branch_builder('source',
 
878
            format='development6-rich-root')
 
879
        self.builder.start_series()
 
880
        self.builder.build_snapshot('initial', None,
 
881
            [('add', ('', 'tree-root', 'directory', None))])
 
882
        self.repo = self.builder.get_branch().repository
 
883
        self.addCleanup(self.builder.finish_series)
 
884
 
 
885
    def assertParentIds(self, expected_result, rev_set):
 
886
        self.assertEqual(sorted(expected_result),
 
887
            sorted(self.repo._find_parent_ids_of_revisions(rev_set)))
 
888
 
 
889
    def test_simple(self):
 
890
        self.builder.build_snapshot('revid1', None, [])
 
891
        self.builder.build_snapshot('revid2', ['revid1'], [])
 
892
        rev_set = ['revid2']
 
893
        self.assertParentIds(['revid1'], rev_set)
 
894
 
 
895
    def test_not_first_parent(self):
 
896
        self.builder.build_snapshot('revid1', None, [])
 
897
        self.builder.build_snapshot('revid2', ['revid1'], [])
 
898
        self.builder.build_snapshot('revid3', ['revid2'], [])
 
899
        rev_set = ['revid3', 'revid2']
 
900
        self.assertParentIds(['revid1'], rev_set)
 
901
 
 
902
    def test_not_null(self):
 
903
        rev_set = ['initial']
 
904
        self.assertParentIds([], rev_set)
 
905
 
 
906
    def test_not_null_set(self):
 
907
        self.builder.build_snapshot('revid1', None, [])
 
908
        rev_set = [_mod_revision.NULL_REVISION]
 
909
        self.assertParentIds([], rev_set)
 
910
 
 
911
    def test_ghost(self):
 
912
        self.builder.build_snapshot('revid1', None, [])
 
913
        rev_set = ['ghost', 'revid1']
 
914
        self.assertParentIds(['initial'], rev_set)
 
915
 
 
916
    def test_ghost_parent(self):
 
917
        self.builder.build_snapshot('revid1', None, [])
 
918
        self.builder.build_snapshot('revid2', ['revid1', 'ghost'], [])
 
919
        rev_set = ['revid2', 'revid1']
 
920
        self.assertParentIds(['ghost', 'initial'], rev_set)
 
921
 
 
922
    def test_righthand_parent(self):
 
923
        self.builder.build_snapshot('revid1', None, [])
 
924
        self.builder.build_snapshot('revid2a', ['revid1'], [])
 
925
        self.builder.build_snapshot('revid2b', ['revid1'], [])
 
926
        self.builder.build_snapshot('revid3', ['revid2a', 'revid2b'], [])
 
927
        rev_set = ['revid3', 'revid2a']
 
928
        self.assertParentIds(['revid1', 'revid2b'], rev_set)
 
929
 
 
930
 
667
931
class TestWithBrokenRepo(TestCaseWithTransport):
668
932
    """These tests seem to be more appropriate as interface tests?"""
669
933
 
744
1008
        """
745
1009
        broken_repo = self.make_broken_repository()
746
1010
        empty_repo = self.make_repository('empty-repo')
 
1011
        # See bug https://bugs.launchpad.net/bzr/+bug/389141 for information
 
1012
        # about why this was turned into expectFailure
 
1013
        self.expectFailure('new Stream fetch fills in missing compression'
 
1014
           ' parents (bug #389141)',
 
1015
           self.assertRaises, (errors.RevisionNotPresent, errors.BzrCheckError),
 
1016
                              empty_repo.fetch, broken_repo)
747
1017
        self.assertRaises((errors.RevisionNotPresent, errors.BzrCheckError),
748
1018
                          empty_repo.fetch, broken_repo)
749
1019
 
929
1199
        tree.lock_read()
930
1200
        self.addCleanup(tree.unlock)
931
1201
        packs = tree.branch.repository._pack_collection
 
1202
        packs.reset()
932
1203
        packs.ensure_loaded()
933
1204
        name = packs.names()[0]
934
1205
        pack_1 = packs.get_pack_by_name(name)
1049
1320
        pack_transport = self.get_transport('pack')
1050
1321
        index_transport = self.get_transport('index')
1051
1322
        upload_transport.mkdir('.')
1052
 
        collection = pack_repo.RepositoryPackCollection(repo=None,
 
1323
        collection = pack_repo.RepositoryPackCollection(
 
1324
            repo=None,
1053
1325
            transport=self.get_transport('.'),
1054
1326
            index_transport=index_transport,
1055
1327
            upload_transport=upload_transport,
1056
1328
            pack_transport=pack_transport,
1057
1329
            index_builder_class=BTreeBuilder,
1058
 
            index_class=BTreeGraphIndex)
 
1330
            index_class=BTreeGraphIndex,
 
1331
            use_chk_index=False)
1059
1332
        pack = pack_repo.NewPack(collection)
1060
1333
        self.assertIsInstance(pack.revision_index, BTreeBuilder)
1061
1334
        self.assertIsInstance(pack.inventory_index, BTreeBuilder)
1122
1395
        self.assertTrue(new_pack.signature_index._optimize_for_size)
1123
1396
 
1124
1397
 
1125
 
class TestInterDifferingSerializer(TestCaseWithTransport):
1126
 
 
1127
 
    def test_progress_bar(self):
1128
 
        tree = self.make_branch_and_tree('tree')
1129
 
        tree.commit('rev1', rev_id='rev-1')
1130
 
        tree.commit('rev2', rev_id='rev-2')
1131
 
        tree.commit('rev3', rev_id='rev-3')
1132
 
        repo = self.make_repository('repo')
1133
 
        inter_repo = repository.InterDifferingSerializer(
1134
 
            tree.branch.repository, repo)
1135
 
        pb = progress.InstrumentedProgress(to_file=StringIO())
1136
 
        pb.never_throttle = True
1137
 
        inter_repo.fetch('rev-1', pb)
1138
 
        self.assertEqual('Transferring revisions', pb.last_msg)
1139
 
        self.assertEqual(1, pb.last_cnt)
1140
 
        self.assertEqual(1, pb.last_total)
1141
 
        inter_repo.fetch('rev-3', pb)
1142
 
        self.assertEqual(2, pb.last_cnt)
1143
 
        self.assertEqual(2, pb.last_total)
 
1398
class TestCrossFormatPacks(TestCaseWithTransport):
 
1399
 
 
1400
    def log_pack(self, hint=None):
 
1401
        self.calls.append(('pack', hint))
 
1402
        self.orig_pack(hint=hint)
 
1403
        if self.expect_hint:
 
1404
            self.assertTrue(hint)
 
1405
 
 
1406
    def run_stream(self, src_fmt, target_fmt, expect_pack_called):
 
1407
        self.expect_hint = expect_pack_called
 
1408
        self.calls = []
 
1409
        source_tree = self.make_branch_and_tree('src', format=src_fmt)
 
1410
        source_tree.lock_write()
 
1411
        self.addCleanup(source_tree.unlock)
 
1412
        tip = source_tree.commit('foo')
 
1413
        target = self.make_repository('target', format=target_fmt)
 
1414
        target.lock_write()
 
1415
        self.addCleanup(target.unlock)
 
1416
        source = source_tree.branch.repository._get_source(target._format)
 
1417
        self.orig_pack = target.pack
 
1418
        target.pack = self.log_pack
 
1419
        search = target.search_missing_revision_ids(
 
1420
            source_tree.branch.repository, tip)
 
1421
        stream = source.get_stream(search)
 
1422
        from_format = source_tree.branch.repository._format
 
1423
        sink = target._get_sink()
 
1424
        sink.insert_stream(stream, from_format, [])
 
1425
        if expect_pack_called:
 
1426
            self.assertLength(1, self.calls)
 
1427
        else:
 
1428
            self.assertLength(0, self.calls)
 
1429
 
 
1430
    def run_fetch(self, src_fmt, target_fmt, expect_pack_called):
 
1431
        self.expect_hint = expect_pack_called
 
1432
        self.calls = []
 
1433
        source_tree = self.make_branch_and_tree('src', format=src_fmt)
 
1434
        source_tree.lock_write()
 
1435
        self.addCleanup(source_tree.unlock)
 
1436
        tip = source_tree.commit('foo')
 
1437
        target = self.make_repository('target', format=target_fmt)
 
1438
        target.lock_write()
 
1439
        self.addCleanup(target.unlock)
 
1440
        source = source_tree.branch.repository
 
1441
        self.orig_pack = target.pack
 
1442
        target.pack = self.log_pack
 
1443
        target.fetch(source)
 
1444
        if expect_pack_called:
 
1445
            self.assertLength(1, self.calls)
 
1446
        else:
 
1447
            self.assertLength(0, self.calls)
 
1448
 
 
1449
    def test_sink_format_hint_no(self):
 
1450
        # When the target format says packing makes no difference, pack is not
 
1451
        # called.
 
1452
        self.run_stream('1.9', 'rich-root-pack', False)
 
1453
 
 
1454
    def test_sink_format_hint_yes(self):
 
1455
        # When the target format says packing makes a difference, pack is
 
1456
        # called.
 
1457
        self.run_stream('1.9', '2a', True)
 
1458
 
 
1459
    def test_sink_format_same_no(self):
 
1460
        # When the formats are the same, pack is not called.
 
1461
        self.run_stream('2a', '2a', False)
 
1462
 
 
1463
    def test_IDS_format_hint_no(self):
 
1464
        # When the target format says packing makes no difference, pack is not
 
1465
        # called.
 
1466
        self.run_fetch('1.9', 'rich-root-pack', False)
 
1467
 
 
1468
    def test_IDS_format_hint_yes(self):
 
1469
        # When the target format says packing makes a difference, pack is
 
1470
        # called.
 
1471
        self.run_fetch('1.9', '2a', True)
 
1472
 
 
1473
    def test_IDS_format_same_no(self):
 
1474
        # When the formats are the same, pack is not called.
 
1475
        self.run_fetch('2a', '2a', False)