~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_upgrade.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-05-04 12:10:51 UTC
  • mfrom: (5819.1.4 777007-developer-doc)
  • Revision ID: pqm@pqm.ubuntu.com-20110504121051-aovlsmqiivjmc4fc
(jelmer) Small fixes to developer documentation. (Jonathan Riddell)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
2
 
# Authors: Robert Collins <robert.collins@canonical.com>
3
 
# -*- coding: utf-8 -*-
 
1
# Copyright (C) 2006-2011 Canonical Ltd
4
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
14
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
 
# 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
18
16
 
19
17
"""Black box tests for the upgrade ui."""
20
 
 
21
18
import os
 
19
import stat
22
20
 
23
 
import bzrlib
24
 
import bzrlib.bzrdir as bzrdir
25
 
import bzrlib.repository as repository
26
 
from bzrlib.tests import TestCaseWithTransport
27
 
from bzrlib.tests.blackbox import TestUIFactory
 
21
from bzrlib import (
 
22
    bzrdir,
 
23
    controldir,
 
24
    lockable_files,
 
25
    ui,
 
26
    )
 
27
from bzrlib.tests import (
 
28
    features,
 
29
    TestCaseWithTransport,
 
30
    )
28
31
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
29
 
from bzrlib.transport import get_transport
30
 
import bzrlib.ui as ui
 
32
from bzrlib.repofmt.knitpack_repo import RepositoryFormatKnitPack1
 
33
 
 
34
 
 
35
class OldBzrDir(bzrdir.BzrDirMeta1):
 
36
    """An test bzr dir implementation"""
 
37
 
 
38
    def needs_format_conversion(self, format):
 
39
        return not isinstance(format, self.__class__)
 
40
 
 
41
 
 
42
class ConvertOldTestToMeta(controldir.Converter):
 
43
    """A trivial converter, used for testing."""
 
44
 
 
45
    def convert(self, to_convert, pb):
 
46
        ui.ui_factory.note('starting upgrade from old test format to 2a')
 
47
        to_convert.control_transport.put_bytes(
 
48
            'branch-format',
 
49
            bzrdir.BzrDirMetaFormat1().get_format_string(),
 
50
            mode=to_convert._get_file_mode())
 
51
        return bzrdir.BzrDir.open(to_convert.user_url)
 
52
 
 
53
 
 
54
class OldBzrDirFormat(bzrdir.BzrDirMetaFormat1):
 
55
 
 
56
    _lock_class = lockable_files.TransportLock
 
57
 
 
58
    def get_converter(self, format=None):
 
59
        return ConvertOldTestToMeta()
 
60
 
 
61
    def get_format_string(self):
 
62
        return "Ancient Test Format"
 
63
 
 
64
    def _open(self, transport):
 
65
        return OldBzrDir(transport, self)
31
66
 
32
67
 
33
68
class TestWithUpgradableBranches(TestCaseWithTransport):
34
69
 
35
70
    def setUp(self):
36
71
        super(TestWithUpgradableBranches, self).setUp()
37
 
        self.old_format = bzrdir.BzrDirFormat.get_default_format()
38
 
        self.old_repo_format = \
39
 
            bzrlib.repository.RepositoryFormat.get_default_format()
40
 
        self.old_ui_factory = ui.ui_factory
41
 
        self.addCleanup(self.restoreDefaults)
42
 
 
43
 
        ui.ui_factory = TestUIFactory()
44
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
45
 
        bzrlib.repository.RepositoryFormat.set_default_format(
46
 
            bzrlib.repository.RepositoryFormat7())
47
 
        # FIXME RBC 20060120 we should be able to do this via ui calls only.
48
 
        # setup a format 5 branch we can upgrade from.
49
 
        t = get_transport(self.get_url())
50
 
        t.mkdir('format_5_branch')
51
 
        bzrdir.BzrDirFormat5().initialize(self.get_url('format_5_branch'))
52
 
        bzrdir.BzrDir.create_standalone_workingtree('current_format_branch')
53
 
        d = bzrdir.BzrDir.create('metadir_weave_branch')
