120
124
def cancel_read_body(self):
121
125
self._fake_client.expecting_body = False
127
def read_streamed_body(self):
124
131
class FakeClient(_SmartClient):
125
132
"""Lookalike for _SmartClient allowing testing."""
127
def __init__(self, responses):
128
# We don't call the super init because there is no medium.
134
def __init__(self, responses, fake_medium_base='fake base'):
129
135
"""Create a FakeClient.
131
:param respones: A list of response-tuple, body-data pairs to be sent
137
:param responses: A list of response-tuple, body-data pairs to be sent
134
140
self.responses = responses
136
142
self.expecting_body = False
143
_SmartClient.__init__(self, FakeMedium(fake_medium_base, self._calls))
138
145
def call(self, method, *args):
139
146
self._calls.append(('call', method, args))
145
152
self.expecting_body = True
146
153
return result[0], FakeProtocol(result[1], self)
155
def call_with_body_bytes_expecting_body(self, method, args, body):
156
self._calls.append(('call_with_body_bytes_expecting_body', method,
158
result = self.responses.pop(0)
159
self.expecting_body = True
160
return result[0], FakeProtocol(result[1], self)
163
class FakeMedium(object):
165
def __init__(self, base, client_calls):
167
self.connection = FakeConnection(client_calls)
168
self._client_calls = client_calls
171
class FakeConnection(object):
173
def __init__(self, client_calls):
174
self._remote_is_at_least_1_2 = True
175
self._client_calls = client_calls
177
def disconnect(self):
178
self._client_calls.append(('disconnect medium',))
181
class TestVfsHas(tests.TestCase):
183
def test_unicode_path(self):
184
client = FakeClient([(('yes',), )], '/')
185
transport = RemoteTransport('bzr://localhost/', _client=client)
186
filename = u'/hell\u00d8'.encode('utf8')
187
result = transport.has(filename)
189
[('call', 'has', (filename,))],
191
self.assertTrue(result)
149
194
class TestBzrDirOpenBranch(tests.TestCase):
151
196
def test_branch_present(self):
152
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )])
153
197
transport = MemoryTransport()
154
198
transport.mkdir('quack')
155
199
transport = transport.clone('quack')
200
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
156
202
bzrdir = RemoteBzrDir(transport, _client=client)
157
203
result = bzrdir.open_branch()
158
204
self.assertEqual(
159
[('call', 'BzrDir.open_branch', ('///quack/',)),
160
('call', 'BzrDir.find_repository', ('///quack/',))],
205
[('call', 'BzrDir.open_branch', ('quack/',)),
206
('call', 'BzrDir.find_repositoryV2', ('quack/',))],
162
208
self.assertIsInstance(result, RemoteBranch)
163
209
self.assertEqual(bzrdir, result.bzrdir)
165
211
def test_branch_missing(self):
166
client = FakeClient([(('nobranch',), )])
167
212
transport = MemoryTransport()
168
213
transport.mkdir('quack')
169
214
transport = transport.clone('quack')
215
client = FakeClient([(('nobranch',), )], transport.base)
170
216
bzrdir = RemoteBzrDir(transport, _client=client)
171
217
self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
172
218
self.assertEqual(
173
[('call', 'BzrDir.open_branch', ('///quack/',))],
176
def check_open_repository(self, rich_root, subtrees):
219
[('call', 'BzrDir.open_branch', ('quack/',))],
222
def test__get_tree_branch(self):
223
# _get_tree_branch is a form of open_branch, but it should only ask for
224
# branch opening, not any other network requests.
227
calls.append("Called")
229
transport = MemoryTransport()
230
# no requests on the network - catches other api calls being made.
231
client = FakeClient([], transport.base)
232
bzrdir = RemoteBzrDir(transport, _client=client)
233
# patch the open_branch call to record that it was called.
234
bzrdir.open_branch = open_branch
235
self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
236
self.assertEqual(["Called"], calls)
237
self.assertEqual([], client._calls)
239
def test_url_quoting_of_path(self):
240
# Relpaths on the wire should not be URL-escaped. So "~" should be
241
# transmitted as "~", not "%7E".
242
transport = RemoteTransport('bzr://localhost/~hello/')
243
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
245
bzrdir = RemoteBzrDir(transport, _client=client)
246
result = bzrdir.open_branch()
248
[('call', 'BzrDir.open_branch', ('~hello/',)),
249
('call', 'BzrDir.find_repositoryV2', ('~hello/',))],
252
def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
253
transport = MemoryTransport()
254
transport.mkdir('quack')
255
transport = transport.clone('quack')
178
257
rich_response = 'yes'
182
261
subtree_response = 'yes'
184
263
subtree_response = 'no'
185
client = FakeClient([(('ok', '', rich_response, subtree_response), ),])
186
transport = MemoryTransport()
187
transport.mkdir('quack')
188
transport = transport.clone('quack')
265
[(('ok', '', rich_response, subtree_response, external_lookup), ),],
189
267
bzrdir = RemoteBzrDir(transport, _client=client)
190
268
result = bzrdir.open_repository()
191
269
self.assertEqual(
192
[('call', 'BzrDir.find_repository', ('///quack/',))],
270
[('call', 'BzrDir.find_repositoryV2', ('quack/',))],
194
272
self.assertIsInstance(result, RemoteRepository)
195
273
self.assertEqual(bzrdir, result.bzrdir)
622
class TestRepositoryGetGraph(TestRemoteRepository):
624
def test_get_graph(self):
625
# get_graph returns a graph with the repository as the
628
transport_path = 'quack'
629
repo, client = self.setup_fake_client_and_repository(
630
responses, transport_path)
631
graph = repo.get_graph()
632
self.assertEqual(graph._parents_provider, repo)
635
class TestRepositoryGetParentMap(TestRemoteRepository):
637
def test_get_parent_map_caching(self):
638
# get_parent_map returns from cache until unlock()
639
# setup a reponse with two revisions
640
r1 = u'\u0e33'.encode('utf8')
641
r2 = u'\u0dab'.encode('utf8')
642
lines = [' '.join([r2, r1]), r1]
643
encoded_body = bz2.compress('\n'.join(lines))
644
responses = [(('ok', ), encoded_body), (('ok', ), encoded_body)]
646
transport_path = 'quack'
647
repo, client = self.setup_fake_client_and_repository(
648
responses, transport_path)
650
graph = repo.get_graph()
651
parents = graph.get_parent_map([r2])
652
self.assertEqual({r2: (r1,)}, parents)
653
# locking and unlocking deeper should not reset
656
parents = graph.get_parent_map([r1])
657
self.assertEqual({r1: (NULL_REVISION,)}, parents)
659
[('call_with_body_bytes_expecting_body',
660
'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
663
# now we call again, and it should use the second response.
665
graph = repo.get_graph()
666
parents = graph.get_parent_map([r1])
667
self.assertEqual({r1: (NULL_REVISION,)}, parents)
669
[('call_with_body_bytes_expecting_body',
670
'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
671
('call_with_body_bytes_expecting_body',
672
'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
677
def test_get_parent_map_reconnects_if_unknown_method(self):
679
"Generic bzr smart protocol error: "
680
"bad request 'Repository.get_parent_map'")
682
(('error', error_msg), ''),
684
transport_path = 'quack'
685
repo, client = self.setup_fake_client_and_repository(
686
responses, transport_path)
687
rev_id = 'revision-id'
688
parents = repo.get_parent_map([rev_id])
690
[('call_with_body_bytes_expecting_body',
691
'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
692
('disconnect medium',),
693
('call_expecting_body', 'Repository.get_revision_graph',
541
699
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
543
701
def test_null_revision(self):
783
948
malformed data should be.
785
950
record = ('bytes', [('name1',), ('name2',)])
786
pack_file = self.make_pack_file([record])
787
responses = [(('ok',), pack_file.getvalue()), ]
951
pack_stream = self.make_pack_stream([record])
952
responses = [(('ok',), pack_stream), ]
788
953
transport_path = 'quack'
789
954
repo, client = self.setup_fake_client_and_repository(
790
955
responses, transport_path)
791
stream = repo.get_data_stream(['revid'])
956
search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
957
stream = repo.get_data_stream_for_search(search)
792
958
self.assertRaises(errors.SmartProtocolError, list, stream)
794
960
def test_backwards_compatibility(self):
795
961
"""If the server doesn't recognise this request, fallback to VFS."""
797
963
"Generic bzr smart protocol error: "
798
"bad request 'Repository.stream_knit_data_for_revisions'")
964
"bad request 'Repository.stream_revisions_chunked'")
800
966
(('error', error_msg), '')]
801
967
repo, client = self.setup_fake_client_and_repository(
802
968
responses, 'path')
803
969
self.mock_called = False
804
970
repo._real_repository = MockRealRepository(self)
805
repo.get_data_stream(['revid'])
971
search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
972
repo.get_data_stream_for_search(search)
806
973
self.assertTrue(self.mock_called)
807
974
self.failIf(client.expecting_body,
808
975
"The protocol has been left in an unclean state that will cause "