Skip to content

Commit

Permalink
Dev: report: do not capture stderr when unarchiving tarballs
Browse files Browse the repository at this point in the history
so that errors will get reported
  • Loading branch information
nicholasyang2022 committed Sep 25, 2024
1 parent 286a86e commit 3bb92f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions crmsh/report/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/test_report_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3bb92f4

Please sign in to comment.