2020-01-17 11:20:17 -08:00
|
|
|
import os
|
|
|
|
|
import random
|
2023-07-14 15:12:11 +01:00
|
|
|
from pathlib import Path
|
2024-01-17 14:24:09 -08:00
|
|
|
|
2020-01-17 11:20:17 -08:00
|
|
|
|
2023-08-20 20:49:55 -07:00
|
|
|
import openpilot.system.loggerd.deleter as deleter
|
|
|
|
|
import openpilot.system.loggerd.uploader as uploader
|
2024-01-17 14:24:09 -08:00
|
|
|
from openpilot.common.params import Params
|
|
|
|
|
from openpilot.system.hardware.hw import Paths
|
2023-08-20 20:49:55 -07:00
|
|
|
from openpilot.system.loggerd.xattr_cache import setxattr
|
2020-01-17 11:20:17 -08:00
|
|
|
|
|
|
|
|
|
2024-02-25 21:29:18 +00:00
|
|
|
def create_random_file(file_path: Path, size_mb: float, lock: bool = False, upload_xattr: bytes = None) -> None:
|
2023-07-14 15:12:11 +01:00
|
|
|
file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
2022-03-24 23:23:29 -07:00
|
|
|
if lock:
|
2023-07-14 15:12:11 +01:00
|
|
|
lock_path = str(file_path) + ".lock"
|
2022-03-24 23:23:29 -07:00
|
|
|
os.close(os.open(lock_path, os.O_CREAT | os.O_EXCL))
|
2020-01-17 11:20:17 -08:00
|
|
|
|
2022-03-24 23:23:29 -07:00
|
|
|
chunks = 128
|
|
|
|
|
chunk_bytes = int(size_mb * 1024 * 1024 / chunks)
|
|
|
|
|
data = os.urandom(chunk_bytes)
|
2020-01-17 11:20:17 -08:00
|
|
|
|
2023-07-14 15:12:11 +01:00
|
|
|
with open(file_path, "wb") as f:
|
2022-03-24 23:23:29 -07:00
|
|
|
for _ in range(chunks):
|
|
|
|
|
f.write(data)
|
2020-01-17 11:20:17 -08:00
|
|
|
|
2023-07-20 21:30:26 +01:00
|
|
|
if upload_xattr is not None:
|
|
|
|
|
setxattr(str(file_path), uploader.UPLOAD_ATTR_NAME, upload_xattr)
|
2023-05-16 21:55:24 -07:00
|
|
|
|
2020-01-17 11:20:17 -08:00
|
|
|
class MockResponse():
|
2020-04-10 09:46:03 -07:00
|
|
|
def __init__(self, text, status_code):
|
2020-01-17 11:20:17 -08:00
|
|
|
self.text = text
|
2020-04-10 09:46:03 -07:00
|
|
|
self.status_code = status_code
|
2020-01-17 11:20:17 -08:00
|
|
|
|
|
|
|
|
class MockApi():
|
|
|
|
|
def __init__(self, dongle_id):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
2020-04-10 09:46:03 -07:00
|
|
|
return MockResponse('{"url": "http://localhost/does/not/exist", "headers": {}}', 200)
|
|
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
|
return "fake-token"
|
|
|
|
|
|
|
|
|
|
class MockApiIgnore():
|
|
|
|
|
def __init__(self, dongle_id):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
|
return MockResponse('', 412)
|
2020-01-17 11:20:17 -08:00
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
|
return "fake-token"
|
|
|
|
|
|
2024-05-17 11:01:44 -07:00
|
|
|
class UploaderTestCase:
|
2020-01-17 11:20:17 -08:00
|
|
|
f_type = "UNKNOWN"
|
|
|
|
|
|
2023-07-14 15:12:11 +01:00
|
|
|
root: Path
|
|
|
|
|
seg_num: int
|
|
|
|
|
seg_format: str
|
|
|
|
|
seg_format2: str
|
|
|
|
|
seg_dir: str
|
|
|
|
|
|
2020-04-10 09:46:03 -07:00
|
|
|
def set_ignore(self):
|
|
|
|
|
uploader.Api = MockApiIgnore
|
|
|
|
|
|
2024-05-17 11:01:44 -07:00
|
|
|
def setup_method(self):
|
2020-01-17 11:20:17 -08:00
|
|
|
uploader.Api = MockApi
|
2020-12-29 22:32:03 -08:00
|
|
|
uploader.fake_upload = True
|
|
|
|
|
uploader.force_wifi = True
|
2020-12-31 14:57:01 -08:00
|
|
|
uploader.allow_sleep = False
|
2020-01-17 11:20:17 -08:00
|
|
|
self.seg_num = random.randint(1, 300)
|
2024-03-05 20:43:33 -08:00
|
|
|
self.seg_format = "00000004--0ac3964c96--{}"
|
|
|
|
|
self.seg_format2 = "00000005--4c4e99b08b--{}"
|
2020-01-17 11:20:17 -08:00
|
|
|
self.seg_dir = self.seg_format.format(self.seg_num)
|
|
|
|
|
|
2024-01-17 14:24:09 -08:00
|
|
|
self.params = Params()
|
|
|
|
|
self.params.put("IsOffroad", "1")
|
|
|
|
|
self.params.put("DongleId", "0000000000000000")
|
|
|
|
|
|
2023-07-14 15:12:11 +01:00
|
|
|
def make_file_with_data(self, f_dir: str, fn: str, size_mb: float = .1, lock: bool = False,
|
2024-02-25 21:29:18 +00:00
|
|
|
upload_xattr: bytes = None, preserve_xattr: bytes = None) -> Path:
|
2023-09-07 11:32:47 -07:00
|
|
|
file_path = Path(Paths.log_root()) / f_dir / fn
|
2023-07-20 21:30:26 +01:00
|
|
|
create_random_file(file_path, size_mb, lock, upload_xattr)
|
|
|
|
|
|
|
|
|
|
if preserve_xattr is not None:
|
|
|
|
|
setxattr(str(file_path.parent), deleter.PRESERVE_ATTR_NAME, preserve_xattr)
|
2020-01-17 11:20:17 -08:00
|
|
|
|
|
|
|
|
return file_path
|