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 controldir.ControlDir.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()
63
def get_format_string(cls):
64
return "Ancient Test Format"
66
def _open(self, transport):
67
return OldBzrDir(transport, self)
23
import bzrlib.bzrdir as bzrdir
24
import bzrlib.repository as repository
25
from bzrlib.tests import TestCaseWithTransport
26
from bzrlib.tests.blackbox import TestUIFactory
27
from bzrlib.transport import get_transport
28
import bzrlib.ui as ui
70
31
class TestWithUpgradableBranches(TestCaseWithTransport):
73
34
super(TestWithUpgradableBranches, self).setUp()
75
def make_current_format_branch_and_checkout(self):
76
current_tree = self.make_branch_and_tree('current_format_branch',
78
current_tree.branch.create_checkout(
79
self.get_url('current_format_checkout'), lightweight=True)
35
self.old_format = bzrdir.BzrDirFormat.get_default_format()
36
self.old_ui_factory = ui.ui_factory
37
self.addCleanup(self.restoreDefaults)
39
ui.ui_factory = TestUIFactory()
40
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
41
# FIXME RBC 20060120 we should be able to do this via ui calls only.
42
# setup a format 5 branch we can upgrade from.
43
t = get_transport(self.get_url())
44
t.mkdir('format_5_branch')
45
bzrdir.BzrDirFormat5().initialize(self.get_url('format_5_branch'))
46
bzrdir.BzrDir.create_standalone_workingtree('current_format_branch')
47
d = bzrdir.BzrDir.create('metadir_weave_branch')
50
d.create_workingtree()
51
self.run_bzr('checkout',
53
self.get_url('current_format_branch'),
54
'current_format_checkout')
56
def restoreDefaults(self):
57
bzrdir.BzrDirFormat.set_default_format(self.old_format)
58
ui.ui_factory = self.old_ui_factory
81
60
def test_readonly_url_error(self):
82
self.make_branch_and_tree("old_format_branch", format="knit")
83
(out, err) = self.run_bzr(
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'
61
(out, err) = self.run_bzr_captured(
62
['upgrade', self.get_readonly_url('format_5_branch')], 3)
63
self.assertEqual(out, "")
64
self.assertEqual(err, "bzr: ERROR: Upgrade URL cannot work with readonly URL's.\n")
90
66
def test_upgrade_up_to_date(self):
91
self.make_current_format_branch_and_checkout()
92
67
# when up to date we should get a message to that effect
93
burl = self.get_transport('current_format_branch').local_abspath(".")
94
(out, err) = self.run_bzr('upgrade current_format_branch', retcode=0)
96
'Upgrading branch %s/ ...\n'
97
'The branch format %s is already at the most recent format.\n'
98
% (burl, 'Meta directory format 1'),
68
(out, err) = self.run_bzr_captured(
69
['upgrade', 'current_format_branch'], 3)
70
self.assertEqual("", out)
71
self.assertEqualDiff("bzr: ERROR: The branch format Bazaar-NG meta "
72
"directory, format 1 is already at the most "
73
"recent format.\n", err)
101
75
def test_upgrade_up_to_date_checkout_warns_branch_left_alone(self):
102
self.make_current_format_branch_and_checkout()
103
76
# when upgrading a checkout, the branch location and a suggestion
104
# to upgrade it should be emitted even if the checkout is up to
77
# to upgrade it should be emitted even if the checkout is up to
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)
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'),
79
(out, err) = self.run_bzr_captured(
80
['upgrade', 'current_format_checkout'], 3)
81
self.assertEqual("This is a checkout. The branch (%s) needs to be "
82
"upgraded separately.\n"
83
% get_transport(self.get_url('current_format_branch')).base,
85
self.assertEqualDiff("bzr: ERROR: The branch format Bazaar-NG meta "
86
"directory, format 1 is already at the most "
87
"recent format.\n", err)
116
89
def test_upgrade_checkout(self):
117
90
# upgrading a checkout should work
120
93
def test_upgrade_repository_scans_branches(self):
121
# we should get individual upgrade notes for each branch even the
94
# we should get individual upgrade notes for each branch even the
122
95
# anonymous branch
125
def test_upgrade_branch_in_repo(self):
98
def test_ugrade_branch_in_repo(self):
126
99
# upgrading a branch in a repo should warn about not upgrading the repo
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(),
135
self.addCleanup(controldir.ControlDirFormat._set_default_format,
136
controldir.ControlDirFormat.get_default_format())
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)
143
display_url = transport.local_abspath('.')
102
def test_upgrade_explicit_metaformat(self):
103
# users can force an upgrade to metadir format.
104
url = get_transport(self.get_url('format_5_branch')).base
144
105
# check --format takes effect
145
controldir.ControlDirFormat._set_default_format(old_format)
146
backup_dir = 'backup.bzr.~1~'
147
(out, err) = self.run_bzr(
148
['upgrade', '--format=2a', url])
149
self.assertEqualDiff("""Upgrading branch %s/ ...
150
starting upgrade of %s/
151
making backup of %s/.bzr
153
starting upgrade from old test format to 2a
106
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
107
(out, err) = self.run_bzr_captured(
108
['upgrade', '--format=metadir', url])
109
self.assertEqualDiff("""starting upgrade of %s
110
making backup of tree history
111
%s.bzr has been backed up to %s.bzr.backup
112
if conversion fails, you can move this directory back to .bzr
113
if it succeeds, you can remove this directory if you wish
114
starting upgrade from format 5 to 6
115
adding prefixes to weaves
116
adding prefixes to revision-store
117
starting upgrade from format 6 to metadir
155
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
119
""" % (url, url, url), out)
156
120
self.assertEqualDiff("", err)
157
121
self.assertTrue(isinstance(
158
controldir.ControlDir.open(self.get_url(path))._format,
122
bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
159
123
bzrdir.BzrDirMetaFormat1))
161
125
def test_upgrade_explicit_knit(self):
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')
167
display_url = transport.local_abspath('.')
126
# users can force an upgrade to knit format from a metadir weave
128
url = get_transport(self.get_url('metadir_weave_branch')).base
168
129
# check --format takes effect
169
backup_dir = 'backup.bzr.~1~'
170
(out, err) = self.run_bzr(
171
['upgrade', '--format=pack-0.92', url])
172
self.assertEqualDiff("""Upgrading branch %s/ ...
173
starting upgrade of %s/
174
making backup of %s/.bzr
130
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
131
(out, err) = self.run_bzr_captured(
132
['upgrade', '--format=knit', url])
133
self.assertEqualDiff("""starting upgrade of %s
134
making backup of tree history
135
%s.bzr has been backed up to %s.bzr.backup
136
if conversion fails, you can move this directory back to .bzr
137
if it succeeds, you can remove this directory if you wish
176
138
starting repository conversion
177
139
repository converted
179
""" % (display_url, display_url, display_url, display_url, backup_dir),
141
""" % (url, url, url), out)
181
142
self.assertEqualDiff("", err)
182
converted_dir = controldir.ControlDir.open(self.get_url('branch'))
143
converted_dir = bzrdir.BzrDir.open(self.get_url('metadir_weave_branch'))
183
144
self.assertTrue(isinstance(converted_dir._format,
184
145
bzrdir.BzrDirMetaFormat1))
185
146
self.assertTrue(isinstance(converted_dir.open_repository()._format,
186
RepositoryFormatKnitPack1))
188
def test_upgrade_repo(self):
189
self.run_bzr('init-repository --format=pack-0.92 repo')
190
self.run_bzr('upgrade --format=2a repo')
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,))
198
def assertBranchFormat(self, dir, format):
199
branch = controldir.ControlDir.open_tree_or_branch(self.get_url(dir))[1]
200
branch_format = branch._format
201
meta_format = controldir.format_registry.make_bzrdir(format)
202
expected_format = meta_format.get_branch_format()
203
self.assertEqual(expected_format, branch_format)
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))
211
def test_upgrade_dry_run_supported(self):
212
self.assertLegalOption('--dry-run')
213
self.assertBranchFormat('branch-foo', 'pack-0.92')
215
def test_upgrade_permission_check(self):
216
"""'backup.bzr' should retain permissions of .bzr. Bug #262450"""
217
self.requireFeature(features.posix_permissions_feature)
218
old_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
219
backup_dir = 'backup.bzr.~1~'
220
self.run_bzr('init --format=1.6')
221
os.chmod('.bzr', old_perms)
222
self.run_bzr('upgrade')
223
new_perms = os.stat(backup_dir).st_mode & 0777
224
self.assertTrue(new_perms == old_perms)
226
def test_upgrade_with_existing_backup_dir(self):
227
self.make_branch_and_tree("old_format_branch", format="knit")
228
t = self.get_transport("old_format_branch")
230
display_url = t.local_abspath('.')
231
backup_dir1 = 'backup.bzr.~1~'
232
backup_dir2 = 'backup.bzr.~2~'
233
# explicitly create backup_dir1. bzr should create the .~2~ directory
236
(out, err) = self.run_bzr(
237
['upgrade', '--format=2a', url])
238
self.assertEqualDiff("""Upgrading branch %s/ ...
239
starting upgrade of %s/
240
making backup of %s/.bzr
242
starting repository conversion
245
""" % (display_url, display_url, display_url, display_url, backup_dir2), out)
246
self.assertEqualDiff("", err)
247
self.assertTrue(isinstance(
248
controldir.ControlDir.open(self.get_url("old_format_branch"))._format,
249
bzrdir.BzrDirMetaFormat1))
250
self.assertTrue(t.has(backup_dir2))
253
class SFTPTests(TestCaseWithSFTPServer):
254
"""Tests for upgrade over sftp."""
256
def test_upgrade_url(self):
257
self.run_bzr('init --format=pack-0.92')
258
t = self.get_transport()
260
display_url = urlutils.unescape_for_display(url,
262
out, err = self.run_bzr(['upgrade', '--format=2a', url])
263
backup_dir = 'backup.bzr.~1~'
264
self.assertEqualDiff("""Upgrading branch %s ...
265
starting upgrade of %s
266
making backup of %s.bzr
268
starting repository conversion
271
""" % (display_url, display_url, display_url, display_url, backup_dir), out)
272
self.assertEqual('', err)
275
class UpgradeRecommendedTests(TestCaseWithTransport):
277
def test_recommend_upgrade_wt4(self):
278
# using a deprecated format gives a warning
279
self.run_bzr('init --knit a')
280
out, err = self.run_bzr('status a')
281
self.assertContainsRe(err, 'bzr upgrade .*[/\\\\]a')
283
def test_no_upgrade_recommendation_from_bzrdir(self):
284
# we should only get a recommendation to upgrade when we're accessing
285
# the actual workingtree, not when we only open a bzrdir that contains
287
self.run_bzr('init --knit a')
288
out, err = self.run_bzr('revno a')
289
if err.find('upgrade') > -1:
290
self.fail("message shouldn't suggest upgrade:\n%s" % err)
292
def test_upgrade_shared_repo(self):
293
repo = self.make_repository('repo', format='2a', shared=True)
294
branch = self.make_branch_and_tree('repo/branch', format="pack-0.92")
295
self.get_transport('repo/branch/.bzr/repository').delete_tree('.')
296
out, err = self.run_bzr(['upgrade'], working_dir='repo/branch')
147
repository.RepositoryFormatKnit1))