Skip to content

Commit

Permalink
scripts: twister: Adapt Twister Unit tests to Windows
Browse files Browse the repository at this point in the history
Some solutions used in twister are not multiplatform.
This changes them and fixes some relevant tests,
disabling those deemed Linux-only and removing prints.

Signed-off-by: Lukasz Mrugala <lukaszx.mrugala@intel.com>
  • Loading branch information
LukaszMrugala committed Aug 28, 2024
1 parent ce42c88 commit d76f691
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 74 deletions.
26 changes: 16 additions & 10 deletions scripts/pylib/twister/twisterlib/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ def terminate_process(proc):
so we need to use try_kill_process_by_pid.
"""

for child in psutil.Process(proc.pid).children(recursive=True):
parent = psutil.Process(proc.pid)
to_terminate = parent.children(recursive=True)
to_terminate.append(parent)

for p in to_terminate:
try:
p.terminate()
except (ProcessLookupError, psutil.NoSuchProcess):
pass
_, alive = psutil.wait_procs(to_terminate, timeout=1)

for p in alive:
try:
os.kill(child.pid, signal.SIGTERM)
p.kill()
except (ProcessLookupError, psutil.NoSuchProcess):
pass
proc.terminate()
# sleep for a while before attempting to kill
time.sleep(0.5)
proc.kill()
_, alive = psutil.wait_procs(to_terminate, timeout=1)


class Handler:
Expand Down Expand Up @@ -186,10 +194,8 @@ def try_kill_process_by_pid(self):
pid = int(open(self.pid_fn).read())
os.unlink(self.pid_fn)
self.pid_fn = None # clear so we don't try to kill the binary twice
try:
os.kill(pid, signal.SIGKILL)
except (ProcessLookupError, psutil.NoSuchProcess):
pass
p = psutil.Process(pid)
terminate_process(p)

def _output_reader(self, proc):
self.line = proc.stdout.readline()
Expand Down
12 changes: 9 additions & 3 deletions scripts/pylib/twister/twisterlib/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import xml.etree.ElementTree as ET
import string
from datetime import datetime
from pathlib import PosixPath
from pathlib import PosixPath, WindowsPath

from twisterlib.statuses import TwisterStatus

Expand Down Expand Up @@ -267,8 +267,14 @@ def json_report(self, filename, version="NA", platform=None, filters=None):
report_options = self.env.non_default_options()

# Resolve known JSON serialization problems.
for k,v in report_options.items():
report_options[k] = str(v) if type(v) in [PosixPath] else v
for k, v in report_options.items():
pathlikes = [PosixPath, WindowsPath]
value = v
if type(v) in pathlikes:
value = os.fspath(v)
if type(v) in [list]:
value = [os.fspath(x) if type(x) in pathlikes else x for x in v]
report_options[k] = value

report = {}
report["environment"] = {"os": os.name,
Expand Down
14 changes: 14 additions & 0 deletions scripts/pylib/twister/twisterlib/twister_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def setup_logging(outdir, log_file, verbose, timestamps):
logger.addHandler(ch)
logger.addHandler(fh)

def end_logging():
handlers = logger.handlers[:]
for handler in handlers:
logger.removeHandler(handler)
handler.close()

def init_color(colorama_strip):
colorama.init(strip=colorama_strip)
Expand Down Expand Up @@ -115,6 +120,7 @@ def main(options, default_options):
hwm = HardwareMap(env)
ret = hwm.discover()
if ret == 0:
end_logging()
return 0

env.hwm = hwm
Expand All @@ -124,15 +130,18 @@ def main(options, default_options):
tplan.discover()
except RuntimeError as e:
logger.error(f"{e}")
end_logging()
return 1

if tplan.report() == 0:
end_logging()
return 0

try:
tplan.load()
except RuntimeError as e:
logger.error(f"{e}")
end_logging()
return 1

if VERBOSE > 1:
Expand Down Expand Up @@ -162,13 +171,16 @@ def main(options, default_options):

if options.save_tests:
report.json_report(options.save_tests)
end_logging()
return 0

if options.report_summary is not None:
if options.report_summary < 0:
logger.error("The report summary value cannot be less than 0")
end_logging()
return 1
report.synopsis()
end_logging()
return 0

if options.device_testing and not options.build_only:
Expand All @@ -179,6 +191,7 @@ def main(options, default_options):
if options.dry_run:
duration = time.time() - start_time
logger.info("Completed in %d seconds" % (duration))
end_logging()
return 0

if options.short_build_path:
Expand Down Expand Up @@ -235,6 +248,7 @@ def main(options, default_options):
artifacts.package()

logger.info("Run completed")
end_logging()
if (
runner.results.failed
or runner.results.error
Expand Down
3 changes: 1 addition & 2 deletions scripts/tests/twister/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ def mocked_abspath(path):

with mock.patch('subprocess.run', mock.Mock(side_effect=mock_run)):
twister_env.check_zephyr_version()
print(expected_logs)
print(caplog.text)

assert twister_env.version == expected_version
assert twister_env.commit_date == expected_commit_date
assert all([expected_log in caplog.text for expected_log in expected_logs])
Expand Down
Loading

0 comments on commit d76f691

Please sign in to comment.