1
# Copyright (C) 2006-2011 Canonical Ltd
1
# Copyright (C) 2006 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
3
# -*- coding: utf-8 -*-
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
13
# GNU General Public License for more details.
13
15
# You should have received a copy of the GNU General Public License
14
16
# 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
19
"""Black box tests for the upgrade ui."""
28
from bzrlib.tests import (
30
TestCaseWithTransport,
32
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)
23
import bzrlib.bzrdir as bzrdir
24
from bzrlib.tests import TestCaseWithTransport
25
from bzrlib.transport import get_transport
26
import bzrlib.ui as ui
29
class TestUIFactory(ui.UIFactory):
30
"""A UI Factory which never captures its output.
33
def note(self, fmt_string, *args, **kwargs):
34
"""See progress.ProgressBar.note()."""
35
print fmt_string % args
37
def progress_bar(self):
40
def update(self, message, count, total):
41
"""See progress.ProgressBar.update()."""
69
44
class TestWithUpgradableBranches(TestCaseWithTransport):
72
47
super(TestWithUpgradableBranches, self).setUp()
74
def make_current_format_branch_and_checkout(self):
75
current_tree = self.make_branch_and_tree('current_format_branch',
77
current_tree.branch.create_checkout(
78
self.get_url('current_format_checkout'), lightweight=True)
48
self.old_format = bzrdir.BzrDirFormat.get_default_format()
49
self.old_ui_factory = ui.ui_factory
50
self.addCleanup(self.restoreDefaults)
52
ui.ui_factory = TestUIFactory()
53
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
54
# FIXME RBC 20060120 we should be able to do this via ui calls only.
55
# setup a format 5 branch we can upgrade from.
56
t = get_transport(self.get_url())
57
t.mkdir('format_5_branch')
58
bzrdir.BzrDirFormat5().initialize(self.get_url('format_5_branch'))
59
bzrdir.BzrDir.create_standalone_workingtree('current_format_branch')
60
self.run_bzr('checkout',
61
self.get_url('current_format_branch'),
62
'current_format_checkout')
64
def restoreDefaults(self):
65
bzrdir.BzrDirFormat.set_default_format(self.old_format)
66
ui.ui_factory = self.old_ui_factory
80
68
def test_readonly_url_error(self):
81
self.make_branch_and_tree("old_format_branch", format="knit")
82
(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'
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 URL's.\n")
89
74
def test_upgrade_up_to_date(self):
90
self.make_current_format_branch_and_checkout()
91
75
# when up to date we should get a message to that effect
92
(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'
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)
99
83
def test_upgrade_up_to_date_checkout_warns_branch_left_alone(self):
100
self.make_current_format_branch_and_checkout()
101
84
# when upgrading a checkout, the branch location and a suggestion
102
# to upgrade it should be emitted even if the checkout is up to
85
# 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
(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'
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,
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)
118
97
def test_upgrade_checkout(self):
119
98
# upgrading a checkout should work
122
101
def test_upgrade_repository_scans_branches(self):
123
# we should get individual upgrade notes for each branch even the
102
# we should get individual upgrade notes for each branch even the
124
103
# anonymous branch
127
def test_upgrade_branch_in_repo(self):
106
def test_ugrade_branch_in_repo(self):
128
107
# 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('.')
146
# check --format takes effect
147
controldir.ControlDirFormat._set_default_format(old_format)
148
backup_dir = 'backup.bzr.~1~'
149
(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
157
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
158
self.assertEqualDiff("", err)
159
self.assertTrue(isinstance(
160
bzrdir.BzrDir.open(self.get_url(path))._format,
161
bzrdir.BzrDirMetaFormat1))
163
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('.')
170
# check --format takes effect
171
backup_dir = 'backup.bzr.~1~'
172
(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
178
starting repository conversion
181
""" % (display_url, display_url, display_url, display_url, backup_dir),
183
self.assertEqualDiff("", err)
184
converted_dir = bzrdir.BzrDir.open(self.get_url('branch'))
185
self.assertTrue(isinstance(converted_dir._format,
186
bzrdir.BzrDirMetaFormat1))
187
self.assertTrue(isinstance(converted_dir.open_repository()._format,
188
RepositoryFormatKnitPack1))
190
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')
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)
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")
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
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
244
starting repository conversion
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))
255
class SFTPTests(TestCaseWithSFTPServer):
256
"""Tests for upgrade over sftp."""
258
def test_upgrade_url(self):
259
self.run_bzr('init --format=pack-0.92')
260
t = self.get_transport()
262
display_url = urlutils.unescape_for_display(url,
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
268
making backup of %s.bzr
270
starting repository conversion
273
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
274
self.assertEqual('', err)
277
class UpgradeRecommendedTests(TestCaseWithTransport):
279
def test_recommend_upgrade_wt4(self):
280
# using a deprecated format gives a warning
281
self.run_bzr('init --knit a')
282
out, err = self.run_bzr('status a')
283
self.assertContainsRe(err, 'bzr upgrade .*[/\\\\]a')
285
def test_no_upgrade_recommendation_from_bzrdir(self):
286
# we should only get a recommendation to upgrade when we're accessing
287
# the actual workingtree, not when we only open a bzrdir that contains
289
self.run_bzr('init --knit a')
290
out, err = self.run_bzr('revno a')
291
if err.find('upgrade') > -1:
292
self.fail("message shouldn't suggest upgrade:\n%s" % err)
294
def test_upgrade_shared_repo(self):
295
repo = self.make_repository('repo', format='2a', shared=True)
296
branch = self.make_branch_and_tree('repo/branch', format="pack-0.92")
297
self.get_transport('repo/branch/.bzr/repository').delete_tree('.')
298
out, err = self.run_bzr(['upgrade'], working_dir='repo/branch')
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
113
# check --format takes effect
114
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
115
(out, err) = self.run_bzr_captured(
116
['upgrade', '--format=metadir', 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
127
""" % (url, url, url), out)
128
self.assertEqualDiff("", err)
129
self.assertTrue(isinstance(
130
bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
131
bzrdir.BzrDirMetaFormat1))