mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-02 05:55:08 +00:00
157 lines
5.1 KiB
Python
157 lines
5.1 KiB
Python
"""Completion reporting survives the worker boundary and is scoped by the server."""
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import click
|
|
import pytest
|
|
|
|
from unshackle.core.api import download_manager as dm
|
|
from unshackle.core.api import download_worker
|
|
from unshackle.core.api.job_channel import WorkerResult
|
|
from unshackle.core.config import config
|
|
from unshackle.core.dl_options import DEFAULT_DOWNLOAD_PARAMS
|
|
|
|
PRESENT = [
|
|
{
|
|
"service": "EXAMPLE",
|
|
"title_id": "example-1",
|
|
"reason": "already_present",
|
|
"variants": [{"quality": 720, "range": "SDR", "codec": "H.264"}],
|
|
}
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("fails", [False, True], ids=["success", "later-failure"])
|
|
def test_worker_takes_the_payload_scope_and_reports_already_present(monkeypatch, tmp_path, fails):
|
|
seen = {}
|
|
|
|
def perform(*args, **kwargs):
|
|
seen.update(kwargs)
|
|
kwargs["progress_callback"]({"already_present": PRESENT})
|
|
if fails:
|
|
raise RuntimeError("later title failed")
|
|
return []
|
|
|
|
monkeypatch.setattr(download_worker, "perform_download", perform)
|
|
monkeypatch.setattr(download_worker, "relay_prompts", lambda *a: None)
|
|
payload = tmp_path / "payload.json"
|
|
progress = tmp_path / "progress.json"
|
|
scope = "api-" + "a" * 64
|
|
payload.write_text(
|
|
json.dumps(
|
|
{
|
|
"job_id": "test",
|
|
"service": "EXAMPLE",
|
|
"title_id": "example-1",
|
|
"archive_scope": scope,
|
|
"parameters": {"archive_scope": "cli"},
|
|
}
|
|
)
|
|
)
|
|
assert download_worker.main(["worker", str(payload), str(tmp_path / "result.json"), str(progress)]) == int(fails)
|
|
assert seen["archive_scope"] == scope
|
|
assert json.loads(progress.read_text())["already_present"] == PRESENT
|
|
|
|
|
|
def test_api_options_default_off():
|
|
assert DEFAULT_DOWNLOAD_PARAMS["skip_existing"] is False
|
|
assert DEFAULT_DOWNLOAD_PARAMS["force"] is False
|
|
|
|
|
|
def test_manager_uses_owner_scope_and_keeps_already_present_in_history(monkeypatch, tmp_path):
|
|
payloads = []
|
|
|
|
class FakeWorker:
|
|
returncode = 0
|
|
|
|
async def wait(self, timeout):
|
|
return True
|
|
|
|
def read_progress(self):
|
|
return {"already_present": PRESENT}
|
|
|
|
def output(self):
|
|
return "", ""
|
|
|
|
async def result(self):
|
|
return WorkerResult(status="success")
|
|
|
|
async def close(self):
|
|
pass
|
|
|
|
async def spawn(payload):
|
|
payloads.append(payload)
|
|
return FakeWorker()
|
|
|
|
monkeypatch.setattr(dm.WorkerProcess, "spawn", spawn)
|
|
history = tmp_path / "api_history.jsonl"
|
|
monkeypatch.setattr(dm, "history_path", lambda: history)
|
|
manager = dm.DownloadQueueManager()
|
|
job = manager.create_job("EXAMPLE", "example-1", owner_key="test-owner", archive_scope="cli")
|
|
assert asyncio.run(manager.run_download_async(job)) == []
|
|
assert [p.archive_scope for p in payloads] == ["api-" + hashlib.sha256(b"test-owner").hexdigest()]
|
|
assert job.to_dict()["already_present"] == PRESENT
|
|
assert job.skipped_titles == []
|
|
job.status = dm.JobStatus.COMPLETED
|
|
dm.record_job_history(job)
|
|
assert json.loads(history.read_text())["already_present"] == PRESENT
|
|
|
|
|
|
def test_ownerless_job_has_no_archive_scope(monkeypatch):
|
|
payloads = []
|
|
|
|
async def spawn(payload):
|
|
payloads.append(payload)
|
|
raise RuntimeError("stop")
|
|
|
|
monkeypatch.setattr(dm.WorkerProcess, "spawn", spawn)
|
|
manager = dm.DownloadQueueManager()
|
|
with pytest.raises(RuntimeError, match="stop"):
|
|
asyncio.run(manager.run_download_async(manager.create_job("EXAMPLE", "example-1")))
|
|
assert payloads[0].archive_scope == "api-unowned"
|
|
|
|
|
|
def test_request_cannot_choose_archive_scope_or_credential_namespace(monkeypatch, tmp_path):
|
|
import unshackle.commands.dl as dl_module
|
|
from unshackle.core.api import handlers
|
|
from unshackle.core.services import Services
|
|
|
|
seen = {}
|
|
|
|
class Captured(Exception):
|
|
pass
|
|
|
|
class FakeDl:
|
|
cli = dl_module.dl.cli
|
|
|
|
def __init__(self, ctx, **kwargs):
|
|
pass
|
|
|
|
def result(self, **kwargs):
|
|
seen.update(scope=self.archive_scope, cache=self.archive_cache)
|
|
raise Captured
|
|
|
|
class FakeService:
|
|
cli = click.Command("svc", callback=lambda title: object())
|
|
|
|
monkeypatch.setattr(dl_module, "dl", FakeDl)
|
|
monkeypatch.setattr(Services, "get_path", staticmethod(lambda s: tmp_path))
|
|
monkeypatch.setattr(Services, "load", staticmethod(lambda s: FakeService))
|
|
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
|
|
monkeypatch.setattr(config.directories, "cache", tmp_path / "cache")
|
|
monkeypatch.setattr(config, "credentials", {})
|
|
scope = "api-" + "c" * 64
|
|
with pytest.raises(Captured):
|
|
dm.perform_download(
|
|
"test",
|
|
"EXAMPLE",
|
|
"example-1",
|
|
{"archive_scope": "cli", "archive_cache": "/not-allowed", "credential": "test-user:test-password"},
|
|
archive_scope=scope,
|
|
)
|
|
assert seen == {"scope": scope, "cache": tmp_path / "cache"}
|
|
assert Path(config.directories.cache) != tmp_path / "cache"
|