54
 
        d.create_repository()
55
 
        d.create_branch()
56
 
        d.create_workingtree()
57
 
        self.run_bzr('checkout',
58
 
                     '--lightweight',
59
 
                     self.get_url('current_format_branch'),
60
 
                     'current_format_checkout')
61
 
 
62
 
    def restoreDefaults(self):
63
 
        bzrdir.BzrDirFormat.set_default_format(self.old_format)
64
 
        bzrlib.repository.RepositoryFormat.set_default_format(
65
 
            self.old_repo_format)
66
 
        ui.ui_factory = self.old_ui_factory
 
72
 
 
73
    def make_current_format_branch_and_checkout(self):
 
74
        current_tree = self.make_branch_and_tree('current_format_branch',
 
75
                                                 format='default')
 
76
        current_tree.branch.create_checkout(
 
77
            self.get_url('current_format_checkout'), lightweight=True)
67
78
 
68
79
    def test_readonly_url_error(self):
69
 
        (out, err) = self.run_bzr_captured(
70
 
            ['upgrade', self.get_readonly_url('format_5_branch')], 3)
71
 
        self.assertEqual(out, "")
72
 
        self.assertEqual(err, "bzr: ERROR: Upgrade URL cannot work with readonly URLs.\n")
 
80
        self.make_branch_and_tree("old_format_branch", format="knit")
 
81
        (out, err) = self.run_bzr(
 
82
            ['upgrade', self.get_readonly_url("old_format_branch")], retcode=3)
 
83
        err_msg = 'Upgrade URL cannot work with readonly URLs.'
 
84
        self.assertEqualDiff('conversion error: %s\nbzr: ERROR: %s\n'
 
85
                             % (err_msg, err_msg),
 
86
                             err)
73
87
 
74
88
    def test_upgrade_up_to_date(self):
 
89
        self.make_current_format_branch_and_checkout()
75
90
        # when up to date we should get a message to that effect
76
 
        (out, err) = self.run_bzr_captured(
77
 
            ['upgrade', 'current_format_branch'], 3)
78
 
        self.assertEqual("", out)
79
 
        self.assertEqualDiff("bzr: ERROR: The branch format Bazaar-NG meta "
80
 
                             "directory, format 1 is already at the most "
81
 
                             "recent format.\n", err)
 
91
        (out, err) = self.run_bzr('upgrade current_format_branch', retcode=3)
 
92
        err_msg = ('The branch format %s is already at the most recent format.'
 
93
                   % ('Meta directory format 1'))
 
94
        self.assertEqualDiff('conversion error: %s\nbzr: ERROR: %s\n'
 
95
                             % (err_msg, err_msg),
 
96
                             err)
82
97
 
83
98
    def test_upgrade_up_to_date_checkout_warns_branch_left_alone(self):
 
99
        self.make_current_format_branch_and_checkout()
84
100
        # when upgrading a checkout, the branch location and a suggestion
85
 
        # to upgrade it should be emitted even if the checkout is up to 
 
101
        # to upgrade it should be emitted even if the checkout is up to
86
102
        # date
87
 
        (out, err) = self.run_bzr_captured(
88
 
            ['upgrade', 'current_format_checkout'], 3)
89
 
        self.assertEqual("This is a checkout. The branch (%s) needs to be "
90
 
                         "upgraded separately.\n" 
91
 
                         % get_transport(self.get_url('current_format_branch')).base,
92
 
                         out)
93
 
        self.assertEqualDiff("bzr: ERROR: The branch format Bazaar-NG meta "
94
 
                             "directory, format 1 is already at the most "
95
 
                             "recent format.\n", err)
 
103
        burl = self.get_transport('current_format_branch').base
 
104
        curl = self.get_transport('current_format_checkout').base
 
105
        (out, err) = self.run_bzr('upgrade current_format_checkout', retcode=3)
 
106
        self.assertEqual(
 
107
            'Upgrading branch %s ...\nThis is a checkout.'
 
108
            ' The branch (%s) needs to be upgraded separately.\n'
 
109
            % (curl, burl),
 
110
            out)
 
