Skip to content

Commit 61d0147

Browse files
Danpkittenis
authored andcommitted
Updated ssh client copy remote file tests
1 parent e962a5d commit 61d0147

File tree

1 file changed

+19
-90
lines changed

1 file changed

+19
-90
lines changed

tests/test_ssh_client.py

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,39 @@ def test_ssh_client_copy_remote_directory(self):
185185
"""Tests copying a remote directory to the localhost"""
186186
remote_test_directory = 'remote_test_dir'
187187
local_test_directory = 'local_test_dir'
188-
for path in [local_test_path, remote_test_path]:
188+
for path in [remote_test_directory, local_test_directory]:
189189
try:
190190
shutil.rmtree(path)
191191
except OSError:
192192
pass
193193
os.mkdir(remote_test_directory)
194194
test_files = []
195+
test_file_data = 'test'
195196
for i in range(0, 10):
196197
file_name = 'foo' + str(i)
197198
test_files.append(file_name)
198199
file_path = os.path.join(remote_test_directory, file_name)
199200
test_file = open(file_path, 'w')
200-
test_file.write('test')
201+
test_file.write(test_file_data)
201202
test_file.close()
202203
client = SSHClient(self.host, port=self.listen_port,
203204
pkey=self.user_key)
204-
client.copy_file_to_local(remote_test_directory, local_test_directory, recurse=True)
205-
for test_file in test_files:
206-
file_path = os.path.join(local_test_directory, test_file)
207-
self.assertTrue(os.path.exists(file_path))
208-
shutil.rmtree(remote_test_directory)
209-
shutil.rmtree(local_test_directory)
205+
try:
206+
self.assertRaises(ValueError, client.copy_remote_file, remote_test_directory, local_test_directory)
207+
client.copy_remote_file(remote_test_directory, local_test_directory, recurse=True)
208+
for test_file in test_files:
209+
file_path = os.path.join(local_test_directory, test_file)
210+
self.assertTrue(os.path.isfile(file_path))
211+
copied_file = open(file_path, 'r')
212+
copied_file_data = copied_file.read().strip()
213+
copied_file.close()
214+
self.assertEqual(test_file_data, copied_file_data,
215+
msg="Data in destination file %s does "
216+
"not match source %s" % (
217+
copied_file_data, test_file_data))
218+
finally:
219+
shutil.rmtree(remote_test_directory)
220+
shutil.rmtree(local_test_directory)
210221

211222
def test_ssh_client_directory_no_recurse(self):
212223
"""Tests copying directories with SSH client. Copy all the files from
@@ -233,88 +244,6 @@ def test_ssh_client_directory_no_recurse(self):
233244
self.assertRaises(ValueError, client.copy_file, local_test_path, remote_test_path)
234245
shutil.rmtree(local_test_path)
235246

236-
def test_ssh_client_sftp_from_remote(self):
237-
"""Test copying a file from a remote host to the local host. Copy
238-
remote filename to local host, check that the data is intact, make a
239-
directory on the localhost, then delete the file and directory."""
240-
test_file_data = 'test'
241-
remote_filename = 'test_remote'
242-
local_test_dir, local_filename = 'local_test_dir', 'test_local'
243-
local_filename = os.path.join(local_test_dir, local_filename)
244-
remote_file = open(remote_filename, 'w')
245-
remote_file.write(test_file_data)
246-
remote_file.close()
247-
client = SSHClient(self.host, port=self.listen_port,
248-
pkey=self.user_key)
249-
client.copy_file_to_local(remote_filename, local_filename)
250-
self.assertTrue(os.path.isdir(local_test_dir),
251-
msg="SFTP create local directory failed")
252-
self.assertTrue(os.path.isfile(local_filename),
253-
msg="SFTP copy failed")
254-
copied_file = open(local_filename, 'r')
255-
copied_file_data = copied_file.readlines()[0].strip()
256-
copied_file.close()
257-
self.assertEqual(test_file_data, copied_file_data,
258-
msg="Data in destination file %s does \
259-
not match source %s" % (copied_file_data, test_file_data))
260-
for filepath in [local_filename, remote_filename]:
261-
os.remove(filepath)
262-
os.rmdir(local_test_dir)
263-
del client
264-
265-
def test_ssh_client_sftp_from_remote_directory(self):
266-
"""Tests copying remote files to local directory. Copy all the files
267-
from the remote directory, then make sure they're all present."""
268-
test_file_data = 'test'
269-
remote_test_path = 'directory_test_remote'
270-
local_test_path = 'directory_test_local'
271-
for path in [local_test_path, remote_test_path]:
272-
try:
273-
shutil.rmtree(path)
274-
except OSError:
275-
pass
276-
os.mkdir(remote_test_path)
277-
os.mkdir(os.path.join(remote_test_path, 'subdir'))
278-
local_file_paths = []
279-
for i in range(0, 10):
280-
remote_file_path = os.path.join(remote_test_path, 'subdir', 'foo' + str(i))
281-
local_file_path = os.path.join(local_test_path, 'subdir', 'foo' + str(i))
282-
local_file_paths.append(local_file_path)
283-
test_file = open(remote_file_path, 'w')
284-
test_file.write(test_file_data)
285-
test_file.close()
286-
client = SSHClient(self.host, port=self.listen_port,
287-
pkey=self.user_key)
288-
client.copy_file_to_local(remote_test_path, local_test_path, recurse=True)
289-
for path in local_file_paths:
290-
self.assertTrue(os.path.isfile(path))
291-
shutil.rmtree(local_test_path)
292-
shutil.rmtree(remote_test_path)
293-
294-
def test_ssh_client_remote_directory_no_recurse(self):
295-
"""Tests copying directories with SSH client. Copy all the files from
296-
local directory to server, then make sure they are all present."""
297-
test_file_data = 'test'
298-
remote_test_path = 'directory_test'
299-
local_test_path = 'directory_test_copied'
300-
try:
301-
shutil.rmtree(remote_test_path)
302-
except OSError:
303-
pass
304-
os.mkdir(remote_test_path)
305-
local_file_paths = []
306-
for i in range(0, 10):
307-
remote_file_path = os.path.join(remote_test_path, 'foo' + str(i))
308-
local_file_path = os.path.join(local_test_path, 'foo' + str(i))
309-
local_file_paths.append(local_file_path)
310-
test_file = open(remote_file_path, 'w')
311-
test_file.write(test_file_data)
312-
test_file.close()
313-
client = SSHClient(self.host, port=self.listen_port,
314-
pkey=self.user_key)
315-
self.assertRaises(ValueError, client.copy_file_to_local, remote_test_path, local_test_path)
316-
shutil.rmtree(remote_test_path)
317-
318247
def test_ssh_agent_authentication(self):
319248
"""Test authentication via SSH agent.
320249
Do not provide public key to use when creating SSHClient,

0 commit comments

Comments
 (0)