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))
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):
169
class TestVfsHas(tests.TestCase):
171
def test_unicode_path(self):
172
client = FakeClient([(('yes',), )], '/')
173
transport = RemoteTransport('bzr://localhost/', _client=client)
174
filename = u'/hell\u00d8'.encode('utf8')
175
result = transport.has(filename)
177
[('call', 'has', (filename,))],
179
self.assertTrue(result)
149
182
class TestBzrDirOpenBranch(tests.TestCase):
151
184
def test_branch_present(self):
152
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )])
153
185
transport = MemoryTransport()
154
186
transport.mkdir('quack')
155
187
transport = transport.clone('quack')
188
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )],
156
190
bzrdir = RemoteBzrDir(transport, _client=client)
157
191
result = bzrdir.open_branch()
158
192
self.assertEqual(
159
[('call', 'BzrDir.open_branch', ('///quack/',)),
160
('call', 'BzrDir.find_repository', ('///quack/',))],
193
[('call', 'BzrDir.open_branch', ('quack/',)),
194
('call', 'BzrDir.find_repository', ('quack/',))],
162
196
self.assertIsInstance(result, RemoteBranch)
163
197
self.assertEqual(bzrdir, result.bzrdir)
165
199
def test_branch_missing(self):
166
client = FakeClient([(('nobranch',), )])
167
200
transport = MemoryTransport()
168
201
transport.mkdir('quack')
169
202
transport = transport.clone('quack')
203
client = FakeClient([(('nobranch',), )], transport.base)
170
204
bzrdir = RemoteBzrDir(transport, _client=client)
171
205
self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
172
206
self.assertEqual(
173
[('call', 'BzrDir.open_branch', ('///quack/',))],
207
[('call', 'BzrDir.open_branch', ('quack/',))],
210
def test__get_tree_branch(self):
211
# _get_tree_branch is a form of open_branch, but it should only ask for
212
# branch opening, not any other network requests.
215
calls.append("Called")
217
transport = MemoryTransport()
218
# no requests on the network - catches other api calls being made.
219
client = FakeClient([], transport.base)
220
bzrdir = RemoteBzrDir(transport, _client=client)
221
# patch the open_branch call to record that it was called.
222
bzrdir.open_branch = open_branch
223
self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
224
self.assertEqual(["Called"], calls)
225
self.assertEqual([], client._calls)
227
def test_url_quoting_of_path(self):
228
# Relpaths on the wire should not be URL-escaped. So "~" should be
229
# transmitted as "~", not "%7E".
230
transport = RemoteTransport('bzr://localhost/~hello/')
231
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )],
233
bzrdir = RemoteBzrDir(transport, _client=client)
234
result = bzrdir.open_branch()
236
[('call', 'BzrDir.open_branch', ('~hello/',)),
237
('call', 'BzrDir.find_repository', ('~hello/',))],
176
240
def check_open_repository(self, rich_root, subtrees):
241
transport = MemoryTransport()
242
transport.mkdir('quack')
243
transport = transport.clone('quack')
178
245
rich_response = 'yes'
182
249
subtree_response = 'yes'
184
251
subtree_response = 'no'
185
client = FakeClient([(('ok', '', rich_response, subtree_response), ),])
186
transport = MemoryTransport()
187
transport.mkdir('quack')
188
transport = transport.clone('quack')
252
client = FakeClient([(('ok', '', rich_response, subtree_response), ),],
189
254
bzrdir = RemoteBzrDir(transport, _client=client)
190
255
result = bzrdir.open_repository()
191
256
self.assertEqual(
192
[('call', 'BzrDir.find_repository', ('///quack/',))],
257
[('call', 'BzrDir.find_repository', ('quack/',))],
194
259
self.assertIsInstance(result, RemoteRepository)
195
260
self.assertEqual(bzrdir, result.bzrdir)
249
314
result = branch.last_revision_info()
251
316
self.assertEqual(
252
[('call', 'Branch.last_revision_info', ('///quack/',))],
317
[('call', 'Branch.last_revision_info', ('quack/',))],
254
319
self.assertEqual((0, NULL_REVISION), result)
256
321
def test_non_empty_branch(self):
257
322
# in a non-empty branch we also decode the response properly
258
323
revid = u'\xc8'.encode('utf8')
259
client = FakeClient([(('ok', '2', revid), )])
260
324
transport = MemoryTransport()
325
client = FakeClient([(('ok', '2', revid), )], transport.base)
261
326
transport.mkdir('kwaak')
262
327
transport = transport.clone('kwaak')
263
328
# we do not want bzrdir to make any remote calls
608
class TestRepositoryGetGraph(TestRemoteRepository):
610
def test_get_graph(self):
611
# get_graph returns a graph with the repository as the
614
transport_path = 'quack'
615
repo, client = self.setup_fake_client_and_repository(
616
responses, transport_path)
617
graph = repo.get_graph()
618
self.assertEqual(graph._parents_provider, repo)
621
class TestRepositoryGetParentMap(TestRemoteRepository):
623
def test_get_parent_map_caching(self):
624
# get_parent_map returns from cache until unlock()
625
# setup a reponse with two revisions
626
r1 = u'\u0e33'.encode('utf8')
627
r2 = u'\u0dab'.encode('utf8')
628
lines = [' '.join([r2, r1]), r1]
629
encoded_body = bz2.compress('\n'.join(lines))
630
responses = [(('ok', ), encoded_body), (('ok', ), encoded_body)]
632
transport_path = 'quack'
633
repo, client = self.setup_fake_client_and_repository(
634
responses, transport_path)
636
graph = repo.get_graph()
637
parents = graph.get_parent_map([r2])
638
self.assertEqual({r2: (r1,)}, parents)
639
# locking and unlocking deeper should not reset
642
parents = graph.get_parent_map([r1])
643
self.assertEqual({r1: (NULL_REVISION,)}, parents)
645
[('call_with_body_bytes_expecting_body',
646
'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
649
# now we call again, and it should use the second response.
651
graph = repo.get_graph()
652
parents = graph.get_parent_map([r1])
653
self.assertEqual({r1: (NULL_REVISION,)}, parents)
655
[('call_with_body_bytes_expecting_body',
656
'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
657
('call_with_body_bytes_expecting_body',
658
'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
541
664
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
543
666
def test_null_revision(self):
783
913
malformed data should be.
785
915
record = ('bytes', [('name1',), ('name2',)])
786
pack_file = self.make_pack_file([record])
787
responses = [(('ok',), pack_file.getvalue()), ]
916
pack_stream = self.make_pack_stream([record])
917
responses = [(('ok',), pack_stream), ]
788
918
transport_path = 'quack'
789
919
repo, client = self.setup_fake_client_and_repository(
790
920
responses, transport_path)
791
stream = repo.get_data_stream(['revid'])
921
search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
922
stream = repo.get_data_stream_for_search(search)
792
923
self.assertRaises(errors.SmartProtocolError, list, stream)
794
925
def test_backwards_compatibility(self):
795
926
"""If the server doesn't recognise this request, fallback to VFS."""
797
928
"Generic bzr smart protocol error: "
798
"bad request 'Repository.stream_knit_data_for_revisions'")
929
"bad request 'Repository.stream_revisions_chunked'")
800
931
(('error', error_msg), '')]
801
932
repo, client = self.setup_fake_client_and_repository(
802
933
responses, 'path')
803
934
self.mock_called = False
804
935
repo._real_repository = MockRealRepository(self)
805
repo.get_data_stream(['revid'])
936
search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
937
repo.get_data_stream_for_search(search)
806
938
self.assertTrue(self.mock_called)
807
939
self.failIf(client.expecting_body,
808
940
"The protocol has been left in an unclean state that will cause "