~bzr-pqm/bzr/bzr.dev

2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
16
17
"""Tests for fetch between repositories of the same type."""
18
19
from bzrlib import (
20
    bzrdir,
21
    errors,
2696.3.10 by Martin Pool
Add missing import
22
    gpg,
4360.2.1 by Robert Collins
Don't return ghosts in the keyset for PendingAncestryResult.
23
    graph,
4324.3.1 by Robert Collins
When adding rich root data follow the standard revision graph rules, so it does not create 'inconstent parents'.
24
    remote,
2948.3.1 by John Arbash Meinel
Fix bug #158333, make sure that Repository.fetch(self) is properly a no-op for all Repository implementations.
25
    repository,
4392.2.2 by John Arbash Meinel
Add tests that ensure we can fetch branches with ghosts in their ancestry.
26
    tests,
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
27
    )
4324.3.1 by Robert Collins
When adding rich root data follow the standard revision graph rules, so it does not create 'inconstent parents'.
28
from bzrlib.inventory import ROOT_ID
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
29
from bzrlib.tests import TestSkipped
3689.1.1 by John Arbash Meinel
Rename repository_implementations tests into per_repository tests
30
from bzrlib.tests.per_repository import TestCaseWithRepository
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
31
from bzrlib.transport import get_transport
32
33
34
class TestFetchSameRepository(TestCaseWithRepository):
35
36
    def test_fetch(self):
37
        # smoke test fetch to ensure that the convenience function works.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
38
        # it is defined as a convenience function with the underlying
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
39
        # functionality provided by an InterRepository
40
        tree_a = self.make_branch_and_tree('a')
41
        self.build_tree(['a/foo'])
42
        tree_a.add('foo', 'file1')
43
        tree_a.commit('rev1', rev_id='rev1')
44
        # fetch with a default limit (grab everything)
2711.2.6 by Martin Pool
Fix up conversion of create_repository to make_repository in test_fetch
45
        repo = self.make_repository('b')
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
46
        if (tree_a.branch.repository.supports_rich_root() and not
47
            repo.supports_rich_root()):
48
            raise TestSkipped('Cannot fetch from model2 to model1')
49
        repo.fetch(tree_a.branch.repository,
50
                   revision_id=None)
51
4145.1.1 by Robert Collins
Explicitly prevent fetching while the target repository is in a write group.
52
    def test_fetch_fails_in_write_group(self):
53
        # fetch() manages a write group itself, fetching within one isn't safe.
54
        repo = self.make_repository('a')
55
        repo.lock_write()
56
        self.addCleanup(repo.unlock)
57
        repo.start_write_group()
58
        self.addCleanup(repo.abort_write_group)
59
        # Don't need a specific class - not expecting flow control based on
60
        # this.
61
        self.assertRaises(errors.BzrError, repo.fetch, repo)
62
4060.1.4 by Robert Collins
Streaming fetch from remote servers.
63
    def test_fetch_to_knit3(self):
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
64
        # create a repository of the sort we are testing.
3242.2.17 by Aaron Bentley
Fix broken tests
65
        tree_a = self.make_branch_and_tree('a')
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
66
        self.build_tree(['a/foo'])
67
        tree_a.add('foo', 'file1')
68
        tree_a.commit('rev1', rev_id='rev1')
69
        # create a knit-3 based format to fetch into
70
        f = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
71
        try:
72
            format = tree_a.branch.repository._format
73
            format.check_conversion_target(f.repository_format)
74
            # if we cannot convert data to knit3, skip the test.
75
        except errors.BadConversionTarget, e:
76
            raise TestSkipped(str(e))
77
        self.get_transport().mkdir('b')
78
        b_bzrdir = f.initialize(self.get_url('b'))
79
        knit3_repo = b_bzrdir.create_repository()
80
        # fetch with a default limit (grab everything)
81
        knit3_repo.fetch(tree_a.branch.repository, revision_id=None)
2592.3.96 by Robert Collins
Merge index improvements (includes bzr.dev).
82
        # Reopen to avoid any in-memory caching - ensure its reading from
