mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-22 20:11:30 +00:00
test(backups): simulating async service start n stop
This commit is contained in:
parent
8e29634d02
commit
d33e9d6335
|
@ -1,7 +1,12 @@
|
||||||
"""Class representing Bitwarden service"""
|
"""Class representing Bitwarden service"""
|
||||||
import base64
|
import base64
|
||||||
import typing
|
import typing
|
||||||
|
import subprocess
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from os import path
|
||||||
|
|
||||||
|
# from enum import Enum
|
||||||
|
|
||||||
from selfprivacy_api.jobs import Job
|
from selfprivacy_api.jobs import Job
|
||||||
from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus
|
from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus
|
||||||
|
@ -11,13 +16,24 @@ import selfprivacy_api.utils.network as network_utils
|
||||||
|
|
||||||
from selfprivacy_api.services.test_service.icon import BITWARDEN_ICON
|
from selfprivacy_api.services.test_service.icon import BITWARDEN_ICON
|
||||||
|
|
||||||
|
DEFAULT_DELAY = 0
|
||||||
|
|
||||||
|
|
||||||
class DummyService(Service):
|
class DummyService(Service):
|
||||||
"""A test service"""
|
"""A test service"""
|
||||||
|
|
||||||
|
folders: List[str] = []
|
||||||
|
|
||||||
def __init_subclass__(cls, folders: List[str]):
|
def __init_subclass__(cls, folders: List[str]):
|
||||||
cls.folders = folders
|
cls.folders = folders
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
dir = self.folders[0]
|
||||||
|
status_file = path.join(dir, "service_status")
|
||||||
|
with open(status_file, "w") as file:
|
||||||
|
file.write(ServiceStatus.ACTIVE.value)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_id() -> str:
|
def get_id() -> str:
|
||||||
"""Return service id."""
|
"""Return service id."""
|
||||||
|
@ -61,38 +77,61 @@ class DummyService(Service):
|
||||||
def is_enabled() -> bool:
|
def is_enabled() -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def get_status() -> ServiceStatus:
|
def status_file(cls) -> str:
|
||||||
"""
|
dir = cls.folders[0]
|
||||||
Return Bitwarden status from systemd.
|
return path.join(dir, "service_status")
|
||||||
Use command return code to determine status.
|
|
||||||
|
|
||||||
Return code 0 means service is running.
|
@classmethod
|
||||||
Return code 1 or 2 means service is in error stat.
|
def set_status(cls, status: ServiceStatus):
|
||||||
Return code 3 means service is stopped.
|
with open(cls.status_file(), "w") as file:
|
||||||
Return code 4 means service is off.
|
status_string = file.write(status.value)
|
||||||
"""
|
|
||||||
return ServiceStatus.ACTIVE
|
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def enable():
|
def get_status(cls) -> ServiceStatus:
|
||||||
|
with open(cls.status_file(), "r") as file:
|
||||||
|
status_string = file.read().strip()
|
||||||
|
return ServiceStatus[status_string]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def change_status_with_async_delay(
|
||||||
|
cls, new_status: ServiceStatus, delay_sec: float
|
||||||
|
):
|
||||||
|
"""simulating a delay on systemd side"""
|
||||||
|
dir = cls.folders[0]
|
||||||
|
status_file = path.join(dir, "service_status")
|
||||||
|
|
||||||
|
command = [
|
||||||
|
"bash",
|
||||||
|
"-c",
|
||||||
|
f" sleep {delay_sec} && echo {new_status.value} > {status_file}",
|
||||||
|
]
|
||||||
|
handle = subprocess.Popen(command)
|
||||||
|
if delay_sec == 0:
|
||||||
|
handle.communicate()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def enable(cls):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def disable():
|
def disable(cls, delay):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def stop():
|
def stop(cls, delay=DEFAULT_DELAY):
|
||||||
pass
|
cls.set_status(ServiceStatus.DEACTIVATING)
|
||||||
|
cls.change_status_with_async_delay(ServiceStatus.INACTIVE, delay)
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def start():
|
def start(cls, delay=DEFAULT_DELAY):
|
||||||
pass
|
cls.set_status(ServiceStatus.ACTIVATING)
|
||||||
|
cls.change_status_with_async_delay(ServiceStatus.ACTIVE, delay)
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def restart():
|
def restart(cls, delay=DEFAULT_DELAY):
|
||||||
pass
|
cls.set_status(ServiceStatus.RELOADING) # is a correct one?
|
||||||
|
cls.change_status_with_async_delay(ServiceStatus.ACTIVE, delay)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_configuration():
|
def get_configuration():
|
||||||
|
@ -112,7 +151,7 @@ class DummyService(Service):
|
||||||
return storage_usage
|
return storage_usage
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_drive(cls) -> str:
|
def get_drive() -> str:
|
||||||
return "sda1"
|
return "sda1"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
Loading…
Reference in a new issue