53
53
RemoteBranchFormat,
56
57
RemoteRepositoryFormat,
58
59
from bzrlib.repofmt import groupcompress_repo, pack_repo
59
60
from bzrlib.revision import NULL_REVISION
60
from bzrlib.smart import medium
61
from bzrlib.smart import medium, request
61
62
from bzrlib.smart.client import _SmartClient
62
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
63
from bzrlib.smart.repository import (
64
SmartServerRepositoryGetParentMap,
65
SmartServerRepositoryGetStream_1_19,
63
67
from bzrlib.tests import (
65
split_suite_by_condition,
70
from bzrlib.tests.scenarios import load_tests_apply_scenarios
69
71
from bzrlib.transport.memory import MemoryTransport
70
72
from bzrlib.transport.remote import (
72
74
RemoteSSHTransport,
73
75
RemoteTCPTransport,
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 = [
79
load_tests = load_tests_apply_scenarios
82
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
81
{'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
86
{'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
83
{'transport_server': test_server.SmartTCPServer_for_testing})]
84
return multiply_tests(to_adapt, smart_server_version_scenarios, result)
87
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
88
{'transport_server': test_server.SmartTCPServer_for_testing})]
90
92
super(BasicRemoteObjectTests, self).setUp()
94
96
self.addCleanup(self.transport.disconnect)
96
98
def test_create_remote_bzrdir(self):
97
b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
99
b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
98
100
self.assertIsInstance(b, BzrDir)
100
102
def test_open_remote_branch(self):
101
103
# open a standalone branch in the working directory
102
b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
104
b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
103
105
branch = b.open_branch()
104
106
self.assertIsInstance(branch, Branch)
468
470
client.add_expected_call(
469
471
'BzrDir.cloning_metadir', ('quack/', 'False'),
470
472
'success', (control_name, '', ('branch', ''))),
471
a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
473
a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
473
475
result = a_bzrdir.cloning_metadir()
474
476
# We should have got a reference control dir with default branch and
494
496
client.add_expected_call(
495
497
'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
496
498
self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
497
remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
499
RemoteBzrDirFormat(), _client=client, _force_probe=True)
498
500
self.assertFinished(client)
500
502
def test_present_without_workingtree(self):
501
503
client, transport = self.make_fake_client_and_transport()
502
504
client.add_expected_call(
503
505
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
504
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
506
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
505
507
_client=client, _force_probe=True)
506
508
self.assertIsInstance(bd, RemoteBzrDir)
507
509
self.assertFalse(bd.has_workingtree())
512
514
client, transport = self.make_fake_client_and_transport()
513
515
client.add_expected_call(
514
516
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
515
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
517
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
516
518
_client=client, _force_probe=True)
517
519
self.assertIsInstance(bd, RemoteBzrDir)
518
520
self.assertTrue(bd.has_workingtree())
525
527
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
526
528
client.add_expected_call(
527
529
'BzrDir.open', ('quack/',), 'success', ('yes',))
528
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
530
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
529
531
_client=client, _force_probe=True)
530
532
self.assertIsInstance(bd, RemoteBzrDir)
531
533
self.assertFinished(client)
547
549
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
548
550
client.add_expected_call(
549
551
'BzrDir.open', ('quack/',), 'success', ('yes',))
550
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
552
bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
551
553
_client=client, _force_probe=True)
552
554
self.assertIsInstance(bd, RemoteBzrDir)
553
555
self.assertFinished(client)
723
725
format = branch._format
724
726
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())
727
757
class TestBzrDirCreateRepository(TestRemote):
1142
1172
[('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])
1145
1240
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1147
1242
def test_empty_branch(self):
2774
2869
('pack collection autopack',)],
2872
def test_oom_error_reporting(self):
2873
"""An out-of-memory condition on the server is reported clearly"""
2874
transport_path = 'quack'
2875
repo, client = self.setup_fake_client_and_repository(transport_path)
2876
client.add_expected_call(
2877
'PackRepository.autopack', ('quack/',),
2878
'error', ('MemoryError',))
2879
err = self.assertRaises(errors.BzrError, repo.autopack)
2880
self.assertContainsRe(str(err), "^remote server out of mem")
2778
2883
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2779
2884
"""Base class for unit tests for bzrlib.remote._translate_error."""
2885
2997
expected_error = errors.DivergedBranches(branch, other_branch)
2886
2998
self.assertEqual(expected_error, translated_error)
3000
def test_NotStacked(self):
3001
branch = self.make_branch('')
3002
translated_error = self.translateTuple(('NotStacked',), branch=branch)
3003
expected_error = errors.NotStacked(branch)
3004
self.assertEqual(expected_error, translated_error)
2888
3006
def test_ReadError_no_args(self):
2889
3007
path = 'a path'
2890
3008
translated_error = self.translateTuple(('ReadError',), path=path)
2935
3054
expected_error = errors.PermissionDenied(path, extra)
2936
3055
self.assertEqual(expected_error, translated_error)
3057
# GZ 2011-03-02: TODO test for PermissionDenied with non-ascii 'extra'
3059
def test_NoSuchFile_context_path(self):
3060
local_path = "local path"
3061
translated_error = self.translateTuple(('ReadError', "remote path"),
3063
expected_error = errors.ReadError(local_path)
3064
self.assertEqual(expected_error, translated_error)
3066
def test_NoSuchFile_without_context(self):
3067
remote_path = "remote path"
3068
translated_error = self.translateTuple(('ReadError', remote_path))
3069
expected_error = errors.ReadError(remote_path)
3070
self.assertEqual(expected_error, translated_error)
3072
def test_ReadOnlyError(self):
3073
translated_error = self.translateTuple(('ReadOnlyError',))
3074
expected_error = errors.TransportNotPossible("readonly transport")
3075
self.assertEqual(expected_error, translated_error)
3077
def test_MemoryError(self):
3078
translated_error = self.translateTuple(('MemoryError',))
3079
self.assertStartsWith(str(translated_error),
3080
"remote server out of memory")
3082
def test_generic_IndexError_no_classname(self):
3083
err = errors.ErrorFromSmartServer(('error', "list index out of range"))
3084
translated_error = self.translateErrorFromSmartServer(err)
3085
expected_error = errors.UnknownErrorFromSmartServer(err)
3086
self.assertEqual(expected_error, translated_error)
3088
# GZ 2011-03-02: TODO test generic non-ascii error string
3090
def test_generic_KeyError(self):
3091
err = errors.ErrorFromSmartServer(('error', 'KeyError', "1"))
3092
translated_error = self.translateErrorFromSmartServer(err)
3093
expected_error = errors.UnknownErrorFromSmartServer(err)
3094
self.assertEqual(expected_error, translated_error)
2939
3097
class TestErrorTranslationRobustness(TestErrorTranslationBase):
2940
3098
"""Unit tests for bzrlib.remote._translate_error's robustness.
3183
3341
def test_copy_content_into_avoids_revision_history(self):
3184
3342
local = self.make_branch('local')
3185
remote_backing_tree = self.make_branch_and_tree('remote')
3186
remote_backing_tree.commit("Commit.")
3343
builder = self.make_branch_builder('remote')
3344
builder.build_commit(message="Commit.")
3187
3345
remote_branch_url = self.smart_server.get_url() + 'remote'
3188
3346
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3189
3347
local.repository.fetch(remote_branch.repository)
3190
3348
self.hpss_calls = []
3191
3349
remote_branch.copy_content_into(local)
3192
3350
self.assertFalse('Branch.revision_history' in self.hpss_calls)
3352
def test_fetch_everything_needs_just_one_call(self):
3353
local = self.make_branch('local')
3354
builder = self.make_branch_builder('remote')
3355
builder.build_commit(message="Commit.")
3356
remote_branch_url = self.smart_server.get_url() + 'remote'
3357
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3358
self.hpss_calls = []
3359
local.repository.fetch(remote_branch.repository,
3360
fetch_spec=graph.EverythingResult(remote_branch.repository))
3361
self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
3363
def override_verb(self, verb_name, verb):
3364
request_handlers = request.request_handlers
3365
orig_verb = request_handlers.get(verb_name)
3366
request_handlers.register(verb_name, verb, override_existing=True)
3367
self.addCleanup(request_handlers.register, verb_name, orig_verb,
3368
override_existing=True)
3370
def test_fetch_everything_backwards_compat(self):
3371
"""Can fetch with EverythingResult even with pre 2.4 servers.
3373
Pre-2.4 do not support 'everything' searches with the
3374
Repository.get_stream_1.19 verb.
3377
class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
3378
"""A version of the Repository.get_stream_1.19 verb patched to
3379
reject 'everything' searches the way 2.3 and earlier do.
3381
def recreate_search(self, repository, search_bytes, discard_excess=False):
3382
verb_log.append(search_bytes.split('\n', 1)[0])
3383
if search_bytes == 'everything':
3384
return (None, request.FailedSmartServerResponse(('BadSearch',)))
3385
return super(OldGetStreamVerb,
3386
self).recreate_search(repository, search_bytes,
3387
discard_excess=discard_excess)
3388
self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
3389
local = self.make_branch('local')
3390
builder = self.make_branch_builder('remote')
3391
builder.build_commit(message="Commit.")
3392
remote_branch_url = self.smart_server.get_url() + 'remote'
3393
remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3394
self.hpss_calls = []
3395
local.repository.fetch(remote_branch.repository,
3396
fetch_spec=graph.EverythingResult(remote_branch.repository))
3397
# make sure the overridden verb was used
3398
self.assertLength(1, verb_log)
3399
# more than one HPSS call is needed, but because it's a VFS callback
3400
# its hard to predict exactly how many.
3401
self.assertTrue(len(self.hpss_calls) > 1)