test_dbus.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # systemctl-mqtt - MQTT client triggering 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 datetime
  18. import unittest.mock
  19. import dbus
  20. import pytest
  21. import systemctl_mqtt
  22. _UTC = datetime.timezone(offset=datetime.timedelta(seconds=0))
  23. # pylint: disable=protected-access
  24. def test__get_login_manager():
  25. login_manager = systemctl_mqtt._get_login_manager()
  26. assert isinstance(login_manager, dbus.proxies.Interface)
  27. assert login_manager.dbus_interface == "org.freedesktop.login1.Manager"
  28. # https://freedesktop.org/wiki/Software/systemd/logind/
  29. assert isinstance(login_manager.CanPowerOff(), dbus.String)
  30. @pytest.mark.parametrize("action", ["poweroff", "reboot"])
  31. def test__schedule_shutdown(action):
  32. login_manager_mock = unittest.mock.MagicMock()
  33. with unittest.mock.patch(
  34. "systemctl_mqtt._get_login_manager", return_value=login_manager_mock
  35. ):
  36. systemctl_mqtt._schedule_shutdown(action=action)
  37. login_manager_mock.ScheduleShutdown.assert_called_once()
  38. schedule_args, schedule_kwargs = login_manager_mock.ScheduleShutdown.call_args
  39. assert len(schedule_args) == 2
  40. assert schedule_args[0] == action
  41. shutdown_datetime = datetime.datetime.fromtimestamp(
  42. schedule_args[1] / 10 ** 6, tz=_UTC,
  43. )
  44. delay = shutdown_datetime - datetime.datetime.now(tz=_UTC)
  45. assert delay.total_seconds() == pytest.approx(
  46. datetime.timedelta(seconds=4).total_seconds(), abs=0.1,
  47. )
  48. assert not schedule_kwargs
  49. @pytest.mark.parametrize(
  50. ("topic_suffix", "expected_action_arg"), [("poweroff", "poweroff")]
  51. )
  52. def test_mqtt_topic_suffix_action_mapping(topic_suffix, expected_action_arg):
  53. action = systemctl_mqtt._MQTT_TOPIC_SUFFIX_ACTION_MAPPING[topic_suffix]
  54. login_manager_mock = unittest.mock.MagicMock()
  55. with unittest.mock.patch(
  56. "systemctl_mqtt._get_login_manager", return_value=login_manager_mock
  57. ):
  58. action()
  59. login_manager_mock.ScheduleShutdown.assert_called_once()
  60. schedule_args, schedule_kwargs = login_manager_mock.ScheduleShutdown.call_args
  61. assert len(schedule_args) == 2
  62. assert schedule_args[0] == expected_action_arg
  63. assert not schedule_kwargs