mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 13:05:08 +00:00
refactor(temp): read the free space where the failed step deletes its temp files
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
@@ -9,10 +10,13 @@ from typing import Any
|
||||
import click
|
||||
import pytest
|
||||
|
||||
from unshackle.commands import dl as dl_module
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core import temp as temp_module
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.temp import TASK_PREFIX
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY
|
||||
from unshackle.core.manifests import DASH
|
||||
from unshackle.core.temp import LOW_FREE_SPACE, TASK_PREFIX
|
||||
from unshackle.core.tracks import Video
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -27,9 +31,9 @@ def free_space(monkeypatch: pytest.MonkeyPatch, free: int) -> list[str]:
|
||||
|
||||
def usage(path: Any) -> Any:
|
||||
reads.append(str(path))
|
||||
return shutil._ntuple_diskusage(10 * dl_module.LOW_FREE_SPACE, 0, free) # type: ignore[attr-defined]
|
||||
return shutil._ntuple_diskusage(10 * LOW_FREE_SPACE, 0, free) # type: ignore[attr-defined]
|
||||
|
||||
monkeypatch.setattr(dl_module.shutil, "disk_usage", usage)
|
||||
monkeypatch.setattr(temp_module.shutil, "disk_usage", usage)
|
||||
return reads
|
||||
|
||||
|
||||
@@ -43,7 +47,7 @@ def test_a_nearly_full_temp_volume_is_named_on_the_line(temp: Path, monkeypatch:
|
||||
|
||||
|
||||
def test_enough_free_space_leaves_the_line_as_it_was(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
free_space(monkeypatch, dl_module.LOW_FREE_SPACE)
|
||||
free_space(monkeypatch, LOW_FREE_SPACE)
|
||||
error = ValueError("boom")
|
||||
|
||||
assert dl.failure_lines(error) == [":x: Download Failed...", " ValueError: boom"]
|
||||
@@ -59,7 +63,7 @@ def test_a_later_report_reuses_the_first_reading(temp: Path, monkeypatch: pytest
|
||||
reads = free_space(monkeypatch, 38_000_000)
|
||||
error = OSError(28, "No space left on device")
|
||||
first = dl.failure_lines(error)
|
||||
free_space(monkeypatch, 5 * dl_module.LOW_FREE_SPACE)
|
||||
free_space(monkeypatch, 5 * LOW_FREE_SPACE)
|
||||
|
||||
assert dl.failure_lines(error) == first
|
||||
assert len(reads) == 1
|
||||
@@ -80,18 +84,32 @@ def test_a_task_temp_dir_is_named_by_the_temp_dir_that_holds_it(temp: Path, monk
|
||||
assert reads == [str(temp)]
|
||||
|
||||
|
||||
def test_a_failed_decrypt_is_read_before_its_track_deletes_its_files(
|
||||
temp: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
free_space(monkeypatch, 38_000_000)
|
||||
failure = subprocess.CalledProcessError(2, ["packager"])
|
||||
def test_a_failed_track_is_read_before_it_deletes_its_files(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
written = temp / "Video_v.mp4"
|
||||
seen: list[bool] = []
|
||||
failure = OSError(28, "No space left on device")
|
||||
|
||||
def decrypt(*args: Any, **kwargs: Any) -> None:
|
||||
def usage(path: Any) -> Any:
|
||||
seen.append(written.exists())
|
||||
return SimpleNamespace(free=38_000_000)
|
||||
|
||||
def parser(track: Video, ctx: Any) -> None:
|
||||
written.write_bytes(b"partial")
|
||||
raise failure
|
||||
|
||||
run = SimpleNamespace(keys=SimpleNamespace(decrypt_verified=decrypt))
|
||||
with pytest.raises(subprocess.CalledProcessError):
|
||||
dl.decrypt_verified(run, "drm", temp / "video.mp4", track=None) # type: ignore[arg-type]
|
||||
free_space(monkeypatch, 5 * dl_module.LOW_FREE_SPACE)
|
||||
monkeypatch.setattr(temp_module.shutil, "disk_usage", usage)
|
||||
monkeypatch.setattr(DASH, "download_track", staticmethod(parser))
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
DOWNLOAD_LICENCE_ONLY.clear()
|
||||
track = Video(
|
||||
url="https://x.example/v.mpd", language="en", codec=Video.Codec.AVC, id_="v", descriptor=Video.Descriptor.DASH
|
||||
)
|
||||
with pytest.raises(OSError):
|
||||
track.download( # type: ignore[arg-type]
|
||||
session=SimpleNamespace(proxies={}), prepare_drm=partial(lambda: None), progress=partial(lambda **kw: None)
|
||||
)
|
||||
free_space(monkeypatch, 5 * LOW_FREE_SPACE)
|
||||
|
||||
assert seen == [True]
|
||||
assert not written.exists()
|
||||
assert dl.failure_lines(failure)[1].endswith(f", 38.0 MB free on {temp}")
|
||||
|
||||
@@ -42,7 +42,7 @@ def test_the_resolver_takes_the_run_flags_and_becomes_the_decrypt_hook(
|
||||
)
|
||||
|
||||
keys = cmd.keys
|
||||
assert verify.DECRYPT_HOOK == cmd.decrypt_verified
|
||||
assert verify.DECRYPT_HOOK == cmd.keys.decrypt_verified
|
||||
assert (keys.cdm_only, keys.vaults_only, keys.best_available) == (cdm_only, vaults_only, best_available)
|
||||
assert keys.source is cmd._remote_service and keys.service is service and keys.cdms is cmd
|
||||
assert keys.vaults is cmd.vaults and keys.tag == "EXAMPLE"
|
||||
|
||||
@@ -21,7 +21,7 @@ import unshackle.commands.dl as dl_module
|
||||
from tests.admission import refusal
|
||||
from tests.remote.sessions import launch_session
|
||||
from tests.remote.unit.test_remote_service_helpers import _sent_create_data
|
||||
from unshackle.core import mux_step
|
||||
from unshackle.core import mux_step, temp
|
||||
from unshackle.core.api import download_manager, handlers, key_policy
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.mux_step import hybrid_plan, hybrid_source_tracks, hybrid_step_error, missing_hybrid_error
|
||||
@@ -705,10 +705,38 @@ def test_a_hybrid_failure_is_reported_so_the_title_goes_on(monkeypatch: pytest.M
|
||||
raise error
|
||||
|
||||
monkeypatch.setattr(mux_step, "Hybrid", failed_hybrid)
|
||||
monkeypatch.setattr(temp, "LOW_FREE_SPACE", 0)
|
||||
|
||||
assert hybrid_step_error([], "TEST", "HDR10-DV-2160p.hevc") == str(error)
|
||||
|
||||
|
||||
def test_a_hybrid_that_fills_the_temp_volume_names_the_free_space_before_its_files_go(
|
||||
quiet: None, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
base = tmp_path / "base.mkv"
|
||||
base.write_bytes(b"")
|
||||
stream = tmp_path / "HDR10.hevc"
|
||||
present: list[bool] = []
|
||||
|
||||
def full(self: Hybrid) -> None:
|
||||
stream.write_bytes(b"hevc")
|
||||
raise OSError(28, "No space left on device")
|
||||
|
||||
def usage(path: Any) -> Any:
|
||||
present.append(stream.exists())
|
||||
return SimpleNamespace(free=38_000_000)
|
||||
|
||||
stub_hybrid_steps(monkeypatch, "measure_dv", "level_6")
|
||||
monkeypatch.setattr(Hybrid, "injecting", full)
|
||||
monkeypatch.setattr(temp.shutil, "disk_usage", usage)
|
||||
video = SimpleNamespace(id="b", path=base, width=None, height=2160, range=Video.Range.HDR10)
|
||||
|
||||
error = hybrid_step_error([video], "TEST", "HDR10-DV-2160p.hevc") # type: ignore[list-item]
|
||||
|
||||
assert error == f"[Errno 28] No space left on device, 38.0 MB free on {tmp_path}"
|
||||
assert present == [True] and not stream.exists()
|
||||
|
||||
|
||||
def test_a_working_hybrid_reports_no_error_and_an_exit_passes_through(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
seen: dict[str, Any] = {}
|
||||
monkeypatch.setattr(mux_step, "Hybrid", lambda tracks, source, output_name: seen.update(name=output_name))
|
||||
|
||||
@@ -17,8 +17,8 @@ import pytest
|
||||
from pywidevine.pssh import PSSH
|
||||
|
||||
from tests.core.key_resolver import key_table, resolver, vaults_of
|
||||
from unshackle.commands import dl as dl_module
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core import temp
|
||||
from unshackle.core.drm import Widevine, key_resolution
|
||||
from unshackle.core.drm.key_resolution import ContentKeyResolver
|
||||
from unshackle.core.import_service import ImportService
|
||||
@@ -119,7 +119,7 @@ def test_the_retry_after_a_bad_vault_key_ends_in_the_import_error(
|
||||
|
||||
|
||||
def test_a_download_stopped_by_a_click_exception_reports_only_its_message(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(dl_module, "LOW_FREE_SPACE", 0)
|
||||
monkeypatch.setattr(temp, "LOW_FREE_SPACE", 0)
|
||||
error = ImportService.missing_key(SimpleNamespace(drm=[FakeDRM({})]))
|
||||
|
||||
lines = dl.failure_lines(error)
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core import temp as temp_module
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.mux_step import mux_title
|
||||
from unshackle.core.placement import Placement
|
||||
@@ -17,6 +20,11 @@ from unshackle.core.tracks import tracks as tracks_module
|
||||
pytestmark = pytest.mark.skipif(not Path("/bin/sh").exists(), reason="needs a POSIX shell for the mkvmerge stub")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def roomy_temp(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(temp_module, "LOW_FREE_SPACE", 0)
|
||||
|
||||
|
||||
def mux(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, script: str) -> None:
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path)
|
||||
stub = tmp_path / "mkvmerge"
|
||||
@@ -70,3 +78,24 @@ def test_a_killed_muxer_stops_the_run(tmp_path: Path, monkeypatch: pytest.Monkey
|
||||
mux(tmp_path, monkeypatch, "kill -9 $$")
|
||||
|
||||
assert raised.value.reason == "mkvmerge failed (signal 9)"
|
||||
|
||||
|
||||
def test_the_stop_names_a_nearly_full_temp_volume_read_before_the_temp_files_go(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
rpu = tmp_path / "RPU.bin"
|
||||
rpu.write_bytes(b"rpu")
|
||||
seen: list[bool] = []
|
||||
|
||||
def usage(path: Any) -> Any:
|
||||
seen.append(rpu.exists())
|
||||
return SimpleNamespace(free=38_000_000)
|
||||
|
||||
monkeypatch.setattr(temp_module, "LOW_FREE_SPACE", 1024**3)
|
||||
monkeypatch.setattr(temp_module.shutil, "disk_usage", usage)
|
||||
with pytest.raises(RunStopped) as raised:
|
||||
mux(tmp_path, monkeypatch, "exit 2")
|
||||
|
||||
assert raised.value.reason == f"mkvmerge failed (exit 2), 38.0 MB free on {tmp_path}"
|
||||
assert seen == [True]
|
||||
assert not rpu.exists()
|
||||
|
||||
@@ -13,6 +13,7 @@ from uuid import UUID
|
||||
import pytest
|
||||
|
||||
from tests.core.test_all_drm import playready, widevine
|
||||
from unshackle.core import temp
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.drm import ClearKey, ClearKeyCENC, ClearKeyCTR, MonaLisa, verify
|
||||
from unshackle.core.events import events
|
||||
@@ -192,6 +193,22 @@ def test_a_failure_closes_the_decrypter_and_removes_the_output(tmp_path, seen):
|
||||
assert not ctx.save_path.exists()
|
||||
|
||||
|
||||
def test_a_failed_rolling_merge_reads_the_free_space_before_it_removes_the_output(tmp_path, monkeypatch):
|
||||
ctx = make_ctx(tmp_path)
|
||||
present: list[bool] = []
|
||||
|
||||
def usage(path: Any) -> Any:
|
||||
present.append(ctx.save_path.exists())
|
||||
return SimpleNamespace(free=38_000_000)
|
||||
|
||||
monkeypatch.setattr(temp.shutil, "disk_usage", usage)
|
||||
with pytest.raises(ConnectionError) as raised:
|
||||
fetch(ctx, FakeDownloader(fail_after=2), rolling=True, init=INIT)
|
||||
|
||||
assert present == [True] and not ctx.save_path.exists()
|
||||
assert temp.low_free_space(raised.value).startswith(", 38.0 MB free on ")
|
||||
|
||||
|
||||
def test_a_short_rolling_merge_raises_and_removes_the_output(tmp_path):
|
||||
ctx = make_ctx(tmp_path)
|
||||
ctx.save_path.write_bytes(INIT)
|
||||
|
||||
Reference in New Issue
Block a user