83
        # disk.
84
        knit3_repo = b_bzrdir.open_repository()
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
85
        rev1_tree = knit3_repo.revision_tree('rev1')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
86
        rev1_tree.lock_read()
87
        try:
88
            lines = rev1_tree.get_file_lines(rev1_tree.get_root_id())
89
        finally:
90
            rev1_tree.unlock()
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
91
        self.assertEqual([], lines)
92
        b_branch = b_bzrdir.create_branch()
93
        b_branch.pull(tree_a.branch)
94
        try:
95
            tree_b = b_bzrdir.create_workingtree()
96
        except errors.NotLocalUrl:
4060.1.4 by Robert Collins
Streaming fetch from remote servers.
97
            try:
98
                tree_b = b_branch.create_checkout('b', lightweight=True)
99
            except errors.NotLocalUrl:
100
                raise TestSkipped("cannot make working tree with transport %r"
2696.3.2 by Martin Pool
Move some per-repository tests from big test_repository to test_fetch
101
                              % b_bzrdir.transport)
102
        tree_b.commit('no change', rev_id='rev2')
103
        rev2_tree = knit3_repo.revision_tree('rev2')
104
        self.assertEqual('rev1', rev2_tree.inventory.root.revision)
2696.3.9 by Martin Pool
merge trunk
105
4324.3.1 by Robert Collins
When adding rich root data follow the standard revision graph rules, so it does not create 'inconstent parents'.
106
    def do_test_fetch_to_rich_root_sets_parents_correctly(self, result,
107
        snapshots, root_id=ROOT_ID, allow_lefthand_ghost=False):
108
        """Assert that result is the parents of 'tip' after fetching snapshots.
109
110
        This helper constructs a 1.9 format source, and a test-format target
111
        and fetches the result of building snapshots in the source, then
112
        asserts that the parents of tip are result.
113
114
        :param result: A parents list for the inventories.get_parent_map call.
115
        :param snapshots: An iterable of snapshot parameters for
116
            BranchBuilder.build_snapshot.
117
        '"""
118
        # This overlaps slightly with the tests for commit builder about graph
119
        # consistency.
120
        # Cases:
121
        repo = self.make_repository('target')
4324.3.3 by Robert Collins
Fix silly typo.
122
        remote_format = isinstance(repo, remote.RemoteRepository)
4324.3.1 by Robert Collins
When adding rich root data follow the standard revision graph rules, so it does not create 'inconstent parents'.
123
        if not repo._format.rich_root_data and not remote_format:
124
            return # not relevant
125
        builder = self.make_branch_builder('source', format='1.9')
126
        builder.start_series()
127
        for revision_id, parent_ids, actions in snapshots:
128
            builder.build_snapshot(revision_id, parent_ids, actions,
129
            allow_leftmost_as_ghost=allow_lefthand_ghost)
130
        builder.finish_series()
131
        source = builder.get_branch()
132
        if remote_format and not repo._format.rich_root_data:
133
            # use a manual rich root format to ensure the code path is tested.
134
            repo = self.make_repository('remote-target',
135
                format='1.9-rich-root')
136
        repo.lock_write()
137
        self.addCleanup(repo.unlock)
138
        repo.fetch(source.repository)
139
        self.assertEqual(result,
140
            repo.texts.get_parent_map([(root_id, 'tip')])[(root_id, 'tip')])
141
142
    def test_fetch_to_rich_root_set_parent_no_parents(self):
143
        # No parents rev -> No parents
144
        self.do_test_fetch_to_rich_root_sets_parents_correctly((),
145
            [('tip', None, [('add', ('', ROOT_ID, 'directory', ''))]),
146
            ])
147
148
    def test_fetch_to_rich_root_set_parent_1_parent(self):
4392.2.2 by John Arbash Meinel
Add tests that ensure we can fetch branches with ghosts in their ancestry.
149
        # 1 parent rev -> 1 parent
