21
21
from bzrlib import (
28
25
from bzrlib.tests import (
30
28
TestCaseWithTransport,
32
30
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
33
from bzrlib.repofmt.knitpack_repo import RepositoryFormatKnitPack1
36
class OldBzrDir(bzrdir.BzrDirMeta1):
37
"""An test bzr dir implementation"""
39
def needs_format_conversion(self, format):
40
return not isinstance(format, self.__class__)
43
class ConvertOldTestToMeta(controldir.Converter):
44
"""A trivial converter, used for testing."""
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(
50
bzrdir.BzrDirMetaFormat1().get_format_string(),
51
mode=to_convert._get_file_mode())
52
return bzrdir.BzrDir.open(to_convert.user_url)
55
class OldBzrDirFormat(bzrdir.BzrDirMetaFormat1):
57
_lock_class = lockable_files.TransportLock
59
def get_converter(self, format=None):
60
return ConvertOldTestToMeta()
62
def get_format_string(self):
63
return "Ancient Test Format"
65
def _open(self, transport):
66
return OldBzrDir(transport, self)
31
from bzrlib.transport import get_transport
32
from bzrlib.repofmt.knitrepo import (
33
RepositoryFormatKnit1,
69
37
class TestWithUpgradableBranches(TestCaseWithTransport):
72
40
super(TestWithUpgradableBranches, self).setUp()
41
self.addCleanup(bzrdir.BzrDirFormat._set_default_format,
42
bzrdir.BzrDirFormat.get_default_format())
74
44
def make_current_format_branch_and_checkout(self):
75
45
current_tree = self.make_branch_and_tree('current_format_branch',
77
47
current_tree.branch.create_checkout(
78
48
self.get_url('current_format_checkout'), lightweight=True)
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())
55
def make_metadir_weave_branch(self):
56
self.make_branch_and_tree('metadir_weave_branch', format='metaweave')
80
58
def test_readonly_url_error(self):
81
self.make_branch_and_tree("old_format_branch", format="knit")
59
self.make_format_5_branch()
82
60
(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'
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")
89
65
def test_upgrade_up_to_date(self):
90
66
self.make_current_format_branch_and_checkout()
91
67
# when up to date we should get a message to that effect
92
68
(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'
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)
99
74
def test_upgrade_up_to_date_checkout_warns_branch_left_alone(self):
100
75
self.make_current_format_branch_and_checkout()
101
76
# when upgrading a checkout, the branch location and a suggestion
102
77
# to upgrade it should be emitted even if the checkout is up to
104
burl = self.get_transport('current_format_branch').local_abspath(".")
105
curl = self.get_transport('current_format_checkout').local_abspath(".")
106
79
(out, err) = self.run_bzr('upgrade current_format_checkout', retcode=3)
108
'Upgrading branch %s/ ...\nThis is a checkout.'
109
' The branch (%s/) needs to be upgraded separately.\n'
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'
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,
84
self.assertEqualDiff("bzr: ERROR: The branch format Meta "
85
"directory format 1 is already at the most "
86
"recent format.\n", err)
118
88
def test_upgrade_checkout(self):
119
89
# upgrading a checkout should work
124
94
# anonymous branch
127
def test_upgrade_branch_in_repo(self):
97
def test_ugrade_branch_in_repo(self):
128
98
# upgrading a branch in a repo should warn about not upgrading the repo
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(),
137
self.addCleanup(controldir.ControlDirFormat._set_default_format,
138
controldir.ControlDirFormat.get_default_format())
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)
145
display_url = transport.local_abspath('.')
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
146
105
# check --format takes effect
147
controldir.ControlDirFormat._set_default_format(old_format)
106
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
148
107
backup_dir = 'backup.bzr.~1~'
149
108
(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
155
starting upgrade from old test format to 2a
109
['upgrade', '--format=metaweave', url])
110
self.assertEqualDiff("""starting upgrade of %s
111
making backup of %s.bzr
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
157
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
118
""" % (url, url, url, backup_dir), out)
158
119
self.assertEqualDiff("", err)
159
120
self.assertTrue(isinstance(
160
bzrdir.BzrDir.open(self.get_url(path))._format,
121
bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
161
122
bzrdir.BzrDirMetaFormat1))
163
124
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')
169
display_url = transport.local_abspath('.')
125
# users can force an upgrade to knit format from a metadir weave
127
self.make_metadir_weave_branch()
128
url = get_transport(self.get_url('metadir_weave_branch')).base
170
129
# check --format takes effect
130
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
171
131
backup_dir = 'backup.bzr.~1~'
172
132
(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
133
['upgrade', '--format=knit', url])
134
self.assertEqualDiff("""starting upgrade of %s
135
making backup of %s.bzr
178
137
starting repository conversion
179
138
repository converted
181
""" % (display_url, display_url, display_url, display_url, backup_dir),
140
""" % (url, url, url, backup_dir), out)
183
141
self.assertEqualDiff("", err)
184
converted_dir = bzrdir.BzrDir.open(self.get_url('branch'))
142
converted_dir = bzrdir.BzrDir.open(self.get_url('metadir_weave_branch'))
185
143
self.assertTrue(isinstance(converted_dir._format,
186
144
bzrdir.BzrDirMetaFormat1))
187
145
self.assertTrue(isinstance(converted_dir.open_repository()._format,
188
RepositoryFormatKnitPack1))
146
RepositoryFormatKnit1))
190
148
def test_upgrade_repo(self):
191
self.run_bzr('init-repository --format=pack-0.92 repo')
192
self.run_bzr('upgrade --format=2a repo')
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,))
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)
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))
213
def test_upgrade_dry_run_supported(self):
214
self.assertLegalOption('--dry-run')
215
self.assertBranchFormat('branch-foo', 'pack-0.92')
149
self.run_bzr('init-repository --format=metaweave repo')
150
self.run_bzr('upgrade --format=knit repo')
217
152
def test_upgrade_permission_check(self):
218
153
"""'backup.bzr' should retain permissions of .bzr. Bug #262450"""
225
160
new_perms = os.stat(backup_dir).st_mode & 0777
226
161
self.assertTrue(new_perms == old_perms)
228
164
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")
232
display_url = t.local_abspath('.')
165
self.make_format_5_branch()
166
transport = get_transport(self.get_url('format_5_branch'))
168
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
233
169
backup_dir1 = 'backup.bzr.~1~'
234
170
backup_dir2 = 'backup.bzr.~2~'
235
171
# explicitly create backup_dir1. bzr should create the .~2~ directory
173
transport.mkdir(backup_dir1)
238
174
(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
244
starting repository conversion
175
['upgrade', '--format=metaweave', url])
176
self.assertEqualDiff("""starting upgrade of %s
177
making backup of %s.bzr
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
247
""" % (display_url, display_url, display_url, display_url, backup_dir2), out)
184
""" % (url, url, url, backup_dir2), out)
248
185
self.assertEqualDiff("", err)
249
186
self.assertTrue(isinstance(
250
bzrdir.BzrDir.open(self.get_url("old_format_branch"))._format,
187
bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
251
188
bzrdir.BzrDirMetaFormat1))
252
self.assertTrue(t.has(backup_dir2))
189
self.assertTrue(transport.has(backup_dir2))
255
191
class SFTPTests(TestCaseWithSFTPServer):
256
192
"""Tests for upgrade over sftp."""
258
194
def test_upgrade_url(self):
259
self.run_bzr('init --format=pack-0.92')
260
t = self.get_transport()
195
self.run_bzr('init --format=weave')
196
t = get_transport(self.get_url())
262
display_url = urlutils.unescape_for_display(url,
264
out, err = self.run_bzr(['upgrade', '--format=2a', url])
198
out, err = self.run_bzr(['upgrade', '--format=knit', url])
265
199
backup_dir = 'backup.bzr.~1~'
266
self.assertEqualDiff("""Upgrading branch %s ...
267
starting upgrade of %s
200
self.assertEqualDiff("""starting upgrade of %s
268
201
making backup of %s.bzr
203
starting upgrade from format 6 to metadir
270
204
starting repository conversion
271
205
repository converted
273
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
207
""" % (url, url, url,backup_dir), out)
274
208
self.assertEqual('', err)