57
53
RemoteRepositoryFormat,
59
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
55
from bzrlib.repofmt import groupcompress_repo, pack_repo
60
56
from bzrlib.revision import NULL_REVISION
61
from bzrlib.smart import medium, request
57
from bzrlib.smart import server, medium
62
58
from bzrlib.smart.client import _SmartClient
63
from bzrlib.smart.repository import (
64
SmartServerRepositoryGetParentMap,
65
SmartServerRepositoryGetStream_1_19,
59
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
67
60
from bzrlib.tests import (
62
split_suite_by_condition,
70
from bzrlib.tests.scenarios import load_tests_apply_scenarios
65
from bzrlib.transport import get_transport
71
66
from bzrlib.transport.memory import MemoryTransport
72
67
from bzrlib.transport.remote import (
74
69
RemoteSSHTransport,
75
70
RemoteTCPTransport,
79
load_tests = load_tests_apply_scenarios
82
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
73
def load_tests(standard_tests, module, loader):
74
to_adapt, result = split_suite_by_condition(
75
standard_tests, condition_isinstance(BasicRemoteObjectTests))
76
smart_server_version_scenarios = [
86
{'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
78
{'transport_server': server.SmartTCPServer_for_testing_v2_only}),
88
{'transport_server': test_server.SmartTCPServer_for_testing})]
80
{'transport_server': server.SmartTCPServer_for_testing})]
81
return multiply_tests(to_adapt, smart_server_version_scenarios, result)
84
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
92
87
super(BasicRemoteObjectTests, self).setUp()
93
88
self.transport = self.get_transport()
94
89
# make a branch that can be opened over the smart transport
95
90
self.local_wt = BzrDir.create_standalone_workingtree('.')
96
self.addCleanup(self.transport.disconnect)
93
self.transport.disconnect()
94
tests.TestCaseWithTransport.tearDown(self)
98
96
def test_create_remote_bzrdir(self):
99
b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
97
b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
100
98
self.assertIsInstance(b, BzrDir)
102
100
def test_open_remote_branch(self):
103
101
# open a standalone branch in the working directory
104
b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
102
b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
105
103
branch = b.open_branch()
106
104
self.assertIsInstance(branch, Branch)
496
491
client.add_expected_call(
497
492
'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
498
493
self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
499
RemoteBzrDirFormat(), _client=client, _force_probe=True)
494
remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
500
495
self.assertFinished(client)
502
497
def test_present_without_workingtree(self):
503
498
client, transport = self.make_fake_client_and_transport()
504
499
client.add_expected_call(
505
500
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
506
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
501
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
507
502
_client=client, _force_probe=True)
508
503
self.assertIsInstance(bd, RemoteBzrDir)
509
504
self.assertFalse(bd.has_workingtree())
527
522
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
528
523
client.add_expected_call(
529
524
'BzrDir.open', ('quack/',), 'success', ('yes',))
530
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
531
_client=client, _force_probe=True)
532
self.assertIsInstance(bd, RemoteBzrDir)
533
self.assertFinished(client)
535
def test_backwards_compat_hpss_v2(self):
536
client, transport = self.make_fake_client_and_transport()
537
# Monkey-patch fake client to simulate real-world behaviour with v2
538
# server: upon first RPC call detect the protocol version, and because
539
# the version is 2 also do _remember_remote_is_before((1, 6)) before
540
# continuing with the RPC.
541
orig_check_call = client._check_call
542
def check_call(method, args):
543
client._medium._protocol_version = 2
544
client._medium._remember_remote_is_before((1, 6))
545
client._check_call = orig_check_call
546
client._check_call(method, args)
547
client._check_call = check_call
548
client.add_expected_call(
549
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
550
client.add_expected_call(
551
'BzrDir.open', ('quack/',), 'success', ('yes',))
552
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
525
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
553
526
_client=client, _force_probe=True)
554
527
self.assertIsInstance(bd, RemoteBzrDir)
555
528
self.assertFinished(client)
725
698
format = branch._format
726
699
self.assertEqual(network_name, format.network_name())
728
def test_already_open_repo_and_reused_medium(self):
729
"""Bug 726584: create_branch(..., repository=repo) should work
730
regardless of what the smart medium's base URL is.
732
self.transport_server = test_server.SmartTCPServer_for_testing
733
transport = self.get_transport('.')
734
repo = self.make_repository('quack')
735
# Client's medium rooted a transport root (not at the bzrdir)
736
client = FakeClient(transport.base)
737
transport = transport.clone('quack')
738
reference_bzrdir_format = bzrdir.format_registry.get('default')()
739
reference_format = reference_bzrdir_format.get_branch_format()
740
network_name = reference_format.network_name()
741
reference_repo_fmt = reference_bzrdir_format.repository_format
742
reference_repo_name = reference_repo_fmt.network_name()
743
client.add_expected_call(
744
'BzrDir.create_branch', ('extra/quack/', network_name),
745
'success', ('ok', network_name, '', 'no', 'no', 'yes',
746
reference_repo_name))
747
a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
749
branch = a_bzrdir.create_branch(repository=repo)
750
# We should have got a remote branch
751
self.assertIsInstance(branch, remote.RemoteBranch)
752
# its format should have the settings from the response
753
format = branch._format
754
self.assertEqual(network_name, format.network_name())
757
702
class TestBzrDirCreateRepository(TestRemote):
1172
1117
[('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1175
class TestBranchHeadsToFetch(RemoteBranchTestCase):
1177
def test_uses_last_revision_info_and_tags_by_default(self):
1178
transport = MemoryTransport()
1179
client = FakeClient(transport.base)
1180
client.add_expected_call(
1181
'Branch.get_stacked_on_url', ('quack/',),
1182
'error', ('NotStacked',))
1183
client.add_expected_call(
1184
'Branch.last_revision_info', ('quack/',),
1185
'success', ('ok', '1', 'rev-tip'))
1186
client.add_expected_call(
1187
'Branch.get_config_file', ('quack/',),
1188
'success', ('ok',), '')
1189
transport.mkdir('quack')
1190
transport = transport.clone('quack')
1191
branch = self.make_remote_branch(transport, client)
1192
result = branch.heads_to_fetch()
1193
self.assertFinished(client)
1194
self.assertEqual((set(['rev-tip']), set()), result)
1196
def test_uses_last_revision_info_and_tags_when_set(self):
1197
transport = MemoryTransport()
1198
client = FakeClient(transport.base)
1199
client.add_expected_call(
1200
'Branch.get_stacked_on_url', ('quack/',),
1201
'error', ('NotStacked',))
1202
client.add_expected_call(
1203
'Branch.last_revision_info', ('quack/',),
1204
'success', ('ok', '1', 'rev-tip'))
1205
client.add_expected_call(
1206
'Branch.get_config_file', ('quack/',),
1207
'success', ('ok',), 'branch.fetch_tags = True')
1208
# XXX: this will break if the default format's serialization of tags
1209
# changes, or if the RPC for fetching tags changes from get_tags_bytes.
1210
client.add_expected_call(
1211
'Branch.get_tags_bytes', ('quack/',),
1212
'success', ('d5:tag-17:rev-foo5:tag-27:rev-bare',))
1213
transport.mkdir('quack')
1214
transport = transport.clone('quack')
1215
branch = self.make_remote_branch(transport, client)
1216
result = branch.heads_to_fetch()
1217
self.assertFinished(client)
1219
(set(['rev-tip']), set(['rev-foo', 'rev-bar'])), result)
1221
def test_uses_rpc_for_formats_with_non_default_heads_to_fetch(self):
1222
transport = MemoryTransport()
1223
client = FakeClient(transport.base)
1224
client.add_expected_call(
1225
'Branch.get_stacked_on_url', ('quack/',),
1226
'error', ('NotStacked',))
1227
client.add_expected_call(
1228
'Branch.heads_to_fetch', ('quack/',),
1229
'success', (['tip'], ['tagged-1', 'tagged-2']))
1230
transport.mkdir('quack')
1231
transport = transport.clone('quack')
1232
branch = self.make_remote_branch(transport, client)
1233
branch._format._use_default_local_heads_to_fetch = lambda: False
1234
result = branch.heads_to_fetch()
1235
self.assertFinished(client)
1236
self.assertEqual((set(['tip']), set(['tagged-1', 'tagged-2'])), result)
1238
def make_branch_with_tags(self):
1239
self.setup_smart_server_with_call_log()
1240
# Make a branch with a single revision.
1241
builder = self.make_branch_builder('foo')
1242
builder.start_series()
1243
builder.build_snapshot('tip', None, [
1244
('add', ('', 'root-id', 'directory', ''))])
1245
builder.finish_series()
1246
branch = builder.get_branch()
1247
# Add two tags to that branch
1248
branch.tags.set_tag('tag-1', 'rev-1')
1249
branch.tags.set_tag('tag-2', 'rev-2')
1252
def test_backwards_compatible(self):
1253
branch = self.make_branch_with_tags()
1254
c = branch.get_config()
1255
c.set_user_option('branch.fetch_tags', 'True')
1256
self.addCleanup(branch.lock_read().unlock)
1257
# Disable the heads_to_fetch verb
1258
verb = 'Branch.heads_to_fetch'
1259
self.disable_verb(verb)
1260
self.reset_smart_call_log()
1261
result = branch.heads_to_fetch()
1262
self.assertEqual((set(['tip']), set(['rev-1', 'rev-2'])), result)
1264
['Branch.last_revision_info', 'Branch.get_config_file',
1265
'Branch.get_tags_bytes'],
1266
[call.call.method for call in self.hpss_calls])
1268
def test_backwards_compatible_no_tags(self):
1269
branch = self.make_branch_with_tags()
1270
c = branch.get_config()
1271
c.set_user_option('branch.fetch_tags', 'False')
1272
self.addCleanup(branch.lock_read().unlock)
1273
# Disable the heads_to_fetch verb
1274
verb = 'Branch.heads_to_fetch'
1275
self.disable_verb(verb)
1276
self.reset_smart_call_log()
1277
result = branch.heads_to_fetch()
1278
self.assertEqual((set(['tip']), set()), result)
1280
['Branch.last_revision_info', 'Branch.get_config_file'],
1281
[call.call.method for call in self.hpss_calls])
1284
1120
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1286
1122
def test_empty_branch(self):
1791
1626
branch.unlock()
1792
1627
self.assertFinished(client)
1794
def test_set_option_with_dict(self):
1795
client = FakeClient()
1796
client.add_expected_call(
1797
'Branch.get_stacked_on_url', ('memory:///',),
1798
'error', ('NotStacked',),)
1799
client.add_expected_call(
1800
'Branch.lock_write', ('memory:///', '', ''),
1801
'success', ('ok', 'branch token', 'repo token'))
1802
encoded_dict_value = 'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde'
1803
client.add_expected_call(
1804
'Branch.set_config_option_dict', ('memory:///', 'branch token',
1805
'repo token', encoded_dict_value, 'foo', ''),
1807
client.add_expected_call(
1808
'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1810
transport = MemoryTransport()
1811
branch = self.make_remote_branch(transport, client)
1813
config = branch._get_config()
1815
{'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'},
1818
self.assertFinished(client)
1820
1629
def test_backwards_compat_set_option(self):
1821
1630
self.setup_smart_server_with_call_log()
1822
1631
branch = self.make_branch('.')
3099
2868
expected_error = errors.PermissionDenied(path, extra)
3100
2869
self.assertEqual(expected_error, translated_error)
3102
# GZ 2011-03-02: TODO test for PermissionDenied with non-ascii 'extra'
3104
def test_NoSuchFile_context_path(self):
3105
local_path = "local path"
3106
translated_error = self.translateTuple(('ReadError', "remote path"),
3108
expected_error = errors.ReadError(local_path)
3109
self.assertEqual(expected_error, translated_error)
3111
def test_NoSuchFile_without_context(self):
3112
remote_path = "remote path"
3113
translated_error = self.translateTuple(('ReadError', remote_path))
3114
expected_error = errors.ReadError(remote_path)
3115
self.assertEqual(expected_error, translated_error)
3117
def test_ReadOnlyError(self):
3118
translated_error = self.translateTuple(('ReadOnlyError',))
3119
expected_error = errors.TransportNotPossible("readonly transport")
3120
self.assertEqual(expected_error, translated_error)
3122
def test_MemoryError(self):
3123
translated_error = self.translateTuple(('MemoryError',))
3124
self.assertStartsWith(str(translated_error),
3125
"remote server out of memory")
3127
def test_generic_IndexError_no_classname(self):
3128
err = errors.ErrorFromSmartServer(('error', "list index out of range"))
3129
translated_error = self.translateErrorFromSmartServer(err)
3130
expected_error = errors.UnknownErrorFromSmartServer(err)
3131
self.assertEqual(expected_error, translated_error)
3133
# GZ 2011-03-02: TODO test generic non-ascii error string
3135
def test_generic_KeyError(self):
3136
err = errors.ErrorFromSmartServer(('error', 'KeyError', "1"))
3137
translated_error = self.translateErrorFromSmartServer(err)
3138
expected_error = errors.UnknownErrorFromSmartServer(err)
3139
self.assertEqual(expected_error, translated_error)
3142
2872
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3143
2873
"""Unit tests for bzrlib.remote._translate_error's robustness.
3388
3116
def test_copy_content_into_avoids_revision_history(self):
3389
3117
local = self.make_branch('local')
3390
builder = self.make_branch_builder('remote')
3391
builder.build_commit(message="Commit.")
3118
remote_backing_tree = self.make_branch_and_tree('remote')
3119
remote_backing_tree.commit("Commit.")
3392
3120
remote_branch_url = self.smart_server.get_url() + 'remote'
3393
3121
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3394
3122
local.repository.fetch(remote_branch.repository)
3395
3123
self.hpss_calls = []
3396
3124
remote_branch.copy_content_into(local)
3397
3125
self.assertFalse('Branch.revision_history' in self.hpss_calls)
3399
def test_fetch_everything_needs_just_one_call(self):
3400
local = self.make_branch('local')
3401
builder = self.make_branch_builder('remote')
3402
builder.build_commit(message="Commit.")
3403
remote_branch_url = self.smart_server.get_url() + 'remote'
3404
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3405
self.hpss_calls = []
3406
local.repository.fetch(
3407
remote_branch.repository,
3408
fetch_spec=_mod_graph.EverythingResult(remote_branch.repository))
3409
self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
3411
def override_verb(self, verb_name, verb):
3412
request_handlers = request.request_handlers
3413
orig_verb = request_handlers.get(verb_name)
3414
request_handlers.register(verb_name, verb, override_existing=True)
3415
self.addCleanup(request_handlers.register, verb_name, orig_verb,
3416
override_existing=True)
3418
def test_fetch_everything_backwards_compat(self):
3419
"""Can fetch with EverythingResult even with pre 2.4 servers.
3421
Pre-2.4 do not support 'everything' searches with the
3422
Repository.get_stream_1.19 verb.
3425
class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
3426
"""A version of the Repository.get_stream_1.19 verb patched to
3427
reject 'everything' searches the way 2.3 and earlier do.
3429
def recreate_search(self, repository, search_bytes,
3430
discard_excess=False):
3431
verb_log.append(search_bytes.split('\n', 1)[0])
3432
if search_bytes == 'everything':
3434
request.FailedSmartServerResponse(('BadSearch',)))
3435
return super(OldGetStreamVerb,
3436
self).recreate_search(repository, search_bytes,
3437
discard_excess=discard_excess)
3438
self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
3439
local = self.make_branch('local')
3440
builder = self.make_branch_builder('remote')
3441
builder.build_commit(message="Commit.")
3442
remote_branch_url = self.smart_server.get_url() + 'remote'
3443
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3444
self.hpss_calls = []
3445
local.repository.fetch(
3446
remote_branch.repository,
3447
fetch_spec=_mod_graph.EverythingResult(remote_branch.repository))
3448
# make sure the overridden verb was used
3449
self.assertLength(1, verb_log)
3450
# more than one HPSS call is needed, but because it's a VFS callback
3451
# its hard to predict exactly how many.
3452
self.assertTrue(len(self.hpss_calls) > 1)
3455
class TestUpdateBoundBranchWithModifiedBoundLocation(
3456
tests.TestCaseWithTransport):
3457
"""Ensure correct handling of bound_location modifications.
3459
This is tested against a smart server as http://pad.lv/786980 was about a
3460
ReadOnlyError (write attempt during a read-only transaction) which can only
3461
happen in this context.
3465
super(TestUpdateBoundBranchWithModifiedBoundLocation, self).setUp()
3466
self.transport_server = test_server.SmartTCPServer_for_testing
3468
def make_master_and_checkout(self, master_name, checkout_name):
3469
# Create the master branch and its associated checkout
3470
self.master = self.make_branch_and_tree(master_name)
3471
self.checkout = self.master.branch.create_checkout(checkout_name)
3472
# Modify the master branch so there is something to update
3473
self.master.commit('add stuff')
3474
self.last_revid = self.master.commit('even more stuff')
3475
self.bound_location = self.checkout.branch.get_bound_location()
3477
def assertUpdateSucceeds(self, new_location):
3478
self.checkout.branch.set_bound_location(new_location)
3479
self.checkout.update()
3480
self.assertEquals(self.last_revid, self.checkout.last_revision())
3482
def test_without_final_slash(self):
3483
self.make_master_and_checkout('master', 'checkout')
3484
# For unclear reasons some users have a bound_location without a final
3485
# '/', simulate that by forcing such a value
3486
self.assertEndsWith(self.bound_location, '/')
3487
self.assertUpdateSucceeds(self.bound_location.rstrip('/'))
3489
def test_plus_sign(self):
3490
self.make_master_and_checkout('+master', 'checkout')
3491
self.assertUpdateSucceeds(self.bound_location.replace('%2B', '+', 1))
3493
def test_tilda(self):
3494
# Embed ~ in the middle of the path just to avoid any $HOME
3496
self.make_master_and_checkout('mas~ter', 'checkout')
3497
self.assertUpdateSucceeds(self.bound_location.replace('%2E', '~', 1))