~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_fetch.py

(vila) Fix test failures blocking package builds. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
import os
18
 
import re
19
 
import sys
20
 
 
21
 
import bzrlib
22
17
from bzrlib import (
23
18
    bzrdir,
24
19
    errors,
25
20
    osutils,
26
 
    merge,
27
 
    repository,
 
21
    revision as _mod_revision,
28
22
    versionedfile,
29
23
    )
30
24
from bzrlib.branch import Branch
31
 
from bzrlib.bzrdir import BzrDir
32
25
from bzrlib.repofmt import knitrepo
33
26
from bzrlib.tests import TestCaseWithTransport
34
 
from bzrlib.tests.http_utils import TestCaseWithWebserver
35
27
from bzrlib.tests.test_revision import make_branches
36
 
from bzrlib.trace import mutter
37
28
from bzrlib.upgrade import Convert
38
29
from bzrlib.workingtree import WorkingTree
39
30
 
45
36
def has_revision(branch, revision_id):
46
37
    return branch.repository.has_revision(revision_id)
47
38
 
 
39
 
 
40
def revision_history(branch):
 
41
    branch.lock_read()
 
42
    try:
 
43
        graph = branch.repository.get_graph()
 
44
        history = list(graph.iter_lefthand_ancestry(branch.last_revision(),
 
45
            [_mod_revision.NULL_REVISION]))
 
46
    finally:
 
47
        branch.unlock()
 
48
    history.reverse()
 
49
    return history
 
50
 
 
51
 
48
52
def fetch_steps(self, br_a, br_b, writable_a):
49
53
    """A foreign test method for testing fetch locally and remotely."""
50
54
 
51
55
    # TODO RBC 20060201 make this a repository test.
52
56
    repo_b = br_b.repository
53
 
    self.assertFalse(repo_b.has_revision(br_a.revision_history()[3]))
54
 
    self.assertTrue(repo_b.has_revision(br_a.revision_history()[2]))
55
 
    self.assertEquals(len(br_b.revision_history()), 7)
56
 
    br_b.fetch(br_a, br_a.revision_history()[2])
 
57
    self.assertFalse(repo_b.has_revision(revision_history(br_a)[3]))
 
58
    self.assertTrue(repo_b.has_revision(revision_history(br_a)[2]))
 
59
    self.assertEquals(len(revision_history(br_b)), 7)
 
60
    br_b.fetch(br_a, revision_history(br_a)[2])
57
61
    # branch.fetch is not supposed to alter the revision history
58
 
    self.assertEquals(len(br_b.revision_history()), 7)
59
 
    self.assertFalse(repo_b.has_revision(br_a.revision_history()[3]))
 
62
    self.assertEquals(len(revision_history(br_b)), 7)
 
63
    self.assertFalse(repo_b.has_revision(revision_history(br_a)[3]))
60
64
 
61
65
    # fetching the next revision up in sample data copies one revision
62
 
    br_b.fetch(br_a, br_a.revision_history()[3])
63
 
    self.assertTrue(repo_b.has_revision(br_a.revision_history()[3]))
64
 
    self.assertFalse(has_revision(br_a, br_b.revision_history()[6]))
65
 
    self.assertTrue(br_a.repository.has_revision(br_b.revision_history()[5]))
 
66
    br_b.fetch(br_a, revision_history(br_a)[3])
 
67
    self.assertTrue(repo_b.has_revision(revision_history(br_a)[3]))
 
68
    self.assertFalse(has_revision(br_a, revision_history(br_b)[6]))
 
69
    self.assertTrue(br_a.repository.has_revision(revision_history(br_b)[5]))
66
70
 
67
71
    # When a non-branch ancestor is missing, it should be unlisted...
68
72
    # as its not reference from the inventory weave.
70
74
    br_b4.fetch(br_b)
71
75
 
72
76
    writable_a.fetch(br_b)
73
 
    self.assertTrue(has_revision(br_a, br_b.revision_history()[3]))
74
 
    self.assertTrue(has_revision(br_a, br_b.revision_history()[4]))
 
