~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_upgrade.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-23 08:22:05 UTC
  • mfrom: (5053.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100223082205-nn2nzonavf8sfuae
(mbp) merge 2.1 back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
12
12
#
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
16
16
 
17
17
"""Tests for upgrade of old trees.
18
18
 
19
 
This file contains canned versions of some old trees, which are instantiated 
 
19
This file contains canned versions of some old trees, which are instantiated
20
20
and then upgraded to the new format."""
21
21
 
 
22
# TODO queue for upgrade:
 
23
# test the error message when upgrading an unknown BzrDir format.
 
24
 
22
25
import base64
23
26
import os
24
27
import sys
25
28
 
26
 
from bzrlib.selftest import TestCase, TestCaseInTempDir
 
29
from bzrlib import (
 
30
    branch as _mod_branch,
 
31
    bzrdir,
 
32
    progress,
 
33
    repository,
 
34
    workingtree,
 
35
    workingtree_4,
 
36
    )
 
37
import bzrlib.branch
27
38
from bzrlib.branch import Branch
28
 
from bzrlib.revision import is_ancestor
 
39
from bzrlib.tests import TestCaseWithTransport
 
40
from bzrlib.transport import get_transport
29
41
from bzrlib.upgrade import upgrade
30
 
from bzrlib.selftest.treeshape import build_tree_contents
31
 
 
32
 
 
33
 
class TestUpgrade(TestCaseInTempDir):
34
 
    
 
42
 
 
43
 
 
44
class TestUpgrade(TestCaseWithTransport):
 
45
 
35
46
    def test_build_tree(self):
36
47
        """Test tree-building test helper"""
37
 
        build_tree_contents(_upgrade1_template)
 
48
        self.build_tree_contents(_upgrade1_template)
38
49
        self.failUnlessExists('foo')
39
50
        self.failUnlessExists('.bzr/README')
40
51
 
41
52
    def test_upgrade_simple(self):
42
 
        """Upgrade simple v0.0.4 format to v6"""
 
53
        """Upgrade simple v0.0.4 format to latest format"""
43
54
        eq = self.assertEquals
44
 
        build_tree_contents(_upgrade1_template)
45
 
        upgrade('.')
46
 
        b = Branch.open('.')
47
 
        eq(b._branch_format, 6)
 
55
        self.build_tree_contents(_upgrade1_template)
 
56
        upgrade(u'.')
 
57
        control = bzrdir.BzrDir.open('.')
 
58
        b = control.open_branch()
 
59
        # tsk, peeking under the covers.
 
60
        self.failUnless(
 
61
            isinstance(
 
62
                control._format,
 
63
                bzrdir.BzrDirFormat.get_default_format().__class__))
48
64
        rh = b.revision_history()
49
65
        eq(rh,
50
66
           ['mbp@sourcefrog.net-20051004035611-176b16534b086b3c',
51
67
            'mbp@sourcefrog.net-20051004035756-235f2b7dcdddd8dd'])
52
 
        t = b.revision_tree(rh[0])
 
68
        rt = b.repository.revision_tree(rh[0])
53
69
        foo_id = 'foo-20051004035605-91e788d1875603ae'
54
 
        eq(t.get_file_text(foo_id), 'initial contents\n')
55
 
        t = b.revision_tree(rh[1])
56
 
        eq(t.get_file_text(foo_id), 'new contents\n')
 
70
        rt.lock_read()
 
71
        try:
 
72
            eq(rt.get_file_text(foo_id), 'initial contents\n')
 
73
        finally:
 
74
            rt.unlock()
 
75
        rt = b.repository.revision_tree(rh[1])
 
76
        rt.lock_read()
 
77
        try:
 
78
            eq(rt.get_file_text(foo_id), 'new contents\n')
 
79
        finally:
 
80
            rt.unlock()
 
81
        # check a backup was made:
 
82
        backup_dir = 'backup.bzr.~1~'
 
83
        transport = get_transport(b.base)
 
84
        transport.stat(backup_dir)
 
85
        transport.stat(backup_dir + '/README')
 
86
        transport.stat(backup_dir + '/branch-format')
 
87
        transport.stat(backup_dir + '/revision-history')
 
88
        transport.stat(backup_dir + '/merged-patches')
 
89
        transport.stat(backup_dir + '/pending-merged-patches')
 
90
        transport.stat(backup_dir + '/pending-merges')
 
91
        transport.stat(backup_dir + '/branch-name')
 
92
        transport.stat(backup_dir + '/branch-lock')
 
93
        transport.stat(backup_dir + '/inventory')
 
94
        transport.stat(backup_dir + '/stat-cache')
 
95
        transport.stat(backup_dir + '/text-store')
 
96
        transport.stat(backup_dir + '/text-store/foo-20051004035611-1591048e9dc7c2d4.gz')
 
97
        transport.stat(backup_dir + '/text-store/foo-20051004035756-4081373d897c3453.gz')
 
98
        transport.stat(backup_dir + '/inventory-store/')
 
99
        transport.stat(backup_dir + '/inventory-store/mbp@sourcefrog.net-20051004035611-176b16534b086b3c.gz')
 
100
        transport.stat(backup_dir + '/inventory-store/mbp@sourcefrog.net-20051004035756-235f2b7dcdddd8dd.gz')
 
101
        transport.stat(backup_dir + '/revision-store/')
 
102
        transport.stat(backup_dir + '/revision-store/mbp@sourcefrog.net-20051004035611-176b16534b086b3c.gz')
 
103
        transport.stat(backup_dir + '/revision-store/mbp@sourcefrog.net-20051004035756-235f2b7dcdddd8dd.gz')
57
104
 
58
105
    def test_upgrade_with_ghosts(self):
59
106
        """Upgrade v0.0.4 tree containing ghost references.
60
107
 
61
108
        That is, some of the parents of revisions mentioned in the branch
62
 
        aren't present in the branches storage. 
 
109
        aren't present in the branch's storage.
63
110
 
64
 
        This shouldn't normally happen in branches created entirely in 
65
 
        bzr but can happen in imports from baz and arch, or from other  
66
 
        systems, where the importer knows about a revision but not 
 
111
        This shouldn't normally happen in branches created entirely in
 
112
        bzr, but can happen in branches imported from baz and arch, or from
 
113
        other systems, where the importer knows about a revision but not
67
114
        its contents."""
68
115
        eq = self.assertEquals
69
 
        build_tree_contents(_ghost_template)
70
 
        upgrade('.')
71
 
        b = Branch.open('.')
 
116
        self.build_tree_contents(_ghost_template)
 
117
        upgrade(u'.')
 
118
        b = Branch.open(u'.')
72
119
        revision_id = b.revision_history()[1]
73
 
        rev = b.get_revision(revision_id)
 
120
        rev = b.repository.get_revision(revision_id)
74
121
        eq(len(rev.parent_ids), 2)
75
122
        eq(rev.parent_ids[1], 'wibble@wobble-2')
76
123
 
77
124
    def test_upgrade_makes_dir_weaves(self):
78
 
        build_tree_contents(_upgrade_dir_template)
 
125
        self.build_tree_contents(_upgrade_dir_template)
 
126
        old_repodir = bzrlib.bzrdir.BzrDir.open_unsupported('.')
 
127
        old_repo_format = old_repodir.open_repository()._format
79
128
        upgrade('.')
80
 
        # this is the path to the literal file. As format changes 
 
129
        # this is the path to the literal file. As format changes
81
130
        # occur it needs to be updated. FIXME: ask the store for the
82
131
        # path.
83
 
        self.failUnlessExists(
84
 
            '.bzr/weaves/de/dir-20051005095101-da1441ea3fa6917a.weave')
 
132
        repo = bzrlib.repository.Repository.open('.')
 
133
        # it should have changed the format
 
134
        self.assertNotEqual(old_repo_format.__class__, repo._format.__class__)
 
135
        # and we should be able to read the names for the file id
 
136
        # 'dir-20051005095101-da1441ea3fa6917a'
 
137
        repo.lock_read()
 
138
        self.addCleanup(repo.unlock)
 
139
        text_keys = repo.texts.keys()
 
140
        dir_keys = [key for key in text_keys if key[0] ==
 
141
                'dir-20051005095101-da1441ea3fa6917a']
 
142
        self.assertNotEqual([], dir_keys)
 
143
 
 
144
    def test_upgrade_to_meta_sets_workingtree_last_revision(self):
 
145
        self.build_tree_contents(_upgrade_dir_template)
 
146
        upgrade('.', bzrdir.BzrDirMetaFormat1())
 
147
        tree = workingtree.WorkingTree.open('.')
 
148
        self.assertEqual([tree.branch.revision_history()[-1]],
 
149
            tree.get_parent_ids())
 
150
 
 
151
    def test_upgrade_v6_to_meta_no_workingtree(self):
 
152
        # Some format6 branches do not have checkout files. Upgrading
 
153
        # such a branch to metadir must not setup a working tree.
 
154
        self.build_tree_contents(_upgrade1_template)
 
155
        upgrade('.', bzrdir.BzrDirFormat6())
 
156
        transport = get_transport('.')
 
157
        transport.delete_multi(['.bzr/pending-merges', '.bzr/inventory'])
 
158
        self.assertFalse(transport.has('.bzr/stat-cache'))
 
159
        # XXX: upgrade fails if a backup.bzr is already present
 
160
        # -- David Allouche 2006-08-11
 
161
        transport.delete_tree('backup.bzr.~1~')
 
162
        # At this point, we have a format6 branch without checkout files.
 
163
        upgrade('.', bzrdir.BzrDirMetaFormat1())
 
164
        # The upgrade should not have set up a working tree.
 
165
        control = bzrdir.BzrDir.open('.')
 
166
        self.assertFalse(control.has_workingtree())
 
167
        # We have covered the scope of this test, we may as well check that
 
168
        # upgrade has not eaten our data, even if it's a bit redundant with
 
169
        # other tests.
 
170
        self.failUnless(isinstance(control._format, bzrdir.BzrDirMetaFormat1))
 
171
        branch = control.open_branch()
 
172
        self.assertEquals(branch.revision_history(),
 
173
           ['mbp@sourcefrog.net-20051004035611-176b16534b086b3c',
 
174
            'mbp@sourcefrog.net-20051004035756-235f2b7dcdddd8dd'])
 
175
 
 
176
    def test_upgrade_rich_root(self):
 
177
        tree = self.make_branch_and_tree('tree', format='rich-root')
 
178
        rev_id = tree.commit('first post')
 
179
        upgrade('tree')
 
180
 
 
181
    def test_convert_branch5_branch6(self):
 
182
        branch = self.make_branch('branch', format='knit')
 
183
        branch.set_revision_history(['AB', 'CD'])
 
184
        branch.set_parent('file:///EF')
 
185
        branch.set_bound_location('file:///GH')
 
186
        branch.set_push_location('file:///IJ')
 
187
        target = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
 
188
        converter = branch.bzrdir._format.get_converter(target)
 
189
        converter.convert(branch.bzrdir, None)
 
190
        new_branch = _mod_branch.Branch.open(self.get_url('branch'))
 
191
        self.assertIs(new_branch.__class__, _mod_branch.BzrBranch6)
 
192
        self.assertEqual('CD', new_branch.last_revision())
 
193
        self.assertEqual('file:///EF', new_branch.get_parent())
 
194
        self.assertEqual('file:///GH', new_branch.get_bound_location())
 
195
        branch_config = new_branch.get_config()._get_branch_data_config()
 
196
        self.assertEqual('file:///IJ',
 
197
            branch_config.get_user_option('push_location'))
 
198
 
 
199
        branch2 = self.make_branch('branch2', format='knit')
 
200
        converter = branch2.bzrdir._format.get_converter(target)
 
201
        converter.convert(branch2.bzrdir, None)
 
202
        branch2 = _mod_branch.Branch.open(self.get_url('branch'))
 
203
        self.assertIs(branch2.__class__, _mod_branch.BzrBranch6)
 
204
 
 
205
    def test_convert_branch7_branch8(self):
 
206
        branch = self.make_branch('branch', format='1.9')
 
207
        target = bzrdir.format_registry.make_bzrdir('1.9')
 
208
        target.set_branch_format(_mod_branch.BzrBranchFormat8())
 
209
        converter = branch.bzrdir._format.get_converter(target)
 
210
        converter.convert(branch.bzrdir, None)
 
211
        branch = _mod_branch.Branch.open(self.get_url('branch'))
 
212
        self.assertIs(branch.__class__, _mod_branch.BzrBranch8)
 
213
        self.assertEqual({}, branch._get_all_reference_info())
 
214
 
 
215
    def test_convert_knit_dirstate_empty(self):
 
216
        # test that asking for an upgrade from knit to dirstate works.
 
217
        tree = self.make_branch_and_tree('tree', format='knit')
 
218
        target = bzrdir.format_registry.make_bzrdir('dirstate')
 
219
        converter = tree.bzrdir._format.get_converter(target)
 
220
        converter.convert(tree.bzrdir, None)
 
221
        new_tree = workingtree.WorkingTree.open('tree')
 
222
        self.assertIs(new_tree.__class__, workingtree_4.WorkingTree4)
 
223
        self.assertEqual('null:', new_tree.last_revision())
 
224
 
 
225
    def test_convert_knit_dirstate_content(self):
 
226
        # smoke test for dirstate conversion: we call dirstate primitives,
 
227
        # and its there that the core logic is tested.
 
228
        tree = self.make_branch_and_tree('tree', format='knit')
 
229
        self.build_tree(['tree/file'])
 
230
        tree.add(['file'], ['file-id'])
 
231
        target = bzrdir.format_registry.make_bzrdir('dirstate')
 
232
        converter = tree.bzrdir._format.get_converter(target)
 
233
        converter.convert(tree.bzrdir, None)
 
234
        new_tree = workingtree.WorkingTree.open('tree')
 
235
        self.assertIs(new_tree.__class__, workingtree_4.WorkingTree4)
 
236
        self.assertEqual('null:', new_tree.last_revision())
 
237
 
 
238
    def test_convert_knit_one_parent_dirstate(self):
 
239
        # test that asking for an upgrade from knit to dirstate works.
 
240
        tree = self.make_branch_and_tree('tree', format='knit')
 
241
        rev_id = tree.commit('first post')
 
242
        target = bzrdir.format_registry.make_bzrdir('dirstate')
 
243
        converter = tree.bzrdir._format.get_converter(target)
 
244
        converter.convert(tree.bzrdir, None)
 
245
        new_tree = workingtree.WorkingTree.open('tree')
 
246
        self.assertIs(new_tree.__class__, workingtree_4.WorkingTree4)
 
247
        self.assertEqual(rev_id, new_tree.last_revision())
 
248
        for path in ['basis-inventory-cache', 'inventory', 'last-revision',
 
249
            'pending-merges', 'stat-cache']:
 
250
            self.failIfExists('tree/.bzr/checkout/' + path)
 
251
 
 
252
    def test_convert_knit_merges_dirstate(self):
 
253
        tree = self.make_branch_and_tree('tree', format='knit')
 
254
        rev_id = tree.commit('first post')
 
255
        merge_tree = tree.bzrdir.sprout('tree2').open_workingtree()
 
256
        rev_id2 = tree.commit('second post')
 
257
        rev_id3 = merge_tree.commit('second merge post')
 
258
        tree.merge_from_branch(merge_tree.branch)
 
259
        target = bzrdir.format_registry.make_bzrdir('dirstate')
 
260
        converter = tree.bzrdir._format.get_converter(target)
 
261
        converter.convert(tree.bzrdir, None)
 
262
        new_tree = workingtree.WorkingTree.open('tree')
 
263
        self.assertIs(new_tree.__class__, workingtree_4.WorkingTree4)
 
264
        self.assertEqual(rev_id2, new_tree.last_revision())
 
265
        self.assertEqual([rev_id2, rev_id3], new_tree.get_parent_ids())
 
266
        for path in ['basis-inventory-cache', 'inventory', 'last-revision',
 
267
            'pending-merges', 'stat-cache']:
 
268
            self.failIfExists('tree/.bzr/checkout/' + path)
85
269
 
86
270
 
87
271
_upgrade1_template = \
89
273
     ('foo', 'new contents\n'),
90
274
     ('.bzr/',),
91
275
     ('.bzr/README',
92
 
      'This is a Bazaar-NG control directory.\nDo not change any files in this directory.\n'),
 
276
      'This is a Bazaar control directory.\n'
 
277
      'Do not change any files in this directory.\n'
 
278
      'See http://bazaar-vcs.org/ for more information about Bazaar.\n'),
