100 lines
4.8 KiB
Python
100 lines
4.8 KiB
Python
"""Backups (persistance.backup): a platform's data backed up while it holds a site's
|
|
configuration comes back whole into an empty platform, on SQLite and PostgreSQL; a damaged
|
|
backup restores nothing; restoring over a platform needs --force and keeps what was there."""
|
|
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
FIXTURES = Path(__file__).parents[2] / "tests" / "fixtures"
|
|
STORED = "select pipeline_id, version, doc from config.{kind} order by pipeline_id, version"
|
|
|
|
|
|
def _stored(platform) -> list:
|
|
"""Every saved version of every pipeline and concept map (a kind never saved: none)."""
|
|
found = []
|
|
for kind in ("pipelines", "concept_maps"):
|
|
try:
|
|
found += [tuple(r) for r in platform.query(STORED.format(kind=kind))]
|
|
except Exception as e: # its table appears with its first save
|
|
if "no such table" not in str(e) and "does not exist" not in str(e):
|
|
raise
|
|
return found
|
|
|
|
|
|
def _backup(platform, cli: str, *args: str) -> subprocess.CompletedProcess:
|
|
return subprocess.run(
|
|
["python", "-m", "persistance.backup", cli, *args],
|
|
env=platform.env, cwd=platform.env["ISSSTELEON_DATA"], capture_output=True, text=True,
|
|
) # fmt: skip
|
|
|
|
|
|
def _made(platform, tmp_path_factory) -> Path:
|
|
"""A backup of the platform, outside its data folder; its folder."""
|
|
made = _backup(platform, "create", "--to", str(tmp_path_factory.mktemp("backups")))
|
|
assert made.returncode == 0, made.stderr
|
|
return Path(made.stdout.strip())
|
|
|
|
|
|
def _reset(platform) -> None:
|
|
"""The platform emptied, as on a new machine: its data folder and (PostgreSQL) schemas."""
|
|
data = Path(platform.env["ISSSTELEON_DATA"])
|
|
for p in data.iterdir():
|
|
if p.name.startswith("platform") or p.name in ("dlt", "mlflow.db"):
|
|
subprocess.run(["rm", "-rf", str(p)], check=True)
|
|
if "postgres" in platform.env["PLATFORM_DATABASE_URL"]:
|
|
for schema in ("bronze", "silver", "gold", "config"):
|
|
subprocess.run(["psql", platform.env["PLATFORM_DATABASE_URL"], "-qc",
|
|
f"drop schema if exists {schema} cascade"], check=True) # fmt: skip
|
|
|
|
|
|
def test_a_backup_restores_the_site_whole(platform, tmp_path_factory):
|
|
platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json")
|
|
platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") # v2
|
|
platform.save("concept_maps", "erick_site", FIXTURES / "concept_maps" / "erick_site.json")
|
|
before = _stored(platform)
|
|
assert len(before) == 3
|
|
|
|
made = _backup(platform, "create", "--to", str(tmp_path_factory.mktemp("backups")))
|
|
assert made.returncode == 0, made.stderr
|
|
folder = Path(made.stdout.strip())
|
|
manifest = json.loads((folder / "manifest.json").read_text())
|
|
assert all(len(f["sha256"]) == 64 for f in manifest["files"].values())
|
|
assert _backup(platform, "verify", str(folder)).stdout.strip() == "ok"
|
|
|
|
# over a platform: refused without --force
|
|
refused = _backup(platform, "restore", str(folder))
|
|
assert refused.returncode != 0 and "--force" in refused.stderr
|
|
|
|
_reset(platform)
|
|
assert not any(n.startswith("platform") for n in
|
|
(p.name for p in Path(platform.env["ISSSTELEON_DATA"]).iterdir())) # fmt: skip
|
|
restored = _backup(platform, "restore", str(folder))
|
|
assert restored.returncode == 0, restored.stderr
|
|
assert _stored(platform) == before
|
|
|
|
|
|
def test_a_damaged_backup_restores_nothing(platform, tmp_path_factory):
|
|
platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json")
|
|
folder = _made(platform, tmp_path_factory)
|
|
damaged = next(p for p in folder.rglob("*") if p.is_file() and p.name != "manifest.json")
|
|
damaged.write_bytes(damaged.read_bytes() + b"\0") # one byte more
|
|
checked = _backup(platform, "verify", str(folder))
|
|
assert checked.returncode == 1 and "changed since the backup" in checked.stdout
|
|
|
|
restored = _backup(platform, "restore", str(folder), "--force")
|
|
assert restored.returncode != 0 and "damaged" in restored.stderr
|
|
assert len(_stored(platform)) == 1 # the platform as it was
|
|
|
|
|
|
def test_restoring_over_a_platform_keeps_what_was_there(fresh_platform, tmp_path_factory):
|
|
"""SQLite: what the data folder held is set aside, not mixed with the backup's."""
|
|
platform = fresh_platform
|
|
platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json")
|
|
folder = _made(platform, tmp_path_factory)
|
|
platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") # newer
|
|
assert _backup(platform, "restore", str(folder), "--force").returncode == 0
|
|
assert len(_stored(platform)) == 1 # the backup's moment, nothing newer mixed in
|
|
aside = list(Path(platform.env["ISSSTELEON_DATA"]).glob(".before-restore-*"))
|
|
assert len(aside) == 1 and any(aside[0].glob("platform__config.db"))
|