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
22
17
from bzrlib import (
30
23
from bzrlib.branch import Branch
31
from bzrlib.bzrdir import BzrDir
32
24
from bzrlib.repofmt import knitrepo
33
25
from bzrlib.tests import TestCaseWithTransport
34
from bzrlib.tests.http_utils import TestCaseWithWebserver
35
26
from bzrlib.tests.test_revision import make_branches
36
from bzrlib.trace import mutter
37
27
from bzrlib.upgrade import Convert
38
28
from bzrlib.workingtree import WorkingTree
40
30
# These tests are a bit old; please instead add new tests into
41
# interrepository_implementations/ so they'll run on all relevant
31
# per_interrepository/ so they'll run on all relevant
48
38
def fetch_steps(self, br_a, br_b, writable_a):
49
39
"""A foreign test method for testing fetch locally and remotely."""
51
41
# TODO RBC 20060201 make this a repository test.
52
42
repo_b = br_b.repository
53
43
self.assertFalse(repo_b.has_revision(br_a.revision_history()[3]))
54
44
self.assertTrue(repo_b.has_revision(br_a.revision_history()[2]))
55
45
self.assertEquals(len(br_b.revision_history()), 7)
56
self.assertEquals(br_b.fetch(br_a, br_a.revision_history()[2])[0], 0)
46
br_b.fetch(br_a, br_a.revision_history()[2])
57
47
# branch.fetch is not supposed to alter the revision history
58
48
self.assertEquals(len(br_b.revision_history()), 7)
59
49
self.assertFalse(repo_b.has_revision(br_a.revision_history()[3]))
61
51
# fetching the next revision up in sample data copies one revision
62
self.assertEquals(br_b.fetch(br_a, br_a.revision_history()[3])[0], 1)
52
br_b.fetch(br_a, br_a.revision_history()[3])
63
53
self.assertTrue(repo_b.has_revision(br_a.revision_history()[3]))
64
54
self.assertFalse(has_revision(br_a, br_b.revision_history()[6]))
65
55
self.assertTrue(br_a.repository.has_revision(br_b.revision_history()[5]))
67
57
# When a non-branch ancestor is missing, it should be unlisted...
68
58
# as its not reference from the inventory weave.
69
59
br_b4 = self.make_branch('br_4')
70
count, failures = br_b4.fetch(br_b)
71
self.assertEqual(count, 7)
72
self.assertEqual(failures, [])
74
self.assertEqual(writable_a.fetch(br_b)[0], 1)
62
writable_a.fetch(br_b)
75
63
self.assertTrue(has_revision(br_a, br_b.revision_history()[3]))
76
64
self.assertTrue(has_revision(br_a, br_b.revision_history()[4]))
78
66
br_b2 = self.make_branch('br_b2')
79
self.assertEquals(br_b2.fetch(br_b)[0], 7)
80
68
self.assertTrue(has_revision(br_b2, br_b.revision_history()[4]))
81
69
self.assertTrue(has_revision(br_b2, br_a.revision_history()[2]))
82
70
self.assertFalse(has_revision(br_b2, br_a.revision_history()[3]))
84
72
br_a2 = self.make_branch('br_a2')
85
self.assertEquals(br_a2.fetch(br_a)[0], 9)
86
74
self.assertTrue(has_revision(br_a2, br_b.revision_history()[4]))
87
75
self.assertTrue(has_revision(br_a2, br_a.revision_history()[3]))
88
76
self.assertTrue(has_revision(br_a2, br_a.revision_history()[2]))
90
78
br_a3 = self.make_branch('br_a3')
91
# pulling a branch with no revisions grabs nothing, regardless of
79
# pulling a branch with no revisions grabs nothing, regardless of
92
80
# whats in the inventory.
93
self.assertEquals(br_a3.fetch(br_a2)[0], 0)
94
82
for revno in range(4):
96
84
br_a3.repository.has_revision(br_a.revision_history()[revno]))
97
self.assertEqual(br_a3.fetch(br_a2, br_a.revision_history()[2])[0], 3)
85
br_a3.fetch(br_a2, br_a.revision_history()[2])
98
86
# pull the 3 revisions introduced by a@u-0-3
99
fetched = br_a3.fetch(br_a2, br_a.revision_history()[3])[0]
100
self.assertEquals(fetched, 3, "fetched %d instead of 3" % fetched)
101
# InstallFailed should be raised if the branch is missing the revision
87
br_a3.fetch(br_a2, br_a.revision_history()[3])
88
# NoSuchRevision should be raised if the branch is missing the revision
102
89
# that was requested.
103
self.assertRaises(errors.InstallFailed, br_a3.fetch, br_a2, 'pizza')
90
self.assertRaises(errors.NoSuchRevision, br_a3.fetch, br_a2, 'pizza')
105
92
# TODO: Test trying to fetch from a branch that points to a revision not
106
93
# actually present in its repository. Not every branch format allows you
251
238
rev_id).get_file_text('this-file-id'), text)
254
class TestHttpFetch(TestCaseWithWebserver):
255
# FIXME RBC 20060124 this really isn't web specific, perhaps an
256
# instrumented readonly transport? Can we do an instrumented
257
# adapter and use self.get_readonly_url ?
259
def test_fetch(self):
260
#highest indices a: 5, b: 7
261
br_a, br_b = make_branches(self)
262
br_rem_a = Branch.open(self.get_readonly_url('branch1'))
263
fetch_steps(self, br_rem_a, br_b, br_a)
265
def _count_log_matches(self, target, logs):
266
"""Count the number of times the target file pattern was fetched in an http log"""
267
get_succeeds_re = re.compile(
268
'.*"GET .*%s HTTP/1.1" 20[06] - "-" "bzr/%s' %
269
( target, bzrlib.__version__))
272
if get_succeeds_re.match(line):
276
def test_weaves_are_retrieved_once(self):
277
self.build_tree(("source/", "source/file", "target/"))
278
# This test depends on knit dasta storage.
279
wt = self.make_branch_and_tree('source', format='dirstate-tags')
281
wt.add(["file"], ["id"])
282
wt.commit("added file")
283
open("source/file", 'w').write("blah\n")
284
wt.commit("changed file")
285
target = BzrDir.create_branch_and_repo("target/")
286
source = Branch.open(self.get_readonly_url("source/"))
287
self.assertEqual(target.fetch(source), (2, []))
288
# this is the path to the literal file. As format changes
289
# occur it needs to be updated. FIXME: ask the store for the
291
self.log("web server logs are:")
292
http_logs = self.get_readonly_server().logs
293
self.log('\n'.join(http_logs))
294
# unfortunately this log entry is branch format specific. We could
295
# factor out the 'what files does this format use' to a method on the
296
# repository, which would let us to this generically. RBC 20060419
297
# RBC 20080408: Or perhaps we can assert that no files are fully read
299
self.assertEqual(1, self._count_log_matches('/ce/id.kndx', http_logs))
300
self.assertEqual(1, self._count_log_matches('/ce/id.knit', http_logs))
301
self.assertEqual(1, self._count_log_matches('inventory.kndx', http_logs))
302
# this r-h check test will prevent regressions, but it currently already
303
# passes, before the patch to cache-rh is applied :[
304
self.assertTrue(1 >= self._count_log_matches('revision-history',
306
self.assertTrue(1 >= self._count_log_matches('last-revision',
308
# FIXME naughty poking in there.
309
self.get_readonly_server().logs = []
310
# check there is nothing more to fetch. We take care to re-use the
311
# existing transport so that the request logs we're about to examine
312
# aren't cluttered with redundant probes for a smart server.
313
# XXX: Perhaps this further parameterisation: test http with smart
314
# server, and test http without smart server?
315
source = Branch.open(
316
self.get_readonly_url("source/"),
317
possible_transports=[source.bzrdir.root_transport])
318
self.assertEqual(target.fetch(source), (0, []))
319
# should make just two requests
320
http_logs = self.get_readonly_server().logs
321
self.log("web server logs are:")
322
self.log('\n'.join(http_logs))
323
self.assertEqual(1, self._count_log_matches('branch-format', http_logs))
324
self.assertEqual(1, self._count_log_matches('branch/format', http_logs))
325
self.assertEqual(1, self._count_log_matches('repository/format',
327
self.assertTrue(1 >= self._count_log_matches('revision-history',
329
self.assertTrue(1 >= self._count_log_matches('last-revision',
331
self.assertEqual(4, len(http_logs))
334
241
class TestKnitToPackFetch(TestCaseWithTransport):
336
def find_get_record_stream(self, calls):
337
"""In a list of calls, find 'get_record_stream' calls.
243
def find_get_record_stream(self, calls, expected_count=1):
244
"""In a list of calls, find the last 'get_record_stream'.
339
This also ensures that there is only one get_record_stream call.
246
:param expected_count: The number of calls we should exepect to find.
247
If a different number is found, an assertion is raised.
341
249
get_record_call = None
342
251
for call in calls:
343
252
if call[0] == 'get_record_stream':
344
self.assertIs(None, get_record_call,
345
"there should only be one call to"
346
" get_record_stream")
347
254
get_record_call = call
348
self.assertIsNot(None, get_record_call,
349
"there should be exactly one call to "
350
" get_record_stream")
255
self.assertEqual(expected_count, call_count)
351
256
return get_record_call
353
258
def test_fetch_with_deltas_no_delta_closure(self):
367
272
source.inventories = versionedfile.RecordingVersionedFilesDecorator(
368
273
source.inventories)
370
self.assertTrue(target._fetch_uses_deltas)
275
self.assertTrue(target._format._fetch_uses_deltas)
371
276
target.fetch(source, revision_id='rev-one')
372
277
self.assertEqual(('get_record_stream', [('file-id', 'rev-one')],
373
target._fetch_order, False),
278
target._format._fetch_order, False),
374
279
self.find_get_record_stream(source.texts.calls))
375
280
self.assertEqual(('get_record_stream', [('rev-one',)],
376
target._fetch_order, False),
377
self.find_get_record_stream(source.inventories.calls))
281
target._format._fetch_order, False),
282
self.find_get_record_stream(source.inventories.calls, 2))
378
283
self.assertEqual(('get_record_stream', [('rev-one',)],
379
target._fetch_order, False),
284
target._format._fetch_order, False),
380
285
self.find_get_record_stream(source.revisions.calls))
381
286
# XXX: Signatures is special, and slightly broken. The
382
287
# standard item_keys_introduced_by actually does a lookup for every
406
311
source.revisions)
407
312
source.inventories = versionedfile.RecordingVersionedFilesDecorator(
408
313
source.inventories)
409
target._fetch_uses_deltas = False
314
# XXX: This won't work in general, but for the dirstate format it does.
315
self.overrideAttr(target._format, '_fetch_uses_deltas', False)
410
316
target.fetch(source, revision_id='rev-one')
411
317
self.assertEqual(('get_record_stream', [('file-id', 'rev-one')],
412
target._fetch_order, True),
318
target._format._fetch_order, True),
413
319
self.find_get_record_stream(source.texts.calls))
414
320
self.assertEqual(('get_record_stream', [('rev-one',)],
415
target._fetch_order, True),
416
self.find_get_record_stream(source.inventories.calls))
321
target._format._fetch_order, True),
322
self.find_get_record_stream(source.inventories.calls, 2))
417
323
self.assertEqual(('get_record_stream', [('rev-one',)],
418
target._fetch_order, True),
324
target._format._fetch_order, True),
419
325
self.find_get_record_stream(source.revisions.calls))
420
326
# XXX: Signatures is special, and slightly broken. The
421
327
# standard item_keys_introduced_by actually does a lookup for every