53
53
RemoteBranchFormat,
57
56
RemoteRepositoryFormat,
59
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
58
from bzrlib.repofmt import groupcompress_repo, pack_repo
60
59
from bzrlib.revision import NULL_REVISION
61
from bzrlib.smart import medium, request
60
from bzrlib.smart import medium
62
61
from bzrlib.smart.client import _SmartClient
63
from bzrlib.smart.repository import (
64
SmartServerRepositoryGetParentMap,
65
SmartServerRepositoryGetStream_1_19,
62
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
67
63
from bzrlib.tests import (
65
split_suite_by_condition,
70
from bzrlib.tests.scenarios import load_tests_apply_scenarios
71
69
from bzrlib.transport.memory import MemoryTransport
72
70
from bzrlib.transport.remote import (
74
72
RemoteSSHTransport,
75
73
RemoteTCPTransport,
79
load_tests = load_tests_apply_scenarios
82
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
76
def load_tests(standard_tests, module, loader):
77
to_adapt, result = split_suite_by_condition(
78
standard_tests, condition_isinstance(BasicRemoteObjectTests))
79
smart_server_version_scenarios = [
86
{'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
81
{'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
88
{'transport_server': test_server.SmartTCPServer_for_testing})]
83
{'transport_server': test_server.SmartTCPServer_for_testing})]
84
return multiply_tests(to_adapt, smart_server_version_scenarios, result)
87
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
92
90
super(BasicRemoteObjectTests, self).setUp()
96
94
self.addCleanup(self.transport.disconnect)
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)
470
468
client.add_expected_call(
471
469
'BzrDir.cloning_metadir', ('quack/', 'False'),
472
470
'success', (control_name, '', ('branch', ''))),
473
a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
471
a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
475
473
result = a_bzrdir.cloning_metadir()
476
474
# We should have got a reference control dir with default branch and
496
494
client.add_expected_call(
497
495
'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
498
496
self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
499
RemoteBzrDirFormat(), _client=client, _force_probe=True)
497
remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
500
498
self.assertFinished(client)
502
500
def test_present_without_workingtree(self):
503
501
client, transport = self.make_fake_client_and_transport()
504
502
client.add_expected_call(
505
503
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
506
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
504
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
507
505
_client=client, _force_probe=True)
508
506
self.assertIsInstance(bd, RemoteBzrDir)
509
507
self.assertFalse(bd.has_workingtree())
514
512
client, transport = self.make_fake_client_and_transport()
515
513
client.add_expected_call(
516
514
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
517
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
515
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
518
516
_client=client, _force_probe=True)
519
517
self.assertIsInstance(bd, RemoteBzrDir)
520
518
self.assertTrue(bd.has_workingtree())
527
525
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
528
526
client.add_expected_call(
529
527
'BzrDir.open', ('quack/',), 'success', ('yes',))
530
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
528
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
531
529
_client=client, _force_probe=True)
532
530
self.assertIsInstance(bd, RemoteBzrDir)
533
531
self.assertFinished(client)
549
547
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
550
548
client.add_expected_call(
551
549
'BzrDir.open', ('quack/',), 'success', ('yes',))
552
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
550
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
553
551
_client=client, _force_probe=True)
554
552
self.assertIsInstance(bd, RemoteBzrDir)
555
553
self.assertFinished(client)
725
723
format = branch._format
726
724
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
727
class TestBzrDirCreateRepository(TestRemote):
1172
1142
[('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
# XXX: this will break if the default format's serialization of tags
1187
# changes, or if the RPC for fetching tags changes from get_tags_bytes.
1188
client.add_expected_call(
1189
'Branch.get_tags_bytes', ('quack/',),
1190
'success', ('d5:tag-17:rev-foo5:tag-27:rev-bare',))
1191
transport.mkdir('quack')
1192
transport = transport.clone('quack')
1193
branch = self.make_remote_branch(transport, client)
1194
result = branch.heads_to_fetch()
1195
self.assertFinished(client)
1197
(set(['rev-tip']), set(['rev-foo', 'rev-bar'])), result)
1199
def test_uses_rpc_for_formats_with_non_default_heads_to_fetch(self):
1200
transport = MemoryTransport()
1201
client = FakeClient(transport.base)
1202
client.add_expected_call(
1203
'Branch.get_stacked_on_url', ('quack/',),
1204
'error', ('NotStacked',))
1205
client.add_expected_call(
1206
'Branch.heads_to_fetch', ('quack/',),
1207
'success', (['tip'], ['tagged-1', 'tagged-2']))
1208
transport.mkdir('quack')
1209
transport = transport.clone('quack')
1210
branch = self.make_remote_branch(transport, client)
1211
branch._format._use_default_local_heads_to_fetch = lambda: False
1212
result = branch.heads_to_fetch()
1213
self.assertFinished(client)
1214
self.assertEqual((set(['tip']), set(['tagged-1', 'tagged-2'])), result)
1216
def test_backwards_compatible(self):
1217
self.setup_smart_server_with_call_log()
1218
# Make a branch with a single revision.
1219
builder = self.make_branch_builder('foo')
1220
builder.start_series()
1221
builder.build_snapshot('tip', None, [
1222
('add', ('', 'root-id', 'directory', ''))])
1223
builder.finish_series()
1224
branch = builder.get_branch()
1225
# Add two tags to that branch
1226
branch.tags.set_tag('tag-1', 'rev-1')
1227
branch.tags.set_tag('tag-2', 'rev-2')
1228
self.addCleanup(branch.lock_read().unlock)
1229
# Disable the heads_to_fetch verb
1230
verb = 'Branch.heads_to_fetch'
1231
self.disable_verb(verb)
1232
self.reset_smart_call_log()
1233
result = branch.heads_to_fetch()
1234
self.assertEqual((set(['tip']), set(['rev-1', 'rev-2'])), result)
1236
['Branch.last_revision_info', 'Branch.get_tags_bytes'],
1237
[call.call.method for call in self.hpss_calls])
1240
1145
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1242
1147
def test_empty_branch(self):
1405
1310
# unnecessarily invokes _ensure_real upon a call to lock_write.
1406
1311
branch._ensure_real = lambda: None
1407
1312
branch.lock_write()
1408
result = branch._set_last_revision(NULL_REVISION)
1313
result = branch.set_revision_history([])
1409
1314
branch.unlock()
1410
1315
self.assertEqual(None, result)
1411
1316
self.assertFinished(client)
1413
1318
def test_set_nonempty(self):
1414
# set_last_revision_info(N, rev-idN) is translated to calling
1319
# set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
1415
1320
# Branch.set_last_revision(path, rev-idN) on the wire.
1416
1321
transport = MemoryTransport()
1417
1322
transport.mkdir('branch')
1963
1867
class TestRepositoryFormat(TestRemoteRepository):
1965
1869
def test_fast_delta(self):
1966
true_name = groupcompress_repo.RepositoryFormat2a().network_name()
1870
true_name = groupcompress_repo.RepositoryFormatCHK1().network_name()
1967
1871
true_format = RemoteRepositoryFormat()
1968
1872
true_format._network_name = true_name
1969
1873
self.assertEqual(True, true_format.fast_deltas)
1970
false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
1874
false_name = pack_repo.RepositoryFormatKnitPack1().network_name()
1971
1875
false_format = RemoteRepositoryFormat()
1972
1876
false_format._network_name = false_name
1973
1877
self.assertEqual(False, false_format.fast_deltas)
1975
1879
def test_get_format_description(self):
1976
1880
remote_repo_format = RemoteRepositoryFormat()
1977
real_format = repository.format_registry.get_default()
1881
real_format = repository.RepositoryFormat.get_default_format()
1978
1882
remote_repo_format._network_name = real_format.network_name()
1979
1883
self.assertEqual(remoted_description(real_format),
1980
1884
remote_repo_format.get_format_description())
2870
2774
('pack collection autopack',)],
2873
def test_oom_error_reporting(self):
2874
"""An out-of-memory condition on the server is reported clearly"""
2875
transport_path = 'quack'
2876
repo, client = self.setup_fake_client_and_repository(transport_path)
2877
client.add_expected_call(
2878
'PackRepository.autopack', ('quack/',),
2879
'error', ('MemoryError',))
2880
err = self.assertRaises(errors.BzrError, repo.autopack)
2881
self.assertContainsRe(str(err), "^remote server out of mem")
2884
2778
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2885
2779
"""Base class for unit tests for bzrlib.remote._translate_error."""
2998
2885
expected_error = errors.DivergedBranches(branch, other_branch)
2999
2886
self.assertEqual(expected_error, translated_error)
3001
def test_NotStacked(self):
3002
branch = self.make_branch('')
3003
translated_error = self.translateTuple(('NotStacked',), branch=branch)
3004
expected_error = errors.NotStacked(branch)
3005
self.assertEqual(expected_error, translated_error)
3007
2888
def test_ReadError_no_args(self):
3008
2889
path = 'a path'
3009
2890
translated_error = self.translateTuple(('ReadError',), path=path)
3055
2935
expected_error = errors.PermissionDenied(path, extra)
3056
2936
self.assertEqual(expected_error, translated_error)
3058
# GZ 2011-03-02: TODO test for PermissionDenied with non-ascii 'extra'
3060
def test_NoSuchFile_context_path(self):
3061
local_path = "local path"
3062
translated_error = self.translateTuple(('ReadError', "remote path"),
3064
expected_error = errors.ReadError(local_path)
3065
self.assertEqual(expected_error, translated_error)
3067
def test_NoSuchFile_without_context(self):
3068
remote_path = "remote path"
3069
translated_error = self.translateTuple(('ReadError', remote_path))
3070
expected_error = errors.ReadError(remote_path)
3071
self.assertEqual(expected_error, translated_error)
3073
def test_ReadOnlyError(self):
3074
translated_error = self.translateTuple(('ReadOnlyError',))
3075
expected_error = errors.TransportNotPossible("readonly transport")
3076
self.assertEqual(expected_error, translated_error)
3078
def test_MemoryError(self):
3079
translated_error = self.translateTuple(('MemoryError',))
3080
self.assertStartsWith(str(translated_error),
3081
"remote server out of memory")
3083
def test_generic_IndexError_no_classname(self):
3084
err = errors.ErrorFromSmartServer(('error', "list index out of range"))
3085
translated_error = self.translateErrorFromSmartServer(err)
3086
expected_error = errors.UnknownErrorFromSmartServer(err)
3087
self.assertEqual(expected_error, translated_error)
3089
# GZ 2011-03-02: TODO test generic non-ascii error string
3091
def test_generic_KeyError(self):
3092
err = errors.ErrorFromSmartServer(('error', 'KeyError', "1"))
3093
translated_error = self.translateErrorFromSmartServer(err)
3094
expected_error = errors.UnknownErrorFromSmartServer(err)
3095
self.assertEqual(expected_error, translated_error)
3098
2939
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3099
2940
"""Unit tests for bzrlib.remote._translate_error's robustness.
3342
3183
def test_copy_content_into_avoids_revision_history(self):
3343
3184
local = self.make_branch('local')
3344
builder = self.make_branch_builder('remote')
3345
builder.build_commit(message="Commit.")
3185
remote_backing_tree = self.make_branch_and_tree('remote')
3186
remote_backing_tree.commit("Commit.")
3346
3187
remote_branch_url = self.smart_server.get_url() + 'remote'
3347
3188
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3348
3189
local.repository.fetch(remote_branch.repository)
3349
3190
self.hpss_calls = []
3350
3191
remote_branch.copy_content_into(local)
3351
3192
self.assertFalse('Branch.revision_history' in self.hpss_calls)
3353
def test_fetch_everything_needs_just_one_call(self):
3354
local = self.make_branch('local')
3355
builder = self.make_branch_builder('remote')
3356
builder.build_commit(message="Commit.")
3357
remote_branch_url = self.smart_server.get_url() + 'remote'
3358
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3359
self.hpss_calls = []
3360
local.repository.fetch(remote_branch.repository,
3361
fetch_spec=graph.EverythingResult(remote_branch.repository))
3362
self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
3364
def override_verb(self, verb_name, verb):
3365
request_handlers = request.request_handlers
3366
orig_verb = request_handlers.get(verb_name)
3367
request_handlers.register(verb_name, verb, override_existing=True)
3368
self.addCleanup(request_handlers.register, verb_name, orig_verb,
3369
override_existing=True)
3371
def test_fetch_everything_backwards_compat(self):
3372
"""Can fetch with EverythingResult even with pre 2.4 servers.
3374
Pre-2.4 do not support 'everything' searches with the
3375
Repository.get_stream_1.19 verb.
3378
class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
3379
"""A version of the Repository.get_stream_1.19 verb patched to
3380
reject 'everything' searches the way 2.3 and earlier do.
3382
def recreate_search(self, repository, search_bytes, discard_excess=False):
3383
verb_log.append(search_bytes.split('\n', 1)[0])
3384
if search_bytes == 'everything':
3385
return (None, request.FailedSmartServerResponse(('BadSearch',)))
3386
return super(OldGetStreamVerb,
3387
self).recreate_search(repository, search_bytes,
3388
discard_excess=discard_excess)
3389
self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
3390
local = self.make_branch('local')
3391
builder = self.make_branch_builder('remote')
3392
builder.build_commit(message="Commit.")
3393
remote_branch_url = self.smart_server.get_url() + 'remote'
3394
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3395
self.hpss_calls = []
3396
local.repository.fetch(remote_branch.repository,
3397
fetch_spec=graph.EverythingResult(remote_branch.repository))
3398
# make sure the overridden verb was used
3399
self.assertLength(1, verb_log)
3400
# more than one HPSS call is needed, but because it's a VFS callback
3401
# its hard to predict exactly how many.
3402
self.assertTrue(len(self.hpss_calls) > 1)