Files
devplacepy/tests/unit/services/backup/offload.py
T

74 lines
2.4 KiB
Python

# retoor <retoor@molodetz.nl>
import tempfile
from pathlib import Path
from tests.conftest import run_async
from devplacepy.services.backup import offload, store
def _make_backup(target):
job_uid = f"job-{store.generate_uid()}"
uid = store.create_backup(target=target, created_by="test", job_uid=job_uid)
path = Path(tempfile.gettempdir()) / f"backup-offload-test-{uid}.tar.gz"
path.write_bytes(b"x" * 10)
store.finalize_backup(
uid,
filename=path.name,
local_path=str(path),
stats={
"bytes_out": 10,
"bytes_in": 10,
"file_count": 1,
"dir_count": 0,
"sha256": "abc",
},
)
return uid, path
def test_upload_pending_stops_after_auth_error(local_db, monkeypatch):
first_uid, first_path = _make_backup("database")
second_uid, second_path = _make_backup("uploads")
calls = []
async def fake_rclone(*args, timeout=1800.0):
calls.append(args)
return 1, "", "401 Unauthorized from Apache"
monkeypatch.setattr(offload, "_run_rclone", fake_rclone)
logs = []
try:
uploaded = run_async(offload.upload_pending(log=logs.append))
assert uploaded == 0
assert len(calls) == 1
assert any("halted" in line.lower() for line in logs)
finally:
first_path.unlink(missing_ok=True)
second_path.unlink(missing_ok=True)
store.delete_backup(first_uid)
store.delete_backup(second_uid)
def test_is_auth_error_detects_rclone_html_401():
assert offload._is_auth_error(
'read metadata failed: <title>401 Unauthorized</title>: 401 Unauthorized'
)
assert offload._is_auth_error("didn't find section in config file")
assert not offload._is_auth_error("connection timed out")
def test_run_cycle_skips_clearly_when_rclone_binary_missing(monkeypatch):
monkeypatch.setattr(offload.config, "RCLONE_BIN", "/nonexistent/rclone-test-binary")
async def explode(*args, **kwargs):
raise AssertionError("rclone must not be spawned when the binary is missing")
monkeypatch.setattr(offload, "_run_rclone", explode)
logs = []
result = run_async(offload.run_cycle(keep_local=5, keep_remote=5, log=logs.append))
assert result == {"uploaded": 0, "purged_local": 0, "removed_remote": 0}
assert len(logs) == 1
assert "rclone binary not found" in logs[0]
assert "backup_offload_enabled" in logs[0]