~bzr-pqm/bzr/bzr.dev

4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
1
# Copyright (C) 2009 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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
4392.2.2 by John Arbash Meinel
Add tests that ensure we can fetch branches with ghosts in their ancestry.
18
from bzrlib import (
19
    branch,
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
20
    vf_search,
4392.2.2 by John Arbash Meinel
Add tests that ensure we can fetch branches with ghosts in their ancestry.
21
    )
4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
22
from bzrlib.tests.per_repository import TestCaseWithRepository
23
24
5539.2.7 by Andrew Bennetts
Add a test, revise a comment.
25
class TestFetchBase(TestCaseWithRepository):
4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
26
4343.3.17 by John Arbash Meinel
Finally found a way to write a test case to exercise the code path.
27
    def make_source_branch(self):
28
        # It would be nice if there was a way to force this to be memory-only
4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
29
        builder = self.make_branch_builder('source')
30
        content = ['content lines\n'
31
                   'for the first revision\n'
32
                   'which is a marginal amount of content\n'
33
                  ]
34
        builder.start_series()
35
        builder.build_snapshot('A-id', None, [
36
            ('add', ('', 'root-id', 'directory', None)),
37
            ('add', ('a', 'a-id', 'file', ''.join(content))),
38
            ])
39
        content.append('and some more lines for B\n')
40
        builder.build_snapshot('B-id', ['A-id'], [
41
            ('modify', ('a-id', ''.join(content)))])
42
        content.append('and yet even more content for C\n')
43
        builder.build_snapshot('C-id', ['B-id'], [
44
            ('modify', ('a-id', ''.join(content)))])
45
        builder.finish_series()
46
        source_b = builder.get_branch()
47
        source_b.lock_read()
48
        self.addCleanup(source_b.unlock)
4343.3.17 by John Arbash Meinel
Finally found a way to write a test case to exercise the code path.
49
        return content, source_b
50
5539.2.7 by Andrew Bennetts
Add a test, revise a comment.
51
52
class TestFetch(TestFetchBase):
53
4343.3.17 by John Arbash Meinel
Finally found a way to write a test case to exercise the code path.
54
    def test_sprout_from_stacked_with_short_history(self):
4343.3.36 by John Arbash Meinel
Some review feedback from Andrew.
55
        content, source_b = self.make_source_branch()
56
        # Split the generated content into a base branch, and a stacked branch
4343.3.13 by John Arbash Meinel
Update the fetch tests to make sure we are properly testing a smart base branch
57
        # Use 'make_branch' which gives us a bzr:// branch when appropriate,
58
        # rather than creating a branch-on-disk
59
        stack_b = self.make_branch('stack-on')
60
        stack_b.pull(source_b, stop_revision='B-id')
4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
61
        target_b = self.make_branch('target')
4343.3.13 by John Arbash Meinel
Update the fetch tests to make sure we are properly testing a smart base branch
62
        target_b.set_stacked_on_url('../stack-on')
4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
63
        target_b.pull(source_b, stop_revision='C-id')
64
        # At this point, we should have a target branch, with 1 revision, on
65
        # top of the source.
4343.3.17 by John Arbash Meinel
Finally found a way to write a test case to exercise the code path.
66
        final_b = self.make_branch('final')
67
        final_b.pull(target_b)
4343.3.10 by John Arbash Meinel
Add a per_repository_reference test with real stacked repos.
68
        final_b.lock_read()
69
        self.addCleanup(final_b.unlock)
70
        self.assertEqual('C-id', final_b.last_revision())
71
        text_keys = [('a-id', 'A-id'), ('a-id', 'B-id'), ('a-id', 'C-id')]
72
        stream = final_b.repository.texts.get_record_stream(text_keys,
73
            'unordered', True)
4343.3.36 by John Arbash Meinel
Some review feedback from Andrew.
74
        records = sorted([(r.key, r.get_bytes_as('fulltext')) for r in stream])
75
        self.assertEqual([
76
            (('a-id', 'A-id'), ''.join(content[:-2])),
77
            (('a-id', 'B-id'), ''.join(content[:-1])),
78
            (('a-id', 'C-id'), ''.join(content)),
79
            ], records)
4343.3.17 by John Arbash Meinel
Finally found a way to write a test case to exercise the code path.
80
81
    def test_sprout_from_smart_stacked_with_short_history(self):
82
        content, source_b = self.make_source_branch()
83
        transport = self.make_smart_server('server')
84
        transport.ensure_base()
85
        url = transport.abspath('')
86
        stack_b = source_b.bzrdir.sprout(url + '/stack-on', revision_id='B-id')
87
        # self.make_branch only takes relative paths, so we do it the 'hard'
88
        # way
89
        target_transport = transport.clone('target')
90
        target_transport.ensure_base()
91
        target_bzrdir = self.bzrdir_format.initialize_on_transport(
92
                            target_transport)
