forked from kenzuya/unshackle
feat(dl): name the free space of a nearly full temp volume on the failure line
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"""The failure line names the free space of a nearly full temp volume, read once per error."""
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
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.config import config
|
||||
from unshackle.core.temp import TASK_PREFIX
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path)
|
||||
return tmp_path
|
||||
|
||||
|
||||
def free_space(monkeypatch: pytest.MonkeyPatch, free: int) -> list[str]:
|
||||
"""Make each disk_usage read report ``free`` bytes and return the list of read paths."""
|
||||
reads: 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]
|
||||
|
||||
monkeypatch.setattr(dl_module.shutil, "disk_usage", usage)
|
||||
return reads
|
||||
|
||||
|
||||
def test_a_nearly_full_temp_volume_is_named_on_the_line(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
free_space(monkeypatch, 38_000_000)
|
||||
error = subprocess.CalledProcessError(2, ["packager"])
|
||||
|
||||
_, line = dl.failure_lines(error)
|
||||
|
||||
assert line == f" CalledProcessError: {str(error).rstrip('.')}, 38.0 MB free on {temp}"
|
||||
|
||||
|
||||
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)
|
||||
error = ValueError("boom")
|
||||
|
||||
assert dl.failure_lines(error) == [":x: Download Failed...", " ValueError: boom"]
|
||||
|
||||
|
||||
def test_a_temp_dir_that_cannot_be_read_adds_nothing(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path / "missing")
|
||||
|
||||
assert dl.failure_lines(ValueError("boom"))[1] == " ValueError: boom"
|
||||
|
||||
|
||||
def test_a_later_report_reuses_the_first_reading(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
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)
|
||||
|
||||
assert dl.failure_lines(error) == first
|
||||
assert len(reads) == 1
|
||||
|
||||
|
||||
def test_a_click_exception_keeps_only_its_message(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = free_space(monkeypatch, 0)
|
||||
|
||||
assert dl.failure_lines(click.ClickException("no key"))[1] == " no key"
|
||||
assert reads == []
|
||||
|
||||
|
||||
def test_a_task_temp_dir_is_named_by_the_temp_dir_that_holds_it(temp: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = free_space(monkeypatch, 0)
|
||||
monkeypatch.setattr(config.directories, "temp", temp / f"{TASK_PREFIX}example")
|
||||
|
||||
assert dl.failure_lines(ValueError("boom"))[1] == f" ValueError: boom, 0 bytes free on {temp}"
|
||||
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 decrypt(*args: Any, **kwargs: Any) -> None:
|
||||
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)
|
||||
|
||||
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 == keys.decrypt_verified
|
||||
assert verify.DECRYPT_HOOK == cmd.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"
|
||||
|
||||
@@ -17,6 +17,7 @@ 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.drm import Widevine, key_resolution
|
||||
from unshackle.core.drm.key_resolution import ContentKeyResolver
|
||||
@@ -117,7 +118,8 @@ def test_the_retry_after_a_bad_vault_key_ends_in_the_import_error(
|
||||
assert not (tmp_path / "video.mp4.enc").exists()
|
||||
|
||||
|
||||
def test_a_download_stopped_by_a_click_exception_reports_only_its_message() -> None:
|
||||
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)
|
||||
error = ImportService.missing_key(SimpleNamespace(drm=[FakeDRM({})]))
|
||||
|
||||
lines = dl.failure_lines(error)
|
||||
|
||||
@@ -15,6 +15,7 @@ import rnet
|
||||
from tests.downloaders.test_content_encoding import _FakeRnetResponse
|
||||
from tests.downloaders.test_rnet_session_paths import SESSIONS
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.session import MaxRetriesError
|
||||
from unshackle.core.utilities import get_free_port
|
||||
|
||||
@@ -117,14 +118,16 @@ def test_disk_full_is_not_retried(tmp_path, monkeypatch, serve):
|
||||
assert raised.value.errno == errno.ENOSPC
|
||||
assert server.requests == 1
|
||||
assert dl.retries_exhausted(raised.value)
|
||||
assert raised.value.error_code == APIErrorCode.DOWNLOAD_ERROR
|
||||
|
||||
|
||||
def test_status_failure_keeps_the_normal_budget(tmp_path, serve):
|
||||
server = serve(503)
|
||||
with pytest.raises(rq.HTTPError):
|
||||
with pytest.raises(rq.HTTPError) as raised:
|
||||
list(dl.download(url=server.url, save_path=tmp_path / "a.bin", session=rq.Session()))
|
||||
|
||||
assert server.requests == dl.MAX_ATTEMPTS
|
||||
assert not hasattr(raised.value, "error_code")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("make_session", SESSIONS)
|
||||
|
||||
Reference in New Issue
Block a user