test_service_manager.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # systemctl-mqtt - MQTT client triggering & reporting shutdown on systemd-based systems
  2. #
  3. # Copyright (C) 2024 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import unittest.mock
  18. import pytest
  19. import jeepney.io.asyncio
  20. import jeepney.low_level
  21. import systemctl_mqtt
  22. # pylint: disable=protected-access
  23. @pytest.mark.asyncio
  24. async def test__get_unit_path() -> None:
  25. router_mock = unittest.mock.AsyncMock()
  26. reply_mock = unittest.mock.MagicMock()
  27. expected_path = "/org/freedesktop/systemd1/unit/ssh_2eservice"
  28. reply_mock.body = (expected_path,)
  29. router_mock.send_and_get_reply.return_value = reply_mock
  30. service_manager = jeepney.io.asyncio.Proxy(
  31. msggen=systemctl_mqtt._dbus.service_manager.ServiceManager(),
  32. router=router_mock,
  33. )
  34. assert (
  35. await systemctl_mqtt._get_unit_path(
  36. service_manager=service_manager, unit_name="ssh.service"
  37. )
  38. == expected_path
  39. )
  40. router_mock.send_and_get_reply.assert_awaited_once()
  41. (msg,), send_kwargs = router_mock.send_and_get_reply.await_args
  42. assert isinstance(msg, jeepney.low_level.Message)
  43. assert msg.header.fields == {
  44. jeepney.low_level.HeaderFields.path: "/org/freedesktop/systemd1",
  45. jeepney.low_level.HeaderFields.destination: "org.freedesktop.systemd1",
  46. jeepney.low_level.HeaderFields.interface: "org.freedesktop.systemd1.Manager",
  47. jeepney.low_level.HeaderFields.member: "GetUnit",
  48. jeepney.low_level.HeaderFields.signature: "s",
  49. }
  50. assert msg.body == ("ssh.service",)
  51. assert not send_kwargs