32
36
paramiko_loaded = False
39
def set_test_transport_to_sftp(testcase):
40
"""A helper to set transports on test case instances."""
41
from bzrlib.transport.sftp import SFTPAbsoluteServer, SFTPHomeDirServer
42
if getattr(testcase, '_get_remote_is_absolute', None) is None:
43
testcase._get_remote_is_absolute = True
44
if testcase._get_remote_is_absolute:
45
testcase.transport_server = SFTPAbsoluteServer
47
testcase.transport_server = SFTPHomeDirServer
48
testcase.transport_readonly_server = HttpServer
35
51
class TestCaseWithSFTPServer(TestCaseWithTransport):
36
52
"""A test case base class that provides a sftp server on localhost."""
39
if not paramiko_loaded:
40
raise TestSkipped('you must have paramiko to run this test')
41
55
super(TestCaseWithSFTPServer, self).setUp()
42
from bzrlib.transport.sftp import SFTPAbsoluteServer, SFTPHomeDirServer
43
if getattr(self, '_get_remote_is_absolute', None) is None:
44
self._get_remote_is_absolute = True
45
if self._get_remote_is_absolute:
46
self.transport_server = SFTPAbsoluteServer
48
self.transport_server = SFTPHomeDirServer
49
self.transport_readonly_server = bzrlib.transport.http.HttpServer
56
if not paramiko_loaded:
57
raise TestSkipped('you must have paramiko to run this test')
58
set_test_transport_to_sftp(self)
51
60
def get_transport(self, path=None):
52
61
"""Return a transport relative to self._test_root."""
186
197
self.fail('expected exception not raised')
187
198
except TransportError, e:
188
self.assertEquals(str(e),
189
'~janneke: invalid port number')
199
self.assertEquals(str(e),
201
'invalid port number ~janneke in url:\n'
202
'sftp://lilypond.org:~janneke/public_html/bzr/gub ')
204
def test_get_paramiko_vendor(self):
205
"""Test that if no 'ssh' is available we get builtin paramiko"""
206
from bzrlib.transport import ssh
207
# set '.' as the only location in the path, forcing no 'ssh' to exist
208
orig_vendor = ssh._ssh_vendor
209
orig_path = set_or_unset_env('PATH', '.')
211
# No vendor defined yet, query for one
212
ssh._ssh_vendor = None
213
vendor = ssh._get_ssh_vendor()
214
self.assertIsInstance(vendor, ssh.ParamikoVendor)
216
set_or_unset_env('PATH', orig_path)
217
ssh._ssh_vendor = orig_vendor
219
def test_abspath_root_sibling_server(self):
220
from bzrlib.transport.sftp import SFTPSiblingAbsoluteServer
221
server = SFTPSiblingAbsoluteServer()
224
transport = get_transport(server.get_url())
225
self.assertFalse(transport.abspath('/').endswith('/~/'))
226
self.assertTrue(transport.abspath('/').endswith('/'))
192
232
class SFTPBranchTest(TestCaseWithSFTPServer):
225
265
self.assertEquals(b2.revision_history(), ['a1', 'a2'])
228
class SFTPFullHandshakingTest(TestCaseWithSFTPServer):
229
"""Verify that a full-handshake (SSH over loopback TCP) sftp connection works."""
268
class SSHVendorConnection(TestCaseWithSFTPServer):
269
"""Test that the ssh vendors can all connect.
271
Verify that a full-handshake (SSH over loopback TCP) sftp connection works.
273
We have 3 sftp implementations in the test suite:
274
'loopback': Doesn't use ssh, just uses a local socket. Most tests are
275
done this way to save the handshaking time, so it is not
277
'none': This uses paramiko's built-in ssh client and server, and layers
279
None: If 'ssh' exists on the machine, then it will be spawned as a
231
def test_connection(self):
284
super(SSHVendorConnection, self).setUp()
232
285
from bzrlib.transport.sftp import SFTPFullAbsoluteServer
233
self.transport_server = SFTPFullAbsoluteServer
288
"""Just a wrapper so that when created, it will set _vendor"""
289
# SFTPFullAbsoluteServer can handle any vendor,
290
# it just needs to be set between the time it is instantiated
291
# and the time .setUp() is called
292
server = SFTPFullAbsoluteServer()
293
server._vendor = self._test_vendor
295
self._test_vendor = 'loopback'
296
self.transport_server = create_server
297
f = open('a_file', 'wb')
303
def set_vendor(self, vendor):
304
self._test_vendor = vendor
306
def test_connection_paramiko(self):
307
from bzrlib.transport import ssh
308
self.set_vendor(ssh.ParamikoVendor())
309
t = self.get_transport()
310
self.assertEqual('foobar\n', t.get('a_file').read())
312
def test_connection_vendor(self):
313
raise TestSkipped("We don't test spawning real ssh,"
314
" because it prompts for a password."
315
" Enable this test if we figure out"
316
" how to prevent this.")
317
self.set_vendor(None)
318
t = self.get_transport()
319
self.assertEqual('foobar\n', t.get('a_file').read())
322
class SSHVendorBadConnection(TestCaseWithTransport):
323
"""Test that the ssh vendors handle bad connection properly
325
We don't subclass TestCaseWithSFTPServer, because we don't actually
326
need an SFTP connection.
330
if not paramiko_loaded:
331
raise TestSkipped('you must have paramiko to run this test')
332
super(SSHVendorBadConnection, self).setUp()
333
import bzrlib.transport.ssh
335
# open a random port, so we know nobody else is using it
336
# but don't actually listen on the port.
338
s.bind(('localhost', 0))
339
self.bogus_url = 'sftp://%s:%s/' % s.getsockname()
341
orig_vendor = bzrlib.transport.ssh._ssh_vendor
343
bzrlib.transport.ssh._ssh_vendor = orig_vendor
345
self.addCleanup(reset)
347
def set_vendor(self, vendor):
348
import bzrlib.transport.ssh
349
bzrlib.transport.ssh._ssh_vendor = vendor
351
def test_bad_connection_paramiko(self):
352
"""Test that a real connection attempt raises the right error"""
353
from bzrlib.transport import ssh
354
self.set_vendor(ssh.ParamikoVendor())
355
self.assertRaises(errors.ConnectionError,
356
bzrlib.transport.get_transport, self.bogus_url)
358
def test_bad_connection_ssh(self):
359
"""None => auto-detect vendor"""
360
self.set_vendor(None)
361
# This is how I would normally test the connection code
362
# it makes it very clear what we are testing.
363
# However, 'ssh' will create stipple on the output, so instead
364
# I'm using run_bzr_subprocess, and parsing the output
366
# t = bzrlib.transport.get_transport(self.bogus_url)
367
# except errors.ConnectionError:
370
# except errors.NameError, e:
371
# if 'SSHException' in str(e):
372
# raise TestSkipped('Known NameError bug in paramiko 1.6.1')
375
# self.fail('Excepted ConnectionError to be raised')
377
out, err = self.run_bzr_subprocess('log', self.bogus_url, retcode=3)
378
self.assertEqual('', out)
379
if "NameError: global name 'SSHException'" in err:
380
# We aren't fixing this bug, because it is a bug in
381
# paramiko, but we know about it, so we don't have to
383
raise TestSkipped('Known NameError bug with paramiko-1.6.1')
384
self.assertContainsRe(err, r'bzr: ERROR: Unable to connect to SSH host'
385
r' 127\.0\.0\.1:\d+; ')
388
class SFTPLatencyKnob(TestCaseWithSFTPServer):
389
"""Test that the testing SFTPServer's latency knob works."""
391
def test_latency_knob_slows_transport(self):
392
# change the latency knob to 500ms. We take about 40ms for a
393
# loopback connection ordinarily.
394
start_time = time.time()
395
self.get_server().add_latency = 0.5
396
transport = self.get_transport()
397
with_latency_knob_time = time.time() - start_time
398
self.assertTrue(with_latency_knob_time > 0.4)
400
def test_default(self):
401
# This test is potentially brittle: under extremely high machine load
402
# it could fail, but that is quite unlikely
403
start_time = time.time()
404
transport = self.get_transport()
405
regular_time = time.time() - start_time
406
self.assertTrue(regular_time < 0.5)
409
class FakeSocket(object):
410
"""Fake socket object used to test the SocketDelay wrapper without
417
def send(self, data, flags=0):
421
def sendall(self, data, flags=0):
425
def recv(self, size, flags=0):
426
if size < len(self._data):
427
result = self._data[:size]
428
self._data = self._data[size:]
436
class TestSocketDelay(TestCase):
440
if not paramiko_loaded:
441
raise TestSkipped('you must have paramiko to run this test')
443
def test_delay(self):
444
from bzrlib.transport.sftp import SocketDelay
445
sending = FakeSocket()
446
receiving = SocketDelay(sending, 0.1, bandwidth=1000000,
448
# check that simulated time is charged only per round-trip:
449
t1 = SocketDelay.simulated_time
450
receiving.send("connect1")
451
self.assertEqual(sending.recv(1024), "connect1")
452
t2 = SocketDelay.simulated_time
453
self.assertAlmostEqual(t2 - t1, 0.1)
454
receiving.send("connect2")
455
self.assertEqual(sending.recv(1024), "connect2")
456
sending.send("hello")
457
self.assertEqual(receiving.recv(1024), "hello")
458
t3 = SocketDelay.simulated_time
459
self.assertAlmostEqual(t3 - t2, 0.1)
460
sending.send("hello")
461
self.assertEqual(receiving.recv(1024), "hello")
462
sending.send("hello")
463
self.assertEqual(receiving.recv(1024), "hello")
464
sending.send("hello")
465
self.assertEqual(receiving.recv(1024), "hello")
466
t4 = SocketDelay.simulated_time
467
self.assertAlmostEqual(t4, t3)
469
def test_bandwidth(self):
470
from bzrlib.transport.sftp import SocketDelay
471
sending = FakeSocket()
472
receiving = SocketDelay(sending, 0, bandwidth=8.0/(1024*1024),
474
# check that simulated time is charged only per round-trip:
475
t1 = SocketDelay.simulated_time
476
receiving.send("connect")
477
self.assertEqual(sending.recv(1024), "connect")
478
sending.send("a" * 100)
479
self.assertEqual(receiving.recv(1024), "a" * 100)
480
t2 = SocketDelay.simulated_time
481
self.assertAlmostEqual(t2 - t1, 100 + 7)