374
259
def test_missing_dir(self):
375
260
"""Initializing a missing directory should fail like the bzrdir api."""
376
261
backing = self.get_transport()
377
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
262
request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
378
263
self.assertRaises(errors.NoSuchFile,
379
264
request.execute, 'subdir')
381
266
def test_initialized_dir(self):
382
267
"""Initializing an extant bzrdir should fail like the bzrdir api."""
383
268
backing = self.get_transport()
384
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
269
request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
385
270
self.make_bzrdir('subdir')
386
271
self.assertRaises(errors.FileExists,
387
272
request.execute, 'subdir')
390
class TestSmartServerRequestBzrDirInitializeEx(
391
tests.TestCaseWithMemoryTransport):
392
"""Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
394
The main unit tests in test_bzrdir exercise the API comprehensively.
397
def test_empty_dir(self):
398
"""Initializing an empty dir should succeed and do it."""
399
backing = self.get_transport()
400
name = self.make_bzrdir('reference')._format.network_name()
401
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
403
smart_req.SmartServerResponse(('', '', '', '', '', '', name,
404
'False', '', '', '')),
405
request.execute(name, '', 'True', 'False', 'False', '', '', '', '',
407
made_dir = bzrdir.BzrDir.open_from_transport(backing)
408
# no branch, tree or repository is expected with the current
410
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
411
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
412
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
414
def test_missing_dir(self):
415
"""Initializing a missing directory should fail like the bzrdir api."""
416
backing = self.get_transport()
417
name = self.make_bzrdir('reference')._format.network_name()
418
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
419
self.assertRaises(errors.NoSuchFile, request.execute, name,
420
'subdir/dir', 'False', 'False', 'False', '', '', '', '', 'False')
422
def test_initialized_dir(self):
423
"""Initializing an extant directory should fail like the bzrdir api."""
424
backing = self.get_transport()
425
name = self.make_bzrdir('reference')._format.network_name()
426
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
427
self.make_bzrdir('subdir')
428
self.assertRaises(errors.FileExists, request.execute, name, 'subdir',
429
'False', 'False', 'False', '', '', '', '', 'False')
432
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
434
def test_no_directory(self):
435
backing = self.get_transport()
436
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
437
self.assertEqual(smart_req.SmartServerResponse(('no', )),
438
request.execute('does-not-exist'))
440
def test_empty_directory(self):
441
backing = self.get_transport()
442
backing.mkdir('empty')
443
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
444
self.assertEqual(smart_req.SmartServerResponse(('no', )),
445
request.execute('empty'))
447
def test_outside_root_client_path(self):
448
backing = self.get_transport()
449
request = smart_dir.SmartServerRequestOpenBzrDir(backing,
450
root_client_path='root')
451
self.assertEqual(smart_req.SmartServerResponse(('no', )),
452
request.execute('not-root'))
455
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
457
def test_no_directory(self):
458
backing = self.get_transport()
459
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
460
self.assertEqual(smart_req.SmartServerResponse(('no', )),
461
request.execute('does-not-exist'))
463
def test_empty_directory(self):
464
backing = self.get_transport()
465
backing.mkdir('empty')
466
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
467
self.assertEqual(smart_req.SmartServerResponse(('no', )),
468
request.execute('empty'))
470
def test_present_without_workingtree(self):
471
backing = self.get_transport()
472
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
473
self.make_bzrdir('.')
474
self.assertEqual(smart_req.SmartServerResponse(('yes', 'no')),
477
def test_outside_root_client_path(self):
478
backing = self.get_transport()
479
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing,
480
root_client_path='root')
481
self.assertEqual(smart_req.SmartServerResponse(('no',)),
482
request.execute('not-root'))
485
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
487
def test_present_with_workingtree(self):
488
self.vfs_transport_factory = test_server.LocalURLServer
489
backing = self.get_transport()
490
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
491
bd = self.make_bzrdir('.')
492
bd.create_repository()
494
bd.create_workingtree()
495
self.assertEqual(smart_req.SmartServerResponse(('yes', 'yes')),
499
275
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
501
277
def test_no_branch(self):
502
278
"""When there is no branch, ('nobranch', ) is returned."""
503
279
backing = self.get_transport()
504
request = smart_dir.SmartServerRequestOpenBranch(backing)
280
request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
505
281
self.make_bzrdir('.')
506
self.assertEqual(smart_req.SmartServerResponse(('nobranch', )),
282
self.assertEqual(SmartServerResponse(('nobranch', )),
507
283
request.execute(''))
509
285
def test_branch(self):
510
286
"""When there is a branch, 'ok' is returned."""
511
287
backing = self.get_transport()
512
request = smart_dir.SmartServerRequestOpenBranch(backing)
288
request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
513
289
self.make_branch('.')
514
self.assertEqual(smart_req.SmartServerResponse(('ok', '')),
290
self.assertEqual(SmartServerResponse(('ok', '')),
515
291
request.execute(''))
517
293
def test_branch_reference(self):
518
294
"""When there is a branch reference, the reference URL is returned."""
519
self.vfs_transport_factory = test_server.LocalURLServer
520
295
backing = self.get_transport()
521
request = smart_dir.SmartServerRequestOpenBranch(backing)
296
request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
522
297
branch = self.make_branch('branch')
523
298
checkout = branch.create_checkout('reference',lightweight=True)
524
reference_url = _mod_branch.BranchReferenceFormat().get_reference(
299
reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
526
300
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
527
self.assertEqual(smart_req.SmartServerResponse(('ok', reference_url)),
301
self.assertEqual(SmartServerResponse(('ok', reference_url)),
528
302
request.execute('reference'))
530
def test_notification_on_branch_from_repository(self):
531
"""When there is a repository, the error should return details."""
532
backing = self.get_transport()
533
request = smart_dir.SmartServerRequestOpenBranch(backing)
534
repo = self.make_repository('.')
535
self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
539
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
541
def test_no_branch(self):
542
"""When there is no branch, ('nobranch', ) is returned."""
543
backing = self.get_transport()
544
self.make_bzrdir('.')
545
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
546
self.assertEqual(smart_req.SmartServerResponse(('nobranch', )),
549
def test_branch(self):
550
"""When there is a branch, 'ok' is returned."""
551
backing = self.get_transport()
552
expected = self.make_branch('.')._format.network_name()
553
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
554
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
555
('branch', expected)),
558
def test_branch_reference(self):
559
"""When there is a branch reference, the reference URL is returned."""
560
self.vfs_transport_factory = test_server.LocalURLServer
561
backing = self.get_transport()
562
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
563
branch = self.make_branch('branch')
564
checkout = branch.create_checkout('reference',lightweight=True)
565
reference_url = _mod_branch.BranchReferenceFormat().get_reference(
567
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
568
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
569
('ref', reference_url)),
570
request.execute('reference'))
572
def test_stacked_branch(self):
573
"""Opening a stacked branch does not open the stacked-on branch."""
574
trunk = self.make_branch('trunk')
575
feature = self.make_branch('feature')
576
feature.set_stacked_on_url(trunk.base)
578
_mod_branch.Branch.hooks.install_named_hook(
579
'open', opened_branches.append, None)
580
backing = self.get_transport()
581
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
584
response = request.execute('feature')
586
request.teardown_jail()
587
expected_format = feature._format.network_name()
588
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
589
('branch', expected_format)),
591
self.assertLength(1, opened_branches)
593
def test_notification_on_branch_from_repository(self):
594
"""When there is a repository, the error should return details."""
595
backing = self.get_transport()
596
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
597
repo = self.make_repository('.')
598
self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
602
class TestSmartServerRequestOpenBranchV3(TestCaseWithChrootedTransport):
604
def test_no_branch(self):
605
"""When there is no branch, ('nobranch', ) is returned."""
606
backing = self.get_transport()
607
self.make_bzrdir('.')
608
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
609
self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
612
def test_branch(self):
613
"""When there is a branch, 'ok' is returned."""
614
backing = self.get_transport()
615
expected = self.make_branch('.')._format.network_name()
616
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
617
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
618
('branch', expected)),
621
def test_branch_reference(self):
622
"""When there is a branch reference, the reference URL is returned."""
623
self.vfs_transport_factory = test_server.LocalURLServer
624
backing = self.get_transport()
625
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
626
branch = self.make_branch('branch')
627
checkout = branch.create_checkout('reference',lightweight=True)
628
reference_url = _mod_branch.BranchReferenceFormat().get_reference(
630
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
631
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
632
('ref', reference_url)),
633
request.execute('reference'))
635
def test_stacked_branch(self):
636
"""Opening a stacked branch does not open the stacked-on branch."""
637
trunk = self.make_branch('trunk')
638
feature = self.make_branch('feature')
639
feature.set_stacked_on_url(trunk.base)
641
_mod_branch.Branch.hooks.install_named_hook(
642
'open', opened_branches.append, None)
643
backing = self.get_transport()
644
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
647
response = request.execute('feature')
649
request.teardown_jail()
650
expected_format = feature._format.network_name()
651
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
652
('branch', expected_format)),
654
self.assertLength(1, opened_branches)
656
def test_notification_on_branch_from_repository(self):
657
"""When there is a repository, the error should return details."""
658
backing = self.get_transport()
659
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
660
repo = self.make_repository('.')
661
self.assertEqual(smart_req.SmartServerResponse(
662
('nobranch', 'location is a repository')),
666
305
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
668
307
def test_empty(self):
669
308
"""For an empty branch, the body is empty."""
670
309
backing = self.get_transport()
671
request = smart_branch.SmartServerRequestRevisionHistory(backing)
310
request = smart.branch.SmartServerRequestRevisionHistory(backing)
672
311
self.make_branch('.')
673
self.assertEqual(smart_req.SmartServerResponse(('ok', ), ''),
312
self.assertEqual(SmartServerResponse(('ok', ), ''),
674
313
request.execute(''))
676
315
def test_not_empty(self):
677
316
"""For a non-empty branch, the body is empty."""
678
317
backing = self.get_transport()
679
request = smart_branch.SmartServerRequestRevisionHistory(backing)
318
request = smart.branch.SmartServerRequestRevisionHistory(backing)
680
319
tree = self.make_branch_and_memory_tree('.')
681
320
tree.lock_write()
731
369
r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
733
371
self.assertEqual(
734
smart_req.SmartServerResponse(('ok', '2', rev_id_utf8)),
372
SmartServerResponse(('ok', '2', rev_id_utf8)),
735
373
request.execute(''))
738
class TestSmartServerBranchRequestGetConfigFile(
739
tests.TestCaseWithMemoryTransport):
376
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithMemoryTransport):
741
378
def test_default(self):
742
379
"""With no file, we get empty content."""
743
380
backing = self.get_transport()
744
request = smart_branch.SmartServerBranchGetConfigFile(backing)
381
request = smart.branch.SmartServerBranchGetConfigFile(backing)
745
382
branch = self.make_branch('.')
746
383
# there should be no file by default
748
self.assertEqual(smart_req.SmartServerResponse(('ok', ), content),
385
self.assertEqual(SmartServerResponse(('ok', ), content),
749
386
request.execute(''))
751
388
def test_with_content(self):
752
389
# SmartServerBranchGetConfigFile should return the content from
753
390
# branch.control_files.get('branch.conf') for now - in the future it may
754
# perform more complex processing.
391
# perform more complex processing.
755
392
backing = self.get_transport()
756
request = smart_branch.SmartServerBranchGetConfigFile(backing)
393
request = smart.branch.SmartServerBranchGetConfigFile(backing)
757
394
branch = self.make_branch('.')
758
395
branch._transport.put_bytes('branch.conf', 'foo bar baz')
759
self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'foo bar baz'),
396
self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
760
397
request.execute(''))
763
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
765
def get_lock_tokens(self, branch):
766
branch_token = branch.lock_write().branch_token
767
repo_token = branch.repository.lock_write().repository_token
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):
485
branch_token = branch.lock_write()
486
repo_token = branch.repository.lock_write()
768
487
branch.repository.unlock()
488
self.addCleanup(branch.unlock)
769
489
return branch_token, repo_token
772
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
774
def test_value_name(self):
775
branch = self.make_branch('.')
776
request = smart_branch.SmartServerBranchRequestSetConfigOption(
777
branch.bzrdir.root_transport)
778
branch_token, repo_token = self.get_lock_tokens(branch)
779
config = branch._get_config()
780
result = request.execute('', branch_token, repo_token, 'bar', 'foo',
782
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
783
self.assertEqual('bar', config.get_option('foo'))
787
def test_value_name_section(self):
788
branch = self.make_branch('.')
789
request = smart_branch.SmartServerBranchRequestSetConfigOption(
790
branch.bzrdir.root_transport)
791
branch_token, repo_token = self.get_lock_tokens(branch)
792
config = branch._get_config()
793
result = request.execute('', branch_token, repo_token, 'bar', 'foo',
795
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
796
self.assertEqual('bar', config.get_option('foo', 'gam'))
801
class TestSmartServerBranchRequestSetConfigOptionDict(TestLockedBranch):
804
TestLockedBranch.setUp(self)
805
# A dict with non-ascii keys and values to exercise unicode
807
self.encoded_value_dict = (
808
'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
810
'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
812
def test_value_name(self):
813
branch = self.make_branch('.')
814
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
815
branch.bzrdir.root_transport)
816
branch_token, repo_token = self.get_lock_tokens(branch)
817
config = branch._get_config()
818
result = request.execute('', branch_token, repo_token,
819
self.encoded_value_dict, 'foo', '')
820
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
821
self.assertEqual(self.value_dict, config.get_option('foo'))
825
def test_value_name_section(self):
826
branch = self.make_branch('.')
827
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
828
branch.bzrdir.root_transport)
829
branch_token, repo_token = self.get_lock_tokens(branch)
830
config = branch._get_config()
831
result = request.execute('', branch_token, repo_token,
832
self.encoded_value_dict, 'foo', 'gam')
833
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
834
self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
839
class TestSmartServerBranchRequestSetTagsBytes(TestLockedBranch):
840
# Only called when the branch format and tags match [yay factory
841
# methods] so only need to test straight forward cases.
843
def test_set_bytes(self):
844
base_branch = self.make_branch('base')
845
tag_bytes = base_branch._get_tags_bytes()
846
# get_lock_tokens takes out a lock.
847
branch_token, repo_token = self.get_lock_tokens(base_branch)
848
request = smart_branch.SmartServerBranchSetTagsBytes(
849
self.get_transport())
850
response = request.execute('base', branch_token, repo_token)
851
self.assertEqual(None, response)
852
response = request.do_chunk(tag_bytes)
853
self.assertEqual(None, response)
854
response = request.do_end()
856
smart_req.SuccessfulSmartServerResponse(()), response)
859
def test_lock_failed(self):
860
base_branch = self.make_branch('base')
861
base_branch.lock_write()
862
tag_bytes = base_branch._get_tags_bytes()
863
request = smart_branch.SmartServerBranchSetTagsBytes(
864
self.get_transport())
865
self.assertRaises(errors.TokenMismatch, request.execute,
866
'base', 'wrong token', 'wrong token')
867
# The request handler will keep processing the message parts, so even
868
# if the request fails immediately do_chunk and do_end are still
870
request.do_chunk(tag_bytes)
876
class SetLastRevisionTestBase(TestLockedBranch):
877
"""Base test case for verbs that implement set_last_revision."""
880
tests.TestCaseWithMemoryTransport.setUp(self)
881
backing_transport = self.get_transport()
882
self.request = self.request_class(backing_transport)
883
self.tree = self.make_branch_and_memory_tree('.')
885
def lock_branch(self):
886
return self.get_lock_tokens(self.tree.branch)
888
def unlock_branch(self):
889
self.tree.branch.unlock()
891
def set_last_revision(self, revision_id, revno):
892
branch_token, repo_token = self.lock_branch()
893
response = self._set_last_revision(
894
revision_id, revno, branch_token, repo_token)
898
def assertRequestSucceeds(self, revision_id, revno):
899
response = self.set_last_revision(revision_id, revno)
900
self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
904
class TestSetLastRevisionVerbMixin(object):
905
"""Mixin test case for verbs that implement set_last_revision."""
907
def test_set_null_to_null(self):
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):
908
497
"""An empty branch can have its last revision set to 'null:'."""
909
self.assertRequestSucceeds('null:', 0)
911
def test_NoSuchRevision(self):
912
"""If the revision_id is not present, the verb returns NoSuchRevision.
914
revision_id = 'non-existent revision'
915
self.assertEqual(smart_req.FailedSmartServerResponse(('NoSuchRevision',
917
self.set_last_revision(revision_id, 1))
919
def make_tree_with_two_commits(self):
920
self.tree.lock_write()
922
rev_id_utf8 = u'\xc8'.encode('utf-8')
923
r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
924
r2 = self.tree.commit('2nd commit', rev_id='rev-2')
927
def test_branch_last_revision_info_is_updated(self):
928
"""A branch's tip can be set to a revision that is present in its
931
# Make a branch with an empty revision history, but two revisions in
933
self.make_tree_with_two_commits()
934
rev_id_utf8 = u'\xc8'.encode('utf-8')
935
self.tree.branch.set_last_revision_info(0, 'null:')
937
(0, 'null:'), self.tree.branch.last_revision_info())
938
# We can update the branch to a revision that is present in the
940
self.assertRequestSucceeds(rev_id_utf8, 1)
942
(1, rev_id_utf8), self.tree.branch.last_revision_info())
944
def test_branch_last_revision_info_rewind(self):
945
"""A branch's tip can be set to a revision that is an ancestor of the
948
self.make_tree_with_two_commits()
949
rev_id_utf8 = u'\xc8'.encode('utf-8')
951
(2, 'rev-2'), self.tree.branch.last_revision_info())
952
self.assertRequestSucceeds(rev_id_utf8, 1)
954
(1, rev_id_utf8), self.tree.branch.last_revision_info())
956
def test_TipChangeRejected(self):
957
"""If a pre_change_branch_tip hook raises TipChangeRejected, the verb
958
returns TipChangeRejected.
960
rejection_message = u'rejection message\N{INTERROBANG}'
961
def hook_that_rejects(params):
962
raise errors.TipChangeRejected(rejection_message)
963
_mod_branch.Branch.hooks.install_named_hook(
964
'pre_change_branch_tip', hook_that_rejects, None)
966
smart_req.FailedSmartServerResponse(
967
('TipChangeRejected', rejection_message.encode('utf-8'))),
968
self.set_last_revision('null:', 0))
971
class TestSmartServerBranchRequestSetLastRevision(
972
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
973
"""Tests for Branch.set_last_revision verb."""
975
request_class = smart_branch.SmartServerBranchRequestSetLastRevision
977
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
978
return self.request.execute(
979
'', branch_token, repo_token, revision_id)
982
class TestSmartServerBranchRequestSetLastRevisionInfo(
983
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
984
"""Tests for Branch.set_last_revision_info verb."""
986
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionInfo
988
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
989
return self.request.execute(
990
'', branch_token, repo_token, revno, revision_id)
992
def test_NoSuchRevision(self):
993
"""Branch.set_last_revision_info does not have to return
994
NoSuchRevision if the revision_id is absent.
996
raise tests.TestNotApplicable()
999
class TestSmartServerBranchRequestSetLastRevisionEx(
1000
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1001
"""Tests for Branch.set_last_revision_ex verb."""
1003
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionEx
1005
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1006
return self.request.execute(
1007
'', branch_token, repo_token, revision_id, 0, 0)
1009
def assertRequestSucceeds(self, revision_id, revno):
1010
response = self.set_last_revision(revision_id, revno)
1012
smart_req.SuccessfulSmartServerResponse(('ok', revno, revision_id)),
1015
def test_branch_last_revision_info_rewind(self):
1016
"""A branch's tip can be set to a revision that is an ancestor of the
1017
current tip, but only if allow_overwrite_descendant is passed.
1019
self.make_tree_with_two_commits()
1020
rev_id_utf8 = u'\xc8'.encode('utf-8')
1022
(2, 'rev-2'), self.tree.branch.last_revision_info())
1023
# If allow_overwrite_descendant flag is 0, then trying to set the tip
1024
# to an older revision ID has no effect.
1025
branch_token, repo_token = self.lock_branch()
1026
response = self.request.execute(
1027
'', branch_token, repo_token, rev_id_utf8, 0, 0)
1029
smart_req.SuccessfulSmartServerResponse(('ok', 2, 'rev-2')),
1032
(2, 'rev-2'), self.tree.branch.last_revision_info())
1034
# If allow_overwrite_descendant flag is 1, then setting the tip to an
1036
response = self.request.execute(
1037
'', branch_token, repo_token, rev_id_utf8, 0, 1)
1039
smart_req.SuccessfulSmartServerResponse(('ok', 1, rev_id_utf8)),
1041
self.unlock_branch()
1043
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1045
def make_branch_with_divergent_history(self):
1046
"""Make a branch with divergent history in its repo.
1048
The branch's tip will be 'child-2', and the repo will also contain
1049
'child-1', which diverges from a common base revision.
1051
self.tree.lock_write()
1053
r1 = self.tree.commit('1st commit')
1054
revno_1, revid_1 = self.tree.branch.last_revision_info()
1055
r2 = self.tree.commit('2nd commit', rev_id='child-1')
1056
# Undo the second commit
1057
self.tree.branch.set_last_revision_info(revno_1, revid_1)
1058
self.tree.set_parent_ids([revid_1])
1059
# Make a new second commit, child-2. child-2 has diverged from
1061
new_r2 = self.tree.commit('2nd commit', rev_id='child-2')
1064
def test_not_allow_diverged(self):
1065
"""If allow_diverged is not passed, then setting a divergent history
1066
returns a Diverged error.
1068
self.make_branch_with_divergent_history()
1070
smart_req.FailedSmartServerResponse(('Diverged',)),
1071
self.set_last_revision('child-1', 2))
1072
# The branch tip was not changed.
1073
self.assertEqual('child-2', self.tree.branch.last_revision())
1075
def test_allow_diverged(self):
1076
"""If allow_diverged is passed, then setting a divergent history
1079
self.make_branch_with_divergent_history()
1080
branch_token, repo_token = self.lock_branch()
1081
response = self.request.execute(
1082
'', branch_token, repo_token, 'child-1', 1, 0)
1084
smart_req.SuccessfulSmartServerResponse(('ok', 2, 'child-1')),
1086
self.unlock_branch()
1087
# The branch tip was changed.
1088
self.assertEqual('child-1', self.tree.branch.last_revision())
1091
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1093
def test_get_parent_none(self):
1094
base_branch = self.make_branch('base')
1095
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1096
response = request.execute('base')
1098
smart_req.SuccessfulSmartServerResponse(('',)), response)
1100
def test_get_parent_something(self):
1101
base_branch = self.make_branch('base')
1102
base_branch.set_parent(self.get_url('foo'))
1103
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1104
response = request.execute('base')
1106
smart_req.SuccessfulSmartServerResponse(("../foo",)),
1110
class TestSmartServerBranchRequestSetParent(TestLockedBranch):
1112
def test_set_parent_none(self):
1113
branch = self.make_branch('base', format="1.9")
1115
branch._set_parent_location('foo')
1117
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1118
self.get_transport())
1119
branch_token, repo_token = self.get_lock_tokens(branch)
1121
response = request.execute('base', branch_token, repo_token, '')
1124
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1125
self.assertEqual(None, branch.get_parent())
1127
def test_set_parent_something(self):
1128
branch = self.make_branch('base', format="1.9")
1129
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1130
self.get_transport())
1131
branch_token, repo_token = self.get_lock_tokens(branch)
1133
response = request.execute('base', branch_token, repo_token,
1137
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1138
self.assertEqual('http://bar/', branch.get_parent())
1141
class TestSmartServerBranchRequestGetTagsBytes(
1142
tests.TestCaseWithMemoryTransport):
1143
# Only called when the branch format and tags match [yay factory
1144
# methods] so only need to test straight forward cases.
1146
def test_get_bytes(self):
1147
base_branch = self.make_branch('base')
1148
request = smart_branch.SmartServerBranchGetTagsBytes(
1149
self.get_transport())
1150
response = request.execute('base')
1152
smart_req.SuccessfulSmartServerResponse(('',)), response)
1155
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
1157
def test_get_stacked_on_url(self):
1158
base_branch = self.make_branch('base', format='1.6')
1159
stacked_branch = self.make_branch('stacked', format='1.6')
1160
# typically should be relative
1161
stacked_branch.set_stacked_on_url('../base')
1162
request = smart_branch.SmartServerBranchRequestGetStackedOnURL(
1163
self.get_transport())
1164
response = request.execute('stacked')
1166
smart_req.SmartServerResponse(('ok', '../base')),
1170
class TestSmartServerBranchRequestLockWrite(TestLockedBranch):
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)
546
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithMemoryTransport):
1172
548
def setUp(self):
1173
549
tests.TestCaseWithMemoryTransport.setUp(self)
1175
551
def test_lock_write_on_unlocked_branch(self):
1176
552
backing = self.get_transport()
1177
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
553
request = smart.branch.SmartServerBranchRequestLockWrite(backing)
1178
554
branch = self.make_branch('.', format='knit')
1179
555
repository = branch.repository
1180
556
response = request.execute('')
1181
557
branch_nonce = branch.control_files._lock.peek().get('nonce')
1182
558
repository_nonce = repository.control_files._lock.peek().get('nonce')
1183
self.assertEqual(smart_req.SmartServerResponse(
1184
('ok', branch_nonce, repository_nonce)),
560
SmartServerResponse(('ok', branch_nonce, repository_nonce)),
1186
562
# The branch (and associated repository) is now locked. Verify that
1187
563
# with a new branch object.
1188
564
new_branch = repository.bzrdir.open_branch()
1189
565
self.assertRaises(errors.LockContention, new_branch.lock_write)
1191
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1192
response = request.execute('', branch_nonce, repository_nonce)
1194
567
def test_lock_write_on_locked_branch(self):
1195
568
backing = self.get_transport()
1196
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
569
request = smart.branch.SmartServerBranchRequestLockWrite(backing)
1197
570
branch = self.make_branch('.')
1198
branch_token = branch.lock_write().branch_token
1199
572
branch.leave_lock_in_place()
1201
574
response = request.execute('')
1202
575
self.assertEqual(
1203
smart_req.SmartServerResponse(('LockContention',)), response)
1205
branch.lock_write(branch_token)
1206
branch.dont_leave_lock_in_place()
576
SmartServerResponse(('LockContention',)), response)
1209
578
def test_lock_write_with_tokens_on_locked_branch(self):
1210
579
backing = self.get_transport()
1211
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
580
request = smart.branch.SmartServerBranchRequestLockWrite(backing)
1212
581
branch = self.make_branch('.', format='knit')
1213
branch_token, repo_token = self.get_lock_tokens(branch)
582
branch_token = branch.lock_write()
583
repo_token = branch.repository.lock_write()
584
branch.repository.unlock()
1214
585
branch.leave_lock_in_place()
1215
586
branch.repository.leave_lock_in_place()
1217
588
response = request.execute('',
1218
589
branch_token, repo_token)
1219
590
self.assertEqual(
1220
smart_req.SmartServerResponse(('ok', branch_token, repo_token)),
1223
branch.repository.lock_write(repo_token)
1224
branch.repository.dont_leave_lock_in_place()
1225
branch.repository.unlock()
1226
branch.lock_write(branch_token)
1227
branch.dont_leave_lock_in_place()
591
SmartServerResponse(('ok', branch_token, repo_token)), response)
1230
593
def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
1231
594
backing = self.get_transport()
1232
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
595
request = smart.branch.SmartServerBranchRequestLockWrite(backing)
1233
596
branch = self.make_branch('.', format='knit')
1234
branch_token, repo_token = self.get_lock_tokens(branch)
597
branch_token = branch.lock_write()
598
repo_token = branch.repository.lock_write()
599
branch.repository.unlock()
1235
600
branch.leave_lock_in_place()
1236
601
branch.repository.leave_lock_in_place()
1238
603
response = request.execute('',
1239
604
branch_token+'xxx', repo_token)
1240
605
self.assertEqual(
1241
smart_req.SmartServerResponse(('TokenMismatch',)), response)
1243
branch.repository.lock_write(repo_token)
1244
branch.repository.dont_leave_lock_in_place()
1245
branch.repository.unlock()
1246
branch.lock_write(branch_token)
1247
branch.dont_leave_lock_in_place()
606
SmartServerResponse(('TokenMismatch',)), response)
1250
608
def test_lock_write_on_locked_repo(self):
1251
609
backing = self.get_transport()
1252
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
610
request = smart.branch.SmartServerBranchRequestLockWrite(backing)
1253
611
branch = self.make_branch('.', format='knit')
1254
repo = branch.repository
1255
repo_token = repo.lock_write().repository_token
1256
repo.leave_lock_in_place()
612
branch.repository.lock_write()
613
branch.repository.leave_lock_in_place()
614
branch.repository.unlock()
1258
615
response = request.execute('')
1259
616
self.assertEqual(
1260
smart_req.SmartServerResponse(('LockContention',)), response)
1262
repo.lock_write(repo_token)
1263
repo.dont_leave_lock_in_place()
617
SmartServerResponse(('LockContention',)), response)
1266
619
def test_lock_write_on_readonly_transport(self):
1267
620
backing = self.get_readonly_transport()
1268
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
621
request = smart.branch.SmartServerBranchRequestLockWrite(backing)
1269
622
branch = self.make_branch('.')
1270
623
root = self.get_transport().clone('/')
1271
624
path = urlutils.relative_url(root.base, self.get_transport().base)
1740
932
def test_unlock_on_unlocked_repo(self):
1741
933
backing = self.get_transport()
1742
request = smart_repo.SmartServerRepositoryUnlock(backing)
934
request = smart.repository.SmartServerRepositoryUnlock(backing)
1743
935
repository = self.make_repository('.', format='knit')
1744
936
response = request.execute('', 'some token')
1745
937
self.assertEqual(
1746
smart_req.SmartServerResponse(('TokenMismatch',)), response)
938
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', )),
1749
1046
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
1751
1048
def test_is_readonly_no(self):
1752
1049
backing = self.get_transport()
1753
request = smart_req.SmartServerIsReadonly(backing)
1050
request = smart.request.SmartServerIsReadonly(backing)
1754
1051
response = request.execute()
1755
1052
self.assertEqual(
1756
smart_req.SmartServerResponse(('no',)), response)
1053
SmartServerResponse(('no',)), response)
1758
1055
def test_is_readonly_yes(self):
1759
1056
backing = self.get_readonly_transport()
1760
request = smart_req.SmartServerIsReadonly(backing)
1057
request = smart.request.SmartServerIsReadonly(backing)
1761
1058
response = request.execute()
1762
1059
self.assertEqual(
1763
smart_req.SmartServerResponse(('yes',)), response)
1766
class TestSmartServerRepositorySetMakeWorkingTrees(
1767
tests.TestCaseWithMemoryTransport):
1769
def test_set_false(self):
1770
backing = self.get_transport()
1771
repo = self.make_repository('.', shared=True)
1772
repo.set_make_working_trees(True)
1773
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
1774
request = request_class(backing)
1775
self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
1776
request.execute('', 'False'))
1777
repo = repo.bzrdir.open_repository()
1778
self.assertFalse(repo.make_working_trees())
1780
def test_set_true(self):
1781
backing = self.get_transport()
1782
repo = self.make_repository('.', shared=True)
1783
repo.set_make_working_trees(False)
1784
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
1785
request = request_class(backing)
1786
self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
1787
request.execute('', 'True'))
1788
repo = repo.bzrdir.open_repository()
1789
self.assertTrue(repo.make_working_trees())
1792
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
1794
def make_repo_needing_autopacking(self, path='.'):
1795
# Make a repo in need of autopacking.
1796
tree = self.make_branch_and_tree('.', format='pack-0.92')
1797
repo = tree.branch.repository
1798
# monkey-patch the pack collection to disable autopacking
1799
repo._pack_collection._max_pack_count = lambda count: count
1801
tree.commit('commit %s' % x)
1802
self.assertEqual(10, len(repo._pack_collection.names()))
1803
del repo._pack_collection._max_pack_count
1806
def test_autopack_needed(self):
1807
repo = self.make_repo_needing_autopacking()
1809
self.addCleanup(repo.unlock)
1810
backing = self.get_transport()
1811
request = smart_packrepo.SmartServerPackRepositoryAutopack(
1813
response = request.execute('')
1814
self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
1815
repo._pack_collection.reload_pack_names()
1816
self.assertEqual(1, len(repo._pack_collection.names()))
1818
def test_autopack_not_needed(self):
1819
tree = self.make_branch_and_tree('.', format='pack-0.92')
1820
repo = tree.branch.repository
1822
self.addCleanup(repo.unlock)
1824
tree.commit('commit %s' % x)
1825
backing = self.get_transport()
1826
request = smart_packrepo.SmartServerPackRepositoryAutopack(
1828
response = request.execute('')
1829
self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
1830
repo._pack_collection.reload_pack_names()
1831
self.assertEqual(9, len(repo._pack_collection.names()))
1833
def test_autopack_on_nonpack_format(self):
1834
"""A request to autopack a non-pack repo is a no-op."""
1835
repo = self.make_repository('.', format='knit')
1836
backing = self.get_transport()
1837
request = smart_packrepo.SmartServerPackRepositoryAutopack(
1839
response = request.execute('')
1840
self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
1843
class TestSmartServerVfsGet(tests.TestCaseWithMemoryTransport):
1845
def test_unicode_path(self):
1846
"""VFS requests expect unicode paths to be escaped."""
1847
filename = u'foo\N{INTERROBANG}'
1848
filename_escaped = urlutils.escape(filename)
1849
backing = self.get_transport()
1850
request = vfs.GetRequest(backing)
1851
backing.put_bytes_non_atomic(filename_escaped, 'contents')
1852
self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'contents'),
1853
request.execute(filename_escaped))
1060
SmartServerResponse(('yes',)), response)
1856
1063
class TestHandlers(tests.TestCase):
1857
1064
"""Tests for the request.request_handlers object."""
1859
def test_all_registrations_exist(self):
1860
"""All registered request_handlers can be found."""
1861
# If there's a typo in a register_lazy call, this loop will fail with
1862
# an AttributeError.
1863
for key, item in smart_req.request_handlers.iteritems():
1866
def assertHandlerEqual(self, verb, handler):
1867
self.assertEqual(smart_req.request_handlers.get(verb), handler)
1869
1066
def test_registered_methods(self):
1870
1067
"""Test that known methods are registered to the correct object."""
1871
self.assertHandlerEqual('Branch.get_config_file',
1872
smart_branch.SmartServerBranchGetConfigFile)
1873
self.assertHandlerEqual('Branch.get_parent',
1874
smart_branch.SmartServerBranchGetParent)
1875
self.assertHandlerEqual('Branch.get_tags_bytes',
1876
smart_branch.SmartServerBranchGetTagsBytes)
1877
self.assertHandlerEqual('Branch.lock_write',
1878
smart_branch.SmartServerBranchRequestLockWrite)
1879
self.assertHandlerEqual('Branch.last_revision_info',
1880
smart_branch.SmartServerBranchRequestLastRevisionInfo)
1881
self.assertHandlerEqual('Branch.revision_history',
1882
smart_branch.SmartServerRequestRevisionHistory)
1883
self.assertHandlerEqual('Branch.set_config_option',
1884
smart_branch.SmartServerBranchRequestSetConfigOption)
1885
self.assertHandlerEqual('Branch.set_last_revision',
1886
smart_branch.SmartServerBranchRequestSetLastRevision)
1887
self.assertHandlerEqual('Branch.set_last_revision_info',
1888
smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
1889
self.assertHandlerEqual('Branch.set_last_revision_ex',
1890
smart_branch.SmartServerBranchRequestSetLastRevisionEx)
1891
self.assertHandlerEqual('Branch.set_parent_location',
1892
smart_branch.SmartServerBranchRequestSetParentLocation)
1893
self.assertHandlerEqual('Branch.unlock',
1894
smart_branch.SmartServerBranchRequestUnlock)
1895
self.assertHandlerEqual('BzrDir.find_repository',
1896
smart_dir.SmartServerRequestFindRepositoryV1)
1897
self.assertHandlerEqual('BzrDir.find_repositoryV2',
1898
smart_dir.SmartServerRequestFindRepositoryV2)
1899
self.assertHandlerEqual('BzrDirFormat.initialize',
1900
smart_dir.SmartServerRequestInitializeBzrDir)
1901
self.assertHandlerEqual('BzrDirFormat.initialize_ex_1.16',
1902
smart_dir.SmartServerRequestBzrDirInitializeEx)
1903
self.assertHandlerEqual('BzrDir.cloning_metadir',
1904
smart_dir.SmartServerBzrDirRequestCloningMetaDir)
1905
self.assertHandlerEqual('BzrDir.get_config_file',
1906
smart_dir.SmartServerBzrDirRequestConfigFile)
1907
self.assertHandlerEqual('BzrDir.open_branch',
1908
smart_dir.SmartServerRequestOpenBranch)
1909
self.assertHandlerEqual('BzrDir.open_branchV2',
1910
smart_dir.SmartServerRequestOpenBranchV2)
1911
self.assertHandlerEqual('BzrDir.open_branchV3',
1912
smart_dir.SmartServerRequestOpenBranchV3)
1913
self.assertHandlerEqual('PackRepository.autopack',
1914
smart_packrepo.SmartServerPackRepositoryAutopack)
1915
self.assertHandlerEqual('Repository.gather_stats',
1916
smart_repo.SmartServerRepositoryGatherStats)
1917
self.assertHandlerEqual('Repository.get_parent_map',
1918
smart_repo.SmartServerRepositoryGetParentMap)
1919
self.assertHandlerEqual('Repository.get_rev_id_for_revno',
1920
smart_repo.SmartServerRepositoryGetRevIdForRevno)
1921
self.assertHandlerEqual('Repository.get_revision_graph',
1922
smart_repo.SmartServerRepositoryGetRevisionGraph)
1923
self.assertHandlerEqual('Repository.get_stream',
1924
smart_repo.SmartServerRepositoryGetStream)
1925
self.assertHandlerEqual('Repository.get_stream_1.19',
1926
smart_repo.SmartServerRepositoryGetStream_1_19)
1927
self.assertHandlerEqual('Repository.has_revision',
1928
smart_repo.SmartServerRequestHasRevision)
1929
self.assertHandlerEqual('Repository.insert_stream',
1930
smart_repo.SmartServerRepositoryInsertStream)
1931
self.assertHandlerEqual('Repository.insert_stream_locked',
1932
smart_repo.SmartServerRepositoryInsertStreamLocked)
1933
self.assertHandlerEqual('Repository.is_shared',
1934
smart_repo.SmartServerRepositoryIsShared)
1935
self.assertHandlerEqual('Repository.lock_write',
1936
smart_repo.SmartServerRepositoryLockWrite)
1937
self.assertHandlerEqual('Repository.tarball',
1938
smart_repo.SmartServerRepositoryTarball)
1939
self.assertHandlerEqual('Repository.unlock',
1940
smart_repo.SmartServerRepositoryUnlock)
1941
self.assertHandlerEqual('Transport.is_readonly',
1942
smart_req.SmartServerIsReadonly)
1945
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
1946
"""Tests for SmartTCPServer hooks."""
1949
super(SmartTCPServerHookTests, self).setUp()
1950
self.server = server.SmartTCPServer(self.get_transport())
1952
def test_run_server_started_hooks(self):
1953
"""Test the server started hooks get fired properly."""
1955
server.SmartTCPServer.hooks.install_named_hook('server_started',
1956
lambda backing_urls, url: started_calls.append((backing_urls, url)),
1958
started_ex_calls = []
1959
server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
1960
lambda backing_urls, url: started_ex_calls.append((backing_urls, url)),
1962
self.server._sockname = ('example.com', 42)
1963
self.server.run_server_started_hooks()
1964
self.assertEquals(started_calls,
1965
[([self.get_transport().base], 'bzr://example.com:42/')])
1966
self.assertEquals(started_ex_calls,
1967
[([self.get_transport().base], self.server)])
1969
def test_run_server_started_hooks_ipv6(self):
1970
"""Test that socknames can contain 4-tuples."""
1971
self.server._sockname = ('::', 42, 0, 0)
1973
server.SmartTCPServer.hooks.install_named_hook('server_started',
1974
lambda backing_urls, url: started_calls.append((backing_urls, url)),
1976
self.server.run_server_started_hooks()
1977
self.assertEquals(started_calls,
1978
[([self.get_transport().base], 'bzr://:::42/')])
1980
def test_run_server_stopped_hooks(self):
1981
"""Test the server stopped hooks."""
1982
self.server._sockname = ('example.com', 42)
1984
server.SmartTCPServer.hooks.install_named_hook('server_stopped',
1985
lambda backing_urls, url: stopped_calls.append((backing_urls, url)),
1987
self.server.run_server_stopped_hooks()
1988
self.assertEquals(stopped_calls,
1989
[([self.get_transport().base], 'bzr://example.com:42/')])
1069
smart.request.request_handlers.get('Branch.get_config_file'),
1070
smart.branch.SmartServerBranchGetConfigFile)
1072
smart.request.request_handlers.get('Branch.lock_write'),
1073
smart.branch.SmartServerBranchRequestLockWrite)
1075
smart.request.request_handlers.get('Branch.last_revision_info'),
1076
smart.branch.SmartServerBranchRequestLastRevisionInfo)
1078
smart.request.request_handlers.get('Branch.revision_history'),
1079
smart.branch.SmartServerRequestRevisionHistory)
1081
smart.request.request_handlers.get('Branch.set_last_revision'),
1082
smart.branch.SmartServerBranchRequestSetLastRevision)
1084
smart.request.request_handlers.get('Branch.set_last_revision_info'),
1085
smart.branch.SmartServerBranchRequestSetLastRevisionInfo)
1087
smart.request.request_handlers.get('Branch.unlock'),
1088
smart.branch.SmartServerBranchRequestUnlock)
1090
smart.request.request_handlers.get('BzrDir.find_repository'),
1091
smart.bzrdir.SmartServerRequestFindRepositoryV1)
1093
smart.request.request_handlers.get('BzrDir.find_repositoryV2'),
1094
smart.bzrdir.SmartServerRequestFindRepositoryV2)
1096
smart.request.request_handlers.get('BzrDirFormat.initialize'),
1097
smart.bzrdir.SmartServerRequestInitializeBzrDir)
1099
smart.request.request_handlers.get('BzrDir.open_branch'),
1100
smart.bzrdir.SmartServerRequestOpenBranch)
1102
smart.request.request_handlers.get('Repository.gather_stats'),
1103
smart.repository.SmartServerRepositoryGatherStats)
1105
smart.request.request_handlers.get('Repository.get_parent_map'),
1106
smart.repository.SmartServerRepositoryGetParentMap)
1108
smart.request.request_handlers.get(
1109
'Repository.get_revision_graph'),
1110
smart.repository.SmartServerRepositoryGetRevisionGraph)
1112
smart.request.request_handlers.get('Repository.has_revision'),
1113
smart.repository.SmartServerRequestHasRevision)
1115
smart.request.request_handlers.get('Repository.is_shared'),
1116
smart.repository.SmartServerRepositoryIsShared)
1118
smart.request.request_handlers.get('Repository.lock_write'),
1119
smart.repository.SmartServerRepositoryLockWrite)
1121
smart.request.request_handlers.get('Repository.tarball'),
1122
smart.repository.SmartServerRepositoryTarball)
1124
smart.request.request_handlers.get('Repository.unlock'),
1125
smart.repository.SmartServerRepositoryUnlock)
1127
smart.request.request_handlers.get('Transport.is_readonly'),
1128
smart.request.SmartServerIsReadonly)