77
    self.assertTrue(has_revision(br_a, revision_history(br_b)[3]))
 
78
    self.assertTrue(has_revision(br_a, revision_history(br_b)[4]))
75
79
 
76
80
    br_b2 = self.make_branch('br_b2')
77
81
    br_b2.fetch(br_b)
78
 
    self.assertTrue(has_revision(br_b2, br_b.revision_history()[4]))
79
 
    self.assertTrue(has_revision(br_b2, br_a.revision_history()[2]))
80
 
    self.assertFalse(has_revision(br_b2, br_a.revision_history()[3]))
 
82
    self.assertTrue(has_revision(br_b2, revision_history(br_b)[4]))
 
83
    self.assertTrue(has_revision(br_b2, revision_history(br_a)[2]))
 
84
    self.assertFalse(has_revision(br_b2, revision_history(br_a)[3]))
81
85
 
82
86
    br_a2 = self.make_branch('br_a2')
83
87
    br_a2.fetch(br_a)
84
 
    self.assertTrue(has_revision(br_a2, br_b.revision_history()[4]))
85
 
    self.assertTrue(has_revision(br_a2, br_a.revision_history()[3]))
86
 
    self.assertTrue(has_revision(br_a2, br_a.revision_history()[2]))
 
88
    self.assertTrue(has_revision(br_a2, revision_history(br_b)[4]))
 
89
    self.assertTrue(has_revision(br_a2, revision_history(br_a)[3]))
 
90
    self.assertTrue(has_revision(br_a2, revision_history(br_a)[2]))
87
91
 
88
92
    br_a3 = self.make_branch('br_a3')
89
93
    # pulling a branch with no revisions grabs nothing, regardless of
91
95
    br_a3.fetch(br_a2)
92
96
    for revno in range(4):
93
97
        self.assertFalse(
94
 
            br_a3.repository.has_revision(br_a.revision_history()[revno]))
95
 
    br_a3.fetch(br_a2, br_a.revision_history()[2])
 
98
            br_a3.repository.has_revision(revision_history(br_a)[revno]))
 
99
    br_a3.fetch(br_a2, revision_history(br_a)[2])
96
100
    # pull the 3 revisions introduced by a@u-0-3
97
 
    br_a3.fetch(br_a2, br_a.revision_history()[3])
 
101
    br_a3.fetch(br_a2, revision_history(br_a)[3])
98
102
    # NoSuchRevision should be raised if the branch is missing the revision
99
103
    # that was requested.
100
104
    self.assertRaises(errors.NoSuchRevision, br_a3.fetch, br_a2, 'pizza')
248
252
                    rev_id).get_file_text('this-file-id'), text)
249
253
 
250
254
 
251
 
class TestHttpFetch(TestCaseWithWebserver):
252
 
    # FIXME RBC 20060124 this really isn't web specific, perhaps an
253
 
    # instrumented readonly transport? Can we do an instrumented
254
 
    # adapter and use self.get_readonly_url ?
255
 
 
256
 
    def test_fetch(self):
257
 
        #highest indices a: 5, b: 7
258
 
        br_a, br_b = make_branches(self)
259
 
        br_rem_a = Branch.open(self.get_readonly_url('branch1'))
260
 
        fetch_steps(self, br_rem_a, br_b, br_a)
261
 
 
262
 
    def _count_log_matches(self, target, logs):
263
 
        """Count the number of times the target file pattern was fetched in an http log"""
264
 
        get_succeeds_re = re.compile(
265
 
            '.*"GET .*%s HTTP/1.1" 20[06] - "-" "bzr/%s' %
266
 
            (     target,                    bzrlib.__version__))
267
 
        c = 0
268
 
        for line in logs:
269
 
            if get_succeeds_re.match(line):
270
 
                c += 1
271
 
        return c
272
 
 
273
 
    def test_weaves_are_retrieved_once(self):
274
 
        self.build_tree(("source/", "source/file", "target/"))
275
 
        # This test depends on knit dasta storage.
276
 
        wt = self.make_branch_and_tree('source', format='dirstate-tags')