111
        msg = 'The branch format %s is already at the most recent format.' % (
 
112
            'Meta directory format 1')
 
113
        self.assertEqualDiff('conversion error: %s\nbzr: ERROR: %s\n'
 
114
                             % (msg, msg),
 
115
                             err)
96
116
 
97
117
    def test_upgrade_checkout(self):
98
118
        # upgrading a checkout should work
99
119
        pass
100
120
 
101
121
    def test_upgrade_repository_scans_branches(self):
102
 
        # we should get individual upgrade notes for each branch even the 
 
122
        # we should get individual upgrade notes for each branch even the
103
123
        # anonymous branch
104
124
        pass
105
125
 
106
 
    def test_ugrade_branch_in_repo(self):
 
126
    def test_upgrade_branch_in_repo(self):
107
127
        # upgrading a branch in a repo should warn about not upgrading the repo
108
128
        pass
109
129
 
110
 
    def test_upgrade_explicit_metaformat(self):
111
 
        # users can force an upgrade to metadir format.
112
 
        url = get_transport(self.get_url('format_5_branch')).base
 
130
    def test_upgrade_control_dir(self):
 
131
        old_format = OldBzrDirFormat()
 
132
        self.addCleanup(bzrdir.BzrProber.formats.remove,
 
133
            old_format.get_format_string())
 
134
        bzrdir.BzrProber.formats.register(old_format.get_format_string(),
 
135
            old_format)
 
136
        self.addCleanup(controldir.ControlDirFormat._set_default_format,
 
137
                        controldir.ControlDirFormat.get_default_format())
 
138
 
 
139
        # setup an old format branch we can upgrade from.
 
140
        path = 'old_format_branch'
 
141
        self.make_branch_and_tree(path, format=old_format)
 
142
        url = self.get_transport(path).base
113
143
        # check --format takes effect
114
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
115
 
        (out, err) = self.run_bzr_captured(
116
 
            ['upgrade', '--format=metaweave', url])
117
 
        self.assertEqualDiff("""starting upgrade of %s
118
 
making backup of tree history
119
 
%s.bzr has been backed up to %s.bzr.backup
120
 
if conversion fails, you can move this directory back to .bzr
121
 
if it succeeds, you can remove this directory if you wish
122
 
starting upgrade from format 5 to 6
123
 
adding prefixes to weaves
124
 
adding prefixes to revision-store
125
 
starting upgrade from format 6 to metadir
 
144
        controldir.ControlDirFormat._set_default_format(old_format)
 
145
        backup_dir = 'backup.bzr.~1~'
 
146
        (out, err) = self.run_bzr(
 
147
            ['upgrade', '--format=2a', url])
 
148
        self.assertEqualDiff("""Upgrading branch %s ...
 
149
starting upgrade of %s
 
150
making backup of %s.bzr
 
151
  to %s%s
 
152
starting upgrade from old test format to 2a
126
153
finished
127
 
""" % (url, url, url), out)
 
154
""" % (url, url, url, url, backup_dir), out)
128
155
        self.assertEqualDiff("", err)
129
156
        self.assertTrue(isinstance(
130
 
            bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
 
157
            bzrdir.BzrDir.open(self.get_url(path))._format,
131
158
            bzrdir.BzrDirMetaFormat1))
132
159
 
133
160
    def test_upgrade_explicit_knit(self):
134
 
        # users can force an upgrade to knit format from a metadir weave 
135
 
        # branch
136
 
        url = get_transport(self.get_url('metadir_weave_branch')).base
 
161
        # users can force an upgrade to knit format from a metadir pack 0.92
 
162
        # branch to a 2a branch.
 
163
        self.make_branch_and_tree('branch', format='knit')
 
164
        url = self.get_transport('branch').base
137
165
        # check --format takes effect
138
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
139
 
        (out, err) = self.run_bzr_captured(
140
 
            ['upgrade', '--format=knit', url])
