Files
romm/backend/tests/handler/filesystem/test_sync_handler.py
Georges-Antoine Assi e232a52656 fix(docker): boot when the container runs as a non-default UID (#5189)
* fix(docker): boot when the container runs as a non-default UID

Unraid runs the container as 99:100, which can't write to the image's
/romm (owned by 1000, mode 755), only to the mounted subfolders. The
PSP pending dir at /romm/cache/retroarch_sync was created at import
time, so every gunicorn worker failed to boot.

- Buffer pending PSP save files under the user's assets folder
  (assets/users/<user>/retroarch/psp_pending/), beside the other
  RetroArch Cloud Sync files and outside the client-reachable blob
  categories.
- Move the zip and rom-converto download caches to /tmp/romm/cache,
  and point nginx's internal /cache/ alias there.
- Put gunicorn's control socket in /tmp/gunicorn instead of
  $HOME/.gunicorn, since HOME is / for that UID.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(docker): honor ROMM_TMP_PATH for caches, explain an unwritable sync folder

- The download caches live under $ROMM_TMP_PATH/cache when it's set,
  so installs with a small Docker disk (Unraid's docker.img) can point
  them at a share. The entrypoint exports the same path for nginx's
  /cache/ alias.
- With ENABLE_SYNC_FOLDER_WATCHER on and /romm/sync not writable, /init
  warns and skips the watcher instead of dying on mkdir under errexit.
- The compose example lists the /romm/sync mount the sync features need.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(sync): cover reading back and clearing buffered PSP files

Addresses Greptile's review on #5189.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(sync): apply code-review findings to PSP pending buffer

Remove the emptied psp_pending folder after a merge, define the sync
folder path once in init, update the architecture doc's PSP and blob
locations, and drop a stale .gitignore entry.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(sync): apply simplify findings to PSP pending cleanup

Clear the buffered folder with remove_directory, let the startup check
own creating the sync folder, and reword the test fixture docstring.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(sync): cap buffered PSP files and drop them on delete

Buffered files for an unresolved save folder now count against the
bundle limits, so a folder can't fill the assets share and then fail
every merge once it resolves. A DELETE also removes the buffered copy,
so a deleted file isn't merged back when the folder resolves.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(docker): check the sync folder for push-pull sync too

ENABLE_SYNC_PUSH_PULL keeps SSH keys and device saves under /romm/sync
as well, so an unwritable folder now disables it with the watcher and
warns at boot instead of failing when the task runs.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(docker): strip whitespace from env vars like the backend does

The backend strips every variable it reads, while the init script and
nginx templates used raw values, so a padded ROMM_TMP_PATH sent nginx's
/cache/ alias somewhere the backend never writes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore(config): drop the CACHE_BASE_PATH comment

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* docs(docker): say which backend config strips env whitespace

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* revert(docker): drop the entrypoint env whitespace trimming

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(sync): apply pr-ready code-review findings

Gate push-pull on its SSH keys path rather than a writable sync folder,
accept the same truthy values as the backend, and clear only the merged
PSP pending files so another worker's buffered file survives.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor: apply pr-ready simplify findings

Share one truthy pattern across init's flag checks (the watcher gates
now accept the same values as the backend), disable gunicorn's unused
control socket instead of placing it, derive the converts redirect from
its own cache path, and factor the unknown PSP folder in tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* docs(docker): apply pr-ready humanizer pass to the sync mount note

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(fs): create handler base folders on first write, skip leftover temp files

FSHandler no longer mkdirs its base path when constructed, so a module-level
handler rooted at an unmounted or unwritable folder can't take down boot;
every write path already creates its parent folders. Blob listings skip
.romm_tmp_ files a crashed write left behind, so they are never merged into
a PSP bundle or advertised in the sync manifest. The prefix now lives in one
TEMP_FILE_PREFIX constant.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-08 16:48:50 -04:00

137 lines
5.1 KiB
Python

"""Tests for filesystem sync handler."""
import os
import shutil
import tempfile
from pathlib import Path
import pytest
from handler.filesystem.sync_handler import FSSyncHandler
class TestFSSyncHandler:
@pytest.fixture
def temp_dir(self):
temp_dir = tempfile.mkdtemp()
yield temp_dir
shutil.rmtree(temp_dir, ignore_errors=True)
@pytest.fixture
def handler(self):
return FSSyncHandler.__new__(FSSyncHandler)
@pytest.fixture(autouse=True)
def patch_base_path(self, handler: FSSyncHandler, temp_dir):
handler.base_path = Path(temp_dir)
def test_build_incoming_path(self, handler: FSSyncHandler):
path = handler.build_incoming_path("device-1")
assert path == os.path.join("device-1", "incoming")
def test_build_incoming_path_with_platform(self, handler):
path = handler.build_incoming_path("device-1", "gba")
assert path == os.path.join("device-1", "incoming", "gba")
def test_build_outgoing_path(self, handler: FSSyncHandler):
path = handler.build_outgoing_path("device-1")
assert path == os.path.join("device-1", "outgoing")
def test_build_outgoing_path_with_platform(self, handler: FSSyncHandler):
path = handler.build_outgoing_path("device-1", "snes")
assert path == os.path.join("device-1", "outgoing", "snes")
def test_build_conflicts_path(self, handler: FSSyncHandler):
path = handler.build_conflicts_path("device-1", "gba")
assert path == os.path.join("device-1", "conflicts", "gba")
def test_ensure_device_directories(self, handler: FSSyncHandler, temp_dir):
handler.ensure_device_directories("test-device")
incoming = handler.base_path / handler.build_incoming_path("test-device")
outgoing = handler.base_path / handler.build_outgoing_path("test-device")
assert os.path.isdir(incoming)
assert os.path.isdir(outgoing)
def test_list_incoming_files_empty(self, handler: FSSyncHandler):
result = handler.list_incoming_files("nonexistent-device")
assert result == []
def test_list_incoming_files(self, handler: FSSyncHandler, temp_dir):
handler.ensure_device_directories("dev-1")
incoming_path = str(
handler.base_path / handler.build_incoming_path("dev-1", "gba")
)
os.makedirs(incoming_path, exist_ok=True)
test_file = os.path.join(incoming_path, "save.sav")
with open(test_file, "wb") as f:
f.write(b"test save content")
result = handler.list_incoming_files("dev-1")
assert len(result) == 1
assert result[0]["platform_slug"] == "gba"
assert result[0]["file_name"] == "save.sav"
assert result[0]["file_size"] == 17
def test_compute_file_hash(self, handler: FSSyncHandler, temp_dir):
test_file = os.path.join(temp_dir, "test.bin")
with open(test_file, "wb") as f:
f.write(b"hello world")
hash1 = handler.compute_file_hash(test_file)
hash2 = handler.compute_file_hash(test_file)
assert hash1 == hash2
assert hash1 is not None
assert len(hash1) == 32 # MD5 hex length
def test_compute_file_hash_different_content(
self, handler: FSSyncHandler, temp_dir
):
file_a = os.path.join(temp_dir, "a.bin")
file_b = os.path.join(temp_dir, "b.bin")
with open(file_a, "wb") as f:
f.write(b"content a")
with open(file_b, "wb") as f:
f.write(b"content b")
assert handler.compute_file_hash(file_a) != handler.compute_file_hash(file_b)
def test_write_outgoing_file(self, handler: FSSyncHandler, temp_dir):
path = handler.write_outgoing_file(
device_id="dev-1",
platform_slug="gba",
file_name="save.sav",
data=b"outgoing save data",
)
assert os.path.isfile(path)
with open(path, "rb") as f:
assert f.read() == b"outgoing save data"
def test_remove_incoming_file(self, handler: FSSyncHandler, temp_dir):
handler.ensure_device_directories("dev-1")
incoming = str(handler.base_path / handler.build_incoming_path("dev-1", "gba"))
os.makedirs(incoming, exist_ok=True)
test_file = os.path.join(incoming, "to_remove.sav")
with open(test_file, "wb") as f:
f.write(b"data")
assert os.path.exists(test_file)
handler.remove_incoming_file(test_file)
assert not os.path.exists(test_file)
def test_remove_incoming_file_outside_base_raises(
self, handler: FSSyncHandler, temp_dir
):
outside_file = os.path.join(tempfile.gettempdir(), "outside.txt")
with open(outside_file, "w") as f:
f.write("should not be deleted")
with pytest.raises(ValueError, match="outside the sync base directory"):
handler.remove_incoming_file(outside_file)
# Cleanup
os.unlink(outside_file)
def test_remove_incoming_file_nonexistent(self, handler: FSSyncHandler):
# Should not raise for nonexistent files
handler.remove_incoming_file("/nonexistent/path/file.sav")