@@ -206,6 +206,78 @@ def test_ssh_client_directory_no_recurse(self):
206206 self .assertRaises (ValueError , client .copy_file , local_test_path , remote_test_path )
207207 shutil .rmtree (local_test_path )
208208
209+ def test_ssh_client_sftp_from_remote (self ):
210+ """Test copying a file from a remote host to the local host. Copy
211+ remote filename to local host, check that the data is intact, make a
212+ directory on the localhost, then delete the file and directory."""
213+ test_file_data = 'test'
214+ remote_filename = 'test_remote'
215+ local_test_dir , local_filename = 'local_test_dir' , 'test_local'
216+ local_filename = os .path .join (local_test_dir , local_filename )
217+ remote_file = open (remote_filename , 'w' )
218+ remote_file .write (test_file_data )
219+ remote_file .close ()
220+ client = SSHClient (self .host , port = self .listen_port ,
221+ pkey = self .user_key )
222+ client .copy_file_to_local (remote_filename , local_filename )
223+ self .assertTrue (os .path .isdir (local_test_dir ),
224+ msg = "SFTP create local directory failed" )
225+ self .assertTrue (os .path .isfile (local_filename ),
226+ msg = "SFTP copy failed" )
227+ copied_file = open (local_filename , 'r' )
228+ copied_file_data = copied_file .readlines ()[0 ].strip ()
229+ copied_file .close ()
230+ self .assertEqual (test_file_data , copied_file_data ,
231+ msg = "Data in destination file %s does \
232+ not match source %s" % (copied_file_data , test_file_data ))
233+ for filepath in [local_filename , remote_filename ]:
234+ os .remove (filepath )
235+ os .rmdir (local_test_dir )
236+ del client
237+
238+ def test_ssh_client_sftp_from_remote_directory (self ):
239+ """Tests copying remote files to local directory. Copy all the files
240+ from the remote directory, then make sure they're all present."""
241+ test_file_data = 'test'
242+ remote_test_path = 'directory_test_remote'
243+ local_test_path = 'directory_test_local'
244+ os .mkdir (remote_test_path )
245+ local_file_paths = []
246+ for i in range (0 , 10 ):
247+ remote_file_path = os .path .join (remote_test_path , 'foo' + str (i ))
248+ local_file_path = os .path .join (local_test_path , 'foo' + str (i ))
249+ local_file_paths .append (local_file_path )
250+ test_file = open (remote_file_path , 'w' )
251+ test_file .write (test_file_data )
252+ test_file .close ()
253+ client = SSHClient (self .host , port = self .listen_port ,
254+ pkey = self .user_key )
255+ client .copy_file_to_local (remote_test_path , local_test_path , recurse = True )
256+ for path in local_file_paths :
257+ self .assertTrue (os .path .isfile (path ))
258+ shutil .rmtree (local_test_path )
259+ shutil .rmtree (remote_test_path )
260+
261+ def test_ssh_client_remote_directory_no_recurse (self ):
262+ """Tests copying directories with SSH client. Copy all the files from
263+ local directory to server, then make sure they are all present."""
264+ test_file_data = 'test'
265+ remote_test_path = 'directory_test'
266+ local_test_path = 'directory_test_copied'
267+ os .mkdir (remote_test_path )
268+ local_file_paths = []
269+ for i in range (0 , 10 ):
270+ remote_file_path = os .path .join (remote_test_path , 'foo' + str (i ))
271+ local_file_path = os .path .join (local_test_path , 'foo' + str (i ))
272+ local_file_paths .append (local_file_path )
273+ test_file = open (remote_file_path , 'w' )
274+ test_file .write (test_file_data )
275+ test_file .close ()
276+ client = SSHClient (self .host , port = self .listen_port ,
277+ pkey = self .user_key )
278+ self .assertRaises (ValueError , client .copy_file_to_local , remote_test_path , local_test_path )
279+ shutil .rmtree (remote_test_path )
280+
209281 def test_ssh_agent_authentication (self ):
210282 """Test authentication via SSH agent.
211283 Do not provide public key to use when creating SSHClient,
0 commit comments