141
 
        self.assertEqualDiff("""starting upgrade of %s
142
 
making backup of tree history
143
 
%s.bzr has been backed up to %s.bzr.backup
144
 
if conversion fails, you can move this directory back to .bzr
145
 
if it succeeds, you can remove this directory if you wish
 
166
        backup_dir = 'backup.bzr.~1~'
 
167
        (out, err) = self.run_bzr(
 
168
            ['upgrade', '--format=pack-0.92', url])
 
169
        self.assertEqualDiff("""Upgrading branch %s ...
 
170
starting upgrade of %s
 
171
making backup of %s.bzr
 
172
  to %s%s
146
173
starting repository conversion
147
174
repository converted
148
175
finished
149
 
""" % (url, url, url), out)
 
176
""" % (url, url, url, url, backup_dir),
 
177
                             out)
150
178
        self.assertEqualDiff("", err)
151
 
        converted_dir = bzrdir.BzrDir.open(self.get_url('metadir_weave_branch'))
 
179
        converted_dir = bzrdir.BzrDir.open(self.get_url('branch'))
152
180
        self.assertTrue(isinstance(converted_dir._format,
153
181
                                   bzrdir.BzrDirMetaFormat1))
154
182
        self.assertTrue(isinstance(converted_dir.open_repository()._format,
155
 
                                   repository.RepositoryFormatKnit1))
 
183
                                   RepositoryFormatKnitPack1))
156
184
 
157
185
    def test_upgrade_repo(self):
158
 
        self.run_bzr('init-repository', '--format=metaweave', 'repo')
159
 
        self.run_bzr('upgrade', '--format=knit', 'repo')
 
186
        self.run_bzr('init-repository --format=pack-0.92 repo')
 
187
        self.run_bzr('upgrade --format=2a repo')
 
188
 
 
189
    def assertLegalOption(self, option_str):
 
190
        # Confirm that an option is legal. (Lower level tests are
 
191
        # expected to validate the actual functionality.)
 
192
        self.run_bzr('init --format=pack-0.92 branch-foo')
 
193
        self.run_bzr('upgrade --format=2a branch-foo %s' % (option_str,))
 
194
 
 
195
    def assertBranchFormat(self, dir, format):
 
196
        branch = bzrdir.BzrDir.open_tree_or_branch(self.get_url(dir))[1]
 
197
        branch_format = branch._format
 
198
        meta_format = bzrdir.format_registry.make_bzrdir(format)
 
199
        expected_format = meta_format.get_branch_format()
 
200
        self.assertEqual(expected_format, branch_format)
 
201
 
 
202
    def test_upgrade_clean_supported(self):
 
203
        self.assertLegalOption('--clean')
 
204
        self.assertBranchFormat('branch-foo', '2a')
 
205
        backup_bzr_dir = os.path.join("branch-foo", "backup.bzr.~1~")
 
206
        self.assertFalse(os.path.exists(backup_bzr_dir))
 
207
 
 
208
    def test_upgrade_dry_run_supported(self):
 
209
        self.assertLegalOption('--dry-run')
 
210
        self.assertBranchFormat('branch-foo', 'pack-0.92')
 
211
 
 
212
    def test_upgrade_permission_check(self):
 
213
        """'backup.bzr' should retain permissions of .bzr. Bug #262450"""
 
214
        self.requireFeature(features.posix_permissions_feature)
 
215
        old_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
 
216
        backup_dir = 'backup.bzr.~1~'
 
217
        self.run_bzr('init --format=1.6')
 
218
        os.chmod('.bzr', old_perms)
 
219
        self.run_bzr('upgrade')
 
220
        new_perms = os.stat(backup_dir).st_mode & 0777
 
221
        self.assertTrue(new_perms == old_perms)
 
222
 
 
223
    def test_upgrade_with_existing_backup_dir(self):
 
224
        self.make_branch_and_tree("old_format_branch", format="knit")
 
225
        t = self.get_transport("old_format_branch")
 
226
        url = t.base
 
227
        backup_dir1 = 'backup.bzr.~1~'
 
228
        backup_dir2 = 'backup.bzr.~2~'
 
