-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
sdbus.unittest.IsolatedDbusTestCase
Extension of `unittest.IsolatedAsyncioTestCase` from standard library. Allows to run asyncio tests in isolated D-Bus instance.
- Loading branch information
Showing
10 changed files
with
186 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Unit testing | ||
============ | ||
|
||
Python-sdbus provides several utilities to enable unit testing. | ||
|
||
.. py:currentmodule:: sdbus.unittest | ||
.. py:class:: IsolatedDbusTestCase | ||
Extension of `unittest.IsolatedAsyncioTestCase | ||
<https://docs.python.org/3/library/unittest.html#unittest.IsolatedAsyncioTestCase>`__ | ||
from standard library. | ||
|
||
Creates an isolated instance of session D-Bus. The D-Bus will be closed | ||
and cleaned up after tests are finished. | ||
|
||
Requires ``dbus-daemon`` executable be installed. | ||
|
||
.. py:attribute:: bus | ||
:type: SdBus | ||
|
||
Bus instance connected to isolated D-Bus environment. | ||
|
||
It is also set as a default bus. | ||
|
||
|
||
Usage example: :: | ||
|
||
from sdbus import DbusInterfaceCommonAsync, dbus_method_async | ||
from sdbus.unittest import IsolatedDbusTestCase | ||
|
||
class TestInterface(DbusInterfaceCommonAsync, | ||
interface_name='org.test.test', | ||
): | ||
|
||
@dbus_method_async("s", "s") | ||
async def upper(self, string: str) -> str: | ||
"""Uppercase the input""" | ||
return string.upper() | ||
|
||
def initialize_object() -> Tuple[TestInterface, TestInterface]: | ||
test_object = TestInterface() | ||
test_object.export_to_dbus('/') | ||
|
||
test_object_connection = TestInterface.new_proxy( | ||
"org.example.test", '/') | ||
|
||
return test_object, test_object_connection | ||
|
||
|
||
class TestProxy(IsolatedDbusTestCase): | ||
async def asyncSetUp(self) -> None: | ||
await super().asyncSetUp() | ||
await self.bus.request_name_async("org.example.test", 0) | ||
|
||
async def test_method_kwargs(self) -> None: | ||
test_object, test_object_connection = initialize_object() | ||
|
||
self.assertEqual( | ||
'TEST', | ||
await test_object_connection.upper('test'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
# Copyright (C) 2022 igo95862 | ||
|
||
# This file is part of python-sdbus | ||
|
||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
|
||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
|
||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
from __future__ import annotations | ||
|
||
from asyncio.subprocess import DEVNULL, create_subprocess_exec | ||
from os import environ, kill | ||
from pathlib import Path | ||
from signal import SIGTERM | ||
from tempfile import TemporaryDirectory | ||
from typing import ClassVar | ||
from unittest import IsolatedAsyncioTestCase | ||
|
||
from sdbus import sd_bus_open_user, set_default_bus | ||
|
||
dbus_config = ''' | ||
<busconfig> | ||
<type>session</type> | ||
<pidfile>{pidfile_path}</pidfile> | ||
<auth>EXTERNAL</auth> | ||
<listen>unix:path={socket_path}</listen> | ||
<policy context="default"> | ||
<allow send_destination="*" eavesdrop="true"/> | ||
<allow eavesdrop="true"/> | ||
<allow own="*"/> | ||
</policy> | ||
</busconfig> | ||
''' | ||
|
||
|
||
class IsolatedDbusTestCase(IsolatedAsyncioTestCase): | ||
dbus_executable_name: ClassVar[str] = 'dbus-daemon' | ||
|
||
async def asyncSetUp(self) -> None: | ||
self.temp_dir = TemporaryDirectory() | ||
self.temp_dir_path = Path(self.temp_dir.name) | ||
|
||
self.dbus_socket_path = self.temp_dir_path / 'test_dbus.socket' | ||
self.pid_path = self.temp_dir_path / 'dbus.pid' | ||
|
||
self.dbus_config_file = self.temp_dir_path / 'dbus.config' | ||
|
||
with open(self.dbus_config_file, mode='x') as conf_file: | ||
conf_file.write(dbus_config.format( | ||
socket_path=self.dbus_socket_path, | ||
pidfile_path=self.pid_path)) | ||
|
||
self.dbus_process = await create_subprocess_exec( | ||
self.dbus_executable_name, | ||
'--config-file', self.dbus_config_file, | ||
'--fork', | ||
stdin=DEVNULL, | ||
) | ||
error_code = await self.dbus_process.wait() | ||
if error_code != 0: | ||
raise ChildProcessError('Failed to start dbus daemon') | ||
|
||
self.old_session_bus_address = environ.get('DBUS_SESSION_BUS_ADDRESS') | ||
environ[ | ||
'DBUS_SESSION_BUS_ADDRESS'] = f"unix:path={self.dbus_socket_path}" | ||
|
||
self.bus = sd_bus_open_user() | ||
set_default_bus(self.bus) | ||
|
||
async def asyncTearDown(self) -> None: | ||
with open(self.pid_path) as pid_file: | ||
dbus_pid = int(pid_file.read()) | ||
|
||
kill(dbus_pid, SIGTERM) | ||
self.temp_dir.cleanup() | ||
environ.pop('DBUS_SESSION_BUS_ADDRESS') | ||
if self.old_session_bus_address is not None: | ||
environ['DBUS_SESSION_BUS_ADDRESS'] = self.old_session_bus_address |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.