From 7b8e20e21085ee8cd0812965bc8040d87576e4bd Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 11 Dec 2024 15:55:52 +0000 Subject: [PATCH] fix(root_daemon): not crashing immediately --- selfprivacy_api/root_daemon.py | 10 ++++++++-- selfprivacy_api/utils/__init__.py | 5 +++++ selfprivacy_api/utils/root_interface.py | 9 +++++---- tests/test_common.py | 6 +----- tests/test_root_daemon.py | 6 ++++++ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/selfprivacy_api/root_daemon.py b/selfprivacy_api/root_daemon.py index d32b374..cb3c470 100644 --- a/selfprivacy_api/root_daemon.py +++ b/selfprivacy_api/root_daemon.py @@ -7,8 +7,7 @@ import os.path import socket as socket_module import subprocess - -from tests.test_common import get_test_mode +from typing import Optional SOCKET_PATH = "/tmp/socket_test.s" BUFFER_SIZE = 1024 @@ -141,3 +140,10 @@ def main(socket_path=SOCKET_PATH): if __name__ == "__main__": main() + + +# A copy of the one from utils module. +# It is possible to remove this duplication but unfortunately it is not +# trivial and extra complexity does not worth it at the moment. +def get_test_mode() -> Optional[str]: + return os.environ.get("TEST_MODE") diff --git a/selfprivacy_api/utils/__init__.py b/selfprivacy_api/utils/__init__.py index f52a3e2..0ccc849 100644 --- a/selfprivacy_api/utils/__init__.py +++ b/selfprivacy_api/utils/__init__.py @@ -8,6 +8,7 @@ import subprocess import portalocker import typing import glob +from typing import Optional from traceback import format_tb as format_traceback @@ -252,3 +253,7 @@ def read_account_uri() -> str: with open(account_file[0], "r") as file: account_info = json.load(file) return account_info["registration"]["uri"] + + +def get_test_mode() -> Optional[str]: + return os.environ.get("TEST_MODE") diff --git a/selfprivacy_api/utils/root_interface.py b/selfprivacy_api/utils/root_interface.py index 2296da8..5099467 100644 --- a/selfprivacy_api/utils/root_interface.py +++ b/selfprivacy_api/utils/root_interface.py @@ -1,4 +1,5 @@ from typing import List + # from subprocess import check_output from selfprivacy_api.root_daemon import SOCKET_PATH, socket_module from tests.test_common import get_test_mode @@ -9,16 +10,16 @@ def call_root_function(cmd: List[str]) -> str: return "done" else: return _call_root_daemon(cmd) - -def _call_root_daemon(cmd: List[str]) -> str: + + +def _call_root_daemon(cmd: List[str]) -> str: return _write_to_daemon_socket(cmd) def _write_to_daemon_socket(cmd: List[str]) -> str: sock = socket_module.socket(socket_module.AF_UNIX, socket_module.SOCK_STREAM) sock.connect(SOCKET_PATH) - sock.send(" ".join(cmd).encode("utf-8")+b"\n") + sock.send(" ".join(cmd).encode("utf-8") + b"\n") pipe = sock.makefile("rw") line = pipe.readline() return line - diff --git a/tests/test_common.py b/tests/test_common.py index 5e77d80..a5653f7 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -4,7 +4,7 @@ import os import pytest from typing import Optional -from selfprivacy_api.utils import WriteUserData, ReadUserData +from selfprivacy_api.utils import WriteUserData, ReadUserData, get_test_mode def test_get_api_version(authorized_client): @@ -31,10 +31,6 @@ def test_write_invalid_user_data(): pass -def get_test_mode() -> Optional[str]: - return os.environ.get("TEST_MODE") - - # TODO: Does it make any sense to have such a fixture though? # If it can only be called from tests then it is always test @pytest.fixture diff --git a/tests/test_root_daemon.py b/tests/test_root_daemon.py index 846b1f1..a493f63 100644 --- a/tests/test_root_daemon.py +++ b/tests/test_root_daemon.py @@ -17,6 +17,7 @@ from selfprivacy_api.utils.root_interface import call_root_function from os.path import join, exists from typing import List +from time import sleep @pytest.fixture() @@ -52,6 +53,11 @@ def test_send_command(): # this is a prototype of how we need to run it` proc = subprocess.Popen(args=["python", root_daemon_file], shell=False) + # check that it did not error out + sleep(0.3) + finished = proc.poll() + assert finished is None + # thread = threading.Thread(target=start_root_daemon,args=[]) # thread.start() answer = call_root_function("blabla")