4324.3.1 by Robert Collins
When adding rich root data follow the standard revision graph rules, so it does not create 'inconstent parents'.
150
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
151
            ((ROOT_ID, 'base'),),
152
            [('base', None, [('add', ('', ROOT_ID, 'directory', ''))]),
153
             ('tip', None, []),
154
            ])
155
156
    def test_fetch_to_rich_root_set_parent_1_ghost_parent(self):
157
        # 1 ghost parent -> No parents
158
        self.do_test_fetch_to_rich_root_sets_parents_correctly((),
159
            [('tip', ['ghost'], [('add', ('', ROOT_ID, 'directory', ''))]),
160
            ], allow_lefthand_ghost=True)
161
162
    def test_fetch_to_rich_root_set_parent_2_head_parents(self):
163
        # 2 parents both heads -> 2 parents
164
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
165
            ((ROOT_ID, 'left'), (ROOT_ID, 'right')),
166
            [('base', None, [('add', ('', ROOT_ID, 'directory', ''))]),
167
             ('left', None, []),
168
             ('right', ['base'], []),
169
             ('tip', ['left', 'right'], []),
170
            ])
171
172
    def test_fetch_to_rich_root_set_parent_2_parents_1_head(self):
173
        # 2 parents one head -> 1 parent
174
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
175
            ((ROOT_ID, 'right'),),
176
            [('left', None, [('add', ('', ROOT_ID, 'directory', ''))]),
177
             ('right', None, []),
178
             ('tip', ['left', 'right'], []),
179
            ])
180
181
    def test_fetch_to_rich_root_set_parent_1_parent_different_id_gone(self):
182
        # 1 parent different fileid, ours missing -> no parents
183
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
184
            (),
185
            [('base', None, [('add', ('', ROOT_ID, 'directory', ''))]),
186
             ('tip', None, [('unversion', ROOT_ID),
187
                            ('add', ('', 'my-root', 'directory', '')),
188
                            ]),
189
            ], root_id='my-root')
190
191
    def test_fetch_to_rich_root_set_parent_1_parent_different_id_moved(self):
192
        # 1 parent different fileid, ours moved -> 1 parent
193
        # (and that parent honours the changing revid of the other location)
194
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
195
            (('my-root', 'origin'),),
196
            [('origin', None, [('add', ('', ROOT_ID, 'directory', '')),
197
                             ('add', ('child', 'my-root', 'directory', ''))]),
198
             ('base', None, []),
199
             ('tip', None, [('unversion', 'my-root'),
200
                            ('unversion', ROOT_ID),
201
                            ('add', ('', 'my-root', 'directory', '')),
202
                            ]),
203
            ], root_id='my-root')
204
205
    def test_fetch_to_rich_root_set_parent_2_parent_1_different_id_gone(self):
206
        # 2 parents, 1 different fileid, our second missing -> 1 parent
207
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
208
            (('my-root', 'right'),),
209
            [('base', None, [('add', ('', ROOT_ID, 'directory', ''))]),
210
             ('right', None, [('unversion', ROOT_ID),
211
                              ('add', ('', 'my-root', 'directory', ''))]),
212
             ('tip', ['base', 'right'], [('unversion', ROOT_ID),
213
                            ('add', ('', 'my-root', 'directory', '')),
214
                            ]),
215
            ], root_id='my-root')
216
217
    def test_fetch_to_rich_root_set_parent_2_parent_2_different_id_moved(self):
218
        # 2 parents, 1 different fileid, our second moved -> 2 parent
219
        # (and that parent honours the changing revid of the other location)
220
        self.do_test_fetch_to_rich_root_sets_parents_correctly(
221
            (('my-root', 'right'),),
222
            # 'my-root' at 'child'.
223
            [('origin', None, [('add', ('', ROOT_ID, 'directory', '')),
224
                             ('add', ('child', 'my-root', 'directory', ''))]),
225
             ('base', None, []),
226
            # 'my-root' at root
227
             ('right', None, [('unversion', 'my-root'),
228
                              ('unversion', ROOT_ID),
229
                              ('add', ('', 'my-root', 'directory', ''))]),
230
             ('tip', ['base', 'right'], [('unversion', 'my-root'),
231
                            ('unversion', ROOT_ID),
232
                            ('add', ('', 'my-root', 'directory', '')),
233
                            ]),
234
            ], root_id='my-root')
