2024-03-08 12:14:02 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
2024-03-08 14:33:55 +01:00
|
|
|
import os
|
2024-03-08 12:14:02 +01:00
|
|
|
import subprocess
|
2024-03-08 14:33:55 +01:00
|
|
|
import time
|
2024-03-08 12:14:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RunSubprocessMixin:
|
2024-03-08 14:33:55 +01:00
|
|
|
def is_debug_enabled(self):
|
|
|
|
return "TESTS_DEBUG" in os.environ
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
def run_subprocess(self, args):
|
2024-03-08 14:33:55 +01:00
|
|
|
begin = time.time()
|
|
|
|
if self.is_debug_enabled():
|
|
|
|
print("TEST_CALL", args)
|
2024-03-08 12:14:02 +01:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
args,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
out, err = proc.communicate()
|
2024-03-08 14:33:55 +01:00
|
|
|
if self.is_debug_enabled():
|
|
|
|
print("TEST_CALL completed", time.time() - begin)
|
|
|
|
print("STDOUT:", out.decode('utf-8'))
|
|
|
|
print("STDERR:", err.decode('utf-8'))
|
2024-03-08 12:14:02 +01:00
|
|
|
return out, err, proc.returncode
|
|
|
|
|
|
|
|
def run_subprocess_assert_returncode(self, args, expected_returncode=0):
|
|
|
|
out, err, returncode = self.run_subprocess(args)
|
2024-03-08 14:10:46 +01:00
|
|
|
decoded_out = out.decode('utf-8')
|
|
|
|
decoded_err = err.decode('utf-8')
|
2024-03-08 12:14:02 +01:00
|
|
|
self.assertEqual(
|
|
|
|
returncode,
|
|
|
|
expected_returncode,
|
|
|
|
f"Invalid return code of process {returncode} != {expected_returncode}\n"
|
2024-03-08 14:10:46 +01:00
|
|
|
f"stdout: {decoded_out}\nstderr: {decoded_err}\n",
|
2024-03-08 12:14:02 +01:00
|
|
|
)
|
|
|
|
return out, err
|