test_dbus.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # systemctl-mqtt - MQTT client triggering & reporting shutdown on systemd-based systems
  2. #
  3. # Copyright (C) 2020 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 asyncio
  18. import datetime
  19. import logging
  20. import typing
  21. import unittest.mock
  22. import jeepney
  23. import jeepney.low_level
  24. import jeepney.wrappers
  25. import pytest
  26. import systemctl_mqtt._dbus
  27. # pylint: disable=protected-access
  28. def test_get_login_manager_proxy():
  29. login_manager = systemctl_mqtt._dbus.get_login_manager_proxy()
  30. assert isinstance(login_manager, jeepney.io.blocking.Proxy)
  31. assert login_manager._msggen.interface == "org.freedesktop.login1.Manager"
  32. # https://freedesktop.org/wiki/Software/systemd/logind/
  33. assert login_manager.CanPowerOff() in {("yes",), ("challenge",)}
  34. def test__log_shutdown_inhibitors_some(caplog):
  35. login_manager = unittest.mock.MagicMock()
  36. login_manager.ListInhibitors.return_value = (
  37. [
  38. (
  39. "shutdown:sleep",
  40. "Developer",
  41. "Haven't pushed my commits yet",
  42. "delay",
  43. 1000,
  44. 1234,
  45. ),
  46. ("shutdown", "Editor", "", "Unsafed files open", 0, 42),
  47. ],
  48. )
  49. with caplog.at_level(logging.DEBUG):
  50. systemctl_mqtt._dbus._log_shutdown_inhibitors(login_manager)
  51. assert len(caplog.records) == 2
  52. assert caplog.records[0].levelno == logging.DEBUG
  53. assert (
  54. caplog.records[0].message
  55. == "detected shutdown inhibitor Developer (pid=1234, uid=1000, mode=delay): "
  56. + "Haven't pushed my commits yet"
  57. )
  58. def test__log_shutdown_inhibitors_none(caplog):
  59. login_manager = unittest.mock.MagicMock()
  60. login_manager.ListInhibitors.return_value = ([],)
  61. with caplog.at_level(logging.DEBUG):
  62. systemctl_mqtt._dbus._log_shutdown_inhibitors(login_manager)
  63. assert len(caplog.records) == 1
  64. assert caplog.records[0].levelno == logging.DEBUG
  65. assert caplog.records[0].message == "no shutdown inhibitor locks found"
  66. def test__log_shutdown_inhibitors_fail(caplog):
  67. login_manager = unittest.mock.MagicMock()
  68. login_manager.ListInhibitors.side_effect = DBusErrorResponseMock("error", "mocked")
  69. with caplog.at_level(logging.DEBUG):
  70. systemctl_mqtt._dbus._log_shutdown_inhibitors(login_manager)
  71. assert len(caplog.records) == 1
  72. assert caplog.records[0].levelno == logging.WARNING
  73. assert (
  74. caplog.records[0].message
  75. == "failed to fetch shutdown inhibitors: [error] mocked"
  76. )
  77. @pytest.mark.parametrize("action", ["poweroff", "reboot"])
  78. @pytest.mark.parametrize("delay", [datetime.timedelta(0), datetime.timedelta(hours=1)])
  79. def test__schedule_shutdown(action, delay):
  80. login_manager_mock = unittest.mock.MagicMock()
  81. with unittest.mock.patch(
  82. "systemctl_mqtt._dbus.get_login_manager_proxy", return_value=login_manager_mock
  83. ):
  84. login_manager_mock.ListInhibitors.return_value = ([],)
  85. systemctl_mqtt._dbus.schedule_shutdown(action=action, delay=delay)
  86. login_manager_mock.ScheduleShutdown.assert_called_once()
  87. schedule_args, schedule_kwargs = login_manager_mock.ScheduleShutdown.call_args
  88. assert not schedule_args
  89. assert schedule_kwargs.pop("action") == action
  90. actual_delay = schedule_kwargs.pop("time") - datetime.datetime.now()
  91. assert actual_delay.total_seconds() == pytest.approx(delay.total_seconds(), abs=0.1)
  92. assert not schedule_kwargs
  93. class DBusErrorResponseMock(jeepney.wrappers.DBusErrorResponse):
  94. # pylint: disable=missing-class-docstring,super-init-not-called
  95. def __init__(self, name: str, data: typing.Any):
  96. self.name = name
  97. self.data = data
  98. @pytest.mark.parametrize("action", ["poweroff"])
  99. @pytest.mark.parametrize(
  100. ("error_name", "error_message", "log_message"),
  101. [
  102. (
  103. "test error",
  104. "test message",
  105. "[test error] ('test message',)",
  106. ),
  107. (
  108. "org.freedesktop.DBus.Error.InteractiveAuthorizationRequired",
  109. "Interactive authentication required.",
  110. "unauthorized; missing polkit authorization rules?",
  111. ),
  112. ],
  113. )
  114. def test__schedule_shutdown_fail(
  115. caplog, action, error_name, error_message, log_message
  116. ):
  117. login_manager_mock = unittest.mock.MagicMock()
  118. login_manager_mock.ScheduleShutdown.side_effect = DBusErrorResponseMock(
  119. name=error_name,
  120. data=(error_message,),
  121. )
  122. login_manager_mock.ListInhibitors.return_value = ([],)
  123. with unittest.mock.patch(
  124. "systemctl_mqtt._dbus.get_login_manager_proxy", return_value=login_manager_mock
  125. ), caplog.at_level(logging.DEBUG):
  126. systemctl_mqtt._dbus.schedule_shutdown(
  127. action=action, delay=datetime.timedelta(seconds=21)
  128. )
  129. login_manager_mock.ScheduleShutdown.assert_called_once()
  130. assert len(caplog.records) == 3
  131. assert caplog.records[0].levelno == logging.INFO
  132. assert caplog.records[0].message.startswith(f"scheduling {action} for ")
  133. assert caplog.records[1].levelno == logging.ERROR
  134. assert caplog.records[1].message == f"failed to schedule {action}: {log_message}"
  135. assert "inhibitor" in caplog.records[2].message
  136. def test_suspend(caplog):
  137. login_manager_mock = unittest.mock.MagicMock()
  138. with unittest.mock.patch(
  139. "systemctl_mqtt._dbus.get_login_manager_proxy", return_value=login_manager_mock
  140. ), caplog.at_level(logging.INFO):
  141. systemctl_mqtt._dbus.suspend()
  142. login_manager_mock.Suspend.assert_called_once_with(interactive=False)
  143. assert len(caplog.records) == 1
  144. assert caplog.records[0].levelno == logging.INFO
  145. assert caplog.records[0].message == "suspending system"
  146. def test_lock_all_sessions(caplog):
  147. login_manager_mock = unittest.mock.MagicMock()
  148. with unittest.mock.patch(
  149. "systemctl_mqtt._dbus.get_login_manager_proxy", return_value=login_manager_mock
  150. ), caplog.at_level(logging.INFO):
  151. systemctl_mqtt._dbus.lock_all_sessions()
  152. login_manager_mock.LockSessions.assert_called_once_with()
  153. assert len(caplog.records) == 1
  154. assert caplog.records[0].levelno == logging.INFO
  155. assert caplog.records[0].message == "instruct all sessions to activate screen locks"
  156. @pytest.mark.asyncio
  157. async def test__dbus_signal_loop():
  158. # pylint: disable=too-many-locals,too-many-arguments
  159. state_mock = unittest.mock.AsyncMock()
  160. with unittest.mock.patch(
  161. "jeepney.io.asyncio.open_dbus_router",
  162. ) as open_dbus_router_mock:
  163. async with open_dbus_router_mock() as dbus_router_mock:
  164. pass
  165. add_match_reply = unittest.mock.Mock()
  166. add_match_reply.body = ()
  167. dbus_router_mock.send_and_get_reply.return_value = add_match_reply
  168. msg_queue = asyncio.Queue()
  169. await msg_queue.put(jeepney.low_level.Message(header=None, body=(False,)))
  170. await msg_queue.put(jeepney.low_level.Message(header=None, body=(True,)))
  171. await msg_queue.put(jeepney.low_level.Message(header=None, body=(False,)))
  172. dbus_router_mock.filter = unittest.mock.MagicMock()
  173. dbus_router_mock.filter.return_value.__enter__.return_value = msg_queue
  174. # asyncio.TaskGroup added in python3.11
  175. loop_task = asyncio.create_task(
  176. systemctl_mqtt._dbus_signal_loop(
  177. state=state_mock, mqtt_client=unittest.mock.MagicMock()
  178. )
  179. )
  180. async def _abort_after_msg_queue():
  181. await msg_queue.join()
  182. loop_task.cancel()
  183. with pytest.raises(asyncio.exceptions.CancelledError):
  184. await asyncio.gather(*(loop_task, _abort_after_msg_queue()))
  185. assert unittest.mock.call(bus="SYSTEM") in open_dbus_router_mock.call_args_list
  186. dbus_router_mock.filter.assert_called_once()
  187. (filter_match_rule,) = dbus_router_mock.filter.call_args[0]
  188. assert (
  189. filter_match_rule.header_fields["interface"] == "org.freedesktop.login1.Manager"
  190. )
  191. assert filter_match_rule.header_fields["member"] == "PrepareForShutdown"
  192. add_match_msg = dbus_router_mock.send_and_get_reply.call_args[0][0]
  193. assert (
  194. add_match_msg.header.fields[jeepney.low_level.HeaderFields.member] == "AddMatch"
  195. )
  196. assert add_match_msg.body == (
  197. "interface='org.freedesktop.login1.Manager',member='PrepareForShutdown'"
  198. ",path='/org/freedesktop/login1',type='signal'",
  199. )
  200. assert [
  201. c[1]["active"] for c in state_mock.preparing_for_shutdown_handler.call_args_list
  202. ] == [False, True, False]