Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 157: Fix D-Bus adapter tests in GitHub Actions #159

Merged
merged 5 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 101 additions & 62 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
provides the services as fixtures. The services run in separate threads.
"""

import logging
import subprocess
import sys

import pytest
Expand All @@ -10,109 +12,146 @@
from dbus_service import DbusService, start_dbus_service
else:
DbusService = None
def start_dbus_service():
return None

from wakepy.core import BusType, DbusAddress, DbusMethod

_calculator_service_addr = DbusAddress(
bus=BusType.SESSION,
service="org.github.wakepy.TestCalculatorService",
path="/org/github/wakepy/TestCalculatorService",
interface="org.github.wakepy.TestCalculatorService", # TODO: simplify
)

_numberadd_method = DbusMethod(
name="SimpleNumberAdd",
signature="uu",
params=("first_number", "second_number"),
output_signature="u",
output_params=("result",),
).of(_calculator_service_addr)

_numbermultiply_method = DbusMethod(
name="SimpleNumberMultiply",
signature="ii",
params=("first_number", "second_number"),
output_signature="i",
output_params=("result",),
).of(_calculator_service_addr)


string_operation_service_addr = DbusAddress(
bus=BusType.SESSION,
service="org.github.wakepy.TestStringOperationService",
path="/org/github/wakepy/TestStringOperationService",
interface="org.github.wakepy.TestStringOperationService", # TODO: simplify
)

_string_shorten_method = DbusMethod(
name="ShortenToNChars",
signature="su",
params=("the_string", "max_chars"),
output_signature="su",
output_params=("shortened_string", "n_removed_chars"),
).of(string_operation_service_addr)
start_dbus_service = None

from wakepy.core import DbusAddress, DbusMethod

logger = logging.getLogger(__name__)


@pytest.fixture(scope="session", autouse=True)
def private_bus():
"""A real, private message bus (dbus-daemon) instance which can be used
to test dbus adapters. You can see the bus running with

$ ps -x | grep dbus-daemon | grep -v grep | grep dbus-daemon

It is listed as something like

11150 pts/0 S 0:00 dbus-daemon --session --print-address

