48
53
class TestTransport(tests.TestCase):
49
54
"""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
56
def test__get_set_protocol_handlers(self):
55
57
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)
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())
63
63
def test_get_transport_modules(self):
64
64
handlers = transport._get_protocol_handlers()
65
self.addCleanup(transport._set_protocol_handlers, handlers)
65
66
# don't pollute the current handlers
66
67
transport._clear_protocol_handlers()
67
69
class SampleHandler(object):
68
70
"""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)
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())
86
85
def test_transport_dependency(self):
87
86
"""Transport with missing dependency causes no error"""
88
87
saved_handlers = transport._get_protocol_handlers()
88
self.addCleanup(transport._set_protocol_handlers, saved_handlers)
89
89
# don't pollute the current handlers
90
90
transport._clear_protocol_handlers()
91
transport.register_transport_proto('foo')
92
transport.register_lazy_transport(
93
'foo', 'bzrlib.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)
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')
109
105
def test_transport_fallback(self):
110
106
"""Transport with missing dependency causes no error"""
111
107
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)
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))
124
118
def test_ssh_hints(self):
125
119
"""Transport ssh:// should raise an error pointing out bzr+ssh://"""
127
transport.get_transport('ssh://fooserver/foo')
121
transport.get_transport_from_url('ssh://fooserver/foo')
128
122
except errors.UnsupportedProtocol, e:
130
124
self.assertEquals('Unsupported protocol'
219
202
def test_coalesce_fudge(self):
220
203
self.check([(10, 30, [(0, 10), (20, 10)]),
221
(100, 10, [(0, 10),]),
204
(100, 10, [(0, 10)]),
222
205
], [(10, 10), (30, 10), (100, 10)],
225
208
def test_coalesce_max_size(self):
226
209
self.check([(10, 20, [(0, 10), (10, 10)]),
227
210
(30, 50, [(0, 50)]),
228
211
# If one range is above max_size, it gets its own coalesced
230
(100, 80, [(0, 80),]),],
213
(100, 80, [(0, 80)]),],
231
214
[(10, 10), (20, 10), (30, 50), (100, 80)],
235
217
def test_coalesce_no_max_size(self):
236
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)]),],
218
self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
237
219
[(10, 10), (20, 10), (30, 50), (80, 100)],
240
222
def test_coalesce_default_limit(self):
241
223
# 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)]),
224
ten_mb = 10 * 1024 * 1024
225
self.check([(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
244
226
(10*ten_mb, ten_mb, [(0, ten_mb)])],
245
227
[(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)],
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
230
max_size=1*1024*1024*1024)
458
439
backing_transport = memory.MemoryTransport()
459
440
server = chroot.ChrootServer(backing_transport)
460
441
server.start_server()
462
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
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])
467
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"))
698
738
class TestLocalTransports(tests.TestCase):
700
740
def test_get_transport_from_abspath(self):
722
762
self.assertEquals(t.local_abspath(''), here)
765
class TestLocalTransportMutation(tests.TestCaseInTempDir):
767
def test_local_transport_mkdir(self):
768
here = osutils.abspath('.')
769
t = transport.get_transport(here)
771
self.assertTrue(os.path.exists('test'))
773
def test_local_transport_mkdir_permission_denied(self):
774
# See https://bugs.launchpad.net/bzr/+bug/606537
775
here = osutils.abspath('.')
776
t = transport.get_transport(here)
777
def fake_chmod(path, mode):
778
e = OSError('permission denied')
779
e.errno = errno.EPERM
781
self.overrideAttr(os, 'chmod', fake_chmod)
783
t.mkdir('test2', mode=0707)
784
self.assertTrue(os.path.exists('test'))
785
self.assertTrue(os.path.exists('test2'))
788
class TestLocalTransportWriteStream(tests.TestCaseWithTransport):
790
def test_local_fdatasync_calls_fdatasync(self):
791
"""Check fdatasync on a stream tries to flush the data to the OS.
793
We can't easily observe the external effect but we can at least see
797
fdatasync = getattr(os, 'fdatasync', sentinel)
798
if fdatasync is sentinel:
799
raise tests.TestNotApplicable('fdatasync not supported')
800
t = self.get_transport('.')
801
calls = self.recordCalls(os, 'fdatasync')
802
w = t.open_write_stream('out')
805
with open('out', 'rb') as f:
806
# Should have been flushed.
807
self.assertEquals(f.read(), 'foo')
808
self.assertEquals(len(calls), 1, calls)
810
def test_missing_directory(self):
811
t = self.get_transport('.')
812
self.assertRaises(errors.NoSuchFile, t.open_write_stream, 'dir/foo')
725
815
class TestWin32LocalTransport(tests.TestCase):
727
817
def test_unc_clone_to_root(self):
743
833
def test_parse_url(self):
744
834
t = transport.ConnectedTransport(
745
835
'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)
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)
752
842
self.assertEquals(t.base, 'http://simple.example.com/home/source/')
754
844
def test_parse_url_with_at_in_user(self):
756
846
t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
757
self.assertEquals(t._user, 'user@host.com')
847
self.assertEquals(t._parsed_url.user, 'user@host.com')
759
849
def test_parse_quoted_url(self):
760
850
t = transport.ConnectedTransport(
761
851
'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/')
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/')
768
858
# Base should not keep track of the password
769
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
859
self.assertEquals(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
771
861
def test_parse_invalid_url(self):
772
862
self.assertRaises(errors.InvalidURL,
827
918
def test_reuse_same_transport(self):
828
919
possible_transports = []
829
t1 = transport.get_transport('http://foo/',
920
t1 = transport.get_transport_from_url('http://foo/',
830
921
possible_transports=possible_transports)
831
922
self.assertEqual([t1], possible_transports)
832
t2 = transport.get_transport('http://foo/',
923
t2 = transport.get_transport_from_url('http://foo/',
833
924
possible_transports=[t1])
834
925
self.assertIs(t1, t2)
836
927
# Also check that final '/' are handled correctly
837
t3 = transport.get_transport('http://foo/path/')
838
t4 = transport.get_transport('http://foo/path',
928
t3 = transport.get_transport_from_url('http://foo/path/')
929
t4 = transport.get_transport_from_url('http://foo/path',
839
930
possible_transports=[t3])
840
931
self.assertIs(t3, t4)
842
t5 = transport.get_transport('http://foo/path')
843
t6 = transport.get_transport('http://foo/path/',
933
t5 = transport.get_transport_from_url('http://foo/path')
934
t6 = transport.get_transport_from_url('http://foo/path/',
844
935
possible_transports=[t5])
845
936
self.assertIs(t5, t6)
847
938
def test_don_t_reuse_different_transport(self):
848
t1 = transport.get_transport('http://foo/path')
849
t2 = transport.get_transport('http://bar/path',
939
t1 = transport.get_transport_from_url('http://foo/path')
940
t2 = transport.get_transport_from_url('http://bar/path',
850
941
possible_transports=[t1])
851
942
self.assertIsNot(t1, t2)
854
945
class TestTransportTrace(tests.TestCase):
857
t = transport.get_transport('trace+memory://')
858
self.assertIsInstance(t, bzrlib.transport.trace.TransportTraceDecorator)
947
def test_decorator(self):
948
t = transport.get_transport_from_url('trace+memory://')
949
self.assertIsInstance(
950
t, bzrlib.transport.trace.TransportTraceDecorator)
860
952
def test_clone_preserves_activity(self):
861
t = transport.get_transport('trace+memory://')
953
t = transport.get_transport_from_url('trace+memory://')
862
954
t2 = t.clone('.')
863
955
self.assertTrue(t is not t2)
864
956
self.assertTrue(t._activity is t2._activity)
993
1086
# And the rest are threads
994
1087
for t in started[1:]:
1091
class TestUnhtml(tests.TestCase):
1093
"""Tests for unhtml_roughly"""
1095
def test_truncation(self):
1096
fake_html = "<p>something!\n" * 1000
1097
result = http.unhtml_roughly(fake_html)
1098
self.assertEquals(len(result), 1000)
1099
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"))