Skip to content

Commit

Permalink
fixed test_restart_unit_with_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
goshansp committed Jan 19, 2025
1 parent 6b1c58e commit a70d3d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 2 additions & 3 deletions systemctl_mqtt/_dbus/service_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ def restart_unit(unit_name: str):
proxy.RestartUnit(unit_name, "replace")
_LOGGER.debug("Restarting unit: %s", unit_name)
# pylint: disable=broad-exception-caught
# except jeepney.wrappers.DBusErrorResponse as exc:
except Exception as exc:
_LOGGER.error("Failed to restart unit: %s because %s", unit_name, str(exc))
except jeepney.wrappers.DBusErrorResponse as exc:
_LOGGER.error("Failed to restart unit: %s because %s ", unit_name, exc.name)


def get_service_manager_proxy() -> jeepney.io.blocking.Proxy:
Expand Down
14 changes: 12 additions & 2 deletions tests/dbus/message-generators/test_service_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
import pytest
import jeepney.io.asyncio
import jeepney.low_level
import typing

import systemctl_mqtt

# pylint: disable=protected-access


class DBusErrorResponseMock(jeepney.wrappers.DBusErrorResponse):
# pylint: disable=missing-class-docstring,super-init-not-called
def __init__(self, name: str, data: typing.Any):
self.name = name
self.data = data


@pytest.mark.asyncio
async def test__get_unit_path() -> None:
router_mock = unittest.mock.AsyncMock()
Expand Down Expand Up @@ -83,7 +91,9 @@ def test__restart_unit_method_call():

def test_restart_unit_with_exception():
mock_proxy = unittest.mock.MagicMock()
mock_proxy.RestartUnit.side_effect = Exception("DBus error")
mock_proxy.RestartUnit.side_effect = side_effect = DBusErrorResponseMock(
"DBus error", ("mocked",)
)
with unittest.mock.patch(
"systemctl_mqtt._dbus.service_manager.get_service_manager_proxy",
return_value=mock_proxy,
Expand All @@ -92,5 +102,5 @@ def test_restart_unit_with_exception():
) as mock_logger:
systemctl_mqtt._dbus.service_manager.restart_unit("example.service")
mock_logger.error.assert_called_once_with(
"Failed to restart unit: %s because %s", "example.service", "DBus error"
"Failed to restart unit: %s because %s ", "example.service", "DBus error"
)

0 comments on commit a70d3d7

Please sign in to comment.