93
        target_bzrdir.create_repository()
94
        target_b = target_bzrdir.create_branch()
95
        target_b.set_stacked_on_url('../stack-on')
96
        target_b.pull(source_b, stop_revision='C-id')
97
        # Now we should be able to branch from the remote location to a local
98
        # location
99
        final_b = target_b.bzrdir.sprout('final').open_branch()
100
        self.assertEqual('C-id', final_b.last_revision())
101
102
        # bzrdir.sprout() has slightly different code paths if you supply a
103
        # revision_id versus not. If you supply revision_id, then you get a
104
        # PendingAncestryResult for the search, versus a SearchResult...
105
        final2_b = target_b.bzrdir.sprout('final2',
106
                                          revision_id='C-id').open_branch()
107
        self.assertEqual('C-id', final_b.last_revision())
4392.2.2 by John Arbash Meinel
Add tests that ensure we can fetch branches with ghosts in their ancestry.
108
109
    def make_source_with_ghost_and_stacked_target(self):
110
        builder = self.make_branch_builder('source')
111
        builder.start_series()
112
        builder.build_snapshot('A-id', None, [
113
            ('add', ('', 'root-id', 'directory', None)),
114
            ('add', ('file', 'file-id', 'file', 'content\n'))])
115
        builder.build_snapshot('B-id', ['A-id', 'ghost-id'], [])
116
        builder.finish_series()
117
        source_b = builder.get_branch()
118
        source_b.lock_read()
119
        self.addCleanup(source_b.unlock)
120
        base = self.make_branch('base')
121
        base.pull(source_b, stop_revision='A-id')
122
        stacked = self.make_branch('stacked')
123
        stacked.set_stacked_on_url('../base')
124
        return source_b, base, stacked
125
126
    def test_fetch_with_ghost_stacked(self):
127
        (source_b, base,
128
         stacked) = self.make_source_with_ghost_and_stacked_target()
129
        stacked.pull(source_b, stop_revision='B-id')
130
131
    def test_fetch_into_smart_stacked_with_ghost(self):
132
        (source_b, base,
133
         stacked) = self.make_source_with_ghost_and_stacked_target()
134
        # Now, create a smart server on 'stacked' and re-open to force the
135
        # target to be a smart target
136
        trans = self.make_smart_server('stacked')
137
        stacked = branch.Branch.open(trans.base)
138
        stacked.lock_write()
139
        self.addCleanup(stacked.unlock)
140
        stacked.pull(source_b, stop_revision='B-id')
141
142
    def test_fetch_to_stacked_from_smart_with_ghost(self):
143
        (source_b, base,
144
         stacked) = self.make_source_with_ghost_and_stacked_target()
145
        # Now, create a smart server on 'source' and re-open to force the
146
        # target to be a smart target
147
        trans = self.make_smart_server('source')
148
        source_b = branch.Branch.open(trans.base)
149
        source_b.lock_read()
150
        self.addCleanup(source_b.unlock)
151
        stacked.pull(source_b, stop_revision='B-id')
5539.2.7 by Andrew Bennetts
Add a test, revise a comment.
152
153
154
class TestFetchFromRepoWithUnconfiguredFallbacks(TestFetchBase):
155
156
    def make_stacked_source_repo(self):
157
        _, source_b = self.make_source_branch()
158
        # Use 'make_branch' which gives us a bzr:// branch when appropriate,
159
        # rather than creating a branch-on-disk
160
        stack_b = self.make_branch('stack-on')
161
        stack_b.pull(source_b, stop_revision='B-id')
162
        stacked_b = self.make_branch('stacked')
163
        stacked_b.set_stacked_on_url('../stack-on')
164
        stacked_b.pull(source_b, stop_revision='C-id')
165
        return stacked_b.repository
166
167
    def test_fetch_everything_includes_parent_invs(self):
168
        stacked = self.make_stacked_source_repo()
169
        repo_missing_fallbacks = stacked.bzrdir.open_repository()
170
        self.addCleanup(repo_missing_fallbacks.lock_read().unlock)
171
        target = self.make_repository('target')
172
        self.addCleanup(target.lock_write().unlock)
173
        target.fetch(
174
            repo_missing_fallbacks,
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
175
            fetch_spec=vf_search.EverythingResult(repo_missing_fallbacks))
5539.2.7 by Andrew Bennetts
Add a test, revise a comment.
176
        self.assertEqual(repo_missing_fallbacks.revisions.keys(),
177
            target.revisions.keys())
178
        self.assertEqual(repo_missing_fallbacks.inventories.keys(),
179
            target.inventories.keys())
180
        self.assertEqual(['C-id'],
181
            sorted(k[-1] for k in target.revisions.keys()))
182
        self.assertEqual(['B-id', 'C-id'],
183
            sorted(k[-1] for k in target.inventories.keys()))
184
185
186