Files
unshackle/tests/core/test_mux_failure.py
T

102 lines
3.7 KiB
Python

"""A failed mkvmerge stops the run with one line that names the tool, its exit and its error."""
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
from unshackle.core.run_plan import RunStopped
from unshackle.core.titles import Movie
from unshackle.core.tracks import Video
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"
stub.write_text(f"#!/bin/sh\n{script}\n")
stub.chmod(0o755)
monkeypatch.setattr(binaries, "MKVToolNix", stub)
monkeypatch.setattr(tracks_module, "file_track_ids", lambda path: {"video": 0})
movie = Movie(id_="movie-0001", service=type("EXAMPLE", (), {}), name="Some Movie", year=2024)
video = Video(url="https://x.example/v.m3u8", language="en", codec=Video.Codec.AVC, id_="v")
video.path = tmp_path / "v.mp4"
video.path.write_bytes(b"video")
movie.tracks.add(video)
mux_title(
movie,
Placement(movie, root=tmp_path / "out", no_folder=True, no_source=True, sample=False),
log=logging.getLogger("dl"),
service_tag="EXAMPLE",
split_audio=None,
merge_video=None,
audio_expected=False,
skip_subtitles=True,
quality=[],
range_=[],
vcodec=[],
generate_dv=False,
progress_sink=None,
write_sidecars=None,
)
def test_a_write_error_is_the_reason_of_the_stop(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
error = "The drive may be full. Exception details: No space left to write to"
with pytest.raises(RunStopped) as raised:
mux(tmp_path, monkeypatch, f"echo '#GUI#warning a minor thing'\necho '#GUI#error {error}'\nexit 2")
assert raised.value.reason == f"mkvmerge failed (exit 2): {error}"
assert [r.levelno for r in caplog.records if r.name == "dl" and r.levelno > logging.DEBUG] == [logging.WARNING]
def test_a_failure_with_no_error_line_names_the_exit(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
with pytest.raises(RunStopped) as raised:
mux(tmp_path, monkeypatch, "exit 2")
assert raised.value.reason == "mkvmerge failed (exit 2)"
def test_a_killed_muxer_stops_the_run(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
with pytest.raises(RunStopped) as raised:
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()