Skip to content

Commit

Permalink
Issue 157: Fix D-Bus adapter tests in GitHub Actions (#159)
Browse files Browse the repository at this point in the history
Initiates a private session bus and uses that for the integration tests.

Few reasons for this:
- In GitHub Actions, Ubuntu 22.04.3 the session bus is there, but the
  address is not available in DBUS_SESSION_BUS_ADDRESS. That is a bit
  problematic. 
- One option could have been to create a private bus and set
  os.environ['DBUS_SESSION_BUS_ADDRESS'] to that bus, but there might be
  other strategies python dbus libraries other than jeepney use for
  getting the session dbus address, so making also the DbusMethodCalls
  to expect service in the private bus (not the default "SESSION" bus),
  is a bit more robust.
- Another option would have been to try to dig out the session bus
  address from the running dbus-daemon process in the Ubuntu 22.04.03
  instance, and set DBUS_SESSION_BUS_ADDRESS to that, but that would be
  perhaps a bit more involved task. In addition, not all GitHub Actions
  instances have the dbus-daemon running as session bus (for example,
  the Ubuntu 20.04), so we would had to create a new bus anyway with
  dbus-daemon.
  • Loading branch information
fohrloop authored Jan 14, 2024
1 parent 5e01266 commit 07a6075
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 67 deletions.
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

0 comments on commit 07a6075

Please sign in to comment.