388
556
def test_with_content(self):
389
557
# SmartServerBranchGetConfigFile should return the content from
390
558
# branch.control_files.get('branch.conf') for now - in the future it may
391
# perform more complex processing.
559
# perform more complex processing.
392
560
backing = self.get_transport()
393
561
request = smart.branch.SmartServerBranchGetConfigFile(backing)
394
562
branch = self.make_branch('.')
395
branch.control_files.put_utf8('branch.conf', 'foo bar baz')
563
branch._transport.put_bytes('branch.conf', 'foo bar baz')
396
564
self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
397
565
request.execute(''))
400
class TestSmartServerBranchRequestSetLastRevision(tests.TestCaseWithMemoryTransport):
402
def test_empty(self):
403
backing = self.get_transport()
404
request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
405
b = self.make_branch('.')
406
branch_token = b.lock_write()
407
repo_token = b.repository.lock_write()
408
b.repository.unlock()
410
self.assertEqual(SmartServerResponse(('ok',)),
412
'', branch_token, repo_token,
417
def test_not_present_revision_id(self):
418
backing = self.get_transport()
419
request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
420
b = self.make_branch('.')
421
branch_token = b.lock_write()
422
repo_token = b.repository.lock_write()
423
b.repository.unlock()
425
revision_id = 'non-existent revision'
427
SmartServerResponse(('NoSuchRevision', revision_id)),
429
'', branch_token, repo_token,
434
def test_revision_id_present(self):
435
backing = self.get_transport()
436
request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
437
tree = self.make_branch_and_memory_tree('.')
440
rev_id_utf8 = u'\xc8'.encode('utf-8')
441
r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
442
r2 = tree.commit('2nd commit')
444
branch_token = tree.branch.lock_write()
445
repo_token = tree.branch.repository.lock_write()
446
tree.branch.repository.unlock()
449
SmartServerResponse(('ok',)),
451
'', branch_token, repo_token,
453
self.assertEqual([rev_id_utf8], tree.branch.revision_history())
457
def test_revision_id_present2(self):
458
backing = self.get_transport()
459
request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
460
tree = self.make_branch_and_memory_tree('.')
463
rev_id_utf8 = u'\xc8'.encode('utf-8')
464
r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
465
r2 = tree.commit('2nd commit')
467
tree.branch.set_revision_history([])
468
branch_token = tree.branch.lock_write()
469
repo_token = tree.branch.repository.lock_write()
470
tree.branch.repository.unlock()
473
SmartServerResponse(('ok',)),
475
'', branch_token, repo_token,
477
self.assertEqual([rev_id_utf8], tree.branch.revision_history())
482
class TestSmartServerBranchRequestSetLastRevisionInfo(tests.TestCaseWithTransport):
484
def lock_branch(self, branch):
568
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
570
def get_lock_tokens(self, branch):
485
571
branch_token = branch.lock_write()
486
572
repo_token = branch.repository.lock_write()
487
573
branch.repository.unlock()
488
self.addCleanup(branch.unlock)
489
574
return branch_token, repo_token
491
def make_locked_branch(self, format=None):
492
branch = self.make_branch('.', format=format)
493
branch_token, repo_token = self.lock_branch(branch)
494
return branch, branch_token, repo_token
496
def test_empty(self):
577
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
579
def test_value_name(self):
580
branch = self.make_branch('.')
581
request = smart.branch.SmartServerBranchRequestSetConfigOption(
582
branch.bzrdir.root_transport)
583
branch_token, repo_token = self.get_lock_tokens(branch)
584
config = branch._get_config()
585
result = request.execute('', branch_token, repo_token, 'bar', 'foo',
587
self.assertEqual(SuccessfulSmartServerResponse(()), result)
588
self.assertEqual('bar', config.get_option('foo'))
592
def test_value_name_section(self):
593
branch = self.make_branch('.')
594
request = smart.branch.SmartServerBranchRequestSetConfigOption(
595
branch.bzrdir.root_transport)
596
branch_token, repo_token = self.get_lock_tokens(branch)
597
config = branch._get_config()
598
result = request.execute('', branch_token, repo_token, 'bar', 'foo',
600
self.assertEqual(SuccessfulSmartServerResponse(()), result)
601
self.assertEqual('bar', config.get_option('foo', 'gam'))
606
class SetLastRevisionTestBase(TestLockedBranch):
607
"""Base test case for verbs that implement set_last_revision."""
610
tests.TestCaseWithMemoryTransport.setUp(self)
611
backing_transport = self.get_transport()
612
self.request = self.request_class(backing_transport)
613
self.tree = self.make_branch_and_memory_tree('.')
615
def lock_branch(self):
616
return self.get_lock_tokens(self.tree.branch)
618
def unlock_branch(self):
619
self.tree.branch.unlock()
621
def set_last_revision(self, revision_id, revno):
622
branch_token, repo_token = self.lock_branch()
623
response = self._set_last_revision(
624
revision_id, revno, branch_token, repo_token)
628
def assertRequestSucceeds(self, revision_id, revno):
629
response = self.set_last_revision(revision_id, revno)
630
self.assertEqual(SuccessfulSmartServerResponse(('ok',)), response)
633
class TestSetLastRevisionVerbMixin(object):
634
"""Mixin test case for verbs that implement set_last_revision."""
636
def test_set_null_to_null(self):
497
637
"""An empty branch can have its last revision set to 'null:'."""
498
b, branch_token, repo_token = self.make_locked_branch()
499
backing = self.get_transport()
500
request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
502
response = request.execute('', branch_token, repo_token, '0', 'null:')
503
self.assertEqual(SmartServerResponse(('ok',)), response)
505
def assertBranchLastRevisionInfo(self, expected_info, branch_relpath):
506
branch = bzrdir.BzrDir.open(branch_relpath).open_branch()
507
self.assertEqual(expected_info, branch.last_revision_info())
509
def test_branch_revision_info_is_updated(self):
510
"""This method really does update the branch last revision info."""
511
tree = self.make_branch_and_memory_tree('.')
514
tree.commit('First commit', rev_id='revision-1')
515
tree.commit('Second commit', rev_id='revision-2')
519
branch_token, repo_token = self.lock_branch(branch)
520
backing = self.get_transport()
521
request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
523
self.assertBranchLastRevisionInfo((2, 'revision-2'), '.')
524
response = request.execute(
525
'', branch_token, repo_token, '1', 'revision-1')
526
self.assertEqual(SmartServerResponse(('ok',)), response)
527
self.assertBranchLastRevisionInfo((1, 'revision-1'), '.')
529
def test_not_present_revid(self):
530
"""Some branch formats will check that the revision is present in the
531
repository. When that check fails, a NoSuchRevision error is returned
534
# Make a knit format branch, because that format checks the values
535
# given to set_last_revision_info.
536
b, branch_token, repo_token = self.make_locked_branch(format='knit')
537
backing = self.get_transport()
538
request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
540
response = request.execute(
541
'', branch_token, repo_token, '1', 'not-present')
543
SmartServerResponse(('NoSuchRevision', 'not-present')), response)
638
self.assertRequestSucceeds('null:', 0)
640
def test_NoSuchRevision(self):
641
"""If the revision_id is not present, the verb returns NoSuchRevision.
643
revision_id = 'non-existent revision'
645
FailedSmartServerResponse(('NoSuchRevision', revision_id)),
646
self.set_last_revision(revision_id, 1))
648
def make_tree_with_two_commits(self):
649
self.tree.lock_write()
651
rev_id_utf8 = u'\xc8'.encode('utf-8')
652
r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
653
r2 = self.tree.commit('2nd commit', rev_id='rev-2')
656
def test_branch_last_revision_info_is_updated(self):
657
"""A branch's tip can be set to a revision that is present in its
660
# Make a branch with an empty revision history, but two revisions in
662
self.make_tree_with_two_commits()
663
rev_id_utf8 = u'\xc8'.encode('utf-8')
664
self.tree.branch.set_revision_history([])
666
(0, 'null:'), self.tree.branch.last_revision_info())
667
# We can update the branch to a revision that is present in the
669
self.assertRequestSucceeds(rev_id_utf8, 1)
671
(1, rev_id_utf8), self.tree.branch.last_revision_info())
673
def test_branch_last_revision_info_rewind(self):
674
"""A branch's tip can be set to a revision that is an ancestor of the
677
self.make_tree_with_two_commits()
678
rev_id_utf8 = u'\xc8'.encode('utf-8')
680
(2, 'rev-2'), self.tree.branch.last_revision_info())
681
self.assertRequestSucceeds(rev_id_utf8, 1)
683
(1, rev_id_utf8), self.tree.branch.last_revision_info())
685
def test_TipChangeRejected(self):
686
"""If a pre_change_branch_tip hook raises TipChangeRejected, the verb
687
returns TipChangeRejected.
689
rejection_message = u'rejection message\N{INTERROBANG}'
690
def hook_that_rejects(params):
691
raise errors.TipChangeRejected(rejection_message)
692
Branch.hooks.install_named_hook(
693
'pre_change_branch_tip', hook_that_rejects, None)
695
FailedSmartServerResponse(
696
('TipChangeRejected', rejection_message.encode('utf-8'))),
697
self.set_last_revision('null:', 0))
700
class TestSmartServerBranchRequestSetLastRevision(
701
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
702
"""Tests for Branch.set_last_revision verb."""
704
request_class = smart.branch.SmartServerBranchRequestSetLastRevision
706
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
707
return self.request.execute(
708
'', branch_token, repo_token, revision_id)
711
class TestSmartServerBranchRequestSetLastRevisionInfo(
712
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
713
"""Tests for Branch.set_last_revision_info verb."""
715
request_class = smart.branch.SmartServerBranchRequestSetLastRevisionInfo
717
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
718
return self.request.execute(
719
'', branch_token, repo_token, revno, revision_id)
721
def test_NoSuchRevision(self):
722
"""Branch.set_last_revision_info does not have to return
723
NoSuchRevision if the revision_id is absent.
725
raise tests.TestNotApplicable()
728
class TestSmartServerBranchRequestSetLastRevisionEx(
729
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
730
"""Tests for Branch.set_last_revision_ex verb."""
732
request_class = smart.branch.SmartServerBranchRequestSetLastRevisionEx
734
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
735
return self.request.execute(
736
'', branch_token, repo_token, revision_id, 0, 0)
738
def assertRequestSucceeds(self, revision_id, revno):
739
response = self.set_last_revision(revision_id, revno)
741
SuccessfulSmartServerResponse(('ok', revno, revision_id)),
744
def test_branch_last_revision_info_rewind(self):
745
"""A branch's tip can be set to a revision that is an ancestor of the
746
current tip, but only if allow_overwrite_descendant is passed.
748
self.make_tree_with_two_commits()
749
rev_id_utf8 = u'\xc8'.encode('utf-8')
751
(2, 'rev-2'), self.tree.branch.last_revision_info())
752
# If allow_overwrite_descendant flag is 0, then trying to set the tip
753
# to an older revision ID has no effect.
754
branch_token, repo_token = self.lock_branch()
755
response = self.request.execute(
756
'', branch_token, repo_token, rev_id_utf8, 0, 0)
758
SuccessfulSmartServerResponse(('ok', 2, 'rev-2')),
761
(2, 'rev-2'), self.tree.branch.last_revision_info())
763
# If allow_overwrite_descendant flag is 1, then setting the tip to an
765
response = self.request.execute(
766
'', branch_token, repo_token, rev_id_utf8, 0, 1)
768
SuccessfulSmartServerResponse(('ok', 1, rev_id_utf8)),
772
(1, rev_id_utf8), self.tree.branch.last_revision_info())
774
def make_branch_with_divergent_history(self):
775
"""Make a branch with divergent history in its repo.
777
The branch's tip will be 'child-2', and the repo will also contain
778
'child-1', which diverges from a common base revision.
780
self.tree.lock_write()
782
r1 = self.tree.commit('1st commit')
783
revno_1, revid_1 = self.tree.branch.last_revision_info()
784
r2 = self.tree.commit('2nd commit', rev_id='child-1')
785
# Undo the second commit
786
self.tree.branch.set_last_revision_info(revno_1, revid_1)
787
self.tree.set_parent_ids([revid_1])
788
# Make a new second commit, child-2. child-2 has diverged from
790
new_r2 = self.tree.commit('2nd commit', rev_id='child-2')
793
def test_not_allow_diverged(self):
794
"""If allow_diverged is not passed, then setting a divergent history
795
returns a Diverged error.
797
self.make_branch_with_divergent_history()
799
FailedSmartServerResponse(('Diverged',)),
800
self.set_last_revision('child-1', 2))
801
# The branch tip was not changed.
802
self.assertEqual('child-2', self.tree.branch.last_revision())
804
def test_allow_diverged(self):
805
"""If allow_diverged is passed, then setting a divergent history
808
self.make_branch_with_divergent_history()
809
branch_token, repo_token = self.lock_branch()
810
response = self.request.execute(
811
'', branch_token, repo_token, 'child-1', 1, 0)
813
SuccessfulSmartServerResponse(('ok', 2, 'child-1')),
816
# The branch tip was changed.
817
self.assertEqual('child-1', self.tree.branch.last_revision())
820
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
822
def test_get_parent_none(self):
823
base_branch = self.make_branch('base')
824
request = smart.branch.SmartServerBranchGetParent(self.get_transport())
825
response = request.execute('base')
827
SuccessfulSmartServerResponse(('',)), response)
829
def test_get_parent_something(self):
830
base_branch = self.make_branch('base')
831
base_branch.set_parent(self.get_url('foo'))
832
request = smart.branch.SmartServerBranchGetParent(self.get_transport())
833
response = request.execute('base')
835
SuccessfulSmartServerResponse(("../foo",)),
839
class TestSmartServerBranchRequestSetParent(tests.TestCaseWithMemoryTransport):
841
def test_set_parent_none(self):
842
branch = self.make_branch('base', format="1.9")
844
branch._set_parent_location('foo')
846
request = smart.branch.SmartServerBranchRequestSetParentLocation(
847
self.get_transport())
848
branch_token = branch.lock_write()
849
repo_token = branch.repository.lock_write()
851
response = request.execute('base', branch_token, repo_token, '')
853
branch.repository.unlock()
855
self.assertEqual(SuccessfulSmartServerResponse(()), response)
856
self.assertEqual(None, branch.get_parent())
858
def test_set_parent_something(self):
859
branch = self.make_branch('base', format="1.9")
860
request = smart.branch.SmartServerBranchRequestSetParentLocation(
861
self.get_transport())
862
branch_token = branch.lock_write()
863
repo_token = branch.repository.lock_write()
865
response = request.execute('base', branch_token, repo_token,
868
branch.repository.unlock()
870
self.assertEqual(SuccessfulSmartServerResponse(()), response)
871
self.assertEqual('http://bar/', branch.get_parent())
874
class TestSmartServerBranchRequestGetTagsBytes(tests.TestCaseWithMemoryTransport):
875
# Only called when the branch format and tags match [yay factory
876
# methods] so only need to test straight forward cases.
878
def test_get_bytes(self):
879
base_branch = self.make_branch('base')
880
request = smart.branch.SmartServerBranchGetTagsBytes(
881
self.get_transport())
882
response = request.execute('base')
884
SuccessfulSmartServerResponse(('',)), response)
887
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
889
def test_get_stacked_on_url(self):
890
base_branch = self.make_branch('base', format='1.6')
891
stacked_branch = self.make_branch('stacked', format='1.6')
892
# typically should be relative
893
stacked_branch.set_stacked_on_url('../base')
894
request = smart.branch.SmartServerBranchRequestGetStackedOnURL(
895
self.get_transport())
896
response = request.execute('stacked')
898
SmartServerResponse(('ok', '../base')),
546
902
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithMemoryTransport):
763
1161
request.execute('', 'missingrevision'))
1164
class TestSmartServerRepositoryGetRevIdForRevno(tests.TestCaseWithMemoryTransport):
1166
def test_revno_found(self):
1167
backing = self.get_transport()
1168
request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
1169
tree = self.make_branch_and_memory_tree('.')
1172
rev1_id_utf8 = u'\xc8'.encode('utf-8')
1173
rev2_id_utf8 = u'\xc9'.encode('utf-8')
1174
tree.commit('1st commit', rev_id=rev1_id_utf8)
1175
tree.commit('2nd commit', rev_id=rev2_id_utf8)
1178
self.assertEqual(SmartServerResponse(('ok', rev1_id_utf8)),
1179
request.execute('', 1, (2, rev2_id_utf8)))
1181
def test_known_revid_missing(self):
1182
backing = self.get_transport()
1183
request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
1184
repo = self.make_repository('.')
1186
FailedSmartServerResponse(('nosuchrevision', 'ghost')),
1187
request.execute('', 1, (2, 'ghost')))
1189
def test_history_incomplete(self):
1190
backing = self.get_transport()
1191
request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
1192
parent = self.make_branch_and_memory_tree('parent', format='1.9')
1193
r1 = parent.commit(message='first commit')
1194
r2 = parent.commit(message='second commit')
1195
local = self.make_branch_and_memory_tree('local', format='1.9')
1196
local.branch.pull(parent.branch)
1197
local.set_parent_ids([r2])
1198
r3 = local.commit(message='local commit')
1199
local.branch.create_clone_on_transport(
1200
self.get_transport('stacked'), stacked_on=self.get_url('parent'))
1202
SmartServerResponse(('history-incomplete', 2, r2)),
1203
request.execute('stacked', 1, (3, r3)))
1205
class TestSmartServerRepositoryGetStream(tests.TestCaseWithMemoryTransport):
1207
def make_two_commit_repo(self):
1208
tree = self.make_branch_and_memory_tree('.')
1211
r1 = tree.commit('1st commit')
1212
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1214
repo = tree.branch.repository
1217
def test_ancestry_of(self):
1218
"""The search argument may be a 'ancestry-of' some heads'."""
1219
backing = self.get_transport()
1220
request = smart.repository.SmartServerRepositoryGetStream(backing)
1221
repo, r1, r2 = self.make_two_commit_repo()
1222
fetch_spec = ['ancestry-of', r2]
1223
lines = '\n'.join(fetch_spec)
1224
request.execute('', repo._format.network_name())
1225
response = request.do_body(lines)
1226
self.assertEqual(('ok',), response.args)
1227
stream_bytes = ''.join(response.body_stream)
1228
self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
1230
def test_search(self):
1231
"""The search argument may be a 'search' of some explicit keys."""
1232
backing = self.get_transport()
1233
request = smart.repository.SmartServerRepositoryGetStream(backing)
1234
repo, r1, r2 = self.make_two_commit_repo()
1235
fetch_spec = ['search', '%s %s' % (r1, r2), 'null:', '2']
1236
lines = '\n'.join(fetch_spec)
1237
request.execute('', repo._format.network_name())
1238
response = request.do_body(lines)
1239
self.assertEqual(('ok',), response.args)
1240
stream_bytes = ''.join(response.body_stream)
1241
self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
766
1244
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
768
1246
def test_missing_revision(self):
938
1463
SmartServerResponse(('TokenMismatch',)), response)
941
class TestSmartServerRepositoryTarball(tests.TestCaseWithTransport):
943
def test_repository_tarball(self):
944
backing = self.get_transport()
945
request = smart.repository.SmartServerRepositoryTarball(backing)
946
repository = self.make_repository('.')
947
# make some extraneous junk in the repository directory which should
949
self.build_tree(['.bzr/repository/extra-junk'])
950
response = request.execute('', 'bz2')
951
self.assertEqual(('ok',), response.args)
952
# body should be a tbz2
953
body_file = StringIO(response.body)
954
body_tar = tarfile.open('body_tar.tbz2', fileobj=body_file,
956
# let's make sure there are some key repository components inside it.
957
# the tarfile returns directories with trailing slashes...
958
names = set([n.rstrip('/') for n in body_tar.getnames()])
959
self.assertTrue('.bzr/repository/lock' in names)
960
self.assertTrue('.bzr/repository/format' in names)
961
self.assertTrue('.bzr/repository/extra-junk' not in names,
962
"extraneous file present in tar file")
965
class TestSmartServerRepositoryStreamKnitData(tests.TestCaseWithMemoryTransport):
967
def test_fetch_revisions(self):
968
backing = self.get_transport()
969
request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
970
tree = self.make_branch_and_memory_tree('.')
973
rev_id1_utf8 = u'\xc8'.encode('utf-8')
974
rev_id2_utf8 = u'\xc9'.encode('utf-8')
975
r1 = tree.commit('1st commit', rev_id=rev_id1_utf8)
976
r1 = tree.commit('2nd commit', rev_id=rev_id2_utf8)
979
response = request.execute('', rev_id2_utf8)
980
self.assertEqual(('ok',), response.args)
981
unpacker = pack.ContainerReader(StringIO(response.body))
983
for [name], read_bytes in unpacker.iter_records():
985
bytes = read_bytes(None)
986
# The bytes should be a valid bencoded string.
987
bencode.bdecode(bytes)
988
# XXX: assert that the bencoded knit records have the right
991
def test_no_such_revision_error(self):
992
backing = self.get_transport()
993
request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
994
repo = self.make_repository('.')
995
rev_id1_utf8 = u'\xc8'.encode('utf-8')
996
response = request.execute('', rev_id1_utf8)
998
SmartServerResponse(('NoSuchRevision', rev_id1_utf8)),
1002
class TestSmartServerRepositoryStreamRevisionsChunked(tests.TestCaseWithMemoryTransport):
1004
def test_fetch_revisions(self):
1005
backing = self.get_transport()
1006
request = smart.repository.SmartServerRepositoryStreamRevisionsChunked(
1008
tree = self.make_branch_and_memory_tree('.')
1011
rev_id1_utf8 = u'\xc8'.encode('utf-8')
1012
rev_id2_utf8 = u'\xc9'.encode('utf-8')
1013
tree.commit('1st commit', rev_id=rev_id1_utf8)
1014
tree.commit('2nd commit', rev_id=rev_id2_utf8)
1017
response = request.execute('')
1018
self.assertEqual(None, response)
1019
response = request.do_body("%s\n%s\n1" % (rev_id2_utf8, rev_id1_utf8))
1020
self.assertEqual(('ok',), response.args)
1021
parser = pack.ContainerPushParser()
1023
for stream_bytes in response.body_stream:
1024
parser.accept_bytes(stream_bytes)
1025
for [name], record_bytes in parser.read_pending_records():
1027
# The bytes should be a valid bencoded string.
1028
bencode.bdecode(record_bytes)
1029
# XXX: assert that the bencoded knit records have the right
1032
def test_no_such_revision_error(self):
1033
backing = self.get_transport()
1034
request = smart.repository.SmartServerRepositoryStreamRevisionsChunked(
1036
repo = self.make_repository('.')
1037
rev_id1_utf8 = u'\xc8'.encode('utf-8')
1038
response = request.execute('')
1039
self.assertEqual(None, response)
1040
response = request.do_body("%s\n\n1" % (rev_id1_utf8,))
1042
FailedSmartServerResponse(('NoSuchRevision', )),
1046
1466
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
1048
1468
def test_is_readonly_no(self):
1060
1480
SmartServerResponse(('yes',)), response)
1483
class TestSmartServerRepositorySetMakeWorkingTrees(tests.TestCaseWithMemoryTransport):
1485
def test_set_false(self):
1486
backing = self.get_transport()
1487
repo = self.make_repository('.', shared=True)
1488
repo.set_make_working_trees(True)
1489
request_class = smart.repository.SmartServerRepositorySetMakeWorkingTrees
1490
request = request_class(backing)
1491
self.assertEqual(SuccessfulSmartServerResponse(('ok',)),
1492
request.execute('', 'False'))
1493
repo = repo.bzrdir.open_repository()
1494
self.assertFalse(repo.make_working_trees())
1496
def test_set_true(self):
1497
backing = self.get_transport()
1498
repo = self.make_repository('.', shared=True)
1499
repo.set_make_working_trees(False)
1500
request_class = smart.repository.SmartServerRepositorySetMakeWorkingTrees
1501
request = request_class(backing)
1502
self.assertEqual(SuccessfulSmartServerResponse(('ok',)),
1503
request.execute('', 'True'))
1504
repo = repo.bzrdir.open_repository()
1505
self.assertTrue(repo.make_working_trees())
1508
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
1510
def make_repo_needing_autopacking(self, path='.'):
1511
# Make a repo in need of autopacking.
1512
tree = self.make_branch_and_tree('.', format='pack-0.92')
1513
repo = tree.branch.repository
1514
# monkey-patch the pack collection to disable autopacking
1515
repo._pack_collection._max_pack_count = lambda count: count
1517
tree.commit('commit %s' % x)
1518
self.assertEqual(10, len(repo._pack_collection.names()))
1519
del repo._pack_collection._max_pack_count
1522
def test_autopack_needed(self):
1523
repo = self.make_repo_needing_autopacking()
1525
self.addCleanup(repo.unlock)
1526
backing = self.get_transport()
1527
request = smart.packrepository.SmartServerPackRepositoryAutopack(
1529
response = request.execute('')
1530
self.assertEqual(SmartServerResponse(('ok',)), response)
1531
repo._pack_collection.reload_pack_names()
1532
self.assertEqual(1, len(repo._pack_collection.names()))
1534
def test_autopack_not_needed(self):
1535
tree = self.make_branch_and_tree('.', format='pack-0.92')
1536
repo = tree.branch.repository
1538
self.addCleanup(repo.unlock)
1540
tree.commit('commit %s' % x)
1541
backing = self.get_transport()
1542
request = smart.packrepository.SmartServerPackRepositoryAutopack(
1544
response = request.execute('')
1545
self.assertEqual(SmartServerResponse(('ok',)), response)
1546
repo._pack_collection.reload_pack_names()
1547
self.assertEqual(9, len(repo._pack_collection.names()))
1549
def test_autopack_on_nonpack_format(self):
1550
"""A request to autopack a non-pack repo is a no-op."""
1551
repo = self.make_repository('.', format='knit')
1552
backing = self.get_transport()
1553
request = smart.packrepository.SmartServerPackRepositoryAutopack(
1555
response = request.execute('')
1556
self.assertEqual(SmartServerResponse(('ok',)), response)
1063
1559
class TestHandlers(tests.TestCase):
1064
1560
"""Tests for the request.request_handlers object."""
1562
def test_all_registrations_exist(self):
1563
"""All registered request_handlers can be found."""
1564
# If there's a typo in a register_lazy call, this loop will fail with
1565
# an AttributeError.
1566
for key, item in smart.request.request_handlers.iteritems():
1569
def assertHandlerEqual(self, verb, handler):
1570
self.assertEqual(smart.request.request_handlers.get(verb), handler)
1066
1572
def test_registered_methods(self):
1067
1573
"""Test that known methods are registered to the correct object."""
1069
smart.request.request_handlers.get('Branch.get_config_file'),
1574
self.assertHandlerEqual('Branch.get_config_file',
1070
1575
smart.branch.SmartServerBranchGetConfigFile)
1072
smart.request.request_handlers.get('Branch.lock_write'),
1576
self.assertHandlerEqual('Branch.get_parent',
1577
smart.branch.SmartServerBranchGetParent)
1578
self.assertHandlerEqual('Branch.get_tags_bytes',
1579
smart.branch.SmartServerBranchGetTagsBytes)
1580
self.assertHandlerEqual('Branch.lock_write',
1073
1581
smart.branch.SmartServerBranchRequestLockWrite)
1075
smart.request.request_handlers.get('Branch.last_revision_info'),
1582
self.assertHandlerEqual('Branch.last_revision_info',
1076
1583
smart.branch.SmartServerBranchRequestLastRevisionInfo)
1078
smart.request.request_handlers.get('Branch.revision_history'),
1584
self.assertHandlerEqual('Branch.revision_history',
1079
1585
smart.branch.SmartServerRequestRevisionHistory)
1081
smart.request.request_handlers.get('Branch.set_last_revision'),
1586
self.assertHandlerEqual('Branch.set_config_option',
1587
smart.branch.SmartServerBranchRequestSetConfigOption)
1588
self.assertHandlerEqual('Branch.set_last_revision',
1082
1589
smart.branch.SmartServerBranchRequestSetLastRevision)
1084
smart.request.request_handlers.get('Branch.set_last_revision_info'),
1590
self.assertHandlerEqual('Branch.set_last_revision_info',
1085
1591
smart.branch.SmartServerBranchRequestSetLastRevisionInfo)
1087
smart.request.request_handlers.get('Branch.unlock'),
1592
self.assertHandlerEqual('Branch.set_last_revision_ex',
1593
smart.branch.SmartServerBranchRequestSetLastRevisionEx)
1594
self.assertHandlerEqual('Branch.set_parent_location',
1595
smart.branch.SmartServerBranchRequestSetParentLocation)
1596
self.assertHandlerEqual('Branch.unlock',
1088
1597
smart.branch.SmartServerBranchRequestUnlock)
1090
smart.request.request_handlers.get('BzrDir.find_repository'),
1598
self.assertHandlerEqual('BzrDir.find_repository',
1091
1599
smart.bzrdir.SmartServerRequestFindRepositoryV1)
1093
smart.request.request_handlers.get('BzrDir.find_repositoryV2'),
1600
self.assertHandlerEqual('BzrDir.find_repositoryV2',
1094
1601
smart.bzrdir.SmartServerRequestFindRepositoryV2)
1096
smart.request.request_handlers.get('BzrDirFormat.initialize'),
1602
self.assertHandlerEqual('BzrDirFormat.initialize',
1097
1603
smart.bzrdir.SmartServerRequestInitializeBzrDir)
1099
smart.request.request_handlers.get('BzrDir.open_branch'),
1604
self.assertHandlerEqual('BzrDirFormat.initialize_ex_1.16',
1605
smart.bzrdir.SmartServerRequestBzrDirInitializeEx)
1606
self.assertHandlerEqual('BzrDir.cloning_metadir',
1607
smart.bzrdir.SmartServerBzrDirRequestCloningMetaDir)
1608
self.assertHandlerEqual('BzrDir.get_config_file',
1609
smart.bzrdir.SmartServerBzrDirRequestConfigFile)
1610
self.assertHandlerEqual('BzrDir.open_branch',
1100
1611
smart.bzrdir.SmartServerRequestOpenBranch)
1102
smart.request.request_handlers.get('Repository.gather_stats'),
1612
self.assertHandlerEqual('BzrDir.open_branchV2',
1613
smart.bzrdir.SmartServerRequestOpenBranchV2)
1614
self.assertHandlerEqual('PackRepository.autopack',
1615
smart.packrepository.SmartServerPackRepositoryAutopack)
1616
self.assertHandlerEqual('Repository.gather_stats',
1103
1617
smart.repository.SmartServerRepositoryGatherStats)
1105
smart.request.request_handlers.get('Repository.get_parent_map'),
1618
self.assertHandlerEqual('Repository.get_parent_map',
1106
1619
smart.repository.SmartServerRepositoryGetParentMap)
1108
smart.request.request_handlers.get(
1109
'Repository.get_revision_graph'),
1620
self.assertHandlerEqual('Repository.get_rev_id_for_revno',
1621
smart.repository.SmartServerRepositoryGetRevIdForRevno)
1622
self.assertHandlerEqual('Repository.get_revision_graph',
1110
1623
smart.repository.SmartServerRepositoryGetRevisionGraph)
1112
smart.request.request_handlers.get('Repository.has_revision'),
1624
self.assertHandlerEqual('Repository.get_stream',
1625
smart.repository.SmartServerRepositoryGetStream)
1626
self.assertHandlerEqual('Repository.has_revision',
1113
1627
smart.repository.SmartServerRequestHasRevision)
1115
smart.request.request_handlers.get('Repository.is_shared'),
1628
self.assertHandlerEqual('Repository.insert_stream',
1629
smart.repository.SmartServerRepositoryInsertStream)
1630
self.assertHandlerEqual('Repository.insert_stream_locked',
1631
smart.repository.SmartServerRepositoryInsertStreamLocked)
1632
self.assertHandlerEqual('Repository.is_shared',
1116
1633
smart.repository.SmartServerRepositoryIsShared)
1118
smart.request.request_handlers.get('Repository.lock_write'),
1634
self.assertHandlerEqual('Repository.lock_write',
1119
1635
smart.repository.SmartServerRepositoryLockWrite)
1121
smart.request.request_handlers.get('Repository.tarball'),
1636
self.assertHandlerEqual('Repository.tarball',
1122
1637
smart.repository.SmartServerRepositoryTarball)
1124
smart.request.request_handlers.get('Repository.unlock'),
1638
self.assertHandlerEqual('Repository.unlock',
1125
1639
smart.repository.SmartServerRepositoryUnlock)
1127
smart.request.request_handlers.get('Transport.is_readonly'),
1640
self.assertHandlerEqual('Transport.is_readonly',
1128
1641
smart.request.SmartServerIsReadonly)