fix(root_daemon): not crashing immediately

This commit is contained in:
Houkime 2024-12-11 15:55:52 +00:00
parent 16300cd899
commit 7b8e20e210
5 changed files with 25 additions and 11 deletions

View file

@ -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")

View file

@ -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")

View file

@ -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

View file

@ -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

View file

@ -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")