1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
19
from cStringIO import StringIO
28
from bzrlib.errors import (ConnectionError,
21
from bzrlib.errors import (NoSuchFile, FileExists, TransportNotPossible,
40
23
from bzrlib.tests import TestCase, TestCaseInTempDir
41
from bzrlib.transport import (_CoalescedOffset,
43
_get_protocol_handlers,
44
_set_protocol_handlers,
45
_get_transport_modules,
48
register_lazy_transport,
49
register_transport_proto,
50
_clear_protocol_handlers,
53
from bzrlib.transport.chroot import ChrootServer
54
from bzrlib.transport.memory import MemoryTransport
55
from bzrlib.transport.local import (LocalTransport,
56
EmulatedWin32LocalTransport)
59
# TODO: Should possibly split transport-specific tests into their own files.
24
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
25
from bzrlib.transport import memory, urlescape
29
"""Append the given text (file-like object) to the supplied filename."""
62
36
class TestTransport(TestCase):
63
37
"""Test the non transport-concrete class functionality."""
65
def test__get_set_protocol_handlers(self):
66
handlers = _get_protocol_handlers()
67
self.assertNotEqual([], handlers.keys( ))
69
_clear_protocol_handlers()
70
self.assertEqual([], _get_protocol_handlers().keys())
72
_set_protocol_handlers(handlers)
74
def test_get_transport_modules(self):
75
handlers = _get_protocol_handlers()
76
class SampleHandler(object):
77
"""I exist, isnt that enough?"""
79
_clear_protocol_handlers()
80
register_transport_proto('foo')
81
register_lazy_transport('foo', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
82
register_transport_proto('bar')
83
register_lazy_transport('bar', 'bzrlib.tests.test_transport', 'TestTransport.SampleHandler')
84
self.assertEqual([SampleHandler.__module__, 'bzrlib.transport.chroot'],
85
_get_transport_modules())
87
_set_protocol_handlers(handlers)
89
def test_transport_dependency(self):
90
"""Transport with missing dependency causes no error"""
91
saved_handlers = _get_protocol_handlers()
93
register_transport_proto('foo')
94
register_lazy_transport('foo', 'bzrlib.tests.test_transport',
95
'BadTransportHandler')
97
get_transport('foo://fooserver/foo')
98
except UnsupportedProtocol, e:
100
self.assertEquals('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')
107
# restore original values
108
_set_protocol_handlers(saved_handlers)
110
def test_transport_fallback(self):
111
"""Transport with missing dependency causes no error"""
112
saved_handlers = _get_protocol_handlers()
114
_clear_protocol_handlers()
115
register_transport_proto('foo')
116
register_lazy_transport('foo', 'bzrlib.tests.test_transport',
117
'BackupTransportHandler')
118
register_lazy_transport('foo', 'bzrlib.tests.test_transport',
119
'BadTransportHandler')
120
t = get_transport('foo://fooserver/foo')
121
self.assertTrue(isinstance(t, BackupTransportHandler))
123
_set_protocol_handlers(saved_handlers)
125
def test_LateReadError(self):
126
"""The LateReadError helper should raise on read()."""
127
a_file = LateReadError('a path')
130
except ReadError, error:
131
self.assertEqual('a path', error.path)
132
self.assertRaises(ReadError, a_file.read, 40)
135
def test__combine_paths(self):
137
self.assertEqual('/home/sarah/project/foo',
138
t._combine_paths('/home/sarah', 'project/foo'))
139
self.assertEqual('/etc',
140
t._combine_paths('/home/sarah', '../../etc'))
141
self.assertEqual('/etc',
142
t._combine_paths('/home/sarah', '../../../etc'))
143
self.assertEqual('/etc',
144
t._combine_paths('/home/sarah', '/etc'))
146
def test_local_abspath_non_local_transport(self):
147
# the base implementation should throw
148
t = MemoryTransport()
149
e = self.assertRaises(errors.NotLocalUrl, t.local_abspath, 't')
150
self.assertEqual('memory:///t is not a local path.', str(e))
153
class TestCoalesceOffsets(TestCase):
155
def check(self, expected, offsets, limit=0, fudge=0):
156
coalesce = Transport._coalesce_offsets
157
exp = [_CoalescedOffset(*x) for x in expected]
158
out = list(coalesce(offsets, limit=limit, fudge_factor=fudge))
159
self.assertEqual(exp, out)
161
def test_coalesce_empty(self):
164
def test_coalesce_simple(self):
165
self.check([(0, 10, [(0, 10)])], [(0, 10)])
167
def test_coalesce_unrelated(self):
168
self.check([(0, 10, [(0, 10)]),
170
], [(0, 10), (20, 10)])
172
def test_coalesce_unsorted(self):
173
self.check([(20, 10, [(0, 10)]),
175
], [(20, 10), (0, 10)])
177
def test_coalesce_nearby(self):
178
self.check([(0, 20, [(0, 10), (10, 10)])],
181
def test_coalesce_overlapped(self):
182
self.check([(0, 15, [(0, 10), (5, 10)])],
185
def test_coalesce_limit(self):
186
self.check([(10, 50, [(0, 10), (10, 10), (20, 10),
187
(30, 10), (40, 10)]),
188
(60, 50, [(0, 10), (10, 10), (20, 10),
189
(30, 10), (40, 10)]),
190
], [(10, 10), (20, 10), (30, 10), (40, 10),
191
(50, 10), (60, 10), (70, 10), (80, 10),
192
(90, 10), (100, 10)],
195
def test_coalesce_no_limit(self):
196
self.check([(10, 100, [(0, 10), (10, 10), (20, 10),
197
(30, 10), (40, 10), (50, 10),
198
(60, 10), (70, 10), (80, 10),
200
], [(10, 10), (20, 10), (30, 10), (40, 10),
201
(50, 10), (60, 10), (70, 10), (80, 10),
202
(90, 10), (100, 10)])
204
def test_coalesce_fudge(self):
205
self.check([(10, 30, [(0, 10), (20, 10)]),
206
(100, 10, [(0, 10),]),
207
], [(10, 10), (30, 10), (100, 10)],
39
def test_urlescape(self):
40
self.assertEqual('%25', urlescape('%'))
43
class TestTransportMixIn(object):
44
"""Subclass this, and it will provide a series of tests for a Transport.
45
It assumes that the Transport object is connected to the
46
current working directory. So that whatever is done
47
through the transport, should show up in the working
48
directory, and vice-versa.
50
This also tests to make sure that the functions work with both
51
generators and lists (assuming iter(list) is effectively a generator)
54
def get_transport(self):
55
"""Children should override this to return the Transport object.
57
raise NotImplementedError
60
t = self.get_transport()
62
files = ['a', 'b', 'e', 'g', '%']
63
self.build_tree(files)
64
self.assertEqual(t.has('a'), True)
65
self.assertEqual(t.has('c'), False)
66
self.assertEqual(t.has(urlescape('%')), True)
67
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])),
68
[True, True, False, False, True, False, True, False])
69
self.assertEqual(t.has_any(['a', 'b', 'c']), True)
70
self.assertEqual(t.has_any(['c', 'd', 'f', urlescape('%%')]), False)
71
self.assertEqual(list(t.has_multi(iter(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']))),
72
[True, True, False, False, True, False, True, False])
73
self.assertEqual(t.has_any(['c', 'c', 'c']), False)
74
self.assertEqual(t.has_any(['b', 'b', 'b']), True)
77
t = self.get_transport()
79
files = ['a', 'b', 'e', 'g']
80
self.build_tree(files)
81
self.assertEqual(t.get('a').read(), open('a').read())
82
content_f = t.get_multi(files)
83
for path,f in zip(files, content_f):
84
self.assertEqual(open(path).read(), f.read())
86
content_f = t.get_multi(iter(files))
87
for path,f in zip(files, content_f):
88
self.assertEqual(open(path).read(), f.read())
90
self.assertRaises(NoSuchFile, t.get, 'c')
92
files = list(t.get_multi(['a', 'b', 'c']))
96
self.fail('Failed to raise NoSuchFile for missing file in get_multi')
98
files = list(t.get_multi(iter(['a', 'b', 'c', 'e'])))
102
self.fail('Failed to raise NoSuchFile for missing file in get_multi')
105
t = self.get_transport()
108
self.assertRaises(TransportNotPossible,
109
t.put, 'a', 'some text for a\n')
110
open('a', 'wb').write('some text for a\n')
112
t.put('a', 'some text for a\n')
113
self.assert_(os.path.exists('a'))
114
self.check_file_contents('a', 'some text for a\n')
115
self.assertEqual(t.get('a').read(), 'some text for a\n')
116
# Make sure 'has' is updated
117
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e'])),
118
[True, False, False, False, False])
120
self.assertRaises(TransportNotPossible,
122
[('a', 'new\ncontents for\na\n'),
123
('d', 'contents\nfor d\n')])
124
open('a', 'wb').write('new\ncontents for\na\n')
125
open('d', 'wb').write('contents\nfor d\n')
127
# Put also replaces contents
128
self.assertEqual(t.put_multi([('a', 'new\ncontents for\na\n'),
129
('d', 'contents\nfor d\n')]),
131
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e'])),
132
[True, False, False, True, False])
133
self.check_file_contents('a', 'new\ncontents for\na\n')
134
self.check_file_contents('d', 'contents\nfor d\n')
137
self.assertRaises(TransportNotPossible,
138
t.put_multi, iter([('a', 'diff\ncontents for\na\n'),
139
('d', 'another contents\nfor d\n')]))
140
open('a', 'wb').write('diff\ncontents for\na\n')
141
open('d', 'wb').write('another contents\nfor d\n')
144
t.put_multi(iter([('a', 'diff\ncontents for\na\n'),
145
('d', 'another contents\nfor d\n')]))
147
self.check_file_contents('a', 'diff\ncontents for\na\n')
148
self.check_file_contents('d', 'another contents\nfor d\n')
151
self.assertRaises(TransportNotPossible,
152
t.put, 'path/doesnt/exist/c', 'contents')
154
self.assertRaises(NoSuchFile,
155
t.put, 'path/doesnt/exist/c', 'contents')
157
def test_put_file(self):
158
t = self.get_transport()
160
# Test that StringIO can be used as a file-like object with put
161
f1 = StringIO('this is a string\nand some more stuff\n')
163
open('f1', 'wb').write(f1.read())
169
self.check_file_contents('f1',
170
'this is a string\nand some more stuff\n')
172
f2 = StringIO('here is some text\nand a bit more\n')
173
f3 = StringIO('some text for the\nthird file created\n')
176
open('f2', 'wb').write(f2.read())
177
open('f3', 'wb').write(f3.read())
179
t.put_multi([('f2', f2), ('f3', f3)])
183
self.check_file_contents('f2', 'here is some text\nand a bit more\n')
184
self.check_file_contents('f3', 'some text for the\nthird file created\n')
186
# Test that an actual file object can be used with put
187
f4 = open('f1', 'rb')
189
open('f4', 'wb').write(f4.read())
195
self.check_file_contents('f4',
196
'this is a string\nand some more stuff\n')
198
f5 = open('f2', 'rb')
199
f6 = open('f3', 'rb')
201
open('f5', 'wb').write(f5.read())
202
open('f6', 'wb').write(f6.read())
204
t.put_multi([('f5', f5), ('f6', f6)])
208
self.check_file_contents('f5', 'here is some text\nand a bit more\n')
209
self.check_file_contents('f6', 'some text for the\nthird file created\n')
213
def test_mkdir(self):
214
t = self.get_transport()
218
self.assertEqual(t.has('dir_a'), True)
219
self.assertEqual(t.has('dir_b'), False)
222
self.assertRaises(TransportNotPossible,
227
self.assertEqual(t.has('dir_b'), True)
228
self.assert_(os.path.isdir('dir_b'))
231
self.assertRaises(TransportNotPossible,
232
t.mkdir_multi, ['dir_c', 'dir_d'])
236
t.mkdir_multi(['dir_c', 'dir_d'])
239
self.assertRaises(TransportNotPossible,
240
t.mkdir_multi, iter(['dir_e', 'dir_f']))
244
t.mkdir_multi(iter(['dir_e', 'dir_f']))
245
self.assertEqual(list(t.has_multi(
246
['dir_a', 'dir_b', 'dir_c', 'dir_q',
247
'dir_d', 'dir_e', 'dir_f', 'dir_b'])),
248
[True, True, True, False,
249
True, True, True, True])
250
for d in ['dir_a', 'dir_b', 'dir_c', 'dir_d', 'dir_e', 'dir_f']:
251
self.assert_(os.path.isdir(d))
253
if not self.readonly:
254
self.assertRaises(NoSuchFile, t.mkdir, 'path/doesnt/exist')
255
self.assertRaises(FileExists, t.mkdir, 'dir_a') # Creating a directory again should fail
257
# Make sure the transport recognizes when a
258
# directory is created by other means
259
# Caching Transports will fail, because dir_e was already seen not
260
# to exist. So instead, we will search for a new directory
262
#if not self.readonly:
263
# self.assertRaises(FileExists, t.mkdir, 'dir_e')
266
if not self.readonly:
267
self.assertRaises(FileExists, t.mkdir, 'dir_g')
269
# Test get/put in sub-directories
271
open('dir_a/a', 'wb').write('contents of dir_a/a')
272
open('dir_b/b', 'wb').write('contents of dir_b/b')
275
t.put_multi([('dir_a/a', 'contents of dir_a/a'),
276
('dir_b/b', 'contents of dir_b/b')])
278
for f in ('dir_a/a', 'dir_b/b'):
279
self.assertEqual(t.get(f).read(), open(f).read())
281
def test_copy_to(self):
283
from bzrlib.transport.local import LocalTransport
285
t = self.get_transport()
287
files = ['a', 'b', 'c', 'd']
288
self.build_tree(files)
290
dtmp = tempfile.mkdtemp(dir='.', prefix='test-transport-')
291
dtmp_base = os.path.basename(dtmp)
292
local_t = LocalTransport(dtmp)
294
t.copy_to(files, local_t)
296
self.assertEquals(open(f).read(),
297
open(os.path.join(dtmp_base, f)).read())
299
# Test that copying into a missing directory raises
302
open('e/f', 'wb').write('contents of e')
303
self.assertRaises(NoSuchFile, t.copy_to, ['e/f'], local_t)
305
os.mkdir(os.path.join(dtmp_base, 'e'))
306
t.copy_to(['e/f'], local_t)
308
del dtmp, dtmp_base, local_t
310
dtmp = tempfile.mkdtemp(dir='.', prefix='test-transport-')
311
dtmp_base = os.path.basename(dtmp)
312
local_t = LocalTransport(dtmp)
314
files = ['a', 'b', 'c', 'd']
315
t.copy_to(iter(files), local_t)
317
self.assertEquals(open(f).read(),
318
open(os.path.join(dtmp_base, f)).read())
320
del dtmp, dtmp_base, local_t
322
def test_append(self):
323
t = self.get_transport()
326
open('a', 'wb').write('diff\ncontents for\na\n')
327
open('b', 'wb').write('contents\nfor b\n')
330
('a', 'diff\ncontents for\na\n'),
331
('b', 'contents\nfor b\n')
335
self.assertRaises(TransportNotPossible,
336
t.append, 'a', 'add\nsome\nmore\ncontents\n')
337
_append('a', 'add\nsome\nmore\ncontents\n')
339
t.append('a', 'add\nsome\nmore\ncontents\n')
341
self.check_file_contents('a',
342
'diff\ncontents for\na\nadd\nsome\nmore\ncontents\n')
345
self.assertRaises(TransportNotPossible,
347
[('a', 'and\nthen\nsome\nmore\n'),
348
('b', 'some\nmore\nfor\nb\n')])
349
_append('a', 'and\nthen\nsome\nmore\n')
350
_append('b', 'some\nmore\nfor\nb\n')
352
t.append_multi([('a', 'and\nthen\nsome\nmore\n'),
353
('b', 'some\nmore\nfor\nb\n')])
354
self.check_file_contents('a',
355
'diff\ncontents for\na\n'
356
'add\nsome\nmore\ncontents\n'
357
'and\nthen\nsome\nmore\n')
358
self.check_file_contents('b',
360
'some\nmore\nfor\nb\n')
363
_append('a', 'a little bit more\n')
364
_append('b', 'from an iterator\n')
366
t.append_multi(iter([('a', 'a little bit more\n'),
367
('b', 'from an iterator\n')]))
368
self.check_file_contents('a',
369
'diff\ncontents for\na\n'
370
'add\nsome\nmore\ncontents\n'
371
'and\nthen\nsome\nmore\n'
372
'a little bit more\n')
373
self.check_file_contents('b',
375
'some\nmore\nfor\nb\n'
376
'from an iterator\n')
378
def test_append_file(self):
379
t = self.get_transport()
382
('f1', 'this is a string\nand some more stuff\n'),
383
('f2', 'here is some text\nand a bit more\n'),
384
('f3', 'some text for the\nthird file created\n'),
385
('f4', 'this is a string\nand some more stuff\n'),
386
('f5', 'here is some text\nand a bit more\n'),
387
('f6', 'some text for the\nthird file created\n')
391
for f, val in contents:
392
open(f, 'wb').write(val)
394
t.put_multi(contents)
396
a1 = StringIO('appending to\none\n')
398
_append('f1', a1.read())
404
self.check_file_contents('f1',
405
'this is a string\nand some more stuff\n'
406
'appending to\none\n')
408
a2 = StringIO('adding more\ntext to two\n')
409
a3 = StringIO('some garbage\nto put in three\n')
412
_append('f2', a2.read())
413
_append('f3', a3.read())
415
t.append_multi([('f2', a2), ('f3', a3)])
419
self.check_file_contents('f2',
420
'here is some text\nand a bit more\n'
421
'adding more\ntext to two\n')
422
self.check_file_contents('f3',
423
'some text for the\nthird file created\n'
424
'some garbage\nto put in three\n')
426
# Test that an actual file object can be used with put
427
a4 = open('f1', 'rb')
429
_append('f4', a4.read())
435
self.check_file_contents('f4',
436
'this is a string\nand some more stuff\n'
437
'this is a string\nand some more stuff\n'
438
'appending to\none\n')
440
a5 = open('f2', 'rb')
441
a6 = open('f3', 'rb')
443
_append('f5', a5.read())
444
_append('f6', a6.read())
446
t.append_multi([('f5', a5), ('f6', a6)])
450
self.check_file_contents('f5',
451
'here is some text\nand a bit more\n'
452
'here is some text\nand a bit more\n'
453
'adding more\ntext to two\n')
454
self.check_file_contents('f6',
455
'some text for the\nthird file created\n'
456
'some text for the\nthird file created\n'
457
'some garbage\nto put in three\n')
459
def test_delete(self):
460
# TODO: Test Transport.delete
464
# TODO: Test Transport.move
467
def test_connection_error(self):
468
"""ConnectionError is raised when connection is impossible"""
469
if not hasattr(self, "get_bogus_transport"):
471
t = self.get_bogus_transport()
474
except (ConnectionError, NoSuchFile), e:
476
except (Exception), e:
477
self.failIf(True, 'Wrong exception thrown: %s' % e)
479
self.failIf(True, 'Did not get the expected exception.')
482
class LocalTransportTest(TestCaseInTempDir, TestTransportMixIn):
483
def get_transport(self):
484
from bzrlib.transport.local import LocalTransport
485
return LocalTransport('.')
488
class HttpTransportTest(TestCaseWithWebserver, TestTransportMixIn):
492
def get_transport(self):
493
from bzrlib.transport.http import HttpTransport
494
url = self.get_remote_url('.')
495
return HttpTransport(url)
497
def get_bogus_transport(self):
498
from bzrlib.transport.http import HttpTransport
499
return HttpTransport('http://jasldkjsalkdjalksjdkljasd')
212
502
class TestMemoryTransport(TestCase):
214
504
def test_get_transport(self):
505
memory.MemoryTransport()
217
507
def test_clone(self):
218
transport = MemoryTransport()
219
self.assertTrue(isinstance(transport, MemoryTransport))
220
self.assertEqual("memory:///", transport.clone("/").base)
508
transport = memory.MemoryTransport()
509
self.failUnless(transport.clone() is transport)
222
511
def test_abspath(self):
223
transport = MemoryTransport()
224
self.assertEqual("memory:///relpath", transport.abspath('relpath'))
226
def test_abspath_of_root(self):
227
transport = MemoryTransport()
228
self.assertEqual("memory:///", transport.base)
229
self.assertEqual("memory:///", transport.abspath('/'))
231
def test_abspath_of_relpath_starting_at_root(self):
232
transport = MemoryTransport()
233
self.assertEqual("memory:///foo", transport.abspath('/foo'))
512
transport = memory.MemoryTransport()
513
self.assertEqual("in-memory:relpath", transport.abspath('relpath'))
515
def test_relpath(self):
516
transport = memory.MemoryTransport()
235
518
def test_append_and_get(self):
236
transport = MemoryTransport()
237
transport.append_bytes('path', 'content')
519
transport = memory.MemoryTransport()
520
transport.append('path', StringIO('content'))
238
521
self.assertEqual(transport.get('path').read(), 'content')
239
transport.append_file('path', StringIO('content'))
522
transport.append('path', StringIO('content'))
240
523
self.assertEqual(transport.get('path').read(), 'contentcontent')
242
525
def test_put_and_get(self):
243
transport = MemoryTransport()
244
transport.put_file('path', StringIO('content'))
526
transport = memory.MemoryTransport()
527
transport.put('path', StringIO('content'))
245
528
self.assertEqual(transport.get('path').read(), 'content')
246
transport.put_bytes('path', 'content')
529
transport.put('path', StringIO('content'))
247
530
self.assertEqual(transport.get('path').read(), 'content')
249
532
def test_append_without_dir_fails(self):
250
transport = MemoryTransport()
533
transport = memory.MemoryTransport()
251
534
self.assertRaises(NoSuchFile,
252
transport.append_bytes, 'dir/path', 'content')
535
transport.append, 'dir/path', StringIO('content'))
254
537
def test_put_without_dir_fails(self):
255
transport = MemoryTransport()
538
transport = memory.MemoryTransport()
256
539
self.assertRaises(NoSuchFile,
257
transport.put_file, 'dir/path', StringIO('content'))
540
transport.put, 'dir/path', StringIO('content'))
259
542
def test_get_missing(self):
260
transport = MemoryTransport()
543
transport = memory.MemoryTransport()
261
544
self.assertRaises(NoSuchFile, transport.get, 'foo')
263
546
def test_has_missing(self):
264
transport = MemoryTransport()
547
transport = memory.MemoryTransport()
265
548
self.assertEquals(False, transport.has('foo'))
267
550
def test_has_present(self):
268
transport = MemoryTransport()
269
transport.append_bytes('foo', 'content')
551
transport = memory.MemoryTransport()
552
transport.append('foo', StringIO('content'))
270
553
self.assertEquals(True, transport.has('foo'))
272
def test_list_dir(self):
273
transport = MemoryTransport()
274
transport.put_bytes('foo', 'content')
275
transport.mkdir('dir')
276
transport.put_bytes('dir/subfoo', 'content')
277
transport.put_bytes('dirlike', 'content')
279
self.assertEquals(['dir', 'dirlike', 'foo'], sorted(transport.list_dir('.')))
280
self.assertEquals(['subfoo'], sorted(transport.list_dir('dir')))
282
555
def test_mkdir(self):
283
transport = MemoryTransport()
556
transport = memory.MemoryTransport()
284
557
transport.mkdir('dir')
285
transport.append_bytes('dir/path', 'content')
558
transport.append('dir/path', StringIO('content'))
286
559
self.assertEqual(transport.get('dir/path').read(), 'content')
288
561
def test_mkdir_missing_parent(self):
289
transport = MemoryTransport()
562
transport = memory.MemoryTransport()
290
563
self.assertRaises(NoSuchFile,
291
564
transport.mkdir, 'dir/dir')
293
566
def test_mkdir_twice(self):
294
transport = MemoryTransport()
567
transport = memory.MemoryTransport()
295
568
transport.mkdir('dir')
296
569
self.assertRaises(FileExists, transport.mkdir, 'dir')
298
571
def test_parameters(self):
299
transport = MemoryTransport()
572
transport = memory.MemoryTransport()
300
573
self.assertEqual(True, transport.listable())
301
574
self.assertEqual(False, transport.should_cache())
302
self.assertEqual(False, transport.is_readonly())
304
576
def test_iter_files_recursive(self):
305
transport = MemoryTransport()
577
transport = memory.MemoryTransport()
306
578
transport.mkdir('dir')
307
transport.put_bytes('dir/foo', 'content')
308
transport.put_bytes('dir/bar', 'content')
309
transport.put_bytes('bar', 'content')
579
transport.put('dir/foo', StringIO('content'))
580
transport.put('dir/bar', StringIO('content'))
581
transport.put('bar', StringIO('content'))
310
582
paths = set(transport.iter_files_recursive())
311
583
self.assertEqual(set(['dir/foo', 'dir/bar', 'bar']), paths)
313
585
def test_stat(self):
314
transport = MemoryTransport()
315
transport.put_bytes('foo', 'content')
316
transport.put_bytes('bar', 'phowar')
586
transport = memory.MemoryTransport()
587
transport.put('foo', StringIO('content'))
588
transport.put('bar', StringIO('phowar'))
317
589
self.assertEqual(7, transport.stat('foo').st_size)
318
590
self.assertEqual(6, transport.stat('bar').st_size)
321
class ChrootDecoratorTransportTest(TestCase):
322
"""Chroot decoration specific tests."""
324
def test_abspath(self):
325
# The abspath is always relative to the chroot_url.
326
server = ChrootServer(get_transport('memory:///foo/bar/'))
328
transport = get_transport(server.get_url())
329
self.assertEqual(server.get_url(), transport.abspath('/'))
331
subdir_transport = transport.clone('subdir')
332
self.assertEqual(server.get_url(), subdir_transport.abspath('/'))
335
def test_clone(self):
336
server = ChrootServer(get_transport('memory:///foo/bar/'))
338
transport = get_transport(server.get_url())
339
# relpath from root and root path are the same
340
relpath_cloned = transport.clone('foo')
341
abspath_cloned = transport.clone('/foo')
342
self.assertEqual(server, relpath_cloned.server)
343
self.assertEqual(server, abspath_cloned.server)
346
def test_chroot_url_preserves_chroot(self):
347
"""Calling get_transport on a chroot transport's base should produce a
348
transport with exactly the same behaviour as the original chroot
351
This is so that it is not possible to escape a chroot by doing::
352
url = chroot_transport.base
353
parent_url = urlutils.join(url, '..')
354
new_transport = get_transport(parent_url)
356
server = ChrootServer(get_transport('memory:///path/subpath'))
358
transport = get_transport(server.get_url())
359
new_transport = get_transport(transport.base)
360
self.assertEqual(transport.server, new_transport.server)
361
self.assertEqual(transport.base, new_transport.base)
364
def test_urljoin_preserves_chroot(self):
365
"""Using urlutils.join(url, '..') on a chroot URL should not produce a
366
URL that escapes the intended chroot.
368
This is so that it is not possible to escape a chroot by doing::
369
url = chroot_transport.base
370
parent_url = urlutils.join(url, '..')
371
new_transport = get_transport(parent_url)
373
server = ChrootServer(get_transport('memory:///path/'))
375
transport = get_transport(server.get_url())
377
InvalidURLJoin, urlutils.join, transport.base, '..')
381
class ChrootServerTest(TestCase):
383
def test_construct(self):
384
backing_transport = MemoryTransport()
385
server = ChrootServer(backing_transport)
386
self.assertEqual(backing_transport, server.backing_transport)
388
def test_setUp(self):
389
backing_transport = MemoryTransport()
390
server = ChrootServer(backing_transport)
392
self.assertTrue(server.scheme in _get_protocol_handlers().keys())
394
def test_tearDown(self):
395
backing_transport = MemoryTransport()
396
server = ChrootServer(backing_transport)
399
self.assertFalse(server.scheme in _get_protocol_handlers().keys())
401
def test_get_url(self):
402
backing_transport = MemoryTransport()
403
server = ChrootServer(backing_transport)
405
self.assertEqual('chroot-%d:///' % id(server), server.get_url())
409
class ReadonlyDecoratorTransportTest(TestCase):
410
"""Readonly decoration specific tests."""
412
def test_local_parameters(self):
413
import bzrlib.transport.readonly as readonly
414
# connect to . in readonly mode
415
transport = readonly.ReadonlyTransportDecorator('readonly+.')
416
self.assertEqual(True, transport.listable())
417
self.assertEqual(False, transport.should_cache())
418
self.assertEqual(True, transport.is_readonly())
420
def test_http_parameters(self):
421
from bzrlib.tests.HttpServer import HttpServer
422
import bzrlib.transport.readonly as readonly
423
# connect to . via http which is not listable
424
server = HttpServer()
427
transport = get_transport('readonly+' + server.get_url())
428
self.failUnless(isinstance(transport,
429
readonly.ReadonlyTransportDecorator))
430
self.assertEqual(False, transport.listable())
431
self.assertEqual(True, transport.should_cache())
432
self.assertEqual(True, transport.is_readonly())
437
class FakeNFSDecoratorTests(TestCaseInTempDir):
438
"""NFS decorator specific tests."""
440
def get_nfs_transport(self, url):
441
import bzrlib.transport.fakenfs as fakenfs
442
# connect to url with nfs decoration
443
return fakenfs.FakeNFSTransportDecorator('fakenfs+' + url)
445
def test_local_parameters(self):
446
# the listable, should_cache and is_readonly parameters
447
# are not changed by the fakenfs decorator
448
transport = self.get_nfs_transport('.')
449
self.assertEqual(True, transport.listable())
450
self.assertEqual(False, transport.should_cache())
451
self.assertEqual(False, transport.is_readonly())
453
def test_http_parameters(self):
454
# the listable, should_cache and is_readonly parameters
455
# are not changed by the fakenfs decorator
456
from bzrlib.tests.HttpServer import HttpServer
457
# connect to . via http which is not listable
458
server = HttpServer()
461
transport = self.get_nfs_transport(server.get_url())
462
self.assertIsInstance(
463
transport, bzrlib.transport.fakenfs.FakeNFSTransportDecorator)
464
self.assertEqual(False, transport.listable())
465
self.assertEqual(True, transport.should_cache())
466
self.assertEqual(True, transport.is_readonly())
470
def test_fakenfs_server_default(self):
471
# a FakeNFSServer() should bring up a local relpath server for itself
472
import bzrlib.transport.fakenfs as fakenfs
473
server = fakenfs.FakeNFSServer()
476
# the url should be decorated appropriately
477
self.assertStartsWith(server.get_url(), 'fakenfs+')
478
# and we should be able to get a transport for it
479
transport = get_transport(server.get_url())
480
# which must be a FakeNFSTransportDecorator instance.
481
self.assertIsInstance(
482
transport, fakenfs.FakeNFSTransportDecorator)
486
def test_fakenfs_rename_semantics(self):
487
# a FakeNFS transport must mangle the way rename errors occur to
488
# look like NFS problems.
489
transport = self.get_nfs_transport('.')
490
self.build_tree(['from/', 'from/foo', 'to/', 'to/bar'],
492
self.assertRaises(errors.ResourceBusy,
493
transport.rename, 'from', 'to')
496
class FakeVFATDecoratorTests(TestCaseInTempDir):
497
"""Tests for simulation of VFAT restrictions"""
499
def get_vfat_transport(self, url):
500
"""Return vfat-backed transport for test directory"""
501
from bzrlib.transport.fakevfat import FakeVFATTransportDecorator
502
return FakeVFATTransportDecorator('vfat+' + url)
504
def test_transport_creation(self):
505
from bzrlib.transport.fakevfat import FakeVFATTransportDecorator
506
transport = self.get_vfat_transport('.')
507
self.assertIsInstance(transport, FakeVFATTransportDecorator)
509
def test_transport_mkdir(self):
510
transport = self.get_vfat_transport('.')
511
transport.mkdir('HELLO')
512
self.assertTrue(transport.has('hello'))
513
self.assertTrue(transport.has('Hello'))
515
def test_forbidden_chars(self):
516
transport = self.get_vfat_transport('.')
517
self.assertRaises(ValueError, transport.has, "<NU>")
520
class BadTransportHandler(Transport):
521
def __init__(self, base_url):
522
raise DependencyNotPresent('some_lib', 'testing missing dependency')
525
class BackupTransportHandler(Transport):
526
"""Test transport that works as a backup for the BadTransportHandler"""
530
class TestTransportImplementation(TestCaseInTempDir):
531
"""Implementation verification for transports.
533
To verify a transport we need a server factory, which is a callable
534
that accepts no parameters and returns an implementation of
535
bzrlib.transport.Server.
537
That Server is then used to construct transport instances and test
538
the transport via loopback activity.
540
Currently this assumes that the Transport object is connected to the
541
current working directory. So that whatever is done
542
through the transport, should show up in the working
543
directory, and vice-versa. This is a bug, because its possible to have
544
URL schemes which provide access to something that may not be
545
result in storage on the local disk, i.e. due to file system limits, or
546
due to it being a database or some other non-filesystem tool.
548
This also tests to make sure that the functions work with both
549
generators and lists (assuming iter(list) is effectively a generator)
553
super(TestTransportImplementation, self).setUp()
554
self._server = self.transport_server()
556
self.addCleanup(self._server.tearDown)
558
def get_transport(self, relpath=None):
559
"""Return a connected transport to the local directory.
561
:param relpath: a path relative to the base url.
563
base_url = self._server.get_url()
564
url = self._adjust_url(base_url, relpath)
565
# try getting the transport via the regular interface:
566
t = get_transport(url)
567
# vila--20070607 if the following are commented out the test suite
568
# still pass. Is this really still needed or was it a forgotten
570
if not isinstance(t, self.transport_class):
571
# we did not get the correct transport class type. Override the
572
# regular connection behaviour by direct construction.
573
t = self.transport_class(url)
577
class TestLocalTransports(TestCase):
579
def test_get_transport_from_abspath(self):
580
here = os.path.abspath('.')
581
t = get_transport(here)
582
self.assertIsInstance(t, LocalTransport)
583
self.assertEquals(t.base, urlutils.local_path_to_url(here) + '/')
585
def test_get_transport_from_relpath(self):
586
here = os.path.abspath('.')
587
t = get_transport('.')
588
self.assertIsInstance(t, LocalTransport)
589
self.assertEquals(t.base, urlutils.local_path_to_url('.') + '/')
591
def test_get_transport_from_local_url(self):
592
here = os.path.abspath('.')
593
here_url = urlutils.local_path_to_url(here) + '/'
594
t = get_transport(here_url)
595
self.assertIsInstance(t, LocalTransport)
596
self.assertEquals(t.base, here_url)
598
def test_local_abspath(self):
599
here = os.path.abspath('.')
600
t = get_transport(here)
601
self.assertEquals(t.local_abspath(''), here)
604
class TestWin32LocalTransport(TestCase):
606
def test_unc_clone_to_root(self):
607
# Win32 UNC path like \\HOST\path
608
# clone to root should stop at least at \\HOST part
610
t = EmulatedWin32LocalTransport('file://HOST/path/to/some/dir/')
613
self.assertEquals(t.base, 'file://HOST/')
614
# make sure we reach the root
616
self.assertEquals(t.base, 'file://HOST/')
618
class TestConnectedTransport(TestCase):
619
"""Tests for connected to remote server transports"""
621
def test_parse_url(self):
622
t = ConnectedTransport('sftp://simple.example.com/home/source')
623
self.assertEquals(t._host, 'simple.example.com')
624
self.assertEquals(t._port, None)
625
self.assertEquals(t._path, '/home/source/')
626
self.failUnless(t._user is None)
627
self.failUnless(t._password is None)
629
self.assertEquals(t.base, 'sftp://simple.example.com/home/source/')
631
def test_parse_quoted_url(self):
632
t = ConnectedTransport('http://ro%62ey:h%40t@ex%41mple.com:2222/path')
633
self.assertEquals(t._host, 'exAmple.com')
634
self.assertEquals(t._port, 2222)
635
self.assertEquals(t._user, 'robey')
636
self.assertEquals(t._password, 'h@t')
637
self.assertEquals(t._path, '/path/')
639
# Base should not keep track of the password
640
self.assertEquals(t.base, 'http://robey@exAmple.com:2222/path/')
642
def test_parse_invalid_url(self):
643
self.assertRaises(errors.InvalidURL,
645
'sftp://lily.org:~janneke/public/bzr/gub')
647
def test_relpath(self):
648
t = ConnectedTransport('sftp://user@host.com/abs/path')
650
self.assertEquals(t.relpath('sftp://user@host.com/abs/path/sub'), 'sub')
651
self.assertRaises(errors.PathNotChild, t.relpath,
652
'http://user@host.com/abs/path/sub')
653
self.assertRaises(errors.PathNotChild, t.relpath,
654
'sftp://user2@host.com/abs/path/sub')
655
self.assertRaises(errors.PathNotChild, t.relpath,
656
'sftp://user@otherhost.com/abs/path/sub')
657
self.assertRaises(errors.PathNotChild, t.relpath,
658
'sftp://user@host.com:33/abs/path/sub')
659
# Make sure it works when we don't supply a username
660
t = ConnectedTransport('sftp://host.com/abs/path')
661
self.assertEquals(t.relpath('sftp://host.com/abs/path/sub'), 'sub')
663
# Make sure it works when parts of the path will be url encoded
664
t = ConnectedTransport('sftp://host.com/dev/%path')
665
self.assertEquals(t.relpath('sftp://host.com/dev/%path/sub'), 'sub')
667
def test_connection_sharing_propagate_credentials(self):
668
t = ConnectedTransport('foo://user@host.com/abs/path')
669
self.assertIs(None, t._get_connection())
670
self.assertIs(None, t._password)
671
c = t.clone('subdir')
672
self.assertEquals(None, c._get_connection())
673
self.assertIs(None, t._password)
675
# Simulate the user entering a password
677
connection = object()
678
t._set_connection(connection, password)
679
self.assertIs(connection, t._get_connection())
680
self.assertIs(password, t._get_credentials())
681
self.assertIs(connection, c._get_connection())
682
self.assertIs(password, c._get_credentials())
684
# credentials can be updated
685
new_password = 'even more secret'
686
c._update_credentials(new_password)
687
self.assertIs(connection, t._get_connection())
688
self.assertIs(new_password, t._get_credentials())
689
self.assertIs(connection, c._get_connection())
690
self.assertIs(new_password, c._get_credentials())
693
class TestReusedTransports(TestCase):
694
"""Tests for transport reuse"""
696
def test_reuse_same_transport(self):
697
t1 = get_transport('http://foo/')
698
t2 = get_transport('http://foo/', possible_transports=[t1])
699
self.assertIs(t1, t2)
701
# Also check that final '/' are handled correctly
702
t3 = get_transport('http://foo/path/')
703
t4 = get_transport('http://foo/path', possible_transports=[t3])
704
self.assertIs(t3, t4)
706
t5 = get_transport('http://foo/path')
707
t6 = get_transport('http://foo/path/', possible_transports=[t5])
708
self.assertIs(t5, t6)
710
def test_don_t_reuse_different_transport(self):
711
t1 = get_transport('http://foo/path')
712
t2 = get_transport('http://bar/path', possible_transports=[t1])
713
self.assertIsNot(t1, t2)
716
def get_test_permutations():
717
"""Return transport permutations to be used in testing.
719
This module registers some transports, but they're only for testing
720
registration. We don't really want to run all the transport tests against