1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# Copyright (C) 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for repositories that support CHK indices."""
from bzrlib import (
btree_index,
errors,
osutils,
repository,
)
from bzrlib.remote import RemoteRepository
from bzrlib.versionedfile import VersionedFiles
from bzrlib.tests import TestNotApplicable
from bzrlib.tests.per_repository_chk import TestCaseWithRepositoryCHK
class TestCHKSupport(TestCaseWithRepositoryCHK):
def test_chk_bytes_attribute_is_VersionedFiles(self):
repo = self.make_repository('.')
self.assertIsInstance(repo.chk_bytes, VersionedFiles)
def test_add_bytes_to_chk_bytes_store(self):
repo = self.make_repository('.')
repo.lock_write()
try:
repo.start_write_group()
try:
sha1, len, _ = repo.chk_bytes.add_lines((None,),
None, ["foo\n", "bar\n"], random_id=True)
self.assertEqual('4e48e2c9a3d2ca8a708cb0cc545700544efb5021',
sha1)
self.assertEqual(
set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
repo.chk_bytes.keys())
except:
repo.abort_write_group()
raise
else:
repo.commit_write_group()
finally:
repo.unlock()
# And after an unlock/lock pair
repo.lock_read()
try:
self.assertEqual(
set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
repo.chk_bytes.keys())
finally:
repo.unlock()
# and reopening
repo = repo.bzrdir.open_repository()
repo.lock_read()
try:
self.assertEqual(
set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
repo.chk_bytes.keys())
finally:
repo.unlock()
def test_pack_preserves_chk_bytes_store(self):
leaf_lines = ["chkleaf:\n", "0\n", "1\n", "0\n", "\n"]
leaf_sha1 = osutils.sha_strings(leaf_lines)
node_lines = ["chknode:\n", "0\n", "1\n", "1\n", "foo\n",
"\x00sha1:%s\n" % (leaf_sha1,)]
node_sha1 = osutils.sha_strings(node_lines)
expected_set = set([('sha1:' + leaf_sha1,), ('sha1:' + node_sha1,)])
repo = self.make_repository('.')
repo.lock_write()
try:
repo.start_write_group()
try:
# Internal node pointing at a leaf.
repo.chk_bytes.add_lines((None,), None, node_lines, random_id=True)
except:
repo.abort_write_group()
raise
else:
repo.commit_write_group()
repo.start_write_group()
try:
# Leaf in a separate pack.
repo.chk_bytes.add_lines((None,), None, leaf_lines, random_id=True)
except:
repo.abort_write_group()
raise
else:
repo.commit_write_group()
repo.pack()
self.assertEqual(expected_set, repo.chk_bytes.keys())
finally:
repo.unlock()
# and reopening
repo = repo.bzrdir.open_repository()
repo.lock_read()
try:
self.assertEqual(expected_set, repo.chk_bytes.keys())
finally:
repo.unlock()
def test_chk_bytes_are_fully_buffered(self):
repo = self.make_repository('.')
repo.lock_write()
self.addCleanup(repo.unlock)
repo.start_write_group()
try:
sha1, len, _ = repo.chk_bytes.add_lines((None,),
None, ["foo\n", "bar\n"], random_id=True)
self.assertEqual('4e48e2c9a3d2ca8a708cb0cc545700544efb5021',
sha1)
self.assertEqual(
set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
repo.chk_bytes.keys())
except:
repo.abort_write_group()
raise
else:
repo.commit_write_group()
# This may not always be correct if we change away from BTreeGraphIndex
# in the future. But for now, lets check that chk_bytes are fully
# buffered
index = repo.chk_bytes._index._graph_index._indices[0]
self.assertIsInstance(index, btree_index.BTreeGraphIndex)
self.assertIs(type(index._leaf_node_cache), dict)
# Re-opening the repository should also have a repo with everything
# fully buffered
repo2 = repository.Repository.open(self.get_url())
repo2.lock_read()
self.addCleanup(repo2.unlock)
index = repo2.chk_bytes._index._graph_index._indices[0]
self.assertIsInstance(index, btree_index.BTreeGraphIndex)
self.assertIs(type(index._leaf_node_cache), dict)
class TestCommitWriteGroupIntegrityCheck(TestCaseWithRepositoryCHK):
"""Tests that commit_write_group prevents various kinds of invalid data
from being committed to a CHK repository.
"""
def reopen_repo_and_resume_write_group(self, repo):
resume_tokens = repo.suspend_write_group()
repo.unlock()
reopened_repo = repo.bzrdir.open_repository()
reopened_repo.lock_write()
self.addCleanup(reopened_repo.unlock)
reopened_repo.resume_write_group(resume_tokens)
return reopened_repo
def test_missing_chk_root_for_inventory(self):
"""commit_write_group fails with BzrCheckError when the chk root record
for a new inventory is missing.
"""
repo = self.make_repository('damaged-repo')
builder = self.make_branch_builder('simple-branch')
builder.build_snapshot('A-id', None, [
('add', ('', 'root-id', 'directory', None)),
('add', ('file', 'file-id', 'file', 'content\n'))])
b = builder.get_branch()
b.lock_read()
self.addCleanup(b.unlock)
repo.lock_write()
repo.start_write_group()
# Now, add the objects manually
text_keys = [('file-id', 'A-id'), ('root-id', 'A-id')]
# Directly add the texts, inventory, and revision object for 'A-id' --
# but don't add the chk_bytes.
src_repo = b.repository
repo.texts.insert_record_stream(src_repo.texts.get_record_stream(
text_keys, 'unordered', True))
repo.inventories.insert_record_stream(
src_repo.inventories.get_record_stream(
[('A-id',)], 'unordered', True))
repo.revisions.insert_record_stream(
src_repo.revisions.get_record_stream(
[('A-id',)], 'unordered', True))
# Make sure the presence of the missing data in a fallback does not
# avoid the error.
repo.add_fallback_repository(b.repository)
self.assertRaises(errors.BzrCheckError, repo.commit_write_group)
reopened_repo = self.reopen_repo_and_resume_write_group(repo)
self.assertRaises(
errors.BzrCheckError, reopened_repo.commit_write_group)
reopened_repo.abort_write_group()
def test_missing_chk_root_for_unchanged_inventory(self):
"""commit_write_group fails with BzrCheckError when the chk root record
for a new inventory is missing, even if the parent inventory is present
and has identical content (i.e. the same chk root).
A stacked repository containing only a revision with an identical
inventory to its parent will still have the chk root records for those
inventories.
(In principle the chk records are unnecessary in this case, but in
practice bzr 2.0rc1 (at least) expects to find them.)
"""
repo = self.make_repository('damaged-repo')
# Make a branch where the last two revisions have identical
# inventories.
builder = self.make_branch_builder('simple-branch')
builder.build_snapshot('A-id', None, [
('add', ('', 'root-id', 'directory', None)),
('add', ('file', 'file-id', 'file', 'content\n'))])
builder.build_snapshot('B-id', None, [])
builder.build_snapshot('C-id', None, [])
b = builder.get_branch()
b.lock_read()
self.addCleanup(b.unlock)
# check our setup: B-id and C-id should have identical chk root keys.
inv_b = b.repository.get_inventory('B-id')
inv_c = b.repository.get_inventory('C-id')
if not isinstance(repo, RemoteRepository):
# Remote repositories always return plain inventories
self.assertEqual(inv_b.id_to_entry.key(), inv_c.id_to_entry.key())
# Now, manually insert objects for a stacked repo with only revision
# C-id:
# We need ('revisions', 'C-id'), ('inventories', 'C-id'),
# ('inventories', 'B-id'), and the corresponding chk roots for those
# inventories.
repo.lock_write()
repo.start_write_group()
src_repo = b.repository
repo.inventories.insert_record_stream(
src_repo.inventories.get_record_stream(
[('B-id',), ('C-id',)], 'unordered', True))
repo.revisions.insert_record_stream(
src_repo.revisions.get_record_stream(
[('C-id',)], 'unordered', True))
# Make sure the presence of the missing data in a fallback does not
# avoid the error.
repo.add_fallback_repository(b.repository)
self.assertRaises(errors.BzrCheckError, repo.commit_write_group)
reopened_repo = self.reopen_repo_and_resume_write_group(repo)
self.assertRaises(
errors.BzrCheckError, reopened_repo.commit_write_group)
reopened_repo.abort_write_group()
def test_missing_chk_leaf_for_inventory(self):
"""commit_write_group fails with BzrCheckError when the chk root record
for a parent inventory of a new revision is missing.
"""
repo = self.make_repository('damaged-repo')
if isinstance(repo, RemoteRepository):
raise TestNotApplicable(
"Unable to obtain CHKInventory from remote repo")
b = self.make_branch_with_multiple_chk_nodes()
src_repo = b.repository
src_repo.lock_read()
self.addCleanup(src_repo.unlock)
# Now, manually insert objects for a stacked repo with only revision
# C-id, *except* drop the non-root chk records.
inv_b = src_repo.get_inventory('B-id')
inv_c = src_repo.get_inventory('C-id')
chk_root_keys_only = [
inv_b.id_to_entry.key(), inv_b.parent_id_basename_to_file_id.key(),
inv_c.id_to_entry.key(), inv_c.parent_id_basename_to_file_id.key()]
all_chks = src_repo.chk_bytes.keys()
# Pick a non-root key to drop
key_to_drop = all_chks.difference(chk_root_keys_only).pop()
all_chks.discard(key_to_drop)
repo.lock_write()
repo.start_write_group()
repo.chk_bytes.insert_record_stream(
src_repo.chk_bytes.get_record_stream(
all_chks, 'unordered', True))
repo.texts.insert_record_stream(
src_repo.texts.get_record_stream(
src_repo.texts.keys(), 'unordered', True))
repo.inventories.insert_record_stream(
src_repo.inventories.get_record_stream(
[('B-id',), ('C-id',)], 'unordered', True))
repo.revisions.insert_record_stream(
src_repo.revisions.get_record_stream(
[('C-id',)], 'unordered', True))
# Make sure the presence of the missing data in a fallback does not
# avoid the error.
repo.add_fallback_repository(b.repository)
self.assertRaises(errors.BzrCheckError, repo.commit_write_group)
reopened_repo = self.reopen_repo_and_resume_write_group(repo)
self.assertRaises(
errors.BzrCheckError, reopened_repo.commit_write_group)
reopened_repo.abort_write_group()
def test_missing_chk_root_for_parent_inventory(self):
"""commit_write_group fails with BzrCheckError when the chk root record
for a parent inventory of a new revision is missing.
"""
repo = self.make_repository('damaged-repo')
if isinstance(repo, RemoteRepository):
raise TestNotApplicable(
"Unable to obtain CHKInventory from remote repo")
b = self.make_branch_with_multiple_chk_nodes()
b.lock_read()
self.addCleanup(b.unlock)
# Now, manually insert objects for a stacked repo with only revision
# C-id, *except* the chk root entry for the parent inventory.
# We need ('revisions', 'C-id'), ('inventories', 'C-id'),
# ('inventories', 'B-id'), and the corresponding chk roots for those
# inventories.
inv_c = b.repository.get_inventory('C-id')
chk_keys_for_c_only = [
inv_c.id_to_entry.key(), inv_c.parent_id_basename_to_file_id.key()]
repo.lock_write()
repo.start_write_group()
src_repo = b.repository
repo.chk_bytes.insert_record_stream(
src_repo.chk_bytes.get_record_stream(
chk_keys_for_c_only, 'unordered', True))
repo.inventories.insert_record_stream(
src_repo.inventories.get_record_stream(
[('B-id',), ('C-id',)], 'unordered', True))
repo.revisions.insert_record_stream(
src_repo.revisions.get_record_stream(
[('C-id',)], 'unordered', True))
# Make sure the presence of the missing data in a fallback does not
# avoid the error.
repo.add_fallback_repository(b.repository)
self.assertRaises(errors.BzrCheckError, repo.commit_write_group)
reopened_repo = self.reopen_repo_and_resume_write_group(repo)
self.assertRaises(
errors.BzrCheckError, reopened_repo.commit_write_group)
reopened_repo.abort_write_group()
def make_branch_with_multiple_chk_nodes(self):
# add and modify files with very long file-ids, so that the chk map
# will need more than just a root node.
builder = self.make_branch_builder('simple-branch')
file_adds = []
file_modifies = []
for char in 'abc':
name = char * 10000
file_adds.append(
('add', ('file-' + name, 'file-%s-id' % name, 'file',
'content %s\n' % name)))
file_modifies.append(
('modify', ('file-%s-id' % name, 'new content %s\n' % name)))
builder.build_snapshot('A-id', None, [
('add', ('', 'root-id', 'directory', None))] +
file_adds)
builder.build_snapshot('B-id', None, [])
builder.build_snapshot('C-id', None, file_modifies)
return builder.get_branch()
def test_missing_text_record(self):
"""commit_write_group fails with BzrCheckError when a text is missing.
"""
repo = self.make_repository('damaged-repo')
b = self.make_branch_with_multiple_chk_nodes()
src_repo = b.repository
src_repo.lock_read()
self.addCleanup(src_repo.unlock)
# Now, manually insert objects for a stacked repo with only revision
# C-id, *except* drop one changed text.
all_texts = src_repo.texts.keys()
all_texts.remove(('file-%s-id' % ('c'*10000,), 'C-id'))
repo.lock_write()
repo.start_write_group()
repo.chk_bytes.insert_record_stream(
src_repo.chk_bytes.get_record_stream(
src_repo.chk_bytes.keys(), 'unordered', True))
repo.texts.insert_record_stream(
src_repo.texts.get_record_stream(
all_texts, 'unordered', True))
repo.inventories.insert_record_stream(
src_repo.inventories.get_record_stream(
[('B-id',), ('C-id',)], 'unordered', True))
repo.revisions.insert_record_stream(
src_repo.revisions.get_record_stream(
[('C-id',)], 'unordered', True))
# Make sure the presence of the missing data in a fallback does not
# avoid the error.
repo.add_fallback_repository(b.repository)
self.assertRaises(errors.BzrCheckError, repo.commit_write_group)
reopened_repo = self.reopen_repo_and_resume_write_group(repo)
self.assertRaises(
errors.BzrCheckError, reopened_repo.commit_write_group)
reopened_repo.abort_write_group()
|