|
26 | 26 | import sys
|
27 | 27 | import string
|
28 | 28 | import random
|
| 29 | +from hashlib import sha256 |
29 | 30 | from datetime import datetime
|
30 | 31 | from platform import python_version
|
31 | 32 |
|
32 |
| -from pytest import mark |
33 | 33 | from gevent import joinall, spawn, socket, Greenlet, sleep
|
34 | 34 | from pssh.config import HostConfig
|
35 | 35 | from pssh.clients.native import ParallelSSHClient
|
@@ -1354,29 +1354,81 @@ def test_scp_send_dir_recurse(self):
|
1354 | 1354 | pass
|
1355 | 1355 |
|
1356 | 1356 | def test_scp_send(self):
|
1357 |
| - test_file_data = 'test' |
| 1357 | + server2_host = '127.0.0.11' |
| 1358 | + server3_host = '127.0.0.12' |
| 1359 | + server2 = OpenSSHServer(server2_host, port=self.port) |
| 1360 | + server3 = OpenSSHServer(server3_host, port=self.port) |
| 1361 | + for server in (server2, server3): |
| 1362 | + server.start_server() |
| 1363 | + hosts = [self.host, server2_host, server3_host] |
| 1364 | + client = ParallelSSHClient(hosts, port=self.port, pkey=self.user_key, num_retries=1) |
1358 | 1365 | local_filename = 'test_file'
|
1359 |
| - remote_test_dir, remote_filepath = 'remote_test_dir', 'test_file_copy' |
1360 |
| - with open(local_filename, 'w') as file_h: |
1361 |
| - file_h.writelines([test_file_data + os.linesep]) |
1362 |
| - remote_file_abspath = os.path.expanduser('~/' + remote_filepath) |
1363 |
| - cmds = self.client.scp_send(local_filename, remote_filepath) |
| 1366 | + remote_filepath = 'file_copy' |
| 1367 | + copy_args = [{ |
| 1368 | + 'local_file': local_filename, |
| 1369 | + 'remote_file': 'host_%s_%s' % (n, remote_filepath)} |
| 1370 | + for n in range(len(hosts))] |
| 1371 | + remote_file_names = [arg['remote_file'] for arg in copy_args] |
| 1372 | + sha = sha256() |
| 1373 | + with open(local_filename, 'wb') as file_h: |
| 1374 | + for _ in range(10000): |
| 1375 | + data = os.urandom(1024) |
| 1376 | + file_h.write(data) |
| 1377 | + sha.update(data) |
| 1378 | + source_file_sha = sha.hexdigest() |
| 1379 | + sha = sha256() |
| 1380 | + cmds = client.scp_send('%(local_file)s', '%(remote_file)s', copy_args=copy_args) |
1364 | 1381 | try:
|
1365 | 1382 | joinall(cmds, raise_error=True)
|
1366 | 1383 | except Exception:
|
1367 | 1384 | raise
|
1368 | 1385 | else:
|
1369 |
| - self.assertTrue(os.path.isfile(remote_file_abspath)) |
1370 |
| - remote_contents = open(remote_file_abspath, 'rb').read() |
1371 |
| - local_contents = open(local_filename, 'rb').read() |
1372 |
| - self.assertEqual(local_contents, remote_contents) |
| 1386 | + sleep(.2) |
| 1387 | + for remote_file_name in remote_file_names: |
| 1388 | + remote_file_abspath = os.path.expanduser('~/' + remote_file_name) |
| 1389 | + self.assertTrue(os.path.isfile(remote_file_abspath)) |
| 1390 | + with open(remote_file_abspath, 'rb') as remote_fh: |
| 1391 | + for data in remote_fh: |
| 1392 | + sha.update(data) |
| 1393 | + remote_file_sha = sha.hexdigest() |
| 1394 | + sha = sha256() |
| 1395 | + self.assertEqual(source_file_sha, remote_file_sha) |
1373 | 1396 | finally:
|
1374 | 1397 | try:
|
1375 | 1398 | os.unlink(local_filename)
|
1376 |
| - os.unlink(remote_file_abspath) |
| 1399 | + for remote_file_name in remote_file_names: |
| 1400 | + remote_file_abspath = os.path.expanduser('~/' + remote_file_name) |
| 1401 | + os.unlink(remote_file_abspath) |
1377 | 1402 | except OSError:
|
1378 | 1403 | pass
|
1379 | 1404 |
|
| 1405 | + def test_scp_send_bad_copy_args(self): |
| 1406 | + client = ParallelSSHClient([self.host, self.host]) |
| 1407 | + copy_args = [{'local_file': 'test', 'remote_file': 'test'}] |
| 1408 | + self.assertRaises(HostArgumentException, |
| 1409 | + client.scp_send, '%(local_file)s', '%(remote_file)s', |
| 1410 | + copy_args=copy_args) |
| 1411 | + |
| 1412 | + def test_scp_send_exc(self): |
| 1413 | + client = ParallelSSHClient([self.host], pkey=self.user_key, num_retries=1) |
| 1414 | + def _scp_send(*args): |
| 1415 | + raise Exception |
| 1416 | + def _client_send(*args): |
| 1417 | + return client._handle_greenlet_exc(_scp_send, 'fake') |
| 1418 | + client._scp_send = _client_send |
| 1419 | + cmds = client.scp_send('local_file', 'remote_file') |
| 1420 | + self.assertRaises(Exception, joinall, cmds, raise_error=True) |
| 1421 | + |
| 1422 | + def test_scp_recv_exc(self): |
| 1423 | + client = ParallelSSHClient([self.host], pkey=self.user_key, num_retries=1) |
| 1424 | + def _scp_recv(*args): |
| 1425 | + raise Exception |
| 1426 | + def _client_recv(*args): |
| 1427 | + return client._handle_greenlet_exc(_scp_recv, 'fake') |
| 1428 | + client._scp_recv = _client_recv |
| 1429 | + cmds = client.scp_recv('remote_file', 'local_file') |
| 1430 | + self.assertRaises(Exception, joinall, cmds, raise_error=True) |
| 1431 | + |
1380 | 1432 | def test_scp_recv_failure(self):
|
1381 | 1433 | cmds = self.client.scp_recv(
|
1382 | 1434 | 'fakey fakey fake fake', 'equally fake')
|
|
0 commit comments