93
279
     ('.bzr/branch-format', 'Bazaar-NG branch, format 0.0.4\n'),
94
280
     ('.bzr/revision-history',
95
281
      'mbp@sourcefrog.net-20051004035611-176b16534b086b3c\n'
129
315
    ),
130
316
    ( './.bzr/', ),
131
317
    ( './.bzr/README',
132
 
        'This is a Bazaar-NG control directory.\n'
133
 
        'Do not change any files in this directory.\n'
 
318
      'This is a Bazaar control directory.\n'
 
319
      'Do not change any files in this directory.\n'
 
320
      'See http://bazaar-vcs.org/ for more information about Bazaar.\n'
134
321
    ),
135
322
    ( './.bzr/branch-format',
136
323
        'Bazaar-NG branch, format 0.0.4\n'
194
381
_upgrade_dir_template = [
195
382
    ( './.bzr/', ),
196
383
    ( './.bzr/README',
197
 
        'This is a Bazaar-NG control directory.\n'
198
 
        'Do not change any files in this directory.\n'
 
384
      'This is a Bazaar control directory.\n'
 
385
      'Do not change any files in this directory.\n'
 
386
      'See http://bazaar-vcs.org/ for more information about Bazaar.\n'
199
387
    ),
200
388
    ( './.bzr/branch-format',
201
389
        'Bazaar-NG branch, format 0.0.4\n'