478
447
self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
479
448
self.assertEqual(None, result._repository_format)
480
449
self.assertEqual(None, result._branch_format)
481
self.assertFinished(client)
484
class TestBzrDirOpen(TestRemote):
486
def make_fake_client_and_transport(self, path='quack'):
487
transport = MemoryTransport()
488
transport.mkdir(path)
489
transport = transport.clone(path)
490
client = FakeClient(transport.base)
491
return client, transport
493
def test_absent(self):
494
client, transport = self.make_fake_client_and_transport()
495
client.add_expected_call(
496
'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
497
self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
498
remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
499
self.assertFinished(client)
501
def test_present_without_workingtree(self):
502
client, transport = self.make_fake_client_and_transport()
503
client.add_expected_call(
504
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
505
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
506
_client=client, _force_probe=True)
507
self.assertIsInstance(bd, RemoteBzrDir)
508
self.assertFalse(bd.has_workingtree())
509
self.assertRaises(errors.NoWorkingTree, bd.open_workingtree)
510
self.assertFinished(client)
512
def test_present_with_workingtree(self):
513
client, transport = self.make_fake_client_and_transport()
514
client.add_expected_call(
515
'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
516
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
517
_client=client, _force_probe=True)
518
self.assertIsInstance(bd, RemoteBzrDir)
519
self.assertTrue(bd.has_workingtree())
520
self.assertRaises(errors.NotLocalUrl, bd.open_workingtree)
521
self.assertFinished(client)
523
def test_backwards_compat(self):
524
client, transport = self.make_fake_client_and_transport()
525
client.add_expected_call(
526
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
527
client.add_expected_call(
528
'BzrDir.open', ('quack/',), 'success', ('yes',))
529
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
530
_client=client, _force_probe=True)
531
self.assertIsInstance(bd, RemoteBzrDir)
532
self.assertFinished(client)
534
def test_backwards_compat_hpss_v2(self):
535
client, transport = self.make_fake_client_and_transport()
536
# Monkey-patch fake client to simulate real-world behaviour with v2
537
# server: upon first RPC call detect the protocol version, and because
538
# the version is 2 also do _remember_remote_is_before((1, 6)) before
539
# continuing with the RPC.
540
orig_check_call = client._check_call
541
def check_call(method, args):
542
client._medium._protocol_version = 2
543
client._medium._remember_remote_is_before((1, 6))
544
client._check_call = orig_check_call
545
client._check_call(method, args)
546
client._check_call = check_call
547
client.add_expected_call(
548
'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
549
client.add_expected_call(
550
'BzrDir.open', ('quack/',), 'success', ('yes',))
551
bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
552
_client=client, _force_probe=True)
553
self.assertIsInstance(bd, RemoteBzrDir)
554
self.assertFinished(client)
450
client.finished_test()
557
453
class TestBzrDirOpenBranch(TestRemote):
848
739
self.assertEqual(network_name, repo._format.network_name())
851
class TestBzrDirFormatInitializeEx(TestRemote):
853
def test_success(self):
854
"""Simple test for typical successful call."""
855
fmt = bzrdir.RemoteBzrDirFormat()
856
default_format_name = BzrDirFormat.get_default_format().network_name()
857
transport = self.get_transport()
858
client = FakeClient(transport.base)
859
client.add_expected_call(
860
'BzrDirFormat.initialize_ex_1.16',
861
(default_format_name, 'path', 'False', 'False', 'False', '',
862
'', '', '', 'False'),
864
('.', 'no', 'no', 'yes', 'repo fmt', 'repo bzrdir fmt',
865
'bzrdir fmt', 'False', '', '', 'repo lock token'))
866
# XXX: It would be better to call fmt.initialize_on_transport_ex, but
867
# it's currently hard to test that without supplying a real remote
868
# transport connected to a real server.
869
result = fmt._initialize_on_transport_ex_rpc(client, 'path',
870
transport, False, False, False, None, None, None, None, False)
871
self.assertFinished(client)
873
def test_error(self):
874
"""Error responses are translated, e.g. 'PermissionDenied' raises the
875
corresponding error from the client.
877
fmt = bzrdir.RemoteBzrDirFormat()
878
default_format_name = BzrDirFormat.get_default_format().network_name()
879
transport = self.get_transport()
880
client = FakeClient(transport.base)
881
client.add_expected_call(
882
'BzrDirFormat.initialize_ex_1.16',
883
(default_format_name, 'path', 'False', 'False', 'False', '',
884
'', '', '', 'False'),
886
('PermissionDenied', 'path', 'extra info'))
887
# XXX: It would be better to call fmt.initialize_on_transport_ex, but
888
# it's currently hard to test that without supplying a real remote
889
# transport connected to a real server.
890
err = self.assertRaises(errors.PermissionDenied,
891
fmt._initialize_on_transport_ex_rpc, client, 'path', transport,
892
False, False, False, None, None, None, None, False)
893
self.assertEqual('path', err.path)
894
self.assertEqual(': extra info', err.extra)
895
self.assertFinished(client)
897
def test_error_from_real_server(self):
898
"""Integration test for error translation."""
899
transport = self.make_smart_server('foo')
900
transport = transport.clone('no-such-path')
901
fmt = bzrdir.RemoteBzrDirFormat()
902
err = self.assertRaises(errors.NoSuchFile,
903
fmt.initialize_on_transport_ex, transport, create_prefix=False)
906
742
class OldSmartClient(object):
907
743
"""A fake smart client for test_old_version that just returns a version one
908
744
response to the 'hello' (query version) command.
1091
862
transport = transport.clone('quack')
1092
863
branch = self.make_remote_branch(transport, client)
1093
864
result = branch.tags.get_tag_dict()
1094
self.assertFinished(client)
865
client.finished_test()
1095
866
self.assertEqual({}, result)
1098
class TestBranchSetTagsBytes(RemoteBranchTestCase):
1100
def test_trivial(self):
1101
transport = MemoryTransport()
1102
client = FakeClient(transport.base)
1103
client.add_expected_call(
1104
'Branch.get_stacked_on_url', ('quack/',),
1105
'error', ('NotStacked',))
1106
client.add_expected_call(
1107
'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1109
transport.mkdir('quack')
1110
transport = transport.clone('quack')
1111
branch = self.make_remote_branch(transport, client)
1112
self.lock_remote_branch(branch)
1113
branch._set_tags_bytes('tags bytes')
1114
self.assertFinished(client)
1115
self.assertEqual('tags bytes', client._calls[-1][-1])
1117
def test_backwards_compatible(self):
1118
transport = MemoryTransport()
1119
client = FakeClient(transport.base)
1120
client.add_expected_call(
1121
'Branch.get_stacked_on_url', ('quack/',),
1122
'error', ('NotStacked',))
1123
client.add_expected_call(
1124
'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1125
'unknown', ('Branch.set_tags_bytes',))
1126
transport.mkdir('quack')
1127
transport = transport.clone('quack')
1128
branch = self.make_remote_branch(transport, client)
1129
self.lock_remote_branch(branch)
1130
class StubRealBranch(object):
1133
def _set_tags_bytes(self, bytes):
1134
self.calls.append(('set_tags_bytes', bytes))
1135
real_branch = StubRealBranch()
1136
branch._real_branch = real_branch
1137
branch._set_tags_bytes('tags bytes')
1138
# Call a second time, to exercise the 'remote version already inferred'
1140
branch._set_tags_bytes('tags bytes')
1141
self.assertFinished(client)
1143
[('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1146
869
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1148
871
def test_empty_branch(self):
1597
1331
self.assertEqual('rejection message', err.msg)
1600
class TestBranchGetSetConfig(RemoteBranchTestCase):
1334
class TestBranchControlGetBranchConf(tests.TestCaseWithMemoryTransport):
1335
"""Getting the branch configuration should use an abstract method not vfs.
1602
1338
def test_get_branch_conf(self):
1603
# in an empty branch we decode the response properly
1604
client = FakeClient()
1605
client.add_expected_call(
1606
'Branch.get_stacked_on_url', ('memory:///',),
1607
'error', ('NotStacked',),)
1608
client.add_success_response_with_body('# config file body', 'ok')
1609
transport = MemoryTransport()
1610
branch = self.make_remote_branch(transport, client)
1611
config = branch.get_config()
1612
config.has_explicit_nickname()
1614
[('call', 'Branch.get_stacked_on_url', ('memory:///',)),
1615
('call_expecting_body', 'Branch.get_config_file', ('memory:///',))],
1618
def test_get_multi_line_branch_conf(self):
1619
# Make sure that multiple-line branch.conf files are supported
1621
# https://bugs.edge.launchpad.net/bzr/+bug/354075
1622
client = FakeClient()
1623
client.add_expected_call(
1624
'Branch.get_stacked_on_url', ('memory:///',),
1625
'error', ('NotStacked',),)
1626
client.add_success_response_with_body('a = 1\nb = 2\nc = 3\n', 'ok')
1627
transport = MemoryTransport()
1628
branch = self.make_remote_branch(transport, client)
1629
config = branch.get_config()
1630
self.assertEqual(u'2', config.get_user_option('b'))
1632
def test_set_option(self):
1633
client = FakeClient()
1634
client.add_expected_call(
1635
'Branch.get_stacked_on_url', ('memory:///',),
1636
'error', ('NotStacked',),)
1637
client.add_expected_call(
1638
'Branch.lock_write', ('memory:///', '', ''),
1639
'success', ('ok', 'branch token', 'repo token'))
1640
client.add_expected_call(
1641
'Branch.set_config_option', ('memory:///', 'branch token',
1642
'repo token', 'foo', 'bar', ''),
1644
client.add_expected_call(
1645
'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1647
transport = MemoryTransport()
1648
branch = self.make_remote_branch(transport, client)
1650
config = branch._get_config()
1651
config.set_option('foo', 'bar')
1653
self.assertFinished(client)
1655
def test_backwards_compat_set_option(self):
1656
self.setup_smart_server_with_call_log()
1657
branch = self.make_branch('.')
1658
verb = 'Branch.set_config_option'
1659
self.disable_verb(verb)
1661
self.addCleanup(branch.unlock)
1662
self.reset_smart_call_log()
1663
branch._get_config().set_option('value', 'name')
1664
self.assertLength(10, self.hpss_calls)
1665
self.assertEqual('value', branch._get_config().get_option('name'))
1339
raise tests.KnownFailure('branch.conf is not retrieved by get_config_file')
1340
## # We should see that branch.get_config() does a single rpc to get the
1341
## # remote configuration file, abstracting away where that is stored on
1342
## # the server. However at the moment it always falls back to using the
1343
## # vfs, and this would need some changes in config.py.
1345
## # in an empty branch we decode the response properly
1346
## client = FakeClient([(('ok', ), '# config file body')], self.get_url())
1347
## # we need to make a real branch because the remote_branch.control_files
1348
## # will trigger _ensure_real.
1349
## branch = self.make_branch('quack')
1350
## transport = branch.bzrdir.root_transport
1351
## # we do not want bzrdir to make any remote calls
1352
## bzrdir = RemoteBzrDir(transport, _client=False)
1353
## branch = RemoteBranch(bzrdir, None, _client=client)
1354
## config = branch.get_config()
1355
## self.assertEqual(
1356
## [('call_expecting_body', 'Branch.get_config_file', ('quack/',))],
1668
1360
class TestBranchLockWrite(RemoteBranchTestCase):
2008
1643
errors.UnexpectedSmartServerResponse,
2009
1644
repo.get_parent_map, ['a-revision-id'])
2011
def test_get_parent_map_negative_caches_missing_keys(self):
2012
self.setup_smart_server_with_call_log()
2013
repo = self.make_repository('foo')
2014
self.assertIsInstance(repo, RemoteRepository)
2016
self.addCleanup(repo.unlock)
2017
self.reset_smart_call_log()
2018
graph = repo.get_graph()
2019
self.assertEqual({},
2020
graph.get_parent_map(['some-missing', 'other-missing']))
2021
self.assertLength(1, self.hpss_calls)
2022
# No call if we repeat this
2023
self.reset_smart_call_log()
2024
graph = repo.get_graph()
2025
self.assertEqual({},
2026
graph.get_parent_map(['some-missing', 'other-missing']))
2027
self.assertLength(0, self.hpss_calls)
2028
# Asking for more unknown keys makes a request.
2029
self.reset_smart_call_log()
2030
graph = repo.get_graph()
2031
self.assertEqual({},
2032
graph.get_parent_map(['some-missing', 'other-missing',
2034
self.assertLength(1, self.hpss_calls)
2036
def disableExtraResults(self):
2037
self.overrideAttr(SmartServerRepositoryGetParentMap,
2038
'no_extra_results', True)
2040
def test_null_cached_missing_and_stop_key(self):
2041
self.setup_smart_server_with_call_log()
2042
# Make a branch with a single revision.
2043
builder = self.make_branch_builder('foo')
2044
builder.start_series()
2045
builder.build_snapshot('first', None, [
2046
('add', ('', 'root-id', 'directory', ''))])
2047
builder.finish_series()
2048
branch = builder.get_branch()
2049
repo = branch.repository
2050
self.assertIsInstance(repo, RemoteRepository)
2051
# Stop the server from sending extra results.
2052
self.disableExtraResults()
2054
self.addCleanup(repo.unlock)
2055
self.reset_smart_call_log()
2056
graph = repo.get_graph()
2057
# Query for 'first' and 'null:'. Because 'null:' is a parent of
2058
# 'first' it will be a candidate for the stop_keys of subsequent
2059
# requests, and because 'null:' was queried but not returned it will be
2060
# cached as missing.
2061
self.assertEqual({'first': ('null:',)},
2062
graph.get_parent_map(['first', 'null:']))
2063
# Now query for another key. This request will pass along a recipe of
2064
# start and stop keys describing the already cached results, and this
2065
# recipe's revision count must be correct (or else it will trigger an
2066
# error from the server).
2067
self.assertEqual({}, graph.get_parent_map(['another-key']))
2068
# This assertion guards against disableExtraResults silently failing to
2069
# work, thus invalidating the test.
2070
self.assertLength(2, self.hpss_calls)
2072
def test_get_parent_map_gets_ghosts_from_result(self):
2073
# asking for a revision should negatively cache close ghosts in its
2075
self.setup_smart_server_with_call_log()
2076
tree = self.make_branch_and_memory_tree('foo')
2079
builder = treebuilder.TreeBuilder()
2080
builder.start_tree(tree)
2082
builder.finish_tree()
2083
tree.set_parent_ids(['non-existant'], allow_leftmost_as_ghost=True)
2084
rev_id = tree.commit('')
2088
self.addCleanup(tree.unlock)
2089
repo = tree.branch.repository
2090
self.assertIsInstance(repo, RemoteRepository)
2092
repo.get_parent_map([rev_id])
2093
self.reset_smart_call_log()
2094
# Now asking for rev_id's ghost parent should not make calls
2095
self.assertEqual({}, repo.get_parent_map(['non-existant']))
2096
self.assertLength(0, self.hpss_calls)
2099
1647
class TestGetParentMapAllowsNew(tests.TestCaseWithTransport):
2101
1649
def test_allows_new_revisions(self):
2102
1650
"""get_parent_map's results can be updated by commit."""
2103
smart_server = test_server.SmartTCPServer_for_testing()
2104
self.start_server(smart_server)
1651
smart_server = server.SmartTCPServer_for_testing()
1652
smart_server.setUp()
1653
self.addCleanup(smart_server.tearDown)
2105
1654
self.make_branch('branch')
2106
1655
branch = Branch.open(smart_server.get_url() + '/branch')
2107
1656
tree = branch.create_checkout('tree', lightweight=True)
2190
1739
self.assertEqual(('AnUnexpectedError',), e.error_tuple)
2193
class TestRepositoryGetRevIdForRevno(TestRemoteRepository):
2196
repo, client = self.setup_fake_client_and_repository('quack')
2197
client.add_expected_call(
2198
'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2199
'success', ('ok', 'rev-five'))
2200
result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2201
self.assertEqual((True, 'rev-five'), result)
2202
self.assertFinished(client)
2204
def test_history_incomplete(self):
2205
repo, client = self.setup_fake_client_and_repository('quack')
2206
client.add_expected_call(
2207
'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2208
'success', ('history-incomplete', 10, 'rev-ten'))
2209
result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2210
self.assertEqual((False, (10, 'rev-ten')), result)
2211
self.assertFinished(client)
2213
def test_history_incomplete_with_fallback(self):
2214
"""A 'history-incomplete' response causes the fallback repository to be
2215
queried too, if one is set.
2217
# Make a repo with a fallback repo, both using a FakeClient.
2218
format = remote.response_tuple_to_repo_format(
2219
('yes', 'no', 'yes', self.get_repo_format().network_name()))
2220
repo, client = self.setup_fake_client_and_repository('quack')
2221
repo._format = format
2222
fallback_repo, ignored = self.setup_fake_client_and_repository(
2224
fallback_repo._client = client
2225
fallback_repo._format = format
2226
repo.add_fallback_repository(fallback_repo)
2227
# First the client should ask the primary repo
2228
client.add_expected_call(
2229
'Repository.get_rev_id_for_revno', ('quack/', 1, (42, 'rev-foo')),
2230
'success', ('history-incomplete', 2, 'rev-two'))
2231
# Then it should ask the fallback, using revno/revid from the
2232
# history-incomplete response as the known revno/revid.
2233
client.add_expected_call(
2234
'Repository.get_rev_id_for_revno',('fallback/', 1, (2, 'rev-two')),
2235
'success', ('ok', 'rev-one'))
2236
result = repo.get_rev_id_for_revno(1, (42, 'rev-foo'))
2237
self.assertEqual((True, 'rev-one'), result)
2238
self.assertFinished(client)
2240
def test_nosuchrevision(self):
2241
# 'nosuchrevision' is returned when the known-revid is not found in the
2242
# remote repo. The client translates that response to NoSuchRevision.
2243
repo, client = self.setup_fake_client_and_repository('quack')
2244
client.add_expected_call(
2245
'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2246
'error', ('nosuchrevision', 'rev-foo'))
2248
errors.NoSuchRevision,
2249
repo.get_rev_id_for_revno, 5, (42, 'rev-foo'))
2250
self.assertFinished(client)
2252
def test_branch_fallback_locking(self):
2253
"""RemoteBranch.get_rev_id takes a read lock, and tries to call the
2254
get_rev_id_for_revno verb. If the verb is unknown the VFS fallback
2255
will be invoked, which will fail if the repo is unlocked.
2257
self.setup_smart_server_with_call_log()
2258
tree = self.make_branch_and_memory_tree('.')
2260
rev1 = tree.commit('First')
2261
rev2 = tree.commit('Second')
2263
branch = tree.branch
2264
self.assertFalse(branch.is_locked())
2265
self.reset_smart_call_log()
2266
verb = 'Repository.get_rev_id_for_revno'
2267
self.disable_verb(verb)
2268
self.assertEqual(rev1, branch.get_rev_id(1))
2269
self.assertLength(1, [call for call in self.hpss_calls if
2270
call.call.method == verb])
2273
1742
class TestRepositoryIsShared(TestRemoteRepository):
2275
1744
def test_is_shared(self):
2390
1859
self.assertEqual([], client._calls)
2393
class TestRepositoryInsertStreamBase(TestRemoteRepository):
2394
"""Base class for Repository.insert_stream and .insert_stream_1.19
2398
def checkInsertEmptyStream(self, repo, client):
2399
"""Insert an empty stream, checking the result.
2401
This checks that there are no resume_tokens or missing_keys, and that
2402
the client is finished.
2404
sink = repo._get_sink()
2405
fmt = repository.RepositoryFormat.get_default_format()
2406
resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2407
self.assertEqual([], resume_tokens)
2408
self.assertEqual(set(), missing_keys)
2409
self.assertFinished(client)
2412
class TestRepositoryInsertStream(TestRepositoryInsertStreamBase):
2413
"""Tests for using Repository.insert_stream verb when the _1.19 variant is
2416
This test case is very similar to TestRepositoryInsertStream_1_19.
2420
TestRemoteRepository.setUp(self)
2421
self.disable_verb('Repository.insert_stream_1.19')
2423
def test_unlocked_repo(self):
2424
transport_path = 'quack'
2425
repo, client = self.setup_fake_client_and_repository(transport_path)
2426
client.add_expected_call(
2427
'Repository.insert_stream_1.19', ('quack/', ''),
2428
'unknown', ('Repository.insert_stream_1.19',))
2429
client.add_expected_call(
2430
'Repository.insert_stream', ('quack/', ''),
2432
client.add_expected_call(
2433
'Repository.insert_stream', ('quack/', ''),
2435
self.checkInsertEmptyStream(repo, client)
2437
def test_locked_repo_with_no_lock_token(self):
2438
transport_path = 'quack'
2439
repo, client = self.setup_fake_client_and_repository(transport_path)
2440
client.add_expected_call(
2441
'Repository.lock_write', ('quack/', ''),
2442
'success', ('ok', ''))
2443
client.add_expected_call(
2444
'Repository.insert_stream_1.19', ('quack/', ''),
2445
'unknown', ('Repository.insert_stream_1.19',))
2446
client.add_expected_call(
2447
'Repository.insert_stream', ('quack/', ''),
2449
client.add_expected_call(
2450
'Repository.insert_stream', ('quack/', ''),
2453
self.checkInsertEmptyStream(repo, client)
2455
def test_locked_repo_with_lock_token(self):
2456
transport_path = 'quack'
2457
repo, client = self.setup_fake_client_and_repository(transport_path)
2458
client.add_expected_call(
2459
'Repository.lock_write', ('quack/', ''),
2460
'success', ('ok', 'a token'))
2461
client.add_expected_call(
2462
'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2463
'unknown', ('Repository.insert_stream_1.19',))
2464
client.add_expected_call(
2465
'Repository.insert_stream_locked', ('quack/', '', 'a token'),
2467
client.add_expected_call(
2468
'Repository.insert_stream_locked', ('quack/', '', 'a token'),
2471
self.checkInsertEmptyStream(repo, client)
2473
def test_stream_with_inventory_deltas(self):
2474
"""'inventory-deltas' substreams cannot be sent to the
2475
Repository.insert_stream verb, because not all servers that implement
2476
that verb will accept them. So when one is encountered the RemoteSink
2477
immediately stops using that verb and falls back to VFS insert_stream.
2479
transport_path = 'quack'
2480
repo, client = self.setup_fake_client_and_repository(transport_path)
2481
client.add_expected_call(
2482
'Repository.insert_stream_1.19', ('quack/', ''),
2483
'unknown', ('Repository.insert_stream_1.19',))
2484
client.add_expected_call(
2485
'Repository.insert_stream', ('quack/', ''),
2487
client.add_expected_call(
2488
'Repository.insert_stream', ('quack/', ''),
2490
# Create a fake real repository for insert_stream to fall back on, so
2491
# that we can directly see the records the RemoteSink passes to the
2496
def insert_stream(self, stream, src_format, resume_tokens):
2497
for substream_kind, substream in stream:
2498
self.records.append(
2499
(substream_kind, [record.key for record in substream]))
2500
return ['fake tokens'], ['fake missing keys']
2501
fake_real_sink = FakeRealSink()
2502
class FakeRealRepository:
2503
def _get_sink(self):
2504
return fake_real_sink
2505
def is_in_write_group(self):
2507
def refresh_data(self):
2509
repo._real_repository = FakeRealRepository()
2510
sink = repo._get_sink()
2511
fmt = repository.RepositoryFormat.get_default_format()
2512
stream = self.make_stream_with_inv_deltas(fmt)
2513
resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2514
# Every record from the first inventory delta should have been sent to
2516
expected_records = [
2517
('inventory-deltas', [('rev2',), ('rev3',)]),
2518
('texts', [('some-rev', 'some-file')])]
2519
self.assertEqual(expected_records, fake_real_sink.records)
2520
# The return values from the real sink's insert_stream are propagated
2521
# back to the original caller.
2522
self.assertEqual(['fake tokens'], resume_tokens)
2523
self.assertEqual(['fake missing keys'], missing_keys)
2524
self.assertFinished(client)
2526
def make_stream_with_inv_deltas(self, fmt):
2527
"""Make a simple stream with an inventory delta followed by more
2528
records and more substreams to test that all records and substreams
2529
from that point on are used.
2531
This sends, in order:
2532
* inventories substream: rev1, rev2, rev3. rev2 and rev3 are
2534
* texts substream: (some-rev, some-file)
2536
# Define a stream using generators so that it isn't rewindable.
2537
inv = inventory.Inventory(revision_id='rev1')
2538
inv.root.revision = 'rev1'
2539
def stream_with_inv_delta():
2540
yield ('inventories', inventories_substream())
2541
yield ('inventory-deltas', inventory_delta_substream())
2543
versionedfile.FulltextContentFactory(
2544
('some-rev', 'some-file'), (), None, 'content')])
2545
def inventories_substream():
2546
# An empty inventory fulltext. This will be streamed normally.
2547
text = fmt._serializer.write_inventory_to_string(inv)
2548
yield versionedfile.FulltextContentFactory(
2549
('rev1',), (), None, text)
2550
def inventory_delta_substream():
2551
# An inventory delta. This can't be streamed via this verb, so it
2552
# will trigger a fallback to VFS insert_stream.
2553
entry = inv.make_entry(
2554
'directory', 'newdir', inv.root.file_id, 'newdir-id')
2555
entry.revision = 'ghost'
2556
delta = [(None, 'newdir', 'newdir-id', entry)]
2557
serializer = inventory_delta.InventoryDeltaSerializer(
2558
versioned_root=True, tree_references=False)
2559
lines = serializer.delta_to_lines('rev1', 'rev2', delta)
2560
yield versionedfile.ChunkedContentFactory(
2561
('rev2',), (('rev1',)), None, lines)
2563
lines = serializer.delta_to_lines('rev1', 'rev3', delta)
2564
yield versionedfile.ChunkedContentFactory(
2565
('rev3',), (('rev1',)), None, lines)
2566
return stream_with_inv_delta()
2569
class TestRepositoryInsertStream_1_19(TestRepositoryInsertStreamBase):
2571
def test_unlocked_repo(self):
2572
transport_path = 'quack'
2573
repo, client = self.setup_fake_client_and_repository(transport_path)
2574
client.add_expected_call(
2575
'Repository.insert_stream_1.19', ('quack/', ''),
2577
client.add_expected_call(
2578
'Repository.insert_stream_1.19', ('quack/', ''),
2580
self.checkInsertEmptyStream(repo, client)
2582
def test_locked_repo_with_no_lock_token(self):
2583
transport_path = 'quack'
2584
repo, client = self.setup_fake_client_and_repository(transport_path)
2585
client.add_expected_call(
2586
'Repository.lock_write', ('quack/', ''),
2587
'success', ('ok', ''))
2588
client.add_expected_call(
2589
'Repository.insert_stream_1.19', ('quack/', ''),
2591
client.add_expected_call(
2592
'Repository.insert_stream_1.19', ('quack/', ''),
2595
self.checkInsertEmptyStream(repo, client)
2597
def test_locked_repo_with_lock_token(self):
2598
transport_path = 'quack'
2599
repo, client = self.setup_fake_client_and_repository(transport_path)
2600
client.add_expected_call(
2601
'Repository.lock_write', ('quack/', ''),
2602
'success', ('ok', 'a token'))
2603
client.add_expected_call(
2604
'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2606
client.add_expected_call(
2607
'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2610
self.checkInsertEmptyStream(repo, client)
1862
class TestRepositoryInsertStream(TestRemoteRepository):
1864
def test_unlocked_repo(self):
1865
transport_path = 'quack'
1866
repo, client = self.setup_fake_client_and_repository(transport_path)
1867
client.add_expected_call(
1868
'Repository.insert_stream', ('quack/', ''),
1870
client.add_expected_call(
1871
'Repository.insert_stream', ('quack/', ''),
1873
sink = repo._get_sink()
1874
fmt = repository.RepositoryFormat.get_default_format()
1875
resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
1876
self.assertEqual([], resume_tokens)
1877
self.assertEqual(set(), missing_keys)
1878
client.finished_test()
1880
def test_locked_repo_with_no_lock_token(self):
1881
transport_path = 'quack'
1882
repo, client = self.setup_fake_client_and_repository(transport_path)
1883
client.add_expected_call(
1884
'Repository.lock_write', ('quack/', ''),
1885
'success', ('ok', ''))
1886
client.add_expected_call(
1887
'Repository.insert_stream', ('quack/', ''),
1889
client.add_expected_call(
1890
'Repository.insert_stream', ('quack/', ''),
1893
sink = repo._get_sink()
1894
fmt = repository.RepositoryFormat.get_default_format()
1895
resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
1896
self.assertEqual([], resume_tokens)
1897
self.assertEqual(set(), missing_keys)
1898
client.finished_test()
1900
def test_locked_repo_with_lock_token(self):
1901
transport_path = 'quack'
1902
repo, client = self.setup_fake_client_and_repository(transport_path)
1903
client.add_expected_call(
1904
'Repository.lock_write', ('quack/', ''),
1905
'success', ('ok', 'a token'))
1906
client.add_expected_call(
1907
'Repository.insert_stream_locked', ('quack/', '', 'a token'),
1909
client.add_expected_call(
1910
'Repository.insert_stream_locked', ('quack/', '', 'a token'),
1913
sink = repo._get_sink()
1914
fmt = repository.RepositoryFormat.get_default_format()
1915
resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
1916
self.assertEqual([], resume_tokens)
1917
self.assertEqual(set(), missing_keys)
1918
client.finished_test()
2613
1921
class TestRepositoryTarball(TestRemoteRepository):