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
def test_ssh_client_mkdir_single(self): """Test SFTP mkdir of SSHClient""" remote_dir = 'remote_test_dir1' try: shutil.rmtree(remote_dir) except OSError: pass client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.mkdir(client._make_sftp(), remote_dir) self.assertTrue(os.path.isdir(remote_dir), msg="SFTP recursive mkdir failed") shutil.rmtree(remote_dir) del client
def test_ssh_client_mkdir_recursive(self): """Test SFTP mkdir of SSHClient""" base_path = 'remote_test_dir1' remote_dir = os.path.sep.join( [base_path, 'remote_test_dir2', 'remote_test_dir3']) try: shutil.rmtree(base_path) except OSError: pass client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.mkdir(client._make_sftp(), remote_dir) self.assertTrue(os.path.isdir(remote_dir), msg="SFTP recursive mkdir failed") shutil.rmtree(base_path) del client
def test_ssh_client_mkdir_recursive(self): """Test SFTP mkdir of SSHClient""" base_path = 'remote_test_dir1' remote_dir = os.path.sep.join([base_path, 'remote_test_dir2', 'remote_test_dir3']) try: shutil.rmtree(base_path) except OSError: pass client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.mkdir(client._make_sftp(), remote_dir) self.assertTrue(os.path.isdir(remote_dir), msg="SFTP recursive mkdir failed") shutil.rmtree(base_path) del client
def test_ssh_client_mkdir_recursive_abspath(self): """Test SFTP mkdir of SSHClient with absolute path Absolute SFTP paths resolve under the users' home directory, not the root filesystem """ base_path = 'tmp' remote_dir = os.path.sep.join( [base_path, 'remote_test_dir2', 'remote_test_dir3']) try: shutil.rmtree(base_path) except OSError: pass client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.mkdir(client._make_sftp(), '/' + remote_dir) self.assertTrue(os.path.isdir(remote_dir), msg="SFTP recursive mkdir failed") shutil.rmtree(base_path) del client
def test_ssh_client_mkdir_recursive_abspath(self): """Test SFTP mkdir of SSHClient with absolute path Absolute SFTP paths resolve under the users' home directory, not the root filesystem """ base_path = 'tmp' remote_dir = os.path.sep.join([base_path, 'remote_test_dir2', 'remote_test_dir3']) try: shutil.rmtree(base_path) except OSError: pass client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) client.mkdir(client._make_sftp(), '/' + remote_dir) self.assertTrue(os.path.isdir(remote_dir), msg="SFTP recursive mkdir failed") shutil.rmtree(base_path) del client