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 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 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 systemctl_stop(name): return systemctl.stop(name)