~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2010-01-05 04:30:07 UTC
  • mfrom: (4932 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4934.
  • Revision ID: john@arbash-meinel.com-20100105043007-ehgbldqd3q0gtyws
Merge bzr.dev, resolve conflicts.

Show diffs side-by-side

added added

removed removed

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