Files
romm/backend/tests/handler/sync/retroarch/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

177 lines
6.3 KiB
Python

import os
import threading
from collections.abc import Iterator
from datetime import datetime
from pathlib import Path
from typing import Any, cast
from unittest import mock
import pytest
from handler.database import db_save_handler, db_state_handler
from handler.filesystem import fs_asset_handler
from handler.filesystem.retroarch_sync_handler import BlobFile, FSRetroArchSyncHandler
from handler.redis_handler import async_cache, sync_cache
from handler.sync.retroarch.sync_handler import (
HASH_CACHE_TTL_SECONDS,
asset_md5s,
build_manifest,
cached_hashes,
list_manifest_paths,
user_blob_path,
)
from models.assets import Save, State
from models.user import User
from utils.filesystem import TEMP_FILE_PREFIX
@pytest.fixture(autouse=True)
def _clear_cache() -> Iterator[None]:
sync_cache.flushall()
yield
sync_cache.flushall()
class TestCachedMd5s:
async def test_misses_are_written_back_with_the_ttl(self):
await cached_hashes([("miss", mock.AsyncMock(return_value="fresh-md5"))])
# The test fake stores bytes, where production decodes to str.
assert cast(object, await async_cache.get("miss")) == b"fresh-md5"
ttl = await async_cache.ttl("miss")
assert 0 < ttl <= HASH_CACHE_TTL_SECONDS
async def test_mixes_hits_and_misses_in_order(self):
await async_cache.set("b", "cached-b")
jobs = [
("a", mock.AsyncMock(return_value="fresh-a")),
("b", mock.AsyncMock(return_value="fresh-b")),
("c", mock.AsyncMock(return_value=None)),
]
assert await cached_hashes(jobs) == ["fresh-a", "cached-b", None]
jobs[1][1].assert_not_awaited()
async def test_unreadable_files_are_not_cached(self):
await cached_hashes([("gone", mock.AsyncMock(return_value=None))])
assert await async_cache.exists("gone") == 0
async def test_reads_the_cache_in_one_round_trip(self):
jobs = [(f"key{i}", mock.AsyncMock(return_value=f"md5-{i}")) for i in range(5)]
with mock.patch.object(async_cache, "mget", wraps=async_cache.mget) as mget:
await cached_hashes(jobs)
mget.assert_called_once()
class TestMarkMissing:
async def test_flagging_a_vanished_file_keeps_updated_at(
self, save: Save, state: State
):
"""Device sync and the newest-save pick read updated_at as a write."""
stamp = datetime(2026, 1, 1)
db_save_handler.update_save(save.id, {"updated_at": stamp})
db_state_handler.update_state(state.id, {"updated_at": stamp})
assert await asset_md5s([save, state]) == [None, None]
for row in (
db_save_handler.get_save(user_id=save.user_id, id=save.id),
db_state_handler.get_state(user_id=state.user_id, id=state.id),
):
assert row is not None
assert row.missing_from_fs
# PostgreSQL hands the column back as UTC-aware.
assert row.updated_at.replace(tzinfo=None) == stamp
class TestManifestQueriesOffEventLoop:
@pytest.fixture
def query_threads(self) -> Iterator[list[int]]:
threads: list[int] = []
get_saves = db_save_handler.get_saves
def recording_get_saves(*args: Any, **kwargs: Any) -> Any:
threads.append(threading.get_ident())
return get_saves(*args, **kwargs)
with mock.patch.object(db_save_handler, "get_saves", recording_get_saves):
yield threads
async def test_build_manifest(self, admin_user: User, query_threads: list[int]):
await build_manifest(admin_user, lambda _rom: True)
assert query_threads and threading.get_ident() not in query_threads
async def test_list_manifest_paths(
self, admin_user: User, query_threads: list[int]
):
await list_manifest_paths(admin_user, lambda _rom: True, "saves")
assert query_threads and threading.get_ident() not in query_threads
class TestBlobStorageLocation:
def test_blobs_live_in_the_users_assets_folder(self, admin_user: User):
# The assets root is a documented volume; anything outside it is lost on container recreate.
assert FSRetroArchSyncHandler().base_path == fs_asset_handler.base_path
assert user_blob_path(admin_user, "config/retroarch.cfg") == (
f"{fs_asset_handler.user_folder_path(admin_user)}/retroarch/config/retroarch.cfg"
)
@pytest.fixture
def blob_handler(tmp_path: Path) -> FSRetroArchSyncHandler:
handler = FSRetroArchSyncHandler()
handler.base_path = tmp_path.resolve()
return handler
class TestListBlobFiles:
async def test_lists_nested_files_with_their_stats(
self, blob_handler: FSRetroArchSyncHandler, tmp_path: Path
):
(tmp_path / "config" / "cores" / "Snes9x").mkdir(parents=True)
(tmp_path / "config" / "retroarch.cfg").write_bytes(b"cfg")
nested = tmp_path / "config" / "cores" / "Snes9x" / "Snes9x.opt"
nested.write_bytes(b"options")
files = await blob_handler.list_blob_files("config")
assert sorted(files, key=lambda f: f.relative_path) == [
BlobFile("cores/Snes9x/Snes9x.opt", 7, nested.stat().st_mtime),
BlobFile(
"retroarch.cfg",
3,
(tmp_path / "config" / "retroarch.cfg").stat().st_mtime,
),
]
async def test_does_not_descend_into_symlinked_directories(
self, blob_handler: FSRetroArchSyncHandler, tmp_path: Path
):
outside = tmp_path / "outside"
outside.mkdir()
(outside / "secret").write_bytes(b"secret")
(tmp_path / "config").mkdir()
os.symlink(outside, tmp_path / "config" / "linked")
assert await blob_handler.list_blob_files("config") == []
async def test_skips_interrupted_write_temp_files(
self, blob_handler: FSRetroArchSyncHandler, tmp_path: Path
):
(tmp_path / "config").mkdir()
(tmp_path / "config" / f"{TEMP_FILE_PREFIX}ab12").write_bytes(b"partial")
assert await blob_handler.list_blob_files("config") == []
async def test_missing_prefix_is_empty(self, blob_handler: FSRetroArchSyncHandler):
assert await blob_handler.list_blob_files("config") == []
async def test_traversing_prefix_is_empty(
self, blob_handler: FSRetroArchSyncHandler
):
assert await blob_handler.list_blob_files("../config") == []