~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Robert Collins
  • Date: 2007-10-15 05:23:29 UTC
  • mfrom: (2906 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2907.
  • Revision ID: robertc@robertcollins.net-20071015052329-z5458xq9q2kq72mv
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from bzrlib import (
29
29
    bzrdir,
30
30
    errors,
 
31
    pack,
31
32
    remote,
32
33
    repository,
33
34
    tests,
742
743
        finally:
743
744
            tarball_file.close()
744
745
 
745
 
    def test_sprout_uses_tarball(self):
746
 
        # RemoteRepository.sprout should try to use the
747
 
        # tarball command rather than accessing all the files
748
 
        transport_path = 'srcrepo'
749
 
        expected_responses = [(('ok',), self.tarball_content),
750
 
            ]
751
 
        expected_calls = [('call2', 'Repository.tarball', ('///srcrepo/', 'bz2',),),
752
 
            ]
753
 
        remote_repo, client = self.setup_fake_client_and_repository(
754
 
            expected_responses, transport_path)
755
 
        # make a regular local repository to receive the results
756
 
        dest_transport = MemoryTransport()
757
 
        dest_transport.mkdir('destrepo')
758
 
        bzrdir_format = bzrdir.format_registry.make_bzrdir('default')
759
 
        dest_bzrdir = bzrdir_format.initialize_on_transport(dest_transport)
760
 
        # try to copy...
761
 
        remote_repo.sprout(dest_bzrdir)
762
 
 
763
 
    def test_backwards_compatibility(self):
764
 
        """If the server doesn't recognise this request, fallback to VFS.
765
 
        
766
 
        This happens when a current client talks to an older server that
767
 
        doesn't implement 'Repository.tarball'.
768
 
        """
769
 
        # Make a regular local repository to receive the results
770
 
        dest_transport = MemoryTransport()
771
 
        dest_transport.mkdir('destrepo')
772
 
        bzrdir_format = bzrdir.format_registry.make_bzrdir('default')
773
 
        dest_bzrdir = bzrdir_format.initialize_on_transport(dest_transport)
774
 
 
775
 
        error_msg = (
776
 
            "Generic bzr smart protocol error: "
777
 
            "bad request 'Repository.tarball'")
778
 
        responses = [(('error', error_msg), '')]
779
 
        remote_repo, client = self.setup_fake_client_and_repository(
780
 
            responses, 'path')
781
 
        mock_real_repo = MockRealRepository()
782
 
        remote_repo._real_repository = mock_real_repo
783
 
 
784
 
        # try to copy...
785
 
        remote_repo.sprout(dest_bzrdir)
786
 
 
787
 
        self.assertEqual([('sprout', dest_bzrdir, None)], mock_real_repo.calls,
788
 
            "RemoteRepository didn't fallback to the real repository correctly")
789
 
        self.failIf(client.expecting_body,
790
 
            "The protocol has been left in an unclean state that will cause "
791
 
            "TooManyConcurrentRequests errors.")
792
 
 
793
 
 
794
 
class MockRealRepository(object):
795
 
    """Mock of a RemoteRepository's '_real_repository' attribute.
796
 
    
797
 
    Used by TestRepositoryTarball.test_backwards_compatibility.
798
 
    """
799
 
 
800
 
    def __init__(self):
801
 
        self.calls = []
802
 
 
803
 
    def sprout(self, to_bzrdir, revision_id=None):
804
 
        self.calls.append(('sprout', to_bzrdir, revision_id))
805
 
 
806
746
 
807
747
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
808
748
    """RemoteRepository.copy_content_into optimizations"""
821
761
        self.assertFalse(isinstance(dest_repo, RemoteRepository))
822
762
        self.assertTrue(isinstance(src_repo, RemoteRepository))
823
763
        src_repo.copy_content_into(dest_repo)
 
764
 
 
765
 
 
766
class TestRepositoryStreamKnitData(TestRemoteRepository):
 
767
 
 
768
    def make_pack_file(self, records):
 
769
        pack_file = StringIO()
 
770
        pack_writer = pack.ContainerWriter(pack_file.write)
 
771
        pack_writer.begin()
 
772
        for bytes, names in records:
 
773
            pack_writer.add_bytes_record(bytes, names)
 
774
        pack_writer.end()
 
775
        pack_file.seek(0)
 
776
        return pack_file
 
777
 
 
778
    def test_bad_pack_from_server(self):
 
779
        """A response with invalid data (e.g. it has a record with multiple
 
780
        names) triggers an exception.
 
781
        
 
782
        Not all possible errors will be caught at this stage, but obviously
 
783
        malformed data should be.
 
784
        """
 
785
        record = ('bytes', [('name1',), ('name2',)])
 
786
        pack_file = self.make_pack_file([record])
 
787
        responses = [(('ok',), pack_file.getvalue()), ]
 
788
        transport_path = 'quack'
 
789
        repo, client = self.setup_fake_client_and_repository(
 
790
            responses, transport_path)
 
791
        stream = repo.get_data_stream(['revid'])
 
792
        self.assertRaises(errors.SmartProtocolError, list, stream)
 
793
    
 
794
    def test_backwards_compatibility(self):
 
795
        """If the server doesn't recognise this request, fallback to VFS."""
 
796
        error_msg = (
 
797
            "Generic bzr smart protocol error: "
 
798
            "bad request 'Repository.stream_knit_data_for_revisions'")
 
799
        responses = [
 
800
            (('error', error_msg), '')]
 
801
        repo, client = self.setup_fake_client_and_repository(
 
802
            responses, 'path')
 
803
        self.mock_called = False
 
804
        repo._real_repository = MockRealRepository(self)
 
805
        repo.get_data_stream(['revid'])
 
806
        self.assertTrue(self.mock_called)
 
807
        self.failIf(client.expecting_body,
 
808
            "The protocol has been left in an unclean state that will cause "
 
809
            "TooManyConcurrentRequests errors.")
 
810
 
 
811
 
 
812
class MockRealRepository(object):
 
813
    """Helper class for TestRepositoryStreamKnitData.test_unknown_method."""
 
814
 
 
815
    def __init__(self, test):
 
816
        self.test = test
 
817
 
 
818
    def get_data_stream(self, revision_ids):
 
819
        self.test.assertEqual(['revid'], revision_ids)
 
820
        self.test.mock_called = True
 
821
 
 
822