130 lines
4.2 KiB
Python
130 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from gitea_backup.backup import prune_snapshots, run_backup
|
|
from gitea_backup.config import Config
|
|
from gitea_backup.scheduler import seconds_until_next
|
|
|
|
|
|
class FakeGit:
|
|
def __init__(self) -> None:
|
|
self.calls: list[tuple[str, Path]] = []
|
|
|
|
def mirror_or_fetch(self, clone_url: str, dest: Path) -> None:
|
|
dest.mkdir(parents=True, exist_ok=True)
|
|
(dest / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
|
|
self.calls.append((clone_url, dest))
|
|
|
|
|
|
class FakeClient:
|
|
def __init__(self, repos: list[dict]) -> None:
|
|
self._repos = repos
|
|
self.token = "tok"
|
|
|
|
def __enter__(self) -> FakeClient:
|
|
return self
|
|
|
|
def __exit__(self, *args: object) -> None:
|
|
return None
|
|
|
|
def ping(self) -> str:
|
|
return "1.22.0"
|
|
|
|
def list_repos(self) -> list[dict]:
|
|
return self._repos
|
|
|
|
def clone_url(self, repo: dict) -> str:
|
|
return f"http://oauth2:tok@192.168.1.9:3000/{repo['full_name']}.git"
|
|
|
|
|
|
def _config(root: Path, **kwargs) -> Config:
|
|
values = dict(
|
|
gitea_url="http://192.168.1.9:3000",
|
|
token="tok",
|
|
backup_dir=root,
|
|
backup_at="03:00",
|
|
timezone="Europe/Moscow",
|
|
run_on_start=True,
|
|
snapshots=True,
|
|
keep_days=7,
|
|
include_wiki=True,
|
|
include_lfs=False,
|
|
)
|
|
values.update(kwargs)
|
|
return Config(**values)
|
|
|
|
|
|
class SnapshotPruneTest(unittest.TestCase):
|
|
def test_removes_old_dated_dirs(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
(root / "2026-08-01").mkdir()
|
|
(root / "2026-09-07").mkdir()
|
|
(root / "notes.txt").write_text("skip", encoding="utf-8")
|
|
now = datetime(2026, 9, 8, tzinfo=ZoneInfo("Europe/Moscow"))
|
|
removed = prune_snapshots(root, keep_days=7, now=now)
|
|
self.assertEqual(removed, ["2026-08-01"])
|
|
self.assertTrue((root / "2026-09-07").is_dir())
|
|
|
|
|
|
class SchedulerTest(unittest.TestCase):
|
|
def test_waits_until_tomorrow_if_time_passed(self) -> None:
|
|
tz = ZoneInfo("Europe/Moscow")
|
|
now = datetime(2026, 9, 8, 4, 0, tzinfo=tz)
|
|
wait = seconds_until_next("03:00", tz, now=now)
|
|
self.assertGreater(wait, 22 * 3600)
|
|
self.assertLess(wait, 24 * 3600)
|
|
|
|
def test_waits_same_day_if_upcoming(self) -> None:
|
|
tz = ZoneInfo("Europe/Moscow")
|
|
now = datetime(2026, 9, 8, 1, 0, tzinfo=tz)
|
|
wait = seconds_until_next("03:00", tz, now=now)
|
|
self.assertAlmostEqual(wait, 2 * 3600, delta=1)
|
|
|
|
|
|
class RunBackupTest(unittest.TestCase):
|
|
def test_mirrors_repos_and_writes_status(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
cfg = _config(root)
|
|
git = FakeGit()
|
|
repos = [
|
|
{
|
|
"full_name": "alice/app",
|
|
"clone_url": "http://192.168.1.9:3000/alice/app.git",
|
|
"has_wiki": True,
|
|
"private": True,
|
|
},
|
|
{
|
|
"full_name": "bob/lib",
|
|
"clone_url": "http://192.168.1.9:3000/bob/lib.git",
|
|
"has_wiki": False,
|
|
"private": False,
|
|
},
|
|
]
|
|
status = run_backup(
|
|
cfg,
|
|
client_factory=lambda _c: FakeClient(repos),
|
|
git=git, # type: ignore[arg-type]
|
|
)
|
|
self.assertTrue(status["ok"])
|
|
self.assertEqual(status["repos_ok"], 2)
|
|
self.assertEqual(status["gitea_version"], "1.22.0")
|
|
names = [Path(dest).name for _url, dest in git.calls]
|
|
self.assertIn("app.git", names)
|
|
self.assertIn("app.wiki.git", names)
|
|
self.assertIn("lib.git", names)
|
|
saved = json.loads((root / "status.json").read_text(encoding="utf-8"))
|
|
self.assertEqual(saved["repos_total"], 2)
|
|
self.assertTrue((root / "mirrors" / "alice" / "app.git.meta.json").exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|