229
        # explicitly create backup_dir1. bzr should create the .~2~ directory
 
230
        # as backup
 
231
        t.mkdir(backup_dir1)
 
232
        (out, err) = self.run_bzr(
 
233
            ['upgrade', '--format=2a', url])
 
234
        self.assertEqualDiff("""Upgrading branch %s ...
 
235
starting upgrade of %s
 
236
making backup of %s.bzr
 
237
  to %s%s
 
238
starting repository conversion
 
239
repository converted
 
240
finished
 
241
""" % (url, url, url, url, backup_dir2), out)
 
242
        self.assertEqualDiff("", err)
 
243
        self.assertTrue(isinstance(
 
244
            bzrdir.BzrDir.open(self.get_url("old_format_branch"))._format,
 
245
            bzrdir.BzrDirMetaFormat1))
 
246
        self.assertTrue(t.has(backup_dir2))
160
247
 
161
248
 
162
249
class SFTPTests(TestCaseWithSFTPServer):
163
250
    """Tests for upgrade over sftp."""
164
251
 
165
 
    def setUp(self):
166
 
        super(SFTPTests, self).setUp()
167
 
        self.old_ui_factory = ui.ui_factory
168
 
        self.addCleanup(self.restoreDefaults)
169
 
 
170
 
        ui.ui_factory = TestUIFactory()
171
 
 
172
 
    def restoreDefaults(self):
173
 
        ui.ui_factory = self.old_ui_factory
174
 
 
175
252
    def test_upgrade_url(self):
176
 
        self.run_bzr('init', '--format=weave')
177
 
        t = get_transport(self.get_url())
 
253
        self.run_bzr('init --format=pack-0.92')
 
254
        t = self.get_transport()
178
255
        url = t.base
179
 
        out, err = self.run_bzr('upgrade', '--format=knit', url)
180
 
        self.assertEqualDiff("""starting upgrade of %s
181
 
making backup of tree history
182
 
%s.bzr has been backed up to %s.bzr.backup
183
 
if conversion fails, you can move this directory back to .bzr
184
 
if it succeeds, you can remove this directory if you wish
185
 
starting upgrade from format 6 to metadir
 
256
        out, err = self.run_bzr(['upgrade', '--format=2a', url])
 
257
        backup_dir = 'backup.bzr.~1~'
 
258
        self.assertEqualDiff("""Upgrading branch %s ...
 
259
starting upgrade of %s
 
260
making backup of %s.bzr
 
261
  to %s%s
186
262
starting repository conversion
187
263
repository converted
188
264
finished
189
 
""" % (url, url, url), out)
 
265
""" % (url, url, url, url,backup_dir), out)
190
266
        self.assertEqual('', err)
 
267
 
 
268
 
 
269
class UpgradeRecommendedTests(TestCaseWithTransport):
 
270
 
 
271
    def test_recommend_upgrade_wt4(self):
 
272
        # using a deprecated format gives a warning
 
273
        self.run_bzr('init --knit a')
 
274
        out, err = self.run_bzr('status a')
 
275
        self.assertContainsRe(err, 'bzr upgrade .*[/\\\\]a')
 
276
 
 
277
    def test_no_upgrade_recommendation_from_bzrdir(self):
 
278
        # we should only get a recommendation to upgrade when we're accessing
 
279
        # the actual workingtree, not when we only open a bzrdir that contains
 
280
        # an old workngtree
 
281
        self.run_bzr('init --knit a')
 
282
        out, err = self.run_bzr('revno a')
 
283
        if err.find('upgrade') > -1:
 
284
            self.fail("message shouldn't suggest upgrade:\n%s" % err)
 
285
 
 
286
    def test_upgrade_shared_repo(self):
 
287
        repo = self.make_repository('repo', format='2a', shared=True)
 
288
        branch = self.make_branch_and_tree('repo/branch', format="pack-0.92")
 
289
        self.get_transport('repo/branch/.bzr/repository').delete_tree('.')
 
290
        out, err = self.run_bzr(['upgrade'], working_dir='repo/branch')