~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

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