235
2948.3.1 by John Arbash Meinel
Fix bug #158333, make sure that Repository.fetch(self) is properly a no-op for all Repository implementations.
236
    def test_fetch_all_from_self(self):
237
        tree = self.make_branch_and_tree('.')
238
        rev_id = tree.commit('one')
239
        # This needs to be a new copy of the repository, if this changes, the
240
        # test needs to be rewritten
241
        repo = tree.branch.repository.bzrdir.open_repository()
242
        # This fetch should be a no-op see bug #158333
243
        tree.branch.repository.fetch(repo, None)
244
245
    def test_fetch_from_self(self):
246
        tree = self.make_branch_and_tree('.')
247
        rev_id = tree.commit('one')
248
        repo = tree.branch.repository.bzrdir.open_repository()
249
        # This fetch should be a no-op see bug #158333
250
        tree.branch.repository.fetch(repo, rev_id)
251
252
    def test_fetch_missing_from_self(self):
253
        tree = self.make_branch_and_tree('.')
254
        rev_id = tree.commit('one')
255
        # Even though the fetch() is a NO-OP it should assert the revision id
256
        # is present
257
        repo = tree.branch.repository.bzrdir.open_repository()
258
        self.assertRaises(errors.NoSuchRevision, tree.branch.repository.fetch,
259
                          repo, 'no-such-revision')
260
2696.3.9 by Martin Pool
merge trunk
261
    def makeARepoWithSignatures(self):
262
        wt = self.make_branch_and_tree('a-repo-with-sigs')
263
        wt.commit('rev1', allow_pointless=True, rev_id='rev1')
264
        repo = wt.branch.repository
2592.3.96 by Robert Collins
Merge index improvements (includes bzr.dev).
265
        repo.lock_write()
266
        repo.start_write_group()
2592.3.103 by Robert Collins
Fix some more pack-related test breakage.
267
        repo.sign_revision('rev1', gpg.LoopbackGPGStrategy(None))
2592.3.96 by Robert Collins
Merge index improvements (includes bzr.dev).
268
        repo.commit_write_group()
269
        repo.unlock()
2696.3.9 by Martin Pool
merge trunk
270
        return repo
271
272
    def test_fetch_copies_signatures(self):
273
        source_repo = self.makeARepoWithSignatures()
274
        target_repo = self.make_repository('target')
275
        target_repo.fetch(source_repo, revision_id=None)
276
        self.assertEqual(
277
            source_repo.get_signature_text('rev1'),
278
            target_repo.get_signature_text('rev1'))
2535.3.48 by Andrew Bennetts
Merge from bzr.dev.
279
280
    def make_repository_with_one_revision(self):
3242.2.17 by Aaron Bentley
Fix broken tests
281
        wt = self.make_branch_and_tree('source')
2535.3.48 by Andrew Bennetts
Merge from bzr.dev.
282
        wt.commit('rev1', allow_pointless=True, rev_id='rev1')
283
        return wt.branch.repository
284
285
    def test_fetch_revision_already_exists(self):
286
        # Make a repository with one revision.
287
        source_repo = self.make_repository_with_one_revision()
288
        # Fetch that revision into a second repository.
289
        target_repo = self.make_repository('target')
290
        target_repo.fetch(source_repo, revision_id='rev1')
291
        # Now fetch again; there will be nothing to do.  This should work
292
        # without causing any errors.
293
        target_repo.fetch(source_repo, revision_id='rev1')
294
1551.19.36 by Aaron Bentley
Prevent fetch all from causing pack collisions
295
    def test_fetch_all_same_revisions_twice(self):
296
        # Blind-fetching all the same revisions twice should succeed and be a
297
        # no-op the second time.
298
        repo = self.make_repository('repo')
