@@ -133,6 +133,8 @@ def test_client_shells_read_timeout(self):
133133 def test_client_shells_timeout (self ):
134134 client = ParallelSSHClient ([self .host ], pkey = self .user_key , port = self .port ,
135135 timeout = 0.01 , num_retries = 1 )
136+ client ._make_ssh_client = MagicMock ()
137+ client ._make_ssh_client .side_effect = Timeout
136138 self .assertRaises (Timeout , client .open_shell )
137139
138140 def test_client_shells_join_timeout (self ):
@@ -1517,8 +1519,8 @@ def test_scp_send_dir_recurse(self):
15171519 except OSError :
15181520 pass
15191521
1520- def test_scp_send_large_files_timeout (self ):
1521- hosts = ['127.0.0.1%s' % (i ,) for i in range (1 , 10 )]
1522+ def test_scp_send_larger_files (self ):
1523+ hosts = ['127.0.0.1%s' % (i ,) for i in range (1 , 3 )]
15221524 servers = [OpenSSHServer (host , port = self .port ) for host in hosts ]
15231525 for server in servers :
15241526 server .start_server ()
@@ -1535,7 +1537,7 @@ def test_scp_send_large_files_timeout(self):
15351537 remote_file_names = [arg ['remote_file' ] for arg in copy_args ]
15361538 sha = sha256 ()
15371539 with open (local_filename , 'wb' ) as file_h :
1538- for _ in range (5000 ):
1540+ for _ in range (10000 ):
15391541 data = os .urandom (1024 )
15401542 file_h .write (data )
15411543 sha .update (data )
@@ -1547,13 +1549,15 @@ def test_scp_send_large_files_timeout(self):
15471549 except Exception :
15481550 raise
15491551 else :
1550- sleep ( .2 )
1552+ del client
15511553 for remote_file_name in remote_file_names :
15521554 remote_file_abspath = os .path .expanduser ('~/' + remote_file_name )
15531555 self .assertTrue (os .path .isfile (remote_file_abspath ))
15541556 with open (remote_file_abspath , 'rb' ) as remote_fh :
1555- for data in remote_fh :
1557+ data = remote_fh .read (10240 )
1558+ while data :
15561559 sha .update (data )
1560+ data = remote_fh .read (10240 )
15571561 remote_file_sha = sha .hexdigest ()
15581562 sha = sha256 ()
15591563 self .assertEqual (source_file_sha , remote_file_sha )
@@ -1679,6 +1683,60 @@ def test_scp_recv(self):
16791683 except Exception :
16801684 pass
16811685
1686+ def test_scp_recv_larger_files (self ):
1687+ hosts = ['127.0.0.1%s' % (i ,) for i in range (1 , 3 )]
1688+ servers = [OpenSSHServer (host , port = self .port ) for host in hosts ]
1689+ for server in servers :
1690+ server .start_server ()
1691+ client = ParallelSSHClient (
1692+ hosts , port = self .port , pkey = self .user_key , num_retries = 1 , timeout = 1 ,
1693+ pool_size = len (hosts ),
1694+ )
1695+ dir_name = os .path .dirname (__file__ )
1696+ remote_filename = 'test_file'
1697+ remote_filepath = os .path .join (dir_name , remote_filename )
1698+ local_filename = 'file_copy'
1699+ copy_args = [{
1700+ 'remote_file' : remote_filepath ,
1701+ 'local_file' : os .path .expanduser ("~/" + 'host_%s_%s' % (n , local_filename ))}
1702+ for n in range (len (hosts ))
1703+ ]
1704+ local_file_names = [
1705+ arg ['local_file' ] for arg in copy_args ]
1706+ sha = sha256 ()
1707+ with open (remote_filepath , 'wb' ) as file_h :
1708+ for _ in range (10000 ):
1709+ data = os .urandom (1024 )
1710+ file_h .write (data )
1711+ sha .update (data )
1712+ file_h .flush ()
1713+ source_file_sha = sha .hexdigest ()
1714+ sha = sha256 ()
1715+ cmds = client .scp_recv ('%(remote_file)s' , '%(local_file)s' , copy_args = copy_args )
1716+ try :
1717+ joinall (cmds , raise_error = True )
1718+ except Exception :
1719+ raise
1720+ else :
1721+ del client
1722+ for _local_file_name in local_file_names :
1723+ self .assertTrue (os .path .isfile (_local_file_name ))
1724+ with open (_local_file_name , 'rb' ) as fh :
1725+ data = fh .read (10240 )
1726+ while data :
1727+ sha .update (data )
1728+ data = fh .read (10240 )
1729+ local_file_sha = sha .hexdigest ()
1730+ sha = sha256 ()
1731+ self .assertEqual (source_file_sha , local_file_sha )
1732+ finally :
1733+ try :
1734+ os .unlink (remote_filepath )
1735+ for _local_file_name in local_file_names :
1736+ os .unlink (_local_file_name )
1737+ except OSError :
1738+ pass
1739+
16821740 def test_bad_hosts_value (self ):
16831741 self .assertRaises (TypeError , ParallelSSHClient , 'a host' )
16841742 self .assertRaises (TypeError , ParallelSSHClient , b'a host' )
0 commit comments