def test_ssh_client_local_directory(self): """Tests copying directories with SSH client. Copy all the files from local directory to server, then make sure they are all present.""" test_file_data = 'test' local_test_path = 'directory_test' remote_test_path = 'directory_test_copied' for path in [local_test_path, remote_test_path]: try: shutil.rmtree(path) except OSError: pass os.mkdir(local_test_path) remote_file_paths = [] for i in range(0, 10): local_file_path = os.path.join(local_test_path, 'foo' + str(i)) remote_file_path = os.path.join(remote_test_path, 'foo' + str(i)) remote_file_paths.append(remote_file_path) test_file = open(local_file_path, 'w') test_file.write(test_file_data) test_file.close() client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.copy_file(local_test_path, remote_test_path, recurse=True) for path in remote_file_paths: self.assertTrue(os.path.isfile(path)) shutil.rmtree(local_test_path) shutil.rmtree(remote_test_path)
def test_ssh_client_sftp(self): """Test SFTP features of SSHClient. Copy local filename to server, check that data in both files is the same, make new directory on server, remove files and directory.""" test_file_data = 'test' local_filename = 'test_file' remote_test_dir, remote_filename = 'remote_test_dir', 'test_file_copy' remote_filename = os.path.sep.join([remote_test_dir, remote_filename]) remote_dir = 'remote_dir' test_file = open(local_filename, 'w') test_file.writelines([test_file_data + os.linesep]) test_file.close() client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.copy_file(local_filename, remote_filename) self.assertTrue(os.path.isdir(remote_test_dir), msg="SFTP create remote directory failed") self.assertTrue(os.path.isfile(remote_filename), msg="SFTP copy failed") copied_file = open(remote_filename, 'r') copied_file_data = copied_file.readlines()[0].strip() copied_file.close() self.assertEqual(test_file_data, copied_file_data, msg="Data in destination file %s does \ not match source %s" % (copied_file_data, test_file_data)) for filepath in [local_filename, remote_filename]: os.unlink(filepath) client.mkdir(client._make_sftp(), remote_dir) self.assertTrue(os.path.isdir(remote_dir)) for dirpath in [remote_dir, remote_test_dir]: os.rmdir(dirpath) del client