1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# This program 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 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Jonas Ã…dahl"
__copyright__ = """
(c) 2021 Red Hat
(c) 2017 - 2022 Martin Pitt <martin@piware.de>
"""
import subprocess
import sys
import unittest
import dbus
import dbus.mainloop.glib
from gi.repository import GLib
import dbusmock
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
class TestSystemd(dbusmock.DBusTestCase):
"""Test mocking systemd"""
@classmethod
def setUpClass(cls):
cls.start_session_bus()
cls.start_system_bus()
cls.session_bus = cls.get_dbus(False)
cls.system_bus = cls.get_dbus(True)
def setUp(self):
self.p_mock = None
def tearDown(self):
if self.p_mock:
self.p_mock.stdout.close()
self.p_mock.terminate()
self.p_mock.wait()
def _assert_unit_property(self, unit_obj, name, expect):
value = unit_obj.Get("org.freedesktop.systemd1.Unit", name)
self.assertEqual(str(value), expect)
def _test_base(self, bus, system_bus=True):
dummy_service = "dummy-dbusmock.service"
(self.p_mock, obj_systemd) = self.spawn_server_template("systemd", {}, subprocess.PIPE, system_bus=system_bus)
systemd_mock = dbus.Interface(obj_systemd, dbusmock.MOCK_IFACE)
systemd_mock.AddMockUnit(dummy_service)
main_loop = GLib.MainLoop()
removed_jobs = []
def catch_job_removed(*args, **kwargs):
if kwargs["interface"] == "org.freedesktop.systemd1.Manager" and kwargs["member"] == "JobRemoved":
job_path = str(args[1])
removed_jobs.append(job_path)
main_loop.quit()
def wait_for_job(path):
while True:
main_loop.run()
if path in removed_jobs:
break
bus.add_signal_receiver(
catch_job_removed, interface_keyword="interface", path_keyword="path", member_keyword="member"
)
unit_path = obj_systemd.GetUnit(dummy_service)
unit_obj = bus.get_object("org.freedesktop.systemd1", unit_path)
self._assert_unit_property(unit_obj, "Id", dummy_service)
self._assert_unit_property(unit_obj, "LoadState", "loaded")
self._assert_unit_property(unit_obj, "ActiveState", "inactive")
job_path = obj_systemd.StartUnit(dummy_service, "fail")
wait_for_job(job_path)
self._assert_unit_property(unit_obj, "ActiveState", "active")
job_path = obj_systemd.StopUnit(dummy_service, "fail")
wait_for_job(job_path)
self._assert_unit_property(unit_obj, "ActiveState", "inactive")
self.p_mock.stdout.close()
self.p_mock.terminate()
self.p_mock.wait()
self.p_mock = None
def test_user(self):
self._test_base(self.session_bus, system_bus=False)
def test_system(self):
self._test_base(self.system_bus, system_bus=True)
if __name__ == "__main__":
# avoid writing to stderr
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout))
|