69
68
def test_get_transport_modules(self):
70
69
handlers = _get_protocol_handlers()
70
# don't pollute the current handlers
71
_clear_protocol_handlers()
71
72
class SampleHandler(object):
72
73
"""I exist, isnt that enough?"""
74
75
_clear_protocol_handlers()
75
76
register_transport_proto('foo')
76
register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
77
register_lazy_transport('foo', 'bzrlib.tests.test_transport',
78
'TestTransport.SampleHandler')
77
79
register_transport_proto('bar')
78
register_lazy_transport('bar', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
79
self.assertEqual([SampleHandler.__module__, 'bzrlib.transport.chroot'],
80
register_lazy_transport('bar', 'bzrlib.tests.test_transport',
81
'TestTransport.SampleHandler')
82
self.assertEqual([SampleHandler.__module__,
83
'bzrlib.transport.chroot'],
80
84
_get_transport_modules())
82
86
_set_protocol_handlers(handlers)
192
210
], [(10, 10), (30, 10), (100, 10)],
213
def test_coalesce_max_size(self):
214
self.check([(10, 20, [(0, 10), (10, 10)]),
216
# If one range is above max_size, it gets its own coalesced
218
(100, 80, [(0, 80),]),],
219
[(10, 10), (20, 10), (30, 50), (100, 80)],
223
def test_coalesce_no_max_size(self):
224
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
225
[(10, 10), (20, 10), (30, 50), (80, 100)],
197
229
class TestMemoryTransport(TestCase):
540
567
self._server.setUp()
541
568
self.addCleanup(self._server.tearDown)
543
def get_transport(self):
544
"""Return a connected transport to the local directory."""
570
def get_transport(self, relpath=None):
571
"""Return a connected transport to the local directory.
573
:param relpath: a path relative to the base url.
545
575
base_url = self._server.get_url()
576
url = self._adjust_url(base_url, relpath)
546
577
# try getting the transport via the regular interface:
547
t = get_transport(base_url)
578
t = get_transport(url)
579
# vila--20070607 if the following are commented out the test suite
580
# still pass. Is this really still needed or was it a forgotten
548
582
if not isinstance(t, self.transport_class):
549
583
# we did not get the correct transport class type. Override the
550
584
# regular connection behaviour by direct construction.
551
t = self.transport_class(base_url)
585
t = self.transport_class(url)
555
589
class TestLocalTransports(TestCase):
557
591
def test_get_transport_from_abspath(self):
558
here = os.path.abspath('.')
592
here = osutils.abspath('.')
559
593
t = get_transport(here)
560
594
self.assertIsInstance(t, LocalTransport)
561
595
self.assertEquals(t.base, urlutils.local_path_to_url(here) + '/')
563
597
def test_get_transport_from_relpath(self):
564
here = os.path.abspath('.')
598
here = osutils.abspath('.')
565
599
t = get_transport('.')
566
600
self.assertIsInstance(t, LocalTransport)
567
601
self.assertEquals(t.base, urlutils.local_path_to_url('.') + '/')
569
603
def test_get_transport_from_local_url(self):
570
here = os.path.abspath('.')
604
here = osutils.abspath('.')
571
605
here_url = urlutils.local_path_to_url(here) + '/'
572
606
t = get_transport(here_url)
573
607
self.assertIsInstance(t, LocalTransport)
574
608
self.assertEquals(t.base, here_url)
576
610
def test_local_abspath(self):
577
here = os.path.abspath('.')
611
here = osutils.abspath('.')
578
612
t = get_transport(here)
579
613
self.assertEquals(t.local_abspath(''), here)
594
628
self.assertEquals(t.base, 'file://HOST/')
597
def get_test_permutations():
598
"""Return transport permutations to be used in testing.
600
This module registers some transports, but they're only for testing
601
registration. We don't really want to run all the transport tests against
631
class TestConnectedTransport(TestCase):
632
"""Tests for connected to remote server transports"""
634
def test_parse_url(self):
635
t = ConnectedTransport('http://simple.example.com/home/source')
636
self.assertEquals(t._host, 'simple.example.com')
637
self.assertEquals(t._port, None)
638
self.assertEquals(t._path, '/home/source/')
639
self.failUnless(t._user is None)
640
self.failUnless(t._password is None)
642
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
644
def test_parse_quoted_url(self):
645
t = ConnectedTransport('http://ro%62ey:h%40t@ex%41mple.com:2222/path')
646
self.assertEquals(t._host, 'exAmple.com')
647
self.assertEquals(t._port, 2222)
648
self.assertEquals(t._user, 'robey')
649
self.assertEquals(t._password, 'h@t')
650
self.assertEquals(t._path, '/path/')
652
# Base should not keep track of the password
653
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
655
def test_parse_invalid_url(self):
656
self.assertRaises(errors.InvalidURL,
658
'sftp://lily.org:~janneke/public/bzr/gub')
660
def test_relpath(self):
661
t = ConnectedTransport('sftp://user@host.com/abs/path')
663
self.assertEquals(t.relpath('sftp://user@host.com/abs/path/sub'), 'sub')
664
self.assertRaises(errors.PathNotChild, t.relpath,
665
'http://user@host.com/abs/path/sub')
666
self.assertRaises(errors.PathNotChild, t.relpath,
667
'sftp://user2@host.com/abs/path/sub')
668
self.assertRaises(errors.PathNotChild, t.relpath,
669
'sftp://user@otherhost.com/abs/path/sub')
670
self.assertRaises(errors.PathNotChild, t.relpath,
671
'sftp://user@host.com:33/abs/path/sub')
672
# Make sure it works when we don't supply a username
673
t = ConnectedTransport('sftp://host.com/abs/path')
674
self.assertEquals(t.relpath('sftp://host.com/abs/path/sub'), 'sub')
676
# Make sure it works when parts of the path will be url encoded
677
t = ConnectedTransport('sftp://host.com/dev/%path')
678
self.assertEquals(t.relpath('sftp://host.com/dev/%path/sub'), 'sub')
680
def test_connection_sharing_propagate_credentials(self):
681
t = ConnectedTransport('ftp://user@host.com/abs/path')
682
self.assertEquals('user', t._user)
683
self.assertEquals('host.com', t._host)
684
self.assertIs(None, t._get_connection())
685
self.assertIs(None, t._password)
686
c = t.clone('subdir')
687
self.assertIs(None, c._get_connection())
688
self.assertIs(None, t._password)
690
# Simulate the user entering a password
692
connection = object()
693
t._set_connection(connection, password)
694
self.assertIs(connection, t._get_connection())
695
self.assertIs(password, t._get_credentials())
696
self.assertIs(connection, c._get_connection())
697
self.assertIs(password, c._get_credentials())
699
# credentials can be updated
700
new_password = 'even more secret'
701
c._update_credentials(new_password)
702
self.assertIs(connection, t._get_connection())
703
self.assertIs(new_password, t._get_credentials())
704
self.assertIs(connection, c._get_connection())
705
self.assertIs(new_password, c._get_credentials())
708
class TestReusedTransports(TestCase):
709
"""Tests for transport reuse"""
711
def test_reuse_same_transport(self):
712
possible_transports = []
713
t1 = get_transport('http://foo/',
714
possible_transports=possible_transports)
715
self.assertEqual([t1], possible_transports)
716
t2 = get_transport('http://foo/', possible_transports=[t1])
717
self.assertIs(t1, t2)
719
# Also check that final '/' are handled correctly
720
t3 = get_transport('http://foo/path/')
721
t4 = get_transport('http://foo/path', possible_transports=[t3])
722
self.assertIs(t3, t4)
724
t5 = get_transport('http://foo/path')
725
t6 = get_transport('http://foo/path/', possible_transports=[t5])
726
self.assertIs(t5, t6)
728
def test_don_t_reuse_different_transport(self):
729
t1 = get_transport('http://foo/path')
730
t2 = get_transport('http://bar/path', possible_transports=[t1])
731
self.assertIsNot(t1, t2)
734
class TestTransportTrace(TestCase):
737
transport = get_transport('trace+memory://')
738
self.assertIsInstance(
739
transport, bzrlib.transport.trace.TransportTraceDecorator)
741
def test_clone_preserves_activity(self):
742
transport = get_transport('trace+memory://')
743
transport2 = transport.clone('.')
744
self.assertTrue(transport is not transport2)
745
self.assertTrue(transport._activity is transport2._activity)
747
# the following specific tests are for the operations that have made use of
748
# logging in tests; we could test every single operation but doing that
749
# still won't cause a test failure when the top level Transport API
750
# changes; so there is little return doing that.
752
transport = get_transport('trace+memory:///')
753
transport.put_bytes('foo', 'barish')
756
# put_bytes records the bytes, not the content to avoid memory
758
expected_result.append(('put_bytes', 'foo', 6, None))
759
# get records the file name only.
760
expected_result.append(('get', 'foo'))
761
self.assertEqual(expected_result, transport._activity)
763
def test_readv(self):
764
transport = get_transport('trace+memory:///')
765
transport.put_bytes('foo', 'barish')
766
list(transport.readv('foo', [(0, 1), (3, 2)], adjust_for_latency=True,
769
# put_bytes records the bytes, not the content to avoid memory
771
expected_result.append(('put_bytes', 'foo', 6, None))
772
# readv records the supplied offset request
773
expected_result.append(('readv', 'foo', [(0, 1), (3, 2)], True, 6))
774
self.assertEqual(expected_result, transport._activity)