1
# Copyright (C) 2006-2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Black box tests for the upgrade ui."""
28
from breezy.bzr import (
31
from breezy.tests import (
33
TestCaseWithTransport,
35
from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer
36
from breezy.bzr.knitpack_repo import RepositoryFormatKnitPack1
39
class OldBzrDir(bzrdir.BzrDirMeta1):
40
"""An test brz dir implementation"""
42
def needs_format_conversion(self, format):
43
return not isinstance(format, self.__class__)
46
class ConvertOldTestToMeta(controldir.Converter):
47
"""A trivial converter, used for testing."""
49
def convert(self, to_convert, pb):
50
ui.ui_factory.note('starting upgrade from old test format to 2a')
51
to_convert.control_transport.put_bytes(
53
bzrdir.BzrDirMetaFormat1().get_format_string(),
54
mode=to_convert._get_file_mode())
55
return controldir.ControlDir.open(to_convert.user_url)
58
class OldBzrDirFormat(bzrdir.BzrDirMetaFormat1):
60
_lock_class = lockable_files.TransportLock
62
def get_converter(self, format=None):
63
return ConvertOldTestToMeta()
66
def get_format_string(cls):
67
return "Ancient Test Format"
69
def _open(self, transport):
70
return OldBzrDir(transport, self)
73
class TestWithUpgradableBranches(TestCaseWithTransport):
76
super(TestWithUpgradableBranches, self).setUp()
78
def make_current_format_branch_and_checkout(self):
79
current_tree = self.make_branch_and_tree('current_format_branch',
81
current_tree.branch.create_checkout(
82
self.get_url('current_format_checkout'), lightweight=True)
84
def test_readonly_url_error(self):
85
self.make_branch_and_tree("old_format_branch", format="knit")
86
(out, err) = self.run_bzr(
87
['upgrade', self.get_readonly_url("old_format_branch")], retcode=3)
88
err_msg = 'Upgrade URL cannot work with readonly URLs.'
89
self.assertEqualDiff('conversion error: %s\nbrz: ERROR: %s\n'
93
def test_upgrade_up_to_date(self):
94
self.make_current_format_branch_and_checkout()
95
# when up to date we should get a message to that effect
96
burl = self.get_transport('current_format_branch').local_abspath(".")
97
(out, err) = self.run_bzr('upgrade current_format_branch', retcode=0)
99
'Upgrading branch %s/ ...\n'
100
'The branch format %s is already at the most recent format.\n'
101
% (burl, 'Meta directory format 1'),
104
def test_upgrade_up_to_date_checkout_warns_branch_left_alone(self):
105
self.make_current_format_branch_and_checkout()
106
# when upgrading a checkout, the branch location and a suggestion
107
# to upgrade it should be emitted even if the checkout is up to
109
burl = self.get_transport('current_format_branch').local_abspath(".")
110
curl = self.get_transport('current_format_checkout').local_abspath(".")
111
(out, err) = self.run_bzr('upgrade current_format_checkout', retcode=0)
113
'Upgrading branch %s/ ...\nThis is a checkout.'
114
' The branch (%s/) needs to be upgraded separately.\n'
115
'The branch format %s is already at the most recent format.\n'
116
% (curl, burl, 'Meta directory format 1'),
119
def test_upgrade_checkout(self):
120
# upgrading a checkout should work
123
def test_upgrade_repository_scans_branches(self):
124
# we should get individual upgrade notes for each branch even the
128
def test_upgrade_branch_in_repo(self):
129
# upgrading a branch in a repo should warn about not upgrading the repo
132
def test_upgrade_control_dir(self):
133
old_format = OldBzrDirFormat()
134
self.addCleanup(bzr.BzrProber.formats.remove,
135
old_format.get_format_string())
136
bzr.BzrProber.formats.register(old_format.get_format_string(),
138
self.addCleanup(controldir.ControlDirFormat._set_default_format,
139
controldir.ControlDirFormat.get_default_format())
141
# setup an old format branch we can upgrade from.
142
path = 'old_format_branch'
143
self.make_branch_and_tree(path, format=old_format)
144
transport = self.get_transport(path)
146
display_url = transport.local_abspath('.')
147
# check --format takes effect
148
controldir.ControlDirFormat._set_default_format(old_format)
149
backup_dir = 'backup.bzr.~1~'
150
(out, err) = self.run_bzr(
151
['upgrade', '--format=2a', url])
152
self.assertEqualDiff("""Upgrading branch %s/ ...
153
starting upgrade of %s/
154
making backup of %s/.bzr
156
starting upgrade from old test format to 2a
158
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
159
self.assertEqualDiff("", err)
160
self.assertTrue(isinstance(
161
controldir.ControlDir.open(self.get_url(path))._format,
162
bzrdir.BzrDirMetaFormat1))
164
def test_upgrade_explicit_knit(self):
165
# users can force an upgrade to knit format from a metadir pack 0.92
166
# branch to a 2a branch.
167
self.make_branch_and_tree('branch', format='knit')
168
transport = self.get_transport('branch')
170
display_url = transport.local_abspath('.')
171
# check --format takes effect
172
backup_dir = 'backup.bzr.~1~'
173
(out, err) = self.run_bzr(
174
['upgrade', '--format=pack-0.92', url])
175
self.assertEqualDiff("""Upgrading branch %s/ ...
176
starting upgrade of %s/
177
making backup of %s/.bzr
179
starting repository conversion
182
""" % (display_url, display_url, display_url, display_url, backup_dir),
184
self.assertEqualDiff("", err)
185
converted_dir = controldir.ControlDir.open(self.get_url('branch'))
186
self.assertTrue(isinstance(converted_dir._format,
187
bzrdir.BzrDirMetaFormat1))
188
self.assertTrue(isinstance(converted_dir.open_repository()._format,
189
RepositoryFormatKnitPack1))
191
def test_upgrade_repo(self):
192
self.run_bzr('init-repository --format=pack-0.92 repo')
193
self.run_bzr('upgrade --format=2a repo')
195
def assertLegalOption(self, option_str):
196
# Confirm that an option is legal. (Lower level tests are
197
# expected to validate the actual functionality.)
198
self.run_bzr('init --format=pack-0.92 branch-foo')
199
self.run_bzr('upgrade --format=2a branch-foo %s' % (option_str,))
201
def assertBranchFormat(self, dir, format):
202
branch = controldir.ControlDir.open_tree_or_branch(self.get_url(dir))[1]
203
branch_format = branch._format
204
meta_format = controldir.format_registry.make_controldir(format)
205
expected_format = meta_format.get_branch_format()
206
self.assertEqual(expected_format, branch_format)
208
def test_upgrade_clean_supported(self):
209
self.assertLegalOption('--clean')
210
self.assertBranchFormat('branch-foo', '2a')
211
backup_bzr_dir = os.path.join("branch-foo", "backup.bzr.~1~")
212
self.assertFalse(os.path.exists(backup_bzr_dir))
214
def test_upgrade_dry_run_supported(self):
215
self.assertLegalOption('--dry-run')
216
self.assertBranchFormat('branch-foo', 'pack-0.92')
218
def test_upgrade_permission_check(self):
219
"""'backup.bzr' should retain permissions of .bzr. Bug #262450"""
220
self.requireFeature(features.posix_permissions_feature)
221
old_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
222
backup_dir = 'backup.bzr.~1~'
223
self.run_bzr('init --format=1.6')
224
os.chmod('.bzr', old_perms)
225
self.run_bzr('upgrade')
226
new_perms = os.stat(backup_dir).st_mode & 0o777
227
self.assertTrue(new_perms == old_perms)
229
def test_upgrade_with_existing_backup_dir(self):
230
self.make_branch_and_tree("old_format_branch", format="knit")
231
t = self.get_transport("old_format_branch")
233
display_url = t.local_abspath('.')
234
backup_dir1 = 'backup.bzr.~1~'
235
backup_dir2 = 'backup.bzr.~2~'
236
# explicitly create backup_dir1. brz should create the .~2~ directory
239
(out, err) = self.run_bzr(
240
['upgrade', '--format=2a', url])
241
self.assertEqualDiff("""Upgrading branch %s/ ...
242
starting upgrade of %s/
243
making backup of %s/.bzr
245
starting repository conversion
248
""" % (display_url, display_url, display_url, display_url, backup_dir2), out)
249
self.assertEqualDiff("", err)
250
self.assertTrue(isinstance(
251
controldir.ControlDir.open(self.get_url("old_format_branch"))._format,
252
bzrdir.BzrDirMetaFormat1))
253
self.assertTrue(t.has(backup_dir2))
256
class SFTPTests(TestCaseWithSFTPServer):
257
"""Tests for upgrade over sftp."""
259
def test_upgrade_url(self):
260
self.run_bzr('init --format=pack-0.92')
261
t = self.get_transport()
263
display_url = urlutils.unescape_for_display(url,
265
out, err = self.run_bzr(['upgrade', '--format=2a', url])
266
backup_dir = 'backup.bzr.~1~'
267
self.assertEqualDiff("""Upgrading branch %s ...
268
starting upgrade of %s
269
making backup of %s.bzr
271
starting repository conversion
274
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
275
self.assertEqual('', err)
278
class UpgradeRecommendedTests(TestCaseWithTransport):
280
def test_recommend_upgrade_wt4(self):
281
# using a deprecated format gives a warning
282
self.run_bzr('init --format=knit a')
283
out, err = self.run_bzr('status a')
284
self.assertContainsRe(err, 'brz upgrade .*[/\\\\]a')
286
def test_no_upgrade_recommendation_from_bzrdir(self):
287
# we should only get a recommendation to upgrade when we're accessing
288
# the actual workingtree, not when we only open a bzrdir that contains
290
self.run_bzr('init --format=knit a')
291
out, err = self.run_bzr('revno a')
292
if err.find('upgrade') > -1:
293
self.fail("message shouldn't suggest upgrade:\n%s" % err)
295
def test_upgrade_shared_repo(self):
296
repo = self.make_repository('repo', format='2a', shared=True)
297
branch = self.make_branch_and_tree('repo/branch', format="pack-0.92")
298
self.get_transport('repo/branch/.bzr/repository').delete_tree('.')
299
out, err = self.run_bzr(['upgrade'], working_dir='repo/branch')