299
        tree = self.make_branch_and_tree('tree')
300
        revision_id = tree.commit('test')
301
        repo.fetch(tree.branch.repository)
302
        repo.fetch(tree.branch.repository)
4360.2.1 by Robert Collins
Don't return ghosts in the keyset for PendingAncestryResult.
303
4392.2.2 by John Arbash Meinel
Add tests that ensure we can fetch branches with ghosts in their ancestry.
304
    def make_simple_branch_with_ghost(self):
305
        builder = self.make_branch_builder('source')
306
        builder.start_series()
307
        builder.build_snapshot('A-id', None, [
308
            ('add', ('', 'root-id', 'directory', None)),
309
            ('add', ('file', 'file-id', 'file', 'content\n'))])
310
        builder.build_snapshot('B-id', ['A-id', 'ghost-id'], [])
311
        builder.finish_series()
312
        source_b = builder.get_branch()
313
        source_b.lock_read()
314
        self.addCleanup(source_b.unlock)
315
        return source_b
316
317
    def test_fetch_with_ghost(self):
318
        source_b = self.make_simple_branch_with_ghost()
319
        target = self.make_repository('target')
320
        target.lock_write()
321
        self.addCleanup(target.unlock)
322
        target.fetch(source_b.repository, revision_id='B-id')
323
324
    def test_fetch_into_smart_with_ghost(self):
325
        trans = self.make_smart_server('target')
326
        source_b = self.make_simple_branch_with_ghost()
327
        target = self.make_repository('target')
328
        # Re-open the repository over the smart protocol
329
        target = repository.Repository.open(trans.base)
330
        target.lock_write()
331
        self.addCleanup(target.unlock)
332
        try:
333
            target.fetch(source_b.repository, revision_id='B-id')
334
        except errors.TokenLockingNotSupported:
335
            # The code inside fetch() that tries to lock and then fails, also
336
            # causes weird problems with 'lock_not_held' later on...
337
            target.lock_read()
338
            raise tests.KnownFailure('some repositories fail to fetch'
339
                ' via the smart server because of locking issues.')
340
341
    def test_fetch_from_smart_with_ghost(self):
342
        trans = self.make_smart_server('source')
343
        source_b = self.make_simple_branch_with_ghost()
344
        target = self.make_repository('target')
345
        target.lock_write()
346
        self.addCleanup(target.unlock)
347
        # Re-open the repository over the smart protocol
348
        source = repository.Repository.open(trans.base)
349
        source.lock_read()
350
        self.addCleanup(source.unlock)
351
        target.fetch(source, revision_id='B-id')
352
4360.2.1 by Robert Collins
Don't return ghosts in the keyset for PendingAncestryResult.
353
354
class TestSource(TestCaseWithRepository):
355
    """Tests for/about the results of Repository._get_source."""
356
357
    def test_no_absent_records_in_stream_with_ghosts(self):
358
        # XXX: Arguably should be in interrepository_implementations but
359
        # doesn't actually gain coverage there; need a specific set of
360
        # permutations to cover it.
4360.2.2 by Robert Collins
Add bug info.
361
        # bug lp:376255 was reported about this.
4360.2.1 by Robert Collins
Don't return ghosts in the keyset for PendingAncestryResult.
362
        builder = self.make_branch_builder('repo')
363
        builder.start_series()
364
        builder.build_snapshot('tip', ['ghost'],
365
            [('add', ('', 'ROOT_ID', 'directory', ''))],
366
            allow_leftmost_as_ghost=True)
367
        builder.finish_series()
368
        b = builder.get_branch()
369
        b.lock_read()
370
        self.addCleanup(b.unlock)
371
        repo = b.repository
372
        source = repo._get_source(repo._format)
373
        search = graph.PendingAncestryResult(['tip'], repo)
374
        stream = source.get_stream(search)
375
        for substream_type, substream in stream:
376
            for record in substream:
377
                self.assertNotEqual('absent', record.storage_kind,
378
                    "Absent record for %s" % (((substream_type,) + record.key),))