From 3bb92f4f7a7d59f38282f2402f47091a186c4516 Mon Sep 17 00:00:00 2001 From: nicholasyang Date: Wed, 25 Sep 2024 15:34:05 +0800 Subject: [PATCH] Dev: report: do not capture stderr when unarchiving tarballs so that errors will get reported --- crmsh/report/core.py | 17 +++++++++++------ test/unittests/test_report_core.py | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/crmsh/report/core.py b/crmsh/report/core.py index 825c3ba15..573486c4e 100644 --- a/crmsh/report/core.py +++ b/crmsh/report/core.py @@ -11,7 +11,6 @@ import shutil import json import ast -import typing from inspect import getmembers, isfunction from io import StringIO from typing import List @@ -280,25 +279,31 @@ def start_collector(node: str, context: Context) -> None: elif ret.stderr: print(crmsh.sh.Utils.decode_str(ret.stderr), file=sys.stderr) - compress_data = "" + archive_data_literal = "" for data in ret.stdout.decode('utf-8').split("\n"): if data.startswith(constants.COMPRESS_DATA_FLAG): # crm report data from collector - compress_data = data.lstrip(constants.COMPRESS_DATA_FLAG) + archive_data_literal = data.lstrip(constants.COMPRESS_DATA_FLAG) else: # INFO log data from collector print(data) try: # Safely evaluate the string representation of a tarball from push_data - data_object = ast.literal_eval(compress_data) + archive_data = ast.literal_eval(archive_data_literal) except (SyntaxError, ValueError) as e: logger.error(f"Error evaluating data: {e}") return # Extract the tarball in the specified working directory - cmd = f"cd {context.work_dir} && tar x" - ShellUtils().get_stdout(cmd, input_s=data_object) + child = subprocess.Popen( + ['tar', '-x'], + cwd=context.work_dir, + stdin=subprocess.PIPE, + ) + child.stdin.write(archive_data) + child.stdin.close() + child.wait() def process_dest(context: Context) -> None: diff --git a/test/unittests/test_report_core.py b/test/unittests/test_report_core.py index 3be9fd67e..54002ff67 100644 --- a/test/unittests/test_report_core.py +++ b/test/unittests/test_report_core.py @@ -491,9 +491,9 @@ def test_process_arguments(self, mock_dest, mock_node_list): core.process_arguments(mock_ctx_inst) - @mock.patch('crmsh.sh.ShellUtils.get_stdout') + @mock.patch('subprocess.Popen') @mock.patch('ast.literal_eval') - def test_start_collector(self, mock_literal_eval, mock_get_stdout): + def test_start_collector(self, mock_literal_eval, mock_popen): mock_shell = mock.Mock(crmsh.report.sh.Shell) mock_context = mock.Mock( ssh_user=None,