48
55
class TestTransport(tests.TestCase):
49
56
"""Test the non transport-concrete class functionality."""
51
# FIXME: These tests should use addCleanup() and/or overrideAttr() instead
52
# of try/finally -- vila 20100205
54
58
def test__get_set_protocol_handlers(self):
55
59
handlers = transport._get_protocol_handlers()
56
self.assertNotEqual([], handlers.keys( ))
58
transport._clear_protocol_handlers()
59
self.assertEqual([], transport._get_protocol_handlers().keys())
61
transport._set_protocol_handlers(handlers)
60
self.assertNotEqual([], handlers.keys())
61
transport._clear_protocol_handlers()
62
self.addCleanup(transport._set_protocol_handlers, handlers)
63
self.assertEqual([], transport._get_protocol_handlers().keys())
63
65
def test_get_transport_modules(self):
64
66
handlers = transport._get_protocol_handlers()
67
self.addCleanup(transport._set_protocol_handlers, handlers)
65
68
# don't pollute the current handlers
66
69
transport._clear_protocol_handlers()
67
71
class SampleHandler(object):
68
72
"""I exist, isnt that enough?"""
70
transport._clear_protocol_handlers()
71
transport.register_transport_proto('foo')
72
transport.register_lazy_transport('foo',
73
'bzrlib.tests.test_transport',
74
'TestTransport.SampleHandler')
75
transport.register_transport_proto('bar')
76
transport.register_lazy_transport('bar',
77
'bzrlib.tests.test_transport',
78
'TestTransport.SampleHandler')
79
self.assertEqual([SampleHandler.__module__,
80
'bzrlib.transport.chroot',
81
'bzrlib.transport.pathfilter'],
82
transport._get_transport_modules())
84
transport._set_protocol_handlers(handlers)
73
transport._clear_protocol_handlers()
74
transport.register_transport_proto('foo')
75
transport.register_lazy_transport('foo',
76
'breezy.tests.test_transport',
77
'TestTransport.SampleHandler')
78
transport.register_transport_proto('bar')
79
transport.register_lazy_transport('bar',
80
'breezy.tests.test_transport',
81
'TestTransport.SampleHandler')
82
self.assertEqual([SampleHandler.__module__,
83
'breezy.transport.chroot',
84
'breezy.transport.pathfilter'],
85
transport._get_transport_modules())
86
87
def test_transport_dependency(self):
87
88
"""Transport with missing dependency causes no error"""
88
89
saved_handlers = transport._get_protocol_handlers()
90
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
89
91
# don't pollute the current handlers
90
92
transport._clear_protocol_handlers()
93
transport.register_transport_proto('foo')
94
transport.register_lazy_transport(
95
'foo', 'breezy.tests.test_transport', 'BadTransportHandler')
92
transport.register_transport_proto('foo')
93
transport.register_lazy_transport(
94
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
96
transport.get_transport('foo://fooserver/foo')
97
except errors.UnsupportedProtocol, e:
99
self.assertEquals('Unsupported protocol'
100
' for url "foo://fooserver/foo":'
101
' Unable to import library "some_lib":'
102
' testing missing dependency', str(e))
104
self.fail('Did not raise UnsupportedProtocol')
106
# restore original values
107
transport._set_protocol_handlers(saved_handlers)
97
transport.get_transport_from_url('foo://fooserver/foo')
98
except errors.UnsupportedProtocol as e:
100
self.assertEqual('Unsupported protocol'
101
' for url "foo://fooserver/foo":'
102
' Unable to import library "some_lib":'
103
' testing missing dependency', str(e))
105
self.fail('Did not raise UnsupportedProtocol')
109
107
def test_transport_fallback(self):
110
108
"""Transport with missing dependency causes no error"""
111
109
saved_handlers = transport._get_protocol_handlers()
113
transport._clear_protocol_handlers()
114
transport.register_transport_proto('foo')
115
transport.register_lazy_transport(
116
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
117
transport.register_lazy_transport(
118
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
119
t = transport.get_transport('foo://fooserver/foo')
120
self.assertTrue(isinstance(t, BackupTransportHandler))
122
transport._set_protocol_handlers(saved_handlers)
110
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
111
transport._clear_protocol_handlers()
112
transport.register_transport_proto('foo')
113
transport.register_lazy_transport(
114
'foo', 'breezy.tests.test_transport', 'BackupTransportHandler')
115
transport.register_lazy_transport(
116
'foo', 'breezy.tests.test_transport', 'BadTransportHandler')
117
t = transport.get_transport_from_url('foo://fooserver/foo')
118
self.assertTrue(isinstance(t, BackupTransportHandler))
124
120
def test_ssh_hints(self):
125
121
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
127
transport.get_transport('ssh://fooserver/foo')
128
except errors.UnsupportedProtocol, e:
123
transport.get_transport_from_url('ssh://fooserver/foo')
124
except errors.UnsupportedProtocol as e:
130
self.assertEquals('Unsupported protocol'
126
self.assertEqual('Unsupported protocol'
131
127
' for url "ssh://fooserver/foo":'
132
128
' bzr supports bzr+ssh to operate over ssh,'
133
129
' use "bzr+ssh://fooserver/foo".',
219
204
def test_coalesce_fudge(self):
220
205
self.check([(10, 30, [(0, 10), (20, 10)]),
221
(100, 10, [(0, 10),]),
206
(100, 10, [(0, 10)]),
222
207
], [(10, 10), (30, 10), (100, 10)],
225
210
def test_coalesce_max_size(self):
226
211
self.check([(10, 20, [(0, 10), (10, 10)]),
227
212
(30, 50, [(0, 50)]),
228
213
# If one range is above max_size, it gets its own coalesced
230
(100, 80, [(0, 80),]),],
215
(100, 80, [(0, 80)]),],
231
216
[(10, 10), (20, 10), (30, 50), (100, 80)],
235
219
def test_coalesce_no_max_size(self):
236
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
220
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
237
221
[(10, 10), (20, 10), (30, 50), (80, 100)],
240
224
def test_coalesce_default_limit(self):
241
225
# By default we use a 100MB max size.
242
ten_mb = 10*1024*1024
243
self.check([(0, 10*ten_mb, [(i*ten_mb, ten_mb) for i in range(10)]),
226
ten_mb = 10 * 1024 * 1024
227
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
244
228
(10*ten_mb, ten_mb, [(0, ten_mb)])],
245
229
[(i*ten_mb, ten_mb) for i in range(11)])
246
self.check([(0, 11*ten_mb, [(i*ten_mb, ten_mb) for i in range(11)]),],
247
[(i*ten_mb, ten_mb) for i in range(11)],
230
self.check([(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
231
[(i * ten_mb, ten_mb) for i in range(11)],
248
232
max_size=1*1024*1024*1024)
458
441
backing_transport = memory.MemoryTransport()
459
442
server = chroot.ChrootServer(backing_transport)
460
443
server.start_server()
462
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
444
self.addCleanup(server.stop_server)
445
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
448
class TestHooks(tests.TestCase):
449
"""Basic tests for transport hooks"""
451
def _get_connected_transport(self):
452
return transport.ConnectedTransport("bogus:nowhere")
454
def test_transporthooks_initialisation(self):
455
"""Check all expected transport hook points are set up"""
456
hookpoint = transport.TransportHooks()
457
self.assertTrue("post_connect" in hookpoint,
458
"post_connect not in %s" % (hookpoint,))
460
def test_post_connect(self):
461
"""Ensure the post_connect hook is called when _set_transport is"""
463
transport.Transport.hooks.install_named_hook("post_connect",
465
t = self._get_connected_transport()
466
self.assertLength(0, calls)
467
t._set_connection("connection", "auth")
468
self.assertEqual(calls, [t])
467
471
class PathFilteringDecoratorTransportTest(tests.TestCase):
703
class TestTransportFromPath(tests.TestCaseInTempDir):
705
def test_with_path(self):
706
t = transport.get_transport_from_path(self.test_dir)
707
self.assertIsInstance(t, local.LocalTransport)
708
self.assertEqual(t.base.rstrip("/"),
709
urlutils.local_path_to_url(self.test_dir))
711
def test_with_url(self):
712
t = transport.get_transport_from_path("file:")
713
self.assertIsInstance(t, local.LocalTransport)
714
self.assertEqual(t.base.rstrip("/"),
715
urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
718
class TestTransportFromUrl(tests.TestCaseInTempDir):
720
def test_with_path(self):
721
self.assertRaises(errors.InvalidURL, transport.get_transport_from_url,
724
def test_with_url(self):
725
url = urlutils.local_path_to_url(self.test_dir)
726
t = transport.get_transport_from_url(url)
727
self.assertIsInstance(t, local.LocalTransport)
728
self.assertEqual(t.base.rstrip("/"), url)
730
def test_with_url_and_segment_parameters(self):
731
url = urlutils.local_path_to_url(self.test_dir)+",branch=foo"
732
t = transport.get_transport_from_url(url)
733
self.assertIsInstance(t, local.LocalTransport)
734
self.assertEqual(t.base.rstrip("/"), url)
735
with open(os.path.join(self.test_dir, "afile"), 'w') as f:
737
self.assertTrue(t.has("afile"))
698
740
class TestLocalTransports(tests.TestCase):
700
742
def test_get_transport_from_abspath(self):
701
743
here = osutils.abspath('.')
702
744
t = transport.get_transport(here)
703
745
self.assertIsInstance(t, local.LocalTransport)
704
self.assertEquals(t.base, urlutils.local_path_to_url(here) + '/')
746
self.assertEqual(t.base, urlutils.local_path_to_url(here) + '/')
706
748
def test_get_transport_from_relpath(self):
707
749
here = osutils.abspath('.')
708
750
t = transport.get_transport('.')
709
751
self.assertIsInstance(t, local.LocalTransport)
710
self.assertEquals(t.base, urlutils.local_path_to_url('.') + '/')
752
self.assertEqual(t.base, urlutils.local_path_to_url('.') + '/')
712
754
def test_get_transport_from_local_url(self):
713
755
here = osutils.abspath('.')
714
756
here_url = urlutils.local_path_to_url(here) + '/'
715
757
t = transport.get_transport(here_url)
716
758
self.assertIsInstance(t, local.LocalTransport)
717
self.assertEquals(t.base, here_url)
759
self.assertEqual(t.base, here_url)
719
761
def test_local_abspath(self):
720
762
here = osutils.abspath('.')
721
763
t = transport.get_transport(here)
722
self.assertEquals(t.local_abspath(''), here)
764
self.assertEqual(t.local_abspath(''), here)
767
class TestLocalTransportMutation(tests.TestCaseInTempDir):
769
def test_local_transport_mkdir(self):
770
here = osutils.abspath('.')
771
t = transport.get_transport(here)
773
self.assertTrue(os.path.exists('test'))
775
def test_local_transport_mkdir_permission_denied(self):
776
# See https://bugs.launchpad.net/bzr/+bug/606537
777
here = osutils.abspath('.')
778
t = transport.get_transport(here)
779
def fake_chmod(path, mode):
780
e = OSError('permission denied')
781
e.errno = errno.EPERM
783
self.overrideAttr(os, 'chmod', fake_chmod)
785
t.mkdir('test2', mode=0o707)
786
self.assertTrue(os.path.exists('test'))
787
self.assertTrue(os.path.exists('test2'))
790
class TestLocalTransportWriteStream(tests.TestCaseWithTransport):
792
def test_local_fdatasync_calls_fdatasync(self):
793
"""Check fdatasync on a stream tries to flush the data to the OS.
795
We can't easily observe the external effect but we can at least see
799
fdatasync = getattr(os, 'fdatasync', sentinel)
800
if fdatasync is sentinel:
801
raise tests.TestNotApplicable('fdatasync not supported')
802
t = self.get_transport('.')
803
calls = self.recordCalls(os, 'fdatasync')
804
w = t.open_write_stream('out')
807
with open('out', 'rb') as f:
808
# Should have been flushed.
809
self.assertEqual(f.read(), 'foo')
810
self.assertEqual(len(calls), 1, calls)
812
def test_missing_directory(self):
813
t = self.get_transport('.')
814
self.assertRaises(errors.NoSuchFile, t.open_write_stream, 'dir/foo')
725
817
class TestWin32LocalTransport(tests.TestCase):
727
819
def test_unc_clone_to_root(self):
820
self.requireFeature(features.win32_feature)
728
821
# Win32 UNC path like \\HOST\path
729
822
# clone to root should stop at least at \\HOST part
731
824
t = local.EmulatedWin32LocalTransport('file://HOST/path/to/some/dir/')
733
826
t = t.clone('..')
734
self.assertEquals(t.base, 'file://HOST/')
827
self.assertEqual(t.base, 'file://HOST/')
735
828
# make sure we reach the root
736
829
t = t.clone('..')
737
self.assertEquals(t.base, 'file://HOST/')
830
self.assertEqual(t.base, 'file://HOST/')
740
833
class TestConnectedTransport(tests.TestCase):
743
836
def test_parse_url(self):
744
837
t = transport.ConnectedTransport(
745
838
'http://simple.example.com/home/source')
746
self.assertEquals(t._host, 'simple.example.com')
747
self.assertEquals(t._port, None)
748
self.assertEquals(t._path, '/home/source/')
749
self.failUnless(t._user is None)
750
self.failUnless(t._password is None)
839
self.assertEqual(t._parsed_url.host, 'simple.example.com')
840
self.assertEqual(t._parsed_url.port, None)
841
self.assertEqual(t._parsed_url.path, '/home/source/')
842
self.assertTrue(t._parsed_url.user is None)
843
self.assertTrue(t._parsed_url.password is None)
752
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
845
self.assertEqual(t.base, 'http://simple.example.com/home/source/')
754
847
def test_parse_url_with_at_in_user(self):
756
849
t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
757
self.assertEquals(t._user, 'user@host.com')
850
self.assertEqual(t._parsed_url.user, 'user@host.com')
759
852
def test_parse_quoted_url(self):
760
853
t = transport.ConnectedTransport(
761
854
'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
762
self.assertEquals(t._host, 'exAmple.com')
763
self.assertEquals(t._port, 2222)
764
self.assertEquals(t._user, 'robey')
765
self.assertEquals(t._password, 'h@t')
766
self.assertEquals(t._path, '/path/')
855
self.assertEqual(t._parsed_url.host, 'exAmple.com')
856
self.assertEqual(t._parsed_url.port, 2222)
857
self.assertEqual(t._parsed_url.user, 'robey')
858
self.assertEqual(t._parsed_url.password, 'h@t')
859
self.assertEqual(t._parsed_url.path, '/path/')
768
861
# Base should not keep track of the password
769
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
862
self.assertEqual(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
771
864
def test_parse_invalid_url(self):
772
865
self.assertRaises(errors.InvalidURL,
787
881
'sftp://user@host.com:33/abs/path/sub')
788
882
# Make sure it works when we don't supply a username
789
883
t = transport.ConnectedTransport('sftp://host.com/abs/path')
790
self.assertEquals(t.relpath('sftp://host.com/abs/path/sub'), 'sub')
884
self.assertEqual(t.relpath('sftp://host.com/abs/path/sub'), 'sub')
792
886
# Make sure it works when parts of the path will be url encoded
793
887
t = transport.ConnectedTransport('sftp://host.com/dev/%path')
794
self.assertEquals(t.relpath('sftp://host.com/dev/%path/sub'), 'sub')
888
self.assertEqual(t.relpath('sftp://host.com/dev/%path/sub'), 'sub')
796
890
def test_connection_sharing_propagate_credentials(self):
797
891
t = transport.ConnectedTransport('ftp://user@host.com/abs/path')
798
self.assertEquals('user', t._user)
799
self.assertEquals('host.com', t._host)
892
self.assertEqual('user', t._parsed_url.user)
893
self.assertEqual('host.com', t._parsed_url.host)
800
894
self.assertIs(None, t._get_connection())
801
self.assertIs(None, t._password)
895
self.assertIs(None, t._parsed_url.password)
802
896
c = t.clone('subdir')
803
897
self.assertIs(None, c._get_connection())
804
self.assertIs(None, t._password)
898
self.assertIs(None, t._parsed_url.password)
806
900
# Simulate the user entering a password
807
901
password = 'secret'
827
921
def test_reuse_same_transport(self):
828
922
possible_transports = []
829
t1 = transport.get_transport('http://foo/',
923
t1 = transport.get_transport_from_url('http://foo/',
830
924
possible_transports=possible_transports)
831
925
self.assertEqual([t1], possible_transports)
832
t2 = transport.get_transport('http://foo/',
926
t2 = transport.get_transport_from_url('http://foo/',
833
927
possible_transports=[t1])
834
928
self.assertIs(t1, t2)
836
930
# Also check that final '/' are handled correctly
837
t3 = transport.get_transport('http://foo/path/')
838
t4 = transport.get_transport('http://foo/path',
931
t3 = transport.get_transport_from_url('http://foo/path/')
932
t4 = transport.get_transport_from_url('http://foo/path',
839
933
possible_transports=[t3])
840
934
self.assertIs(t3, t4)
842
t5 = transport.get_transport('http://foo/path')
843
t6 = transport.get_transport('http://foo/path/',
936
t5 = transport.get_transport_from_url('http://foo/path')
937
t6 = transport.get_transport_from_url('http://foo/path/',
844
938
possible_transports=[t5])
845
939
self.assertIs(t5, t6)
847
941
def test_don_t_reuse_different_transport(self):
848
t1 = transport.get_transport('http://foo/path')
849
t2 = transport.get_transport('http://bar/path',
942
t1 = transport.get_transport_from_url('http://foo/path')
943
t2 = transport.get_transport_from_url('http://bar/path',
850
944
possible_transports=[t1])
851
945
self.assertIsNot(t1, t2)
854
948
class TestTransportTrace(tests.TestCase):
857
t = transport.get_transport('trace+memory://')
858
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
950
def test_decorator(self):
951
t = transport.get_transport_from_url('trace+memory://')
952
self.assertIsInstance(
953
t, breezy.transport.trace.TransportTraceDecorator)
860
955
def test_clone_preserves_activity(self):
861
t = transport.get_transport('trace+memory://')
956
t = transport.get_transport_from_url('trace+memory://')
862
957
t2 = t.clone('.')
863
958
self.assertTrue(t is not t2)
864
959
self.assertTrue(t._activity is t2._activity)
989
1089
# And the rest are threads
990
1090
for t in started[1:]:
1094
class TestUnhtml(tests.TestCase):
1096
"""Tests for unhtml_roughly"""
1098
def test_truncation(self):
1099
fake_html = "<p>something!\n" * 1000
1100
result = http.unhtml_roughly(fake_html)
1101
self.assertEqual(len(result), 1000)
1102
self.assertStartsWith(result, " something!")
1105
class SomeDirectory(object):
1107
def look_up(self, name, url):
1111
class TestLocationToUrl(tests.TestCase):
1113
def get_base_location(self):
1114
path = osutils.abspath('/foo/bar')
1115
if path.startswith('/'):
1116
url = 'file://%s' % (path,)
1118
# On Windows, abspaths start with the drive letter, so we have to
1119
# add in the extra '/'
1120
url = 'file:///%s' % (path,)
1123
def test_regular_url(self):
1124
self.assertEqual("file://foo", location_to_url("file://foo"))
1126
def test_directory(self):
1127
directories.register("bar:", SomeDirectory, "Dummy directory")
1128
self.addCleanup(directories.remove, "bar:")
1129
self.assertEqual("http://bar", location_to_url("bar:"))
1131
def test_unicode_url(self):
1132
self.assertRaises(errors.InvalidURL, location_to_url,
1133
"http://fo/\xc3\xaf".decode("utf-8"))
1135
def test_unicode_path(self):
1136
path, url = self.get_base_location()
1137
location = path + "\xc3\xaf".decode("utf-8")
1139
self.assertEqual(url, location_to_url(location))
1141
def test_path(self):
1142
path, url = self.get_base_location()
1143
self.assertEqual(url, location_to_url(path))
1145
def test_relative_file_url(self):
1146
self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
1147
location_to_url("file:bar"))
1149
def test_absolute_file_url(self):
1150
self.assertEqual("file:///bar", location_to_url("file:/bar"))