21
21
# TODO: might be nice to create a versionedfile with some type of corruption
22
22
# considered typical and check that it can be detected/corrected.
24
from gzip import GzipFile
24
25
from itertools import chain, izip
25
26
from StringIO import StringIO
27
28
from bzrlib import (
34
38
from bzrlib.errors import (
35
39
RevisionNotPresent,
36
40
RevisionAlreadyPresent,
39
42
from bzrlib.knit import (
46
47
from bzrlib.tests import (
48
49
TestCaseWithMemoryTransport,
52
split_suite_by_condition,
55
53
from bzrlib.tests.http_utils import TestCaseWithWebserver
56
from bzrlib.trace import mutter
57
from bzrlib.transport import get_transport
58
54
from bzrlib.transport.memory import MemoryTransport
59
from bzrlib.tsort import topo_sort
60
from bzrlib.tuned_gzip import GzipFile
61
55
import bzrlib.versionedfile as versionedfile
62
56
from bzrlib.versionedfile import (
67
61
make_versioned_files_factory,
69
63
from bzrlib.weave import WeaveFile
70
from bzrlib.weavefile import read_weave, write_weave
73
def load_tests(standard_tests, module, loader):
74
"""Parameterize VersionedFiles tests for different implementations."""
75
to_adapt, result = split_suite_by_condition(
76
standard_tests, condition_isinstance(TestVersionedFiles))
77
# We want to be sure of behaviour for:
78
# weaves prefix layout (weave texts)
79
# individually named weaves (weave inventories)
80
# annotated knits - prefix|hash|hash-escape layout, we test the third only
81
# as it is the most complex mapper.
82
# individually named knits
83
# individual no-graph knits in packs (signatures)
84
# individual graph knits in packs (inventories)
85
# individual graph nocompression knits in packs (revisions)
86
# plain text knits in packs (texts)
90
'factory':make_versioned_files_factory(WeaveFile,
91
ConstantMapper('inventory')),
94
'support_partial_insertion': False,
98
'factory':make_file_factory(False, ConstantMapper('revisions')),
101
'support_partial_insertion': False,
103
('named-nograph-nodelta-knit-pack', {
104
'cleanup':cleanup_pack_knit,
105
'factory':make_pack_factory(False, False, 1),
108
'support_partial_insertion': False,
110
('named-graph-knit-pack', {
111
'cleanup':cleanup_pack_knit,
112
'factory':make_pack_factory(True, True, 1),
115
'support_partial_insertion': True,
117
('named-graph-nodelta-knit-pack', {
118
'cleanup':cleanup_pack_knit,
119
'factory':make_pack_factory(True, False, 1),
122
'support_partial_insertion': False,
124
('groupcompress-nograph', {
125
'cleanup':groupcompress.cleanup_pack_group,
126
'factory':groupcompress.make_pack_factory(False, False, 1),
129
'support_partial_insertion':False,
132
len_two_scenarios = [
135
'factory':make_versioned_files_factory(WeaveFile,
139
'support_partial_insertion': False,
141
('annotated-knit-escape', {
143
'factory':make_file_factory(True, HashEscapedPrefixMapper()),
146
'support_partial_insertion': False,
148
('plain-knit-pack', {
149
'cleanup':cleanup_pack_knit,
150
'factory':make_pack_factory(True, True, 2),
153
'support_partial_insertion': True,
156
'cleanup':groupcompress.cleanup_pack_group,
157
'factory':groupcompress.make_pack_factory(True, False, 1),
160
'support_partial_insertion':False,
163
scenarios = len_one_scenarios + len_two_scenarios
164
return multiply_tests(to_adapt, scenarios, result)
64
from bzrlib.weavefile import write_weave
65
from bzrlib.tests.scenarios import load_tests_apply_scenarios
68
load_tests = load_tests_apply_scenarios
167
71
def get_diamond_vf(f, trailing_eol=True, left_only=False):
848
751
self.assertEquals(('references_ghost', 'line_c\n'), origins[2])
850
753
def test_readonly_mode(self):
851
transport = get_transport(self.get_url('.'))
754
t = self.get_transport()
852
755
factory = self.get_factory()
853
vf = factory('id', transport, 0777, create=True, access_mode='w')
854
vf = factory('id', transport, access_mode='r')
756
vf = factory('id', t, 0777, create=True, access_mode='w')
757
vf = factory('id', t, access_mode='r')
855
758
self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
856
759
self.assertRaises(errors.ReadOnlyError,
857
760
vf.add_lines_with_ghosts,
879
782
class TestWeave(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
881
784
def get_file(self, name='foo'):
882
return WeaveFile(name, get_transport(self.get_url('.')), create=True,
883
get_scope=self.get_transaction)
785
return WeaveFile(name, self.get_transport(),
787
get_scope=self.get_transaction)
885
789
def get_file_corrupted_text(self):
886
w = WeaveFile('foo', get_transport(self.get_url('.')), create=True,
887
get_scope=self.get_transaction)
790
w = WeaveFile('foo', self.get_transport(),
792
get_scope=self.get_transaction)
888
793
w.add_lines('v1', [], ['hello\n'])
889
794
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
920
825
def reopen_file(self, name='foo', create=False):
921
return WeaveFile(name, get_transport(self.get_url('.')), create=create,
922
get_scope=self.get_transaction)
826
return WeaveFile(name, self.get_transport(),
828
get_scope=self.get_transaction)
924
830
def test_no_implicit_create(self):
925
831
self.assertRaises(errors.NoSuchFile,
928
get_transport(self.get_url('.')),
834
self.get_transport(),
929
835
get_scope=self.get_transaction)
931
837
def get_factory(self):
998
904
# we should be able to read from http with a versioned file.
999
905
vf = self.get_file()
1000
906
# try an empty file access
1001
readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
907
readonly_vf = self.get_factory()('foo',
908
transport.get_transport_from_url(self.get_readonly_url('.')))
1002
909
self.assertEqual([], readonly_vf.versions())
911
def test_readonly_http_works_with_feeling(self):
912
# we should be able to read from http with a versioned file.
1003
914
# now with feeling.
1004
915
vf.add_lines('1', [], ['a\n'])
1005
916
vf.add_lines('2', ['1'], ['b\n', 'a\n'])
1006
readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
917
readonly_vf = self.get_factory()('foo',
918
transport.get_transport_from_url(self.get_readonly_url('.')))
1007
919
self.assertEqual(['1', '2'], vf.versions())
920
self.assertEqual(['1', '2'], readonly_vf.versions())
1008
921
for version in readonly_vf.versions():
1009
922
readonly_vf.get_lines(version)
1462
1377
class TestVersionedFiles(TestCaseWithMemoryTransport):
1463
1378
"""Tests for the multiple-file variant of VersionedFile."""
1380
# We want to be sure of behaviour for:
1381
# weaves prefix layout (weave texts)
1382
# individually named weaves (weave inventories)
1383
# annotated knits - prefix|hash|hash-escape layout, we test the third only
1384
# as it is the most complex mapper.
1385
# individually named knits
1386
# individual no-graph knits in packs (signatures)
1387
# individual graph knits in packs (inventories)
1388
# individual graph nocompression knits in packs (revisions)
1389
# plain text knits in packs (texts)
1390
len_one_scenarios = [
1393
'factory':make_versioned_files_factory(WeaveFile,
1394
ConstantMapper('inventory')),
1397
'support_partial_insertion': False,
1401
'factory':make_file_factory(False, ConstantMapper('revisions')),
1404
'support_partial_insertion': False,
1406
('named-nograph-nodelta-knit-pack', {
1407
'cleanup':cleanup_pack_knit,
1408
'factory':make_pack_factory(False, False, 1),
1411
'support_partial_insertion': False,
1413
('named-graph-knit-pack', {
1414
'cleanup':cleanup_pack_knit,
1415
'factory':make_pack_factory(True, True, 1),
1418
'support_partial_insertion': True,
1420
('named-graph-nodelta-knit-pack', {
1421
'cleanup':cleanup_pack_knit,
1422
'factory':make_pack_factory(True, False, 1),
1425
'support_partial_insertion': False,
1427
('groupcompress-nograph', {
1428
'cleanup':groupcompress.cleanup_pack_group,
1429
'factory':groupcompress.make_pack_factory(False, False, 1),
1432
'support_partial_insertion':False,
1435
len_two_scenarios = [
1438
'factory':make_versioned_files_factory(WeaveFile,
1442
'support_partial_insertion': False,
1444
('annotated-knit-escape', {
1446
'factory':make_file_factory(True, HashEscapedPrefixMapper()),
1449
'support_partial_insertion': False,
1451
('plain-knit-pack', {
1452
'cleanup':cleanup_pack_knit,
1453
'factory':make_pack_factory(True, True, 2),
1456
'support_partial_insertion': True,
1459
'cleanup':groupcompress.cleanup_pack_group,
1460
'factory':groupcompress.make_pack_factory(True, False, 1),
1463
'support_partial_insertion':False,
1467
scenarios = len_one_scenarios + len_two_scenarios
1465
1469
def get_versionedfiles(self, relpath='files'):
1466
1470
transport = self.get_transport(relpath)
1467
1471
if relpath != '.':
1468
1472
transport.mkdir('.')
1469
1473
files = self.factory(transport)
1470
1474
if self.cleanup is not None:
1471
self.addCleanup(lambda:self.cleanup(files))
1475
self.addCleanup(self.cleanup, files)
1474
1478
def get_simple_key(self, suffix):
1479
1483
return ('FileA',) + (suffix,)
1485
def test_add_fallback_implies_without_fallbacks(self):
1486
f = self.get_versionedfiles('files')
1487
if getattr(f, 'add_fallback_versioned_files', None) is None:
1488
raise TestNotApplicable("%s doesn't support fallbacks"
1489
% (f.__class__.__name__,))
1490
g = self.get_versionedfiles('fallback')
1491
key_a = self.get_simple_key('a')
1492
g.add_lines(key_a, [], ['\n'])
1493
f.add_fallback_versioned_files(g)
1494
self.assertTrue(key_a in f.get_parent_map([key_a]))
1495
self.assertFalse(key_a in f.without_fallbacks().get_parent_map([key_a]))
1481
1497
def test_add_lines(self):
1482
1498
f = self.get_versionedfiles()
1483
1499
key0 = self.get_simple_key('r0')
1557
1573
self.assertRaises(RevisionNotPresent,
1558
1574
files.annotate, prefix + ('missing-key',))
1560
def test_get_annotator(self):
1576
def test_check_no_parameters(self):
1577
files = self.get_versionedfiles()
1579
def test_check_progressbar_parameter(self):
1580
"""A progress bar can be supplied because check can be a generator."""
1581
pb = ui.ui_factory.nested_progress_bar()
1582
self.addCleanup(pb.finished)
1583
files = self.get_versionedfiles()
1584
files.check(progress_bar=pb)
1586
def test_check_with_keys_becomes_generator(self):
1561
1587
files = self.get_versionedfiles()
1562
1588
self.get_diamond_files(files)
1563
origin_key = self.get_simple_key('origin')
1564
base_key = self.get_simple_key('base')
1565
left_key = self.get_simple_key('left')
1566
right_key = self.get_simple_key('right')
1567
merged_key = self.get_simple_key('merged')
1568
# annotator = files.get_annotator()
1569
# introduced full text
1570
origins, lines = files.get_annotator().annotate(origin_key)
1571
self.assertEqual([(origin_key,)], origins)
1572
self.assertEqual(['origin\n'], lines)
1574
origins, lines = files.get_annotator().annotate(base_key)
1575
self.assertEqual([(base_key,)], origins)
1577
origins, lines = files.get_annotator().annotate(merged_key)
1586
# Without a graph everything is new.
1593
self.assertRaises(RevisionNotPresent,
1594
files.get_annotator().annotate, self.get_simple_key('missing-key'))
1590
entries = files.check(keys=keys)
1592
# Texts output should be fulltexts.
1593
self.capture_stream(files, entries, seen.add,
1594
files.get_parent_map(keys), require_fulltext=True)
1595
# All texts should be output.
1596
self.assertEqual(set(keys), seen)
1598
def test_clear_cache(self):
1599
files = self.get_versionedfiles()
1596
1602
def test_construct(self):
1597
1603
"""Each parameterised test can be constructed on a transport."""
1750
1756
f.get_record_stream([key_b], 'unordered', True
1751
1757
).next().get_bytes_as('fulltext'))
1759
def test_get_known_graph_ancestry(self):
1760
f = self.get_versionedfiles()
1762
raise TestNotApplicable('ancestry info only relevant with graph.')
1763
key_a = self.get_simple_key('a')
1764
key_b = self.get_simple_key('b')
1765
key_c = self.get_simple_key('c')
1771
f.add_lines(key_a, [], ['\n'])
1772
f.add_lines(key_b, [key_a], ['\n'])
1773
f.add_lines(key_c, [key_a, key_b], ['\n'])
1774
kg = f.get_known_graph_ancestry([key_c])
1775
self.assertIsInstance(kg, _mod_graph.KnownGraph)
1776
self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1778
def test_known_graph_with_fallbacks(self):
1779
f = self.get_versionedfiles('files')
1781
raise TestNotApplicable('ancestry info only relevant with graph.')
1782
if getattr(f, 'add_fallback_versioned_files', None) is None:
1783
raise TestNotApplicable("%s doesn't support fallbacks"
1784
% (f.__class__.__name__,))
1785
key_a = self.get_simple_key('a')
1786
key_b = self.get_simple_key('b')
1787
key_c = self.get_simple_key('c')
1788
# A only in fallback
1793
g = self.get_versionedfiles('fallback')
1794
g.add_lines(key_a, [], ['\n'])
1795
f.add_fallback_versioned_files(g)
1796
f.add_lines(key_b, [key_a], ['\n'])
1797
f.add_lines(key_c, [key_a, key_b], ['\n'])
1798
kg = f.get_known_graph_ancestry([key_c])
1799
self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1753
1801
def test_get_record_stream_empty(self):
1754
1802
"""An empty stream can be requested without error."""
1755
1803
f = self.get_versionedfiles()
2211
def test_get_annotator(self):
2212
files = self.get_versionedfiles()
2213
self.get_diamond_files(files)
2214
origin_key = self.get_simple_key('origin')
2215
base_key = self.get_simple_key('base')
2216
left_key = self.get_simple_key('left')
2217
right_key = self.get_simple_key('right')
2218
merged_key = self.get_simple_key('merged')
2219
# annotator = files.get_annotator()
2220
# introduced full text
2221
origins, lines = files.get_annotator().annotate(origin_key)
2222
self.assertEqual([(origin_key,)], origins)
2223
self.assertEqual(['origin\n'], lines)
2225
origins, lines = files.get_annotator().annotate(base_key)
2226
self.assertEqual([(base_key,)], origins)
2228
origins, lines = files.get_annotator().annotate(merged_key)
2237
# Without a graph everything is new.
2244
self.assertRaises(RevisionNotPresent,
2245
files.get_annotator().annotate, self.get_simple_key('missing-key'))
2160
2247
def test_get_parent_map(self):
2161
2248
files = self.get_versionedfiles()
2162
2249
if self.key_length == 1:
2366
2453
self.assertIdenticalVersionedFile(source, files)
2455
def test_insert_record_stream_long_parent_chain_out_of_order(self):
2456
"""An out of order stream can either error or work."""
2458
raise TestNotApplicable('ancestry info only relevant with graph.')
2459
# Create a reasonably long chain of records based on each other, where
2460
# most will be deltas.
2461
source = self.get_versionedfiles('source')
2464
content = [('same same %d\n' % n) for n in range(500)]
2465
for letter in 'abcdefghijklmnopqrstuvwxyz':
2466
key = ('key-' + letter,)
2467
if self.key_length == 2:
2468
key = ('prefix',) + key
2469
content.append('content for ' + letter + '\n')
2470
source.add_lines(key, parents, content)
2473
# Create a stream of these records, excluding the first record that the
2474
# rest ultimately depend upon, and insert it into a new vf.
2476
for key in reversed(keys):
2477
streams.append(source.get_record_stream([key], 'unordered', False))
2478
deltas = chain(*streams[:-1])
2479
files = self.get_versionedfiles()
2481
files.insert_record_stream(deltas)
2482
except RevisionNotPresent:
2483
# Must not have corrupted the file.
2486
# Must only report either just the first key as a missing parent,
2487
# no key as missing (for nodelta scenarios).
2488
missing = set(files.get_missing_compression_parent_keys())
2489
missing.discard(keys[0])
2490
self.assertEqual(set(), missing)
2368
2492
def get_knit_delta_source(self):
2369
2493
"""Get a source that can produce a stream with knit delta records,
2370
2494
regardless of this test's scenario.