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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for Transport implementations.
61
62
from bzrlib.transport.memory import MemoryTransport
64
class TransportTestProviderAdapter(TestScenarioApplier):
65
"""A tool to generate a suite testing all transports for a single test.
67
This is done by copying the test once for each transport and injecting
68
the transport_class and transport_server classes into each copy. Each copy
69
is also given a new id() to make it easy to identify.
73
self.scenarios = self._test_permutations()
75
def get_transport_test_permutations(self, module):
76
"""Get the permutations module wants to have tested."""
77
if getattr(module, 'get_test_permutations', None) is None:
79
"transport module %s doesn't provide get_test_permutations()"
82
return module.get_test_permutations()
84
def _test_permutations(self):
85
"""Return a list of the klass, server_factory pairs to test."""
87
for module in _get_transport_modules():
89
permutations = self.get_transport_test_permutations(
90
reduce(getattr, (module).split('.')[1:], __import__(module)))
91
for (klass, server_factory) in permutations:
92
scenario = (server_factory.__name__,
93
{"transport_class":klass,
94
"transport_server":server_factory})
95
result.append(scenario)
96
except errors.DependencyNotPresent, e:
97
# Continue even if a dependency prevents us
98
# from adding this test
65
def get_transport_test_permutations(module):
66
"""Get the permutations module wants to have tested."""
67
if getattr(module, 'get_test_permutations', None) is None:
69
"transport module %s doesn't provide get_test_permutations()"
72
return module.get_test_permutations()
75
def transport_test_permutations():
76
"""Return a list of the klass, server_factory pairs to test."""
78
for module in _get_transport_modules():
80
permutations = get_transport_test_permutations(
81
reduce(getattr, (module).split('.')[1:], __import__(module)))
82
for (klass, server_factory) in permutations:
83
scenario = ('%s,%s' % (klass.__name__, server_factory.__name__),
84
{"transport_class":klass,
85
"transport_server":server_factory})
86
result.append(scenario)
87
except errors.DependencyNotPresent, e:
88
# Continue even if a dependency prevents us
89
# from adding this test
103
94
def load_tests(standard_tests, module, loader):
104
95
"""Multiply tests for tranport implementations."""
105
96
result = loader.suiteClass()
106
adapter = TransportTestProviderAdapter()
107
for test in tests.iter_suite_tests(standard_tests):
108
result.addTests(adapter.adapt(test))
97
scenarios = transport_test_permutations()
98
return multiply_tests(standard_tests, scenarios, result)
112
101
class TransportTests(TestTransportImplementation):
167
156
self.assertEqual(True, t.has('a'))
168
157
self.assertEqual(False, t.has('c'))
169
158
self.assertEqual(True, t.has(urlutils.escape('%')))
170
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])),
171
[True, True, False, False, True, False, True, False])
159
self.assertEqual(list(t.has_multi(['a', 'b', 'c', 'd',
160
'e', 'f', 'g', 'h'])),
161
[True, True, False, False,
162
True, False, True, False])
172
163
self.assertEqual(True, t.has_any(['a', 'b', 'c']))
173
self.assertEqual(False, t.has_any(['c', 'd', 'f', urlutils.escape('%%')]))
174
self.assertEqual(list(t.has_multi(iter(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']))),
175
[True, True, False, False, True, False, True, False])
164
self.assertEqual(False, t.has_any(['c', 'd', 'f',
165
urlutils.escape('%%')]))
166
self.assertEqual(list(t.has_multi(iter(['a', 'b', 'c', 'd',
167
'e', 'f', 'g', 'h']))),
168
[True, True, False, False,
169
True, False, True, False])
176
170
self.assertEqual(False, t.has_any(['c', 'c', 'c']))
177
171
self.assertEqual(True, t.has_any(['b', 'b', 'b']))
179
173
def test_has_root_works(self):
180
from bzrlib.smart import server
181
if self.transport_server is server.SmartTCPServer_for_testing:
174
if self.transport_server is test_server.SmartTCPServer_for_testing:
182
175
raise TestNotApplicable(
183
176
"SmartTCPServer_for_testing intentionally does not allow "
210
203
for content, f in itertools.izip(contents, content_f):
211
204
self.assertEqual(content, f.read())
206
def test_get_unknown_file(self):
207
t = self.get_transport()
209
contents = ['contents of a\n',
212
self.build_tree(files, transport=t, line_endings='binary')
213
213
self.assertRaises(NoSuchFile, t.get, 'c')
214
214
self.assertListRaises(NoSuchFile, t.get_multi, ['a', 'b', 'c'])
215
215
self.assertListRaises(NoSuchFile, t.get_multi, iter(['a', 'b', 'c']))
1070
1083
subdir.stat('./file')
1071
1084
subdir.stat('.')
1086
def test_hardlink(self):
1087
from stat import ST_NLINK
1089
t = self.get_transport()
1091
source_name = "original_target"
1092
link_name = "target_link"
1094
self.build_tree([source_name], transport=t)
1097
t.hardlink(source_name, link_name)
1099
self.failUnless(t.has(source_name))
1100
self.failUnless(t.has(link_name))
1102
st = t.stat(link_name)
1103
self.failUnlessEqual(st[ST_NLINK], 2)
1104
except TransportNotPossible:
1105
raise TestSkipped("Transport %s does not support hardlinks." %
1106
self._server.__class__)
1108
def test_symlink(self):
1109
from stat import S_ISLNK
1111
t = self.get_transport()
1113
source_name = "original_target"
1114
link_name = "target_link"
1116
self.build_tree([source_name], transport=t)
1119
t.symlink(source_name, link_name)
1121
self.failUnless(t.has(source_name))
1122
self.failUnless(t.has(link_name))
1124
st = t.stat(link_name)
1125
self.failUnless(S_ISLNK(st.st_mode))
1126
except TransportNotPossible:
1127
raise TestSkipped("Transport %s does not support symlinks." %
1128
self._server.__class__)
1130
raise tests.KnownFailure("Paramiko fails to create symlinks during tests")
1073
1132
def test_list_dir(self):
1074
1133
# TODO: Test list_dir, just try once, and if it throws, stop testing
1075
1134
t = self.get_transport()
1437
1497
'to/dir/b%2525z',
1500
def test_copy_tree_to_transport(self):
1501
transport = self.get_transport()
1502
if not transport.listable():
1503
self.assertRaises(TransportNotPossible,
1504
transport.iter_files_recursive)
1506
if transport.is_readonly():
1508
self.build_tree(['from/',
1512
'from/dir/b%25z', # make sure quoting is correct
1514
transport=transport)
1515
from_transport = transport.clone('from')
1516
to_transport = transport.clone('to')
1517
to_transport.ensure_base()
1518
from_transport.copy_tree_to_transport(to_transport)
1519
paths = set(transport.iter_files_recursive())
1520
self.assertEqual(paths,
1521
set(['from/dir/foo',
1440
1530
def test_unicode_paths(self):
1441
1531
"""Test that we can read/write files with Unicode names."""
1442
1532
t = self.get_transport()
1476
1570
transport.put_bytes('foo', 'bar')
1477
1571
transport3 = self.get_transport()
1478
1572
self.check_transport_contents('bar', transport3, 'foo')
1479
# its base should be usable.
1480
transport4 = get_transport(transport.base)
1481
self.check_transport_contents('bar', transport4, 'foo')
1483
1574
# now opening at a relative url should give use a sane result:
1484
1575
transport.mkdir('newdir')
1485
transport5 = get_transport(transport.base + "newdir")
1576
transport5 = self.get_transport('newdir')
1486
1577
transport6 = transport5.clone('..')
1487
1578
self.check_transport_contents('bar', transport6, 'foo')