1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
# Copyright (C) 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests for remote bzrdir/branch/repo/etc
These are proxy objects which act on remote objects by sending messages
through a smart client. The proxies are to be created when attempting to open
the object given a transport that supports smartserver rpc operations.
These tests correspond to tests.test_smart, which exercises the server side.
"""
import bz2
from cStringIO import StringIO
from bzrlib import (
errors,
graph,
pack,
remote,
repository,
tests,
)
from bzrlib.branch import Branch
from bzrlib.bzrdir import BzrDir, BzrDirFormat
from bzrlib.remote import (
RemoteBranch,
RemoteBzrDir,
RemoteBzrDirFormat,
RemoteRepository,
)
from bzrlib.revision import NULL_REVISION
from bzrlib.smart import server, medium
from bzrlib.smart.client import _SmartClient
from bzrlib.symbol_versioning import one_four
from bzrlib.transport import get_transport
from bzrlib.transport.memory import MemoryTransport
from bzrlib.transport.remote import RemoteTransport
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
def setUp(self):
self.transport_server = server.SmartTCPServer_for_testing
super(BasicRemoteObjectTests, self).setUp()
self.transport = self.get_transport()
# make a branch that can be opened over the smart transport
self.local_wt = BzrDir.create_standalone_workingtree('.')
def tearDown(self):
self.transport.disconnect()
tests.TestCaseWithTransport.tearDown(self)
def test_create_remote_bzrdir(self):
b = remote.RemoteBzrDir(self.transport)
self.assertIsInstance(b, BzrDir)
def test_open_remote_branch(self):
# open a standalone branch in the working directory
b = remote.RemoteBzrDir(self.transport)
branch = b.open_branch()
self.assertIsInstance(branch, Branch)
def test_remote_repository(self):
b = BzrDir.open_from_transport(self.transport)
repo = b.open_repository()
revid = u'\xc823123123'.encode('utf8')
self.assertFalse(repo.has_revision(revid))
self.local_wt.commit(message='test commit', rev_id=revid)
self.assertTrue(repo.has_revision(revid))
def test_remote_branch_revision_history(self):
b = BzrDir.open_from_transport(self.transport).open_branch()
self.assertEqual([], b.revision_history())
r1 = self.local_wt.commit('1st commit')
r2 = self.local_wt.commit('1st commit', rev_id=u'\xc8'.encode('utf8'))
self.assertEqual([r1, r2], b.revision_history())
def test_find_correct_format(self):
"""Should open a RemoteBzrDir over a RemoteTransport"""
fmt = BzrDirFormat.find_format(self.transport)
self.assertTrue(RemoteBzrDirFormat
in BzrDirFormat._control_server_formats)
self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
def test_open_detected_smart_format(self):
fmt = BzrDirFormat.find_format(self.transport)
d = fmt.open(self.transport)
self.assertIsInstance(d, BzrDir)
def test_remote_branch_repr(self):
b = BzrDir.open_from_transport(self.transport).open_branch()
self.assertStartsWith(str(b), 'RemoteBranch(')
class FakeProtocol(object):
"""Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
def __init__(self, body, fake_client):
self.body = body
self._body_buffer = None
self._fake_client = fake_client
def read_body_bytes(self, count=-1):
if self._body_buffer is None:
self._body_buffer = StringIO(self.body)
bytes = self._body_buffer.read(count)
if self._body_buffer.tell() == len(self._body_buffer.getvalue()):
self._fake_client.expecting_body = False
return bytes
def cancel_read_body(self):
self._fake_client.expecting_body = False
def read_streamed_body(self):
return self.body
class FakeClient(_SmartClient):
"""Lookalike for _SmartClient allowing testing."""
def __init__(self, responses, fake_medium_base='fake base'):
"""Create a FakeClient.
:param responses: A list of response-tuple, body-data pairs to be sent
back to callers. A special case is if the response-tuple is
'unknown verb', then a UnknownSmartMethod will be raised for that
call, using the second element of the tuple as the verb in the
exception.
"""
self.responses = responses
self._calls = []
self.expecting_body = False
_SmartClient.__init__(self, FakeMedium(self._calls), fake_medium_base)
def _get_next_response(self):
response_tuple = self.responses.pop(0)
if response_tuple[0][0] == 'unknown verb':
raise errors.UnknownSmartMethod(response_tuple[0][1])
return response_tuple
def call(self, method, *args):
self._calls.append(('call', method, args))
return self._get_next_response()[0]
def call_expecting_body(self, method, *args):
self._calls.append(('call_expecting_body', method, args))
result = self._get_next_response()
self.expecting_body = True
return result[0], FakeProtocol(result[1], self)
def call_with_body_bytes_expecting_body(self, method, args, body):
self._calls.append(('call_with_body_bytes_expecting_body', method,
args, body))
result = self._get_next_response()
self.expecting_body = True
return result[0], FakeProtocol(result[1], self)
class FakeMedium(object):
def __init__(self, client_calls):
self._remote_is_at_least_1_2 = True
self._client_calls = client_calls
def disconnect(self):
self._client_calls.append(('disconnect medium',))
class TestVfsHas(tests.TestCase):
def test_unicode_path(self):
client = FakeClient([(('yes',), )], '/')
transport = RemoteTransport('bzr://localhost/', _client=client)
filename = u'/hell\u00d8'.encode('utf8')
result = transport.has(filename)
self.assertEqual(
[('call', 'has', (filename,))],
client._calls)
self.assertTrue(result)
class Test_SmartClient_remote_path_from_transport(tests.TestCase):
"""Tests for the behaviour of _SmartClient.remote_path_from_transport."""
def assertRemotePath(self, expected, client_base, transport_base):
"""Assert that the result of _SmartClient.remote_path_from_transport
is the expected value for a given client_base and transport_base.
"""
dummy_medium = 'dummy medium'
client = _SmartClient(dummy_medium, client_base)
transport = get_transport(transport_base)
result = client.remote_path_from_transport(transport)
self.assertEqual(expected, result)
def test_remote_path_from_transport(self):
"""_SmartClient.remote_path_from_transport calculates a URL for the
given transport relative to the root of the client base URL.
"""
self.assertRemotePath('xyz/', 'bzr://host/path', 'bzr://host/xyz')
self.assertRemotePath(
'path/xyz/', 'bzr://host/path', 'bzr://host/path/xyz')
def test_remote_path_from_transport_http(self):
"""Remote paths for HTTP transports are calculated differently to other
transports. They are just relative to the client base, not the root
directory of the host.
"""
for scheme in ['http:', 'https:', 'bzr+http:', 'bzr+https:']:
self.assertRemotePath(
'../xyz/', scheme + '//host/path', scheme + '//host/xyz')
self.assertRemotePath(
'xyz/', scheme + '//host/path', scheme + '//host/path/xyz')
class TestBzrDirOpenBranch(tests.TestCase):
def test_branch_present(self):
transport = MemoryTransport()
transport.mkdir('quack')
transport = transport.clone('quack')
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=client)
result = bzrdir.open_branch()
self.assertEqual(
[('call', 'BzrDir.open_branch', ('quack/',)),
('call', 'BzrDir.find_repositoryV2', ('quack/',))],
client._calls)
self.assertIsInstance(result, RemoteBranch)
self.assertEqual(bzrdir, result.bzrdir)
def test_branch_missing(self):
transport = MemoryTransport()
transport.mkdir('quack')
transport = transport.clone('quack')
client = FakeClient([(('nobranch',), )], transport.base)
bzrdir = RemoteBzrDir(transport, _client=client)
self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
self.assertEqual(
[('call', 'BzrDir.open_branch', ('quack/',))],
client._calls)
def test__get_tree_branch(self):
# _get_tree_branch is a form of open_branch, but it should only ask for
# branch opening, not any other network requests.
calls = []
def open_branch():
calls.append("Called")
return "a-branch"
transport = MemoryTransport()
# no requests on the network - catches other api calls being made.
client = FakeClient([], transport.base)
bzrdir = RemoteBzrDir(transport, _client=client)
# patch the open_branch call to record that it was called.
bzrdir.open_branch = open_branch
self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
self.assertEqual(["Called"], calls)
self.assertEqual([], client._calls)
def test_url_quoting_of_path(self):
# Relpaths on the wire should not be URL-escaped. So "~" should be
# transmitted as "~", not "%7E".
transport = RemoteTransport('bzr://localhost/~hello/')
client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=client)
result = bzrdir.open_branch()
self.assertEqual(
[('call', 'BzrDir.open_branch', ('~hello/',)),
('call', 'BzrDir.find_repositoryV2', ('~hello/',))],
client._calls)
def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
transport = MemoryTransport()
transport.mkdir('quack')
transport = transport.clone('quack')
if rich_root:
rich_response = 'yes'
else:
rich_response = 'no'
if subtrees:
subtree_response = 'yes'
else:
subtree_response = 'no'
client = FakeClient(
[(('ok', '', rich_response, subtree_response, external_lookup), ),],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=client)
result = bzrdir.open_repository()
self.assertEqual(
[('call', 'BzrDir.find_repositoryV2', ('quack/',))],
client._calls)
self.assertIsInstance(result, RemoteRepository)
self.assertEqual(bzrdir, result.bzrdir)
self.assertEqual(rich_root, result._format.rich_root_data)
self.assertEqual(subtrees, result._format.supports_tree_reference)
def test_open_repository_sets_format_attributes(self):
self.check_open_repository(True, True)
self.check_open_repository(False, True)
self.check_open_repository(True, False)
self.check_open_repository(False, False)
self.check_open_repository(False, False, 'yes')
def test_old_server(self):
"""RemoteBzrDirFormat should fail to probe if the server version is too
old.
"""
self.assertRaises(errors.NotBranchError,
RemoteBzrDirFormat.probe_transport, OldServerTransport())
class TestBzrDirOpenRepository(tests.TestCase):
def test_backwards_compat_1_2(self):
transport = MemoryTransport()
transport.mkdir('quack')
transport = transport.clone('quack')
client = FakeClient([
(('unknown verb', 'RemoteRepository.find_repositoryV2'), ''),
(('ok', '', 'no', 'no'), ''),],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=client)
repo = bzrdir.open_repository()
self.assertEqual(
[('call', 'BzrDir.find_repositoryV2', ('quack/',)),
('call', 'BzrDir.find_repository', ('quack/',))],
client._calls)
class OldSmartClient(object):
"""A fake smart client for test_old_version that just returns a version one
response to the 'hello' (query version) command.
"""
def get_request(self):
input_file = StringIO('ok\x011\n')
output_file = StringIO()
client_medium = medium.SmartSimplePipesClientMedium(
input_file, output_file)
return medium.SmartClientStreamMediumRequest(client_medium)
def protocol_version(self):
return 1
class OldServerTransport(object):
"""A fake transport for test_old_server that reports it's smart server
protocol version as version one.
"""
def __init__(self):
self.base = 'fake:'
def get_smart_client(self):
return OldSmartClient()
class TestBranchLastRevisionInfo(tests.TestCase):
def test_empty_branch(self):
# in an empty branch we decode the response properly
transport = MemoryTransport()
client = FakeClient([(('ok', '0', 'null:'), )], transport.base)
transport.mkdir('quack')
transport = transport.clone('quack')
# we do not want bzrdir to make any remote calls
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
result = branch.last_revision_info()
self.assertEqual(
[('call', 'Branch.last_revision_info', ('quack/',))],
client._calls)
self.assertEqual((0, NULL_REVISION), result)
def test_non_empty_branch(self):
# in a non-empty branch we also decode the response properly
revid = u'\xc8'.encode('utf8')
transport = MemoryTransport()
client = FakeClient([(('ok', '2', revid), )], transport.base)
transport.mkdir('kwaak')
transport = transport.clone('kwaak')
# we do not want bzrdir to make any remote calls
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
result = branch.last_revision_info()
self.assertEqual(
[('call', 'Branch.last_revision_info', ('kwaak/',))],
client._calls)
self.assertEqual((2, revid), result)
class TestBranchSetLastRevision(tests.TestCase):
def test_set_empty(self):
# set_revision_history([]) is translated to calling
# Branch.set_last_revision(path, '') on the wire.
transport = MemoryTransport()
transport.mkdir('branch')
transport = transport.clone('branch')
client = FakeClient([
# lock_write
(('ok', 'branch token', 'repo token'), ),
# set_last_revision
(('ok',), ),
# unlock
(('ok',), )],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
# This is a hack to work around the problem that RemoteBranch currently
# unnecessarily invokes _ensure_real upon a call to lock_write.
branch._ensure_real = lambda: None
branch.lock_write()
client._calls = []
result = branch.set_revision_history([])
self.assertEqual(
[('call', 'Branch.set_last_revision',
('branch/', 'branch token', 'repo token', 'null:'))],
client._calls)
branch.unlock()
self.assertEqual(None, result)
def test_set_nonempty(self):
# set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
# Branch.set_last_revision(path, rev-idN) on the wire.
transport = MemoryTransport()
transport.mkdir('branch')
transport = transport.clone('branch')
client = FakeClient([
# lock_write
(('ok', 'branch token', 'repo token'), ),
# set_last_revision
(('ok',), ),
# unlock
(('ok',), )],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
# This is a hack to work around the problem that RemoteBranch currently
# unnecessarily invokes _ensure_real upon a call to lock_write.
branch._ensure_real = lambda: None
# Lock the branch, reset the record of remote calls.
branch.lock_write()
client._calls = []
result = branch.set_revision_history(['rev-id1', 'rev-id2'])
self.assertEqual(
[('call', 'Branch.set_last_revision',
('branch/', 'branch token', 'repo token', 'rev-id2'))],
client._calls)
branch.unlock()
self.assertEqual(None, result)
def test_no_such_revision(self):
# A response of 'NoSuchRevision' is translated into an exception.
client = FakeClient([
# lock_write
(('ok', 'branch token', 'repo token'), ),
# set_last_revision
(('NoSuchRevision', 'rev-id'), ),
# unlock
(('ok',), )])
transport = MemoryTransport()
transport.mkdir('branch')
transport = transport.clone('branch')
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
branch._ensure_real = lambda: None
branch.lock_write()
client._calls = []
self.assertRaises(
errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
branch.unlock()
class TestBranchSetLastRevisionInfo(tests.TestCase):
def test_set_last_revision_info(self):
# set_last_revision_info(num, 'rev-id') is translated to calling
# Branch.set_last_revision_info(num, 'rev-id') on the wire.
transport = MemoryTransport()
transport.mkdir('branch')
transport = transport.clone('branch')
client = FakeClient([
# lock_write
(('ok', 'branch token', 'repo token'), ),
# set_last_revision_info
(('ok',), ),
# unlock
(('ok',), )], transport.base)
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
# This is a hack to work around the problem that RemoteBranch currently
# unnecessarily invokes _ensure_real upon a call to lock_write.
branch._ensure_real = lambda: None
# Lock the branch, reset the record of remote calls.
branch.lock_write()
client._calls = []
result = branch.set_last_revision_info(1234, 'a-revision-id')
self.assertEqual(
[('call', 'Branch.set_last_revision_info',
('branch/', 'branch token', 'repo token',
'1234', 'a-revision-id'))],
client._calls)
self.assertEqual(None, result)
def test_no_such_revision(self):
# A response of 'NoSuchRevision' is translated into an exception.
client = FakeClient([
# lock_write
(('ok', 'branch token', 'repo token'), ),
# set_last_revision_info
(('NoSuchRevision', 'revid'), ),
# unlock
(('ok',), ),
])
transport = MemoryTransport()
transport.mkdir('branch')
transport = transport.clone('branch')
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
# This is a hack to work around the problem that RemoteBranch currently
# unnecessarily invokes _ensure_real upon a call to lock_write.
branch._ensure_real = lambda: None
# Lock the branch, reset the record of remote calls.
branch.lock_write()
client._calls = []
self.assertRaises(
errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
branch.unlock()
def lock_remote_branch(self, branch):
"""Trick a RemoteBranch into thinking it is locked."""
branch._lock_mode = 'w'
branch._lock_count = 2
branch._lock_token = 'branch token'
branch._repo_lock_token = 'repo token'
def test_backwards_compatibility(self):
"""If the server does not support the Branch.set_last_revision_info
verb (which is new in 1.4), then the client falls back to VFS methods.
"""
# This test is a little messy. Unlike most tests in this file, it
# doesn't purely test what a Remote* object sends over the wire, and
# how it reacts to responses from the wire. It instead relies partly
# on asserting that the RemoteBranch will call
# self._real_branch.set_last_revision_info(...).
# First, set up our RemoteBranch with a FakeClient that raises
# UnknownSmartMethod, and a StubRealBranch that logs how it is called.
transport = MemoryTransport()
transport.mkdir('branch')
transport = transport.clone('branch')
client = FakeClient(
[(('unknown verb', 'Branch.set_last_revision_info',), ),],
transport.base)
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
class StubRealBranch(object):
def __init__(self):
self.calls = []
def set_last_revision_info(self, revno, revision_id):
self.calls.append(
('set_last_revision_info', revno, revision_id))
real_branch = StubRealBranch()
branch._real_branch = real_branch
self.lock_remote_branch(branch)
# Call set_last_revision_info, and verify it behaved as expected.
result = branch.set_last_revision_info(1234, 'a-revision-id')
self.assertEqual(
[('call', 'Branch.set_last_revision_info',
('branch/', 'branch token', 'repo token',
'1234', 'a-revision-id')),],
client._calls)
self.assertEqual(
[('set_last_revision_info', 1234, 'a-revision-id')],
real_branch.calls)
class TestBranchControlGetBranchConf(tests.TestCaseWithMemoryTransport):
"""Test branch.control_files api munging...
We special case RemoteBranch.control_files.get('branch.conf') to
call a specific API so that RemoteBranch's can intercept configuration
file reading, allowing them to signal to the client about things like
'email is configured for commits'.
"""
def test_get_branch_conf(self):
# in an empty branch we decode the response properly
client = FakeClient([(('ok', ), 'config file body')], self.get_url())
# we need to make a real branch because the remote_branch.control_files
# will trigger _ensure_real.
branch = self.make_branch('quack')
transport = branch.bzrdir.root_transport
# we do not want bzrdir to make any remote calls
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
result = branch.control_files.get('branch.conf')
self.assertEqual(
[('call_expecting_body', 'Branch.get_config_file', ('quack/',))],
client._calls)
self.assertEqual('config file body', result.read())
class TestBranchLockWrite(tests.TestCase):
def test_lock_write_unlockable(self):
transport = MemoryTransport()
client = FakeClient([(('UnlockableTransport', ), '')], transport.base)
transport.mkdir('quack')
transport = transport.clone('quack')
# we do not want bzrdir to make any remote calls
bzrdir = RemoteBzrDir(transport, _client=False)
branch = RemoteBranch(bzrdir, None, _client=client)
self.assertRaises(errors.UnlockableTransport, branch.lock_write)
self.assertEqual(
[('call', 'Branch.lock_write', ('quack/', '', ''))],
client._calls)
class TestTransportIsReadonly(tests.TestCase):
def test_true(self):
client = FakeClient([(('yes',), '')])
transport = RemoteTransport('bzr://example.com/', medium=False,
_client=client)
self.assertEqual(True, transport.is_readonly())
self.assertEqual(
[('call', 'Transport.is_readonly', ())],
client._calls)
def test_false(self):
client = FakeClient([(('no',), '')])
transport = RemoteTransport('bzr://example.com/', medium=False,
_client=client)
self.assertEqual(False, transport.is_readonly())
self.assertEqual(
[('call', 'Transport.is_readonly', ())],
client._calls)
def test_error_from_old_server(self):
"""bzr 0.15 and earlier servers don't recognise the is_readonly verb.
Clients should treat it as a "no" response, because is_readonly is only
advisory anyway (a transport could be read-write, but then the
underlying filesystem could be readonly anyway).
"""
client = FakeClient([(('unknown verb', 'Transport.is_readonly'), '')])
transport = RemoteTransport('bzr://example.com/', medium=False,
_client=client)
self.assertEqual(False, transport.is_readonly())
self.assertEqual(
[('call', 'Transport.is_readonly', ())],
client._calls)
class TestRemoteRepository(tests.TestCase):
"""Base for testing RemoteRepository protocol usage.
These tests contain frozen requests and responses. We want any changes to
what is sent or expected to be require a thoughtful update to these tests
because they might break compatibility with different-versioned servers.
"""
def setup_fake_client_and_repository(self, responses, transport_path):
"""Create the fake client and repository for testing with.
There's no real server here; we just have canned responses sent
back one by one.
:param transport_path: Path below the root of the MemoryTransport
where the repository will be created.
"""
transport = MemoryTransport()
transport.mkdir(transport_path)
client = FakeClient(responses, transport.base)
transport = transport.clone(transport_path)
# we do not want bzrdir to make any remote calls
bzrdir = RemoteBzrDir(transport, _client=False)
repo = RemoteRepository(bzrdir, None, _client=client)
return repo, client
class TestRepositoryGatherStats(TestRemoteRepository):
def test_revid_none(self):
# ('ok',), body with revisions and size
responses = [(('ok', ), 'revisions: 2\nsize: 18\n')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = repo.gather_stats(None)
self.assertEqual(
[('call_expecting_body', 'Repository.gather_stats',
('quack/','','no'))],
client._calls)
self.assertEqual({'revisions': 2, 'size': 18}, result)
def test_revid_no_committers(self):
# ('ok',), body without committers
responses = [(('ok', ),
'firstrev: 123456.300 3600\n'
'latestrev: 654231.400 0\n'
'revisions: 2\n'
'size: 18\n')]
transport_path = 'quick'
revid = u'\xc8'.encode('utf8')
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = repo.gather_stats(revid)
self.assertEqual(
[('call_expecting_body', 'Repository.gather_stats',
('quick/', revid, 'no'))],
client._calls)
self.assertEqual({'revisions': 2, 'size': 18,
'firstrev': (123456.300, 3600),
'latestrev': (654231.400, 0),},
result)
def test_revid_with_committers(self):
# ('ok',), body with committers
responses = [(('ok', ),
'committers: 128\n'
'firstrev: 123456.300 3600\n'
'latestrev: 654231.400 0\n'
'revisions: 2\n'
'size: 18\n')]
transport_path = 'buick'
revid = u'\xc8'.encode('utf8')
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = repo.gather_stats(revid, True)
self.assertEqual(
[('call_expecting_body', 'Repository.gather_stats',
('buick/', revid, 'yes'))],
client._calls)
self.assertEqual({'revisions': 2, 'size': 18,
'committers': 128,
'firstrev': (123456.300, 3600),
'latestrev': (654231.400, 0),},
result)
class TestRepositoryGetGraph(TestRemoteRepository):
def test_get_graph(self):
# get_graph returns a graph with the repository as the
# parents_provider.
responses = []
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
graph = repo.get_graph()
self.assertEqual(graph._parents_provider, repo)
class TestRepositoryGetParentMap(TestRemoteRepository):
def test_get_parent_map_caching(self):
# get_parent_map returns from cache until unlock()
# setup a reponse with two revisions
r1 = u'\u0e33'.encode('utf8')
r2 = u'\u0dab'.encode('utf8')
lines = [' '.join([r2, r1]), r1]
encoded_body = bz2.compress('\n'.join(lines))
responses = [(('ok', ), encoded_body), (('ok', ), encoded_body)]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
repo.lock_read()
graph = repo.get_graph()
parents = graph.get_parent_map([r2])
self.assertEqual({r2: (r1,)}, parents)
# locking and unlocking deeper should not reset
repo.lock_read()
repo.unlock()
parents = graph.get_parent_map([r1])
self.assertEqual({r1: (NULL_REVISION,)}, parents)
self.assertEqual(
[('call_with_body_bytes_expecting_body',
'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
client._calls)
repo.unlock()
# now we call again, and it should use the second response.
repo.lock_read()
graph = repo.get_graph()
parents = graph.get_parent_map([r1])
self.assertEqual({r1: (NULL_REVISION,)}, parents)
self.assertEqual(
[('call_with_body_bytes_expecting_body',
'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
('call_with_body_bytes_expecting_body',
'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
],
client._calls)
repo.unlock()
def test_get_parent_map_reconnects_if_unknown_method(self):
responses = [
(('unknown verb', 'Repository.get_parent_map'), ''),
(('ok',), '')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
rev_id = 'revision-id'
expected_deprecations = [
'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
'in version 1.4.']
parents = self.callDeprecated(
expected_deprecations, repo.get_parent_map, [rev_id])
self.assertEqual(
[('call_with_body_bytes_expecting_body',
'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
('disconnect medium',),
('call_expecting_body', 'Repository.get_revision_graph',
('quack/', ''))],
client._calls)
def test_get_parent_map_unexpected_response(self):
responses = [
(('something unexpected!',), '')]
repo, client = self.setup_fake_client_and_repository(responses, 'path')
self.assertRaises(
errors.UnexpectedSmartServerResponse,
repo.get_parent_map, ['a-revision-id'])
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
def test_null_revision(self):
# a null revision has the predictable result {}, we should have no wire
# traffic when calling it with this argument
responses = [(('notused', ), '')]
transport_path = 'empty'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = self.applyDeprecated(one_four, repo.get_revision_graph,
NULL_REVISION)
self.assertEqual([], client._calls)
self.assertEqual({}, result)
def test_none_revision(self):
# with none we want the entire graph
r1 = u'\u0e33'.encode('utf8')
r2 = u'\u0dab'.encode('utf8')
lines = [' '.join([r2, r1]), r1]
encoded_body = '\n'.join(lines)
responses = [(('ok', ), encoded_body)]
transport_path = 'sinhala'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = self.applyDeprecated(one_four, repo.get_revision_graph)
self.assertEqual(
[('call_expecting_body', 'Repository.get_revision_graph',
('sinhala/', ''))],
client._calls)
self.assertEqual({r1: (), r2: (r1, )}, result)
def test_specific_revision(self):
# with a specific revision we want the graph for that
# with none we want the entire graph
r11 = u'\u0e33'.encode('utf8')
r12 = u'\xc9'.encode('utf8')
r2 = u'\u0dab'.encode('utf8')
lines = [' '.join([r2, r11, r12]), r11, r12]
encoded_body = '\n'.join(lines)
responses = [(('ok', ), encoded_body)]
transport_path = 'sinhala'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = self.applyDeprecated(one_four, repo.get_revision_graph, r2)
self.assertEqual(
[('call_expecting_body', 'Repository.get_revision_graph',
('sinhala/', r2))],
client._calls)
self.assertEqual({r11: (), r12: (), r2: (r11, r12), }, result)
def test_no_such_revision(self):
revid = '123'
responses = [(('nosuchrevision', revid), '')]
transport_path = 'sinhala'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
# also check that the right revision is reported in the error
self.assertRaises(errors.NoSuchRevision,
self.applyDeprecated, one_four, repo.get_revision_graph, revid)
self.assertEqual(
[('call_expecting_body', 'Repository.get_revision_graph',
('sinhala/', revid))],
client._calls)
class TestRepositoryIsShared(TestRemoteRepository):
def test_is_shared(self):
# ('yes', ) for Repository.is_shared -> 'True'.
responses = [(('yes', ), )]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = repo.is_shared()
self.assertEqual(
[('call', 'Repository.is_shared', ('quack/',))],
client._calls)
self.assertEqual(True, result)
def test_is_not_shared(self):
# ('no', ) for Repository.is_shared -> 'False'.
responses = [(('no', ), )]
transport_path = 'qwack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = repo.is_shared()
self.assertEqual(
[('call', 'Repository.is_shared', ('qwack/',))],
client._calls)
self.assertEqual(False, result)
class TestRepositoryLockWrite(TestRemoteRepository):
def test_lock_write(self):
responses = [(('ok', 'a token'), '')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
result = repo.lock_write()
self.assertEqual(
[('call', 'Repository.lock_write', ('quack/', ''))],
client._calls)
self.assertEqual('a token', result)
def test_lock_write_already_locked(self):
responses = [(('LockContention', ), '')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
self.assertRaises(errors.LockContention, repo.lock_write)
self.assertEqual(
[('call', 'Repository.lock_write', ('quack/', ''))],
client._calls)
def test_lock_write_unlockable(self):
responses = [(('UnlockableTransport', ), '')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
self.assertRaises(errors.UnlockableTransport, repo.lock_write)
self.assertEqual(
[('call', 'Repository.lock_write', ('quack/', ''))],
client._calls)
class TestRepositoryUnlock(TestRemoteRepository):
def test_unlock(self):
responses = [(('ok', 'a token'), ''),
(('ok',), '')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
repo.lock_write()
repo.unlock()
self.assertEqual(
[('call', 'Repository.lock_write', ('quack/', '')),
('call', 'Repository.unlock', ('quack/', 'a token'))],
client._calls)
def test_unlock_wrong_token(self):
# If somehow the token is wrong, unlock will raise TokenMismatch.
responses = [(('ok', 'a token'), ''),
(('TokenMismatch',), '')]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
repo.lock_write()
self.assertRaises(errors.TokenMismatch, repo.unlock)
class TestRepositoryHasRevision(TestRemoteRepository):
def test_none(self):
# repo.has_revision(None) should not cause any traffic.
transport_path = 'quack'
responses = None
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
# The null revision is always there, so has_revision(None) == True.
self.assertEqual(True, repo.has_revision(NULL_REVISION))
# The remote repo shouldn't be accessed.
self.assertEqual([], client._calls)
class TestRepositoryTarball(TestRemoteRepository):
# This is a canned tarball reponse we can validate against
tarball_content = (
'QlpoOTFBWSZTWdGkj3wAAWF/k8aQACBIB//A9+8cIX/v33AACEAYABAECEACNz'
'JqsgJJFPTSnk1A3qh6mTQAAAANPUHkagkSTEkaA09QaNAAAGgAAAcwCYCZGAEY'
'mJhMJghpiaYBUkKammSHqNMZQ0NABkNAeo0AGneAevnlwQoGzEzNVzaYxp/1Uk'
'xXzA1CQX0BJMZZLcPBrluJir5SQyijWHYZ6ZUtVqqlYDdB2QoCwa9GyWwGYDMA'
'OQYhkpLt/OKFnnlT8E0PmO8+ZNSo2WWqeCzGB5fBXZ3IvV7uNJVE7DYnWj6qwB'
'k5DJDIrQ5OQHHIjkS9KqwG3mc3t+F1+iujb89ufyBNIKCgeZBWrl5cXxbMGoMs'
'c9JuUkg5YsiVcaZJurc6KLi6yKOkgCUOlIlOpOoXyrTJjK8ZgbklReDdwGmFgt'
'dkVsAIslSVCd4AtACSLbyhLHryfb14PKegrVDba+U8OL6KQtzdM5HLjAc8/p6n'
'0lgaWU8skgO7xupPTkyuwheSckejFLK5T4ZOo0Gda9viaIhpD1Qn7JqqlKAJqC'
'QplPKp2nqBWAfwBGaOwVrz3y1T+UZZNismXHsb2Jq18T+VaD9k4P8DqE3g70qV'
'JLurpnDI6VS5oqDDPVbtVjMxMxMg4rzQVipn2Bv1fVNK0iq3Gl0hhnnHKm/egy'
'nWQ7QH/F3JFOFCQ0aSPfA='
).decode('base64')
def test_repository_tarball(self):
# Test that Repository.tarball generates the right operations
transport_path = 'repo'
expected_responses = [(('ok',), self.tarball_content),
]
expected_calls = [('call_expecting_body', 'Repository.tarball',
('repo/', 'bz2',),),
]
remote_repo, client = self.setup_fake_client_and_repository(
expected_responses, transport_path)
# Now actually ask for the tarball
tarball_file = remote_repo._get_tarball('bz2')
try:
self.assertEqual(expected_calls, client._calls)
self.assertEqual(self.tarball_content, tarball_file.read())
finally:
tarball_file.close()
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
"""RemoteRepository.copy_content_into optimizations"""
def test_copy_content_remote_to_local(self):
self.transport_server = server.SmartTCPServer_for_testing
src_repo = self.make_repository('repo1')
src_repo = repository.Repository.open(self.get_url('repo1'))
# At the moment the tarball-based copy_content_into can't write back
# into a smart server. It would be good if it could upload the
# tarball; once that works we'd have to create repositories of
# different formats. -- mbp 20070410
dest_url = self.get_vfs_only_url('repo2')
dest_bzrdir = BzrDir.create(dest_url)
dest_repo = dest_bzrdir.create_repository()
self.assertFalse(isinstance(dest_repo, RemoteRepository))
self.assertTrue(isinstance(src_repo, RemoteRepository))
src_repo.copy_content_into(dest_repo)
class TestRepositoryStreamKnitData(TestRemoteRepository):
def make_pack_file(self, records):
pack_file = StringIO()
pack_writer = pack.ContainerWriter(pack_file.write)
pack_writer.begin()
for bytes, names in records:
pack_writer.add_bytes_record(bytes, names)
pack_writer.end()
pack_file.seek(0)
return pack_file
def make_pack_stream(self, records):
pack_serialiser = pack.ContainerSerialiser()
yield pack_serialiser.begin()
for bytes, names in records:
yield pack_serialiser.bytes_record(bytes, names)
yield pack_serialiser.end()
def test_bad_pack_from_server(self):
"""A response with invalid data (e.g. it has a record with multiple
names) triggers an exception.
Not all possible errors will be caught at this stage, but obviously
malformed data should be.
"""
record = ('bytes', [('name1',), ('name2',)])
pack_stream = self.make_pack_stream([record])
responses = [(('ok',), pack_stream), ]
transport_path = 'quack'
repo, client = self.setup_fake_client_and_repository(
responses, transport_path)
search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
stream = repo.get_data_stream_for_search(search)
self.assertRaises(errors.SmartProtocolError, list, stream)
def test_backwards_compatibility(self):
"""If the server doesn't recognise this request, fallback to VFS."""
responses = [
(('unknown verb', 'Repository.stream_revisions_chunked'), '')]
repo, client = self.setup_fake_client_and_repository(
responses, 'path')
self.mock_called = False
repo._real_repository = MockRealRepository(self)
search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
repo.get_data_stream_for_search(search)
self.assertTrue(self.mock_called)
self.failIf(client.expecting_body,
"The protocol has been left in an unclean state that will cause "
"TooManyConcurrentRequests errors.")
class MockRealRepository(object):
"""Helper class for TestRepositoryStreamKnitData.test_unknown_method."""
def __init__(self, test):
self.test = test
def get_data_stream_for_search(self, search):
self.test.assertEqual(set(['revid']), search.get_keys())
self.test.mock_called = True
|