Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Add altp package for parallel execution
Browse files Browse the repository at this point in the history
This patch adds altp package that is an alternative implementation
of ltp package, using asyncio for parallel tests execution. In order to
use it, please set ASYNC_RUN before run.

The altp package also introduces a new UI for parallel execution and it
replaces paramiko library with asyncssh, since paramiko doesn't support
coroutines.

Beware this is an EXPERIMENTAL support and it's not the ending version.
  • Loading branch information
acerv committed May 11, 2023
1 parent d192522 commit dcd6e10
Show file tree
Hide file tree
Showing 36 changed files with 7,667 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11.0"]

steps:
- name: Show OS
Expand All @@ -31,4 +31,4 @@ jobs:
run: python3 runltp-ng --help

- name: Lint with pylint
run: pylint --rcfile=pylint.ini ltp
run: pylint --rcfile=pylint.ini ltp altp
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11.0"]

steps:
- name: Show OS
Expand All @@ -25,7 +25,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: python3 -m pip install pytest pytest-xdist
run: python3 -m pip install asyncssh pytest pytest-asyncio

- name: Test with pytest
run: python3 -m pytest -n 8 -m "not qemu and not ssh"
run: python3 -m pytest -m "not qemu and not ssh and not ltx"
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ INSTALL_DIR := $(BASE_DIR)/runltp-ng.d

install:
mkdir -p $(INSTALL_DIR)/ltp
mkdir -p $(INSTALL_DIR)/altp

install -m 00644 $(top_srcdir)/tools/runltp-ng/ltp/*.py $(INSTALL_DIR)/ltp
install -m 00644 $(top_srcdir)/tools/runltp-ng/ltp/*.py $(INSTALL_DIR)/altp
install -m 00775 $(top_srcdir)/tools/runltp-ng/runltp-ng $(BASE_DIR)/runltp-ng

include $(top_srcdir)/include/mk/generic_leaf_target.mk
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ Once a new SUT class is implemented and placed inside the `ltp` package folder,
`runltp-ng -s help` command can be used to see if application correctly
recognise it.

Parallel execution
==================

The tool now supports a new experimental feature that is implemented inside the
`altp` folder. This particular feature permits to execute multiple tests in
parallel when using host execution or SSH protocol.

To enable the new parallel execution feature, please set the `ASYNC_RUN` flag
as following:

# run syscalls testing suite in parallel on host using 16 workers
ASYNC_RUN=1 ./runltp-ng --run-suite syscalls --workers 16

# run syscalls testing suite in parallel via SSH using 16 workers
# NOTE: asyncssh package must be installed in the system
./runltp-ng --sut=ssh:host myhost.com:user=root:key_file=myhost_id_rsa \
--run-suite syscalls --workers 16

Unfortunately, `paramiko` doesn't support parallel commands execution, so we
switched into `asyncssh` library that is also way more easy to use.

Development
===========

Expand Down
136 changes: 136 additions & 0 deletions altp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""
.. module:: __init__
:platform: Linux
:synopsis: ltp package definition
.. moduleauthor:: Andrea Cervesato <andrea.cervesato@suse.com>
"""
import sys
import signal
import typing
import asyncio
from altp.events import EventsHandler


class LTPException(Exception):
"""
The most generic exception that is raised by any ltp package when
something bad happens.
"""
pass


events = EventsHandler()


def get_event_loop() -> asyncio.BaseEventLoop:
"""
Return the current asyncio event loop.
"""
loop = None

try:
loop = asyncio.get_running_loop()
except (AttributeError, RuntimeError):
pass

if not loop:
try:
loop = asyncio.get_event_loop()
except RuntimeError:
pass

if not loop:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

return loop


def create_task(coro: typing.Coroutine) -> asyncio.Task:
"""
Create a new task.
"""
loop = get_event_loop()
task = loop.create_task(coro)

return task


def cancel_tasks(loop: asyncio.AbstractEventLoop) -> None:
"""
Cancel all asyncio running tasks.
"""
to_cancel = None

# pylint: disable=no-member
if sys.version_info >= (3, 7):
to_cancel = asyncio.all_tasks(loop=loop)
else:
to_cancel = asyncio.Task.all_tasks(loop=loop)

if not to_cancel:
return

for task in to_cancel:
if task.cancelled():
continue

task.cancel()

# pylint: disable=deprecated-argument
if sys.version_info >= (3, 10):
loop.run_until_complete(
asyncio.gather(*to_cancel, return_exceptions=True))
else:
loop.run_until_complete(
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True))

for task in to_cancel:
if task.cancelled():
continue

if task.exception() is not None:
loop.call_exception_handler({
'message': 'unhandled exception during asyncio.run() shutdown',
'exception': task.exception(),
'task': task,
})


def to_thread(coro: callable, *args: typing.Any) -> typing.Any:
"""
Run coroutine inside a thread. This is useful for blocking I/O operations.
"""
loop = get_event_loop()
return loop.run_in_executor(None, coro, *args)


def run(coro: typing.Coroutine) -> typing.Any:
"""
Run coroutine inside running event loop and it cancel all loop
tasks at the end. Useful when we want to run the main() function.
"""
loop = get_event_loop()

def handler() -> None:
cancel_tasks(loop)

# we don't have to handle signal again
loop.remove_signal_handler(signal.SIGTERM)
loop.add_signal_handler(signal.SIGINT, lambda: None)

for sig in (signal.SIGTERM, signal.SIGINT):
loop.add_signal_handler(sig, handler)

try:
return loop.run_until_complete(coro)
finally:
cancel_tasks(loop)


__all__ = [
"LTPException",
"events",
"get_event_loop"
]
Loading

0 comments on commit dcd6e10

Please sign in to comment.