(includes the _start_cmd which is defined below)
"""

_start_cmd = "dbus-daemon --session --print-address"

p = subprocess.Popen(
_start_cmd.split(),
stdout=subprocess.PIPE,
shell=False,
env={"DBUS_VERBOSE": "1"},
)

bus_address = p.stdout.readline().decode("utf-8").strip()

logger.info("Initiated private bus: %s", bus_address)

yield bus_address

logger.info("Terminating private bus")
p.terminate()


@pytest.fixture(scope="session")
def string_operation_service_addr(private_bus: str) -> DbusAddress:
return DbusAddress(
bus=private_bus,
service="org.github.wakepy.TestStringOperationService",
path="/org/github/wakepy/TestStringOperationService",
interface="org.github.wakepy.TestStringOperationService", # TODO: simplify
)


@pytest.fixture(scope="session")
def calculator_service_addr():
return _calculator_service_addr
def calculator_service_addr(private_bus: str) -> DbusAddress:
return DbusAddress(
bus=private_bus,
service="org.github.wakepy.TestCalculatorService",
path="/org/github/wakepy/TestCalculatorService",
interface="org.github.wakepy.TestCalculatorService", # TODO: simplify
)


@pytest.fixture(scope="session")
def numberadd_method():
return _numberadd_method
def numberadd_method(calculator_service_addr: DbusAddress) -> DbusMethod:
return DbusMethod(
name="SimpleNumberAdd",
signature="uu",
params=("first_number", "second_number"),
output_signature="u",
output_params=("result",),
).of(calculator_service_addr)


@pytest.fixture(scope="session")
def numbermultiply_method():
return _numbermultiply_method
def numbermultiply_method(calculator_service_addr: DbusAddress) -> DbusMethod:
return DbusMethod(
name="SimpleNumberMultiply",
signature="ii",
params=("first_number", "second_number"),
output_signature="i",
output_params=("result",),
).of(calculator_service_addr)


@pytest.fixture(scope="session")
def string_shorten_method():
return _string_shorten_method
def string_shorten_method(string_operation_service_addr: DbusAddress) -> DbusMethod:
return DbusMethod(
name="ShortenToNChars",
signature="su",
params=("the_string", "max_chars"),
output_signature="su",
output_params=("shortened_string", "n_removed_chars"),
).of(string_operation_service_addr)


@pytest.fixture(scope="session")
def dbus_calculator_service():
def dbus_calculator_service(
calculator_service_addr: DbusAddress,
numberadd_method: DbusMethod,
numbermultiply_method: DbusMethod,
private_bus: str,
):
"""Provides a Dbus service called org.github.wakepy.TestCalculatorService
in the session bus"""

class TestCalculatorService(DbusService):
addr = _calculator_service_addr
addr = calculator_service_addr

def handle_method(self, method: str, args):
if method == _numberadd_method.name:
if method == numberadd_method.name:
res = args[0] + args[1]
return _numberadd_method.output_signature, (res,)
elif method == _numbermultiply_method.name:
return numberadd_method.output_signature, (res,)
elif method == numbermultiply_method.name:
res = args[0] * args[1]
return _numbermultiply_method.output_signature, (res,)
return numbermultiply_method.output_signature, (res,)

yield from start_dbus_service(TestCalculatorService)
yield from start_dbus_service(TestCalculatorService, bus_address=private_bus)


@pytest.fixture(scope="session")
def dbus_string_operation_service():
def dbus_string_operation_service(
string_operation_service_addr: DbusAddress,
string_shorten_method: DbusMethod,
private_bus: str,
):
"""Provides a Dbus service called org.github.wakepy.TestStringOperationService
in the session bus"""

class TestStringOperationService(DbusService):
addr = string_operation_service_addr

def handle_method(self, method: str, args):
if method == _string_shorten_method.name:
if method == string_shorten_method.name:
string, max_chars = args[0], args[1]
shortened_string = string[:max_chars]
if len(shortened_string) < len(string):
n_removed = len(string) - len(shortened_string)
else:
n_removed = 0
return _string_shorten_method.output_signature, (
return string_shorten_method.output_signature, (
shortened_string,
n_removed,
)

yield from start_dbus_service(TestStringOperationService)
yield from start_dbus_service(TestStringOperationService, bus_address=private_bus)
15 changes: 12 additions & 3 deletions tests/integration/dbus_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,17 @@ def handle_method(self, method: str, args: Tuple) -> Optional[Tuple[str, Tuple]]
"""


def start_dbus_service(service_cls: Type[DbusService]):
"""Start a Dbus Service in a separate thread."""
def start_dbus_service(
service_cls: Type[DbusService], bus_address: Optional[str] = None
):
"""Start a Dbus Service in a separate thread.

Parameters
----------
bus_address:
If given, should be an address of a bus from dbus-daemon
--print-address. If not given, uses the service_cls.addr.bus.
"""

queue_ = queue.Queue()
should_stop = False
Expand All @@ -157,7 +166,7 @@ def start_service(
):
logger.info(f"Launching dbus service: {service.addr.service}")

service_ = service(service.addr.bus, queue_, stop=should_stop)
service_ = service(bus_address or service.addr.bus, queue_, stop=should_stop)
service_.start(
server_name=service.addr.service,
object_path=service.addr.path,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_dbus_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def test_jeepney_dbus_adapter_nonexisting_method(calculator_service_addr):


@pytest.mark.usefixtures("dbus_calculator_service")
def test_jeepney_dbus_adapter_wrong_service_definition():
def test_jeepney_dbus_adapter_wrong_service_definition(private_bus: str):
adapter = JeepneyDbusAdapter()

wrong_service_addr = DbusAddress(
bus=BusType.SESSION,
bus=private_bus,
service="org.github.wakepy.WrongService",
path="/org/github/wakepy/TestCalculatorService",
interface="org.github.wakepy.TestCalculatorService",
Expand Down