tests: capture stderr/stdout/debuglog after terminating test

It was simply wrong before, but now, with capturing stacktrace that can
take sometime it is a must.

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2024-08-03 21:55:03 +02:00
parent a478ad24a9
commit a6ccf19869

View File

@ -1318,18 +1318,35 @@ class TestCase:
return None return None
def process_result_impl( def process_result_impl(self, proc, total_time: float):
self, proc, stdout: str, stderr: str, debug_log: str, total_time: float if proc:
): if proc.returncode is None:
kill_process_group(os.getpgid(proc.pid))
description = "" description = ""
debug_log = ""
if os.path.exists(self.testcase_args.debug_log_file):
with open(self.testcase_args.debug_log_file, "rb") as stream:
debug_log += self.testcase_args.debug_log_file + ":\n"
debug_log += str(stream.read(), errors="replace", encoding="utf-8")
debug_log += "\n"
stdout = ""
if os.path.exists(self.stdout_file):
with open(self.stdout_file, "rb") as stdfd:
stdout = str(stdfd.read(), errors="replace", encoding="utf-8")
stderr = ""
if os.path.exists(self.stderr_file):
with open(self.stderr_file, "rb") as stdfd:
stderr += str(stdfd.read(), errors="replace", encoding="utf-8")
if debug_log: if debug_log:
debug_log = "\n".join(debug_log.splitlines()[:100]) debug_log = "\n".join(debug_log.splitlines()[:100])
if proc: if proc:
if proc.returncode is None: if proc.returncode is None:
kill_process_group(os.getpgid(proc.pid))
if stderr: if stderr:
description += stderr description += stderr
if debug_log: if debug_log:
@ -1658,13 +1675,6 @@ class TestCase:
# Whether the test timed out will be decided later # Whether the test timed out will be decided later
pass pass
debug_log = ""
if os.path.exists(self.testcase_args.debug_log_file):
with open(self.testcase_args.debug_log_file, "rb") as stream:
debug_log += self.testcase_args.debug_log_file + ":\n"
debug_log += str(stream.read(), errors="replace", encoding="utf-8")
debug_log += "\n"
total_time = (datetime.now() - start_time).total_seconds() total_time = (datetime.now() - start_time).total_seconds()
# Normalize randomized database names in stdout, stderr files. # Normalize randomized database names in stdout, stderr files.
@ -1716,17 +1726,7 @@ class TestCase:
"https://localhost:8443/", "https://localhost:8443/",
) )
stdout = "" return proc, total_time
if os.path.exists(self.stdout_file):
with open(self.stdout_file, "rb") as stdfd:
stdout = str(stdfd.read(), errors="replace", encoding="utf-8")
stderr = ""
if os.path.exists(self.stderr_file):
with open(self.stderr_file, "rb") as stdfd:
stderr += str(stdfd.read(), errors="replace", encoding="utf-8")
return proc, stdout, stderr, debug_log, total_time
def run(self, args, suite, client_options, server_logs_level): def run(self, args, suite, client_options, server_logs_level):
start_time = datetime.now() start_time = datetime.now()
@ -1758,14 +1758,14 @@ class TestCase:
if not is_valid_utf_8(self.case_file) or ( if not is_valid_utf_8(self.case_file) or (
self.reference_file and not is_valid_utf_8(self.reference_file) self.reference_file and not is_valid_utf_8(self.reference_file)
): ):
proc, stdout, stderr, debug_log, total_time = self.run_single_test( proc, total_time = self.run_single_test(
server_logs_level, client_options server_logs_level, client_options
) )
result = self.process_result_impl( result = self.process_result_impl(proc, total_time)
proc, stdout, stderr, debug_log, total_time result.check_if_need_retry(
args, result.description, result.description, self.runs_count
) )
result.check_if_need_retry(args, stdout, stderr, self.runs_count)
# to avoid breaking CSV parser # to avoid breaking CSV parser
result.description = result.description.replace("\0", "") result.description = result.description.replace("\0", "")
else: else:
@ -1783,17 +1783,16 @@ class TestCase:
): ):
( (
proc, proc,
stdout,
stderr,
debug_log,
total_time, total_time,
) = self.run_single_test(server_logs_level, client_options) ) = self.run_single_test(server_logs_level, client_options)
result = self.process_result_impl( result = self.process_result_impl(proc, total_time)
proc, stdout, stderr, debug_log, total_time
)
result.check_if_need_retry( result.check_if_need_retry(
args, stdout, stderr, self.runs_count args,
result.description,
result.description,
self.runs_count,
) )
# to avoid breaking CSV parser # to avoid breaking CSV parser
result.description = result.description.replace("\0", "") result.description = result.description.replace("\0", "")