277
 
        branch = wt.branch
278
 
        wt.add(["file"], ["id"])
279
 
        wt.commit("added file")
280
 
        open("source/file", 'w').write("blah\n")
281
 
        wt.commit("changed file")
282
 
        target = BzrDir.create_branch_and_repo("target/")
283
 
        source = Branch.open(self.get_readonly_url("source/"))
284
 
        target.fetch(source)
285
 
        # this is the path to the literal file. As format changes
286
 
        # occur it needs to be updated. FIXME: ask the store for the
287
 
        # path.
288
 
        self.log("web server logs are:")
289
 
        http_logs = self.get_readonly_server().logs
290
 
        self.log('\n'.join(http_logs))
291
 
        # unfortunately this log entry is branch format specific. We could
292
 
        # factor out the 'what files does this format use' to a method on the
293
 
        # repository, which would let us to this generically. RBC 20060419
294
 
        # RBC 20080408: Or perhaps we can assert that no files are fully read
295
 
        # twice?
296
 
        self.assertEqual(1, self._count_log_matches('/ce/id.kndx', http_logs))
297
 
        self.assertEqual(1, self._count_log_matches('/ce/id.knit', http_logs))
298
 
        self.assertEqual(1, self._count_log_matches('inventory.kndx', http_logs))
299
 
        # this r-h check test will prevent regressions, but it currently already
300
 
        # passes, before the patch to cache-rh is applied :[
301
 
        self.assertTrue(1 >= self._count_log_matches('revision-history',
302
 
                                                     http_logs))
303
 
        self.assertTrue(1 >= self._count_log_matches('last-revision',
304
 
                                                     http_logs))
305
 
        # FIXME naughty poking in there.
306
 
        self.get_readonly_server().logs = []
307
 
        # check there is nothing more to fetch.  We take care to re-use the
308
 
        # existing transport so that the request logs we're about to examine
309
 
        # aren't cluttered with redundant probes for a smart server.
310
 
        # XXX: Perhaps this further parameterisation: test http with smart
311
 
        # server, and test http without smart server?
312
 
        source = Branch.open(
313
 
            self.get_readonly_url("source/"),
314
 
            possible_transports=[source.bzrdir.root_transport])
315
 
        target.fetch(source)
316
 
        # should make just two requests
317
 
        http_logs = self.get_readonly_server().logs
318
 
        self.log("web server logs are:")
319
 
        self.log('\n'.join(http_logs))
320
 
        self.assertEqual(1, self._count_log_matches('branch-format', http_logs))
321
 
        self.assertEqual(1, self._count_log_matches('branch/format', http_logs))
322
 
        self.assertEqual(1, self._count_log_matches('repository/format',
323
 
            http_logs))
324
 
        self.assertEqual(1, self._count_log_matches('revisions.kndx',
325
 
            http_logs))
326
 
        self.assertTrue(1 >= self._count_log_matches('revision-history',
327
 
                                                     http_logs))
328
 
        self.assertTrue(1 >= self._count_log_matches('last-revision',
329
 
                                                     http_logs))
330
 
        self.assertLength(5, http_logs)
331
 
 
332
 
 
333
255
class TestKnitToPackFetch(TestCaseWithTransport):
334
256
 
335
257
    def find_get_record_stream(self, calls, expected_count=1):
404
326
        source.inventories = versionedfile.RecordingVersionedFilesDecorator(
405
327
                        source.inventories)
406
328
        # XXX: This won't work in general, but for the dirstate format it does.
407
 
        old_fetch_uses_deltas_setting = target._format._fetch_uses_deltas
408
 
        def restore():
409
 
            target._format._fetch_uses_deltas = old_fetch_uses_deltas_setting
410
 
        self.addCleanup(restore)
411
 
        target._format._fetch_uses_deltas = False
 
329
        self.overrideAttr(target._format, '_fetch_uses_deltas', False)
412
330
        target.fetch(source, revision_id='rev-one')
413
331
        self.assertEqual(('get_record_stream', [('file-id', 'rev-one')],
414
332
                          target._format._fetch_order, True),