53
50
class TestTransport(tests.TestCase):
54
51
"""Test the non transport-concrete class functionality."""
53
# FIXME: These tests should use addCleanup() and/or overrideAttr() instead
54
# of try/finally -- vila 20100205
56
56
def test__get_set_protocol_handlers(self):
57
57
handlers = transport._get_protocol_handlers()
58
self.assertNotEqual([], handlers.keys())
59
transport._clear_protocol_handlers()
60
self.addCleanup(transport._set_protocol_handlers, handlers)
61
self.assertEqual([], transport._get_protocol_handlers().keys())
58
self.assertNotEqual([], handlers.keys( ))
60
transport._clear_protocol_handlers()
61
self.assertEqual([], transport._get_protocol_handlers().keys())
63
transport._set_protocol_handlers(handlers)
63
65
def test_get_transport_modules(self):
64
66
handlers = transport._get_protocol_handlers()
65
self.addCleanup(transport._set_protocol_handlers, handlers)
66
67
# don't pollute the current handlers
67
68
transport._clear_protocol_handlers()
69
69
class SampleHandler(object):
70
70
"""I exist, isnt that enough?"""
71
transport._clear_protocol_handlers()
72
transport.register_transport_proto('foo')
73
transport.register_lazy_transport('foo',
74
'bzrlib.tests.test_transport',
75
'TestTransport.SampleHandler')
76
transport.register_transport_proto('bar')
77
transport.register_lazy_transport('bar',
78
'bzrlib.tests.test_transport',
79
'TestTransport.SampleHandler')
80
self.assertEqual([SampleHandler.__module__,
81
'bzrlib.transport.chroot',
82
'bzrlib.transport.pathfilter'],
83
transport._get_transport_modules())
72
transport._clear_protocol_handlers()
73
transport.register_transport_proto('foo')
74
transport.register_lazy_transport('foo',
75
'bzrlib.tests.test_transport',
76
'TestTransport.SampleHandler')
77
transport.register_transport_proto('bar')
78
transport.register_lazy_transport('bar',
79
'bzrlib.tests.test_transport',
80
'TestTransport.SampleHandler')
81
self.assertEqual([SampleHandler.__module__,
82
'bzrlib.transport.chroot',
83
'bzrlib.transport.pathfilter'],
84
transport._get_transport_modules())
86
transport._set_protocol_handlers(handlers)
85
88
def test_transport_dependency(self):
86
89
"""Transport with missing dependency causes no error"""
87
90
saved_handlers = transport._get_protocol_handlers()
88
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
89
91
# don't pollute the current handlers
90
92
transport._clear_protocol_handlers()
91
transport.register_transport_proto('foo')
92
transport.register_lazy_transport(
93
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
95
transport.get_transport_from_url('foo://fooserver/foo')
96
except errors.UnsupportedProtocol, e:
98
self.assertEquals('Unsupported protocol'
99
' for url "foo://fooserver/foo":'
100
' Unable to import library "some_lib":'
101
' testing missing dependency', str(e))
103
self.fail('Did not raise UnsupportedProtocol')
94
transport.register_transport_proto('foo')
95
transport.register_lazy_transport(
96
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
98
transport.get_transport('foo://fooserver/foo')
99
except errors.UnsupportedProtocol, e:
101
self.assertEquals('Unsupported protocol'
102
' for url "foo://fooserver/foo":'
103
' Unable to import library "some_lib":'
104
' testing missing dependency', str(e))
106
self.fail('Did not raise UnsupportedProtocol')
108
# restore original values
109
transport._set_protocol_handlers(saved_handlers)
105
111
def test_transport_fallback(self):
106
112
"""Transport with missing dependency causes no error"""
107
113
saved_handlers = transport._get_protocol_handlers()
108
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
109
transport._clear_protocol_handlers()
110
transport.register_transport_proto('foo')
111
transport.register_lazy_transport(
112
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
113
transport.register_lazy_transport(
114
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
115
t = transport.get_transport_from_url('foo://fooserver/foo')
116
self.assertTrue(isinstance(t, BackupTransportHandler))
115
transport._clear_protocol_handlers()
116
transport.register_transport_proto('foo')
117
transport.register_lazy_transport(
118
'foo', 'bzrlib.tests.test_transport', 'BackupTransportHandler')
119
transport.register_lazy_transport(
120
'foo', 'bzrlib.tests.test_transport', 'BadTransportHandler')
121
t = transport.get_transport('foo://fooserver/foo')
122
self.assertTrue(isinstance(t, BackupTransportHandler))
124
transport._set_protocol_handlers(saved_handlers)
118
126
def test_ssh_hints(self):
119
127
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
121
transport.get_transport_from_url('ssh://fooserver/foo')
129
transport.get_transport('ssh://fooserver/foo')
122
130
except errors.UnsupportedProtocol, e:
124
132
self.assertEquals('Unsupported protocol'
202
221
def test_coalesce_fudge(self):
203
222
self.check([(10, 30, [(0, 10), (20, 10)]),
204
(100, 10, [(0, 10)]),
223
(100, 10, [(0, 10),]),
205
224
], [(10, 10), (30, 10), (100, 10)],
208
227
def test_coalesce_max_size(self):
209
228
self.check([(10, 20, [(0, 10), (10, 10)]),
210
229
(30, 50, [(0, 50)]),
211
230
# If one range is above max_size, it gets its own coalesced
213
(100, 80, [(0, 80)]),],
232
(100, 80, [(0, 80),]),],
214
233
[(10, 10), (20, 10), (30, 50), (100, 80)],
217
237
def test_coalesce_no_max_size(self):
218
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
238
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
219
239
[(10, 10), (20, 10), (30, 50), (80, 100)],
222
242
def test_coalesce_default_limit(self):
223
243
# By default we use a 100MB max size.
224
ten_mb = 10 * 1024 * 1024
225
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
244
ten_mb = 10*1024*1024
245
self.check([(0, 10*ten_mb, [(i*ten_mb, ten_mb) for i in range(10)]),
226
246
(10*ten_mb, ten_mb, [(0, ten_mb)])],
227
247
[(i*ten_mb, ten_mb) for i in range(11)])
228
self.check([(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
229
[(i * ten_mb, ten_mb) for i in range(11)],
248
self.check([(0, 11*ten_mb, [(i*ten_mb, ten_mb) for i in range(11)]),],
249
[(i*ten_mb, ten_mb) for i in range(11)],
230
250
max_size=1*1024*1024*1024)
439
460
backing_transport = memory.MemoryTransport()
440
461
server = chroot.ChrootServer(backing_transport)
441
462
server.start_server()
442
self.addCleanup(server.stop_server)
443
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
446
class TestHooks(tests.TestCase):
447
"""Basic tests for transport hooks"""
449
def _get_connected_transport(self):
450
return transport.ConnectedTransport("bogus:nowhere")
452
def test_transporthooks_initialisation(self):
453
"""Check all expected transport hook points are set up"""
454
hookpoint = transport.TransportHooks()
455
self.assertTrue("post_connect" in hookpoint,
456
"post_connect not in %s" % (hookpoint,))
458
def test_post_connect(self):
459
"""Ensure the post_connect hook is called when _set_transport is"""
461
transport.Transport.hooks.install_named_hook("post_connect",
463
t = self._get_connected_transport()
464
self.assertLength(0, calls)
465
t._set_connection("connection", "auth")
466
self.assertEqual(calls, [t])
464
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
469
469
class PathFilteringDecoratorTransportTest(tests.TestCase):
701
class TestTransportFromPath(tests.TestCaseInTempDir):
703
def test_with_path(self):
704
t = transport.get_transport_from_path(self.test_dir)
705
self.assertIsInstance(t, local.LocalTransport)
706
self.assertEquals(t.base.rstrip("/"),
707
urlutils.local_path_to_url(self.test_dir))
709
def test_with_url(self):
710
t = transport.get_transport_from_path("file:")
711
self.assertIsInstance(t, local.LocalTransport)
712
self.assertEquals(t.base.rstrip("/"),
713
urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
716
class TestTransportFromUrl(tests.TestCaseInTempDir):
718
def test_with_path(self):
719
self.assertRaises(errors.InvalidURL, transport.get_transport_from_url,
722
def test_with_url(self):
723
url = urlutils.local_path_to_url(self.test_dir)
724
t = transport.get_transport_from_url(url)
725
self.assertIsInstance(t, local.LocalTransport)
726
self.assertEquals(t.base.rstrip("/"), url)
728
def test_with_url_and_segment_parameters(self):
729
url = urlutils.local_path_to_url(self.test_dir)+",branch=foo"
730
t = transport.get_transport_from_url(url)
731
self.assertIsInstance(t, local.LocalTransport)
732
self.assertEquals(t.base.rstrip("/"), url)
733
with open(os.path.join(self.test_dir, "afile"), 'w') as f:
735
self.assertTrue(t.has("afile"))
738
700
class TestLocalTransports(tests.TestCase):
740
702
def test_get_transport_from_abspath(self):
833
787
def test_parse_url(self):
834
788
t = transport.ConnectedTransport(
835
789
'http://simple.example.com/home/source')
836
self.assertEquals(t._parsed_url.host, 'simple.example.com')
837
self.assertEquals(t._parsed_url.port, None)
838
self.assertEquals(t._parsed_url.path, '/home/source/')
839
self.assertTrue(t._parsed_url.user is None)
840
self.assertTrue(t._parsed_url.password is None)
790
self.assertEquals(t._host, 'simple.example.com')
791
self.assertEquals(t._port, None)
792
self.assertEquals(t._path, '/home/source/')
793
self.assertTrue(t._user is None)
794
self.assertTrue(t._password is None)
842
796
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
844
798
def test_parse_url_with_at_in_user(self):
846
800
t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
847
self.assertEquals(t._parsed_url.user, 'user@host.com')
801
self.assertEquals(t._user, 'user@host.com')
849
803
def test_parse_quoted_url(self):
850
804
t = transport.ConnectedTransport(
851
805
'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
852
self.assertEquals(t._parsed_url.host, 'exAmple.com')
853
self.assertEquals(t._parsed_url.port, 2222)
854
self.assertEquals(t._parsed_url.user, 'robey')
855
self.assertEquals(t._parsed_url.password, 'h@t')
856
self.assertEquals(t._parsed_url.path, '/path/')
806
self.assertEquals(t._host, 'exAmple.com')
807
self.assertEquals(t._port, 2222)
808
self.assertEquals(t._user, 'robey')
809
self.assertEquals(t._password, 'h@t')
810
self.assertEquals(t._path, '/path/')
858
812
# Base should not keep track of the password
859
self.assertEquals(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
813
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
861
815
def test_parse_invalid_url(self):
862
816
self.assertRaises(errors.InvalidURL,
918
871
def test_reuse_same_transport(self):
919
872
possible_transports = []
920
t1 = transport.get_transport_from_url('http://foo/',
873
t1 = transport.get_transport('http://foo/',
921
874
possible_transports=possible_transports)
922
875
self.assertEqual([t1], possible_transports)
923
t2 = transport.get_transport_from_url('http://foo/',
876
t2 = transport.get_transport('http://foo/',
924
877
possible_transports=[t1])
925
878
self.assertIs(t1, t2)
927
880
# Also check that final '/' are handled correctly
928
t3 = transport.get_transport_from_url('http://foo/path/')
929
t4 = transport.get_transport_from_url('http://foo/path',
881
t3 = transport.get_transport('http://foo/path/')
882
t4 = transport.get_transport('http://foo/path',
930
883
possible_transports=[t3])
931
884
self.assertIs(t3, t4)
933
t5 = transport.get_transport_from_url('http://foo/path')
934
t6 = transport.get_transport_from_url('http://foo/path/',
886
t5 = transport.get_transport('http://foo/path')
887
t6 = transport.get_transport('http://foo/path/',
935
888
possible_transports=[t5])
936
889
self.assertIs(t5, t6)
938
891
def test_don_t_reuse_different_transport(self):
939
t1 = transport.get_transport_from_url('http://foo/path')
940
t2 = transport.get_transport_from_url('http://bar/path',
892
t1 = transport.get_transport('http://foo/path')
893
t2 = transport.get_transport('http://bar/path',
941
894
possible_transports=[t1])
942
895
self.assertIsNot(t1, t2)
945
898
class TestTransportTrace(tests.TestCase):
947
def test_decorator(self):
948
t = transport.get_transport_from_url('trace+memory://')
949
self.assertIsInstance(
950
t, bzrlib.transport.trace.TransportTraceDecorator)
901
t = transport.get_transport('trace+memory://')
902
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
952
904
def test_clone_preserves_activity(self):
953
t = transport.get_transport_from_url('trace+memory://')
905
t = transport.get_transport('trace+memory://')
954
906
t2 = t.clone('.')
955
907
self.assertTrue(t is not t2)
956
908
self.assertTrue(t._activity is t2._activity)
1097
1048
result = http.unhtml_roughly(fake_html)
1098
1049
self.assertEquals(len(result), 1000)
1099
1050
self.assertStartsWith(result, " something!")
1102
class SomeDirectory(object):
1104
def look_up(self, name, url):
1108
class TestLocationToUrl(tests.TestCase):
1110
def get_base_location(self):
1111
path = osutils.abspath('/foo/bar')
1112
if path.startswith('/'):
1113
url = 'file://%s' % (path,)
1115
# On Windows, abspaths start with the drive letter, so we have to
1116
# add in the extra '/'
1117
url = 'file:///%s' % (path,)
1120
def test_regular_url(self):
1121
self.assertEquals("file://foo", location_to_url("file://foo"))
1123
def test_directory(self):
1124
directories.register("bar:", SomeDirectory, "Dummy directory")
1125
self.addCleanup(directories.remove, "bar:")
1126
self.assertEquals("http://bar", location_to_url("bar:"))
1128
def test_unicode_url(self):
1129
self.assertRaises(errors.InvalidURL, location_to_url,
1130
"http://fo/\xc3\xaf".decode("utf-8"))
1132
def test_unicode_path(self):
1133
path, url = self.get_base_location()
1134
location = path + "\xc3\xaf".decode("utf-8")
1136
self.assertEquals(url, location_to_url(location))
1138
def test_path(self):
1139
path, url = self.get_base_location()
1140
self.assertEquals(url, location_to_url(path))
1142
def test_relative_file_url(self):
1143
self.assertEquals(urlutils.local_path_to_url(".") + "/bar",
1144
location_to_url("file:bar"))
1146
def test_absolute_file_url(self):
1147
self.assertEquals("file:///bar", location_to_url("file:/bar"))