def test_single_unit(): unit = "test-sleep-{}.service".format(uuid.uuid4()) properties = ("Names", "LoadState", "ActiveState") systemd.run(["sleep", "5"], unit=unit) try: r = systemctl.show(unit, properties=properties) assert r == [{ "ActiveState": "active", "LoadState": "loaded", "Names": unit, }] r = systemctl.show(unit) assert len(r) == 1 assert r[0]["ActiveState"] == "active" assert r[0]["LoadState"] == "loaded" assert r[0]["Names"] == unit finally: systemctl.stop(unit) r = systemctl.show(unit, properties=properties) assert r == [{ "ActiveState": "inactive", "LoadState": "not-found", "Names": unit, }]
def test_show_pattern_not_found(): pattern = "test-*-{}.service".format(uuid.uuid4()) properties = ("Names", "LoadState", "ActiveState") r = systemctl.show(pattern, properties=properties) assert r == [] r = systemctl.show(pattern) assert r == []
def test_show_unit_not_found(): unit = "test-sleep-{}.service".format(uuid.uuid4()) properties = ("Names", "LoadState", "ActiveState") r = systemctl.show(unit, properties=properties) assert r == [{ "ActiveState": "inactive", "LoadState": "not-found", "Names": unit, }] r = systemctl.show(unit) assert len(r) == 1 assert r[0]["ActiveState"] == "inactive" assert r[0]["LoadState"] == "not-found" assert r[0]["Names"] == unit
def _lvmetad_configured(): """ Return True if both lvmetad service and socket are masked and disabled, otherwise return False. TODO: remove this function once we don't support Fedora 30. On Fedora 31 and RHEL8 lvmetad is not supported anymore. """ pattern = "lvm2-lvmetad*" properties = ("Names", "LoadState", "ActiveState") units = systemctl.show(pattern, properties=properties) if not units: # There's no lvmetad and thus nothing to configure return True not_configured = [] for unit in units: # ActiveState may be "inactive" or "failed", both are good. if unit["LoadState"] != "masked" or unit["ActiveState"] == "active": not_configured.append(unit) if not_configured: _log("Units need configuration: %s", not_configured) return False return True
def test_multiple_units(): unit1 = "test-sleep-{}.service".format(uuid.uuid4()) unit2 = "test-sleep-{}.service".format(uuid.uuid4()) pattern = "test-sleep-*.service" properties = ("Names", "LoadState", "ActiveState") systemd.run(["sleep", "5"], unit=unit1) try: r = systemctl.show(pattern, properties=properties) assert r == [ { "ActiveState": "active", "LoadState": "loaded", "Names": unit1, }, ] systemd.run(["sleep", "5"], unit=unit2) r = systemctl.show(pattern, properties=properties) assert r == [ { "ActiveState": "active", "LoadState": "loaded", "Names": unit1, }, { "ActiveState": "active", "LoadState": "loaded", "Names": unit2, }, ] r = systemctl.show(pattern) assert len(r) == 2 assert r[0]["LoadState"] == "loaded" assert r[0]["Names"] == unit1 assert r[1]["LoadState"] == "loaded" assert r[1]["Names"] == unit2 finally: systemctl.stop(pattern) r = systemctl.show(pattern, properties=properties) assert r == []
def stop_server(server_id): service = _service_name(server_id) # systemctl.stop() does not have a way to detect that a server was not # running, so we need to check the service state before we stop it. This # is racy, but should be fine since service names are random and we start # them only once. info = systemctl.show(service, properties=("LoadState", )) if info and info[0]["LoadState"] == "not-found": log.info("Transient service %s is not running", service) return log.info("Stopping transient service %s", service) systemctl.stop(service)
def stop_server(server_id): service = _service_name(server_id) # systemctl.stop() does not have a way to detect that a server was not # running, so we need to check the service state before we stop it. This # is racy, but should be fine since service names are random and we start # them only once. info = systemctl.show(service, properties=("LoadState",)) if info and info[0]["LoadState"] == "not-found": log.info("Transient service %s is not running", service) return log.info("Stopping transient service %s", service) systemctl.stop(service)
def _find_libvirt_socket_units(): return systemctl.show("libvirtd*.socket", ( "Names", "LoadState", ))
def _unit_enabled(unit_name): props = systemctl.show(unit_name, ("UnitFileState", )) return props[0]["UnitFileState"] == "enabled"
def _unit_requirements(unit_name): return systemctl.show(unit_name, ("Requires", ))[0]["Requires"]