Files
akyldash/infra/backup/test_archive.py
T
agent 6f809d3196
CI / tests-and-images (pull_request) Has been cancelled
Add guarded rollback and recovery apply tools
2026-10-11 19:30:22 +03:00

155 lines
6.9 KiB
Python

import hashlib
import json
import sqlite3
import tarfile
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
from archive import COMPONENTS, compose_sources, create_backup, restore_to_staging, verify_archive
class BackupArchiveTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory()
self.root = Path(self.temporary.name)
self.private = self.root / "private"
self.private.mkdir(mode=0o700)
self.sources = {}
for name in COMPONENTS:
source = self.root / name
source.mkdir()
self.sources[name] = source
for name in ("production-env", "review-secret"):
path = self.root / f"{name}.txt"
path.write_text("private", encoding="utf-8")
path.chmod(0o644)
self.sources[name] = path
connection = sqlite3.connect(self.sources["data"] / "search-users.sqlite3")
connection.execute("CREATE TABLE users (id INTEGER PRIMARY KEY)")
connection.commit()
connection.close()
self.backup = self.private / "backup.tar.gz"
def tearDown(self):
self.temporary.cleanup()
def test_create_verify_and_restore_only_to_new_staging(self):
create_backup(self.backup, self.sources, "abc123")
manifest = verify_archive(self.backup)
self.assertEqual(manifest["git_revision"], "abc123")
self.assertEqual(manifest["images"], {})
target = self.root / "akyldash-restore-test"
restore_to_staging(self.backup, target)
self.assertTrue((target / "data/search-users.sqlite3").is_file())
self.assertEqual((target / "data/search-users.sqlite3").stat().st_mode & 0o777, 0o600)
self.assertEqual((target / "production-env").stat().st_mode & 0o777, 0o600)
self.assertEqual((target / "review-secret").stat().st_mode & 0o777, 0o600)
self.assertEqual(verify_archive(self.backup)["components"], sorted(COMPONENTS))
def test_rejects_checksum_mismatch_without_creating_staging(self):
create_backup(self.backup, self.sources, "abc123")
self.backup.write_bytes(self.backup.read_bytes() + b"tamper")
target = self.root / "akyldash-restore-tampered"
with self.assertRaisesRegex(ValueError, "SHA-256"):
restore_to_staging(self.backup, target)
self.assertFalse(target.exists())
def test_rejects_non_staging_and_existing_targets(self):
create_backup(self.backup, self.sources, "abc123")
with self.assertRaisesRegex(ValueError, "staging"):
restore_to_staging(self.backup, self.root / "production")
target = self.root / "akyldash-restore-existing"
target.mkdir()
with self.assertRaisesRegex(ValueError, "already exists"):
restore_to_staging(self.backup, target)
def test_create_checks_sqlite_integrity_before_writing_archive(self):
(self.sources["data"] / "search-reviews.sqlite3").write_bytes(b"not a database")
with self.assertRaises(sqlite3.DatabaseError):
create_backup(self.backup, self.sources, "abc123")
self.assertFalse(self.backup.exists())
def test_rejects_unsafe_archive_paths(self):
archive_path = self.private / "unsafe.tar.gz"
with tarfile.open(archive_path, "w:gz") as archive:
archive.addfile(tarfile.TarInfo("manifest.json"))
archive.addfile(tarfile.TarInfo("../outside"))
checksum = hashlib.sha256(archive_path.read_bytes()).hexdigest()
Path(f"{archive_path}.sha256").write_text(f"{checksum} unsafe.tar.gz\n")
with self.assertRaisesRegex(ValueError, "unsafe archive path"):
verify_archive(archive_path)
def test_cold_backup_refuses_a_running_compose_container(self):
config = {"services": {}, "volumes": {}, "secrets": {}}
def run(command, **_kwargs):
if "config" in command:
return SimpleNamespace(stdout=json.dumps(config))
if "ps" in command:
return SimpleNamespace(stdout="container-id\n")
return SimpleNamespace(stdout="true\n")
with patch("archive.subprocess.run", side_effect=run):
with self.assertRaisesRegex(ValueError, "stop all Compose services"):
compose_sources(Path("deploy/production/compose.yaml"), Path("/etc/akyldash/production.env"))
def test_compose_backup_records_sources_and_image_ids(self):
data = self.root / "production-data"
data.mkdir()
secret = self.root / "secret"
secret.write_text("private", encoding="utf-8")
env_file = self.root / "production.env"
env_file.write_text("DATA_ROOT=/var/lib/akyldash/data\n", encoding="utf-8")
volumes = {}
volume_paths = {}
mounts = {
"caddy": [
{"type": "volume", "source": "caddy-data", "target": "/data"},
{"type": "volume", "source": "caddy-config", "target": "/config"},
],
"backend": [{"type": "bind", "source": str(data), "target": "/app/data"}],
"opensearch": [
{"type": "volume", "source": "opensearch-data", "target": "/usr/share/opensearch/data"}
],
}
for name in ("caddy-data", "caddy-config", "opensearch-data"):
path = self.root / f"volume-{name}"
path.mkdir()
volumes[name] = {"name": f"akyldash_{name}"}
volume_paths[f"akyldash_{name}"] = path
config = {
"services": {name: {"volumes": values} for name, values in mounts.items()},
"volumes": volumes,
"secrets": {"review_secret": {"file": str(secret)}},
}
service_names = ["caddy", "backend", "opensearch"]
def run(command, **_kwargs):
if "config" in command:
return SimpleNamespace(stdout=json.dumps(config))
if "ps" in command:
return SimpleNamespace(stdout="\n".join(f"{name}-id" for name in service_names))
if command[1] == "inspect":
name = command[-1].removesuffix("-id")
return SimpleNamespace(stdout=f"false\t{name}\t{name}:local\tsha256:{name}\n")
if command[1] == "volume":
return SimpleNamespace(stdout=f"{volume_paths[command[-1]]}\n")
return SimpleNamespace(stdout="")
with patch("archive.subprocess.run", side_effect=run):
sources, revision, images, deployment_files = compose_sources(
Path.cwd() / "deploy/production/compose.yaml", env_file
)
self.assertEqual(revision, "")
self.assertEqual(set(sources), COMPONENTS)
self.assertEqual(images["backend"]["image_id"], "sha256:backend")
self.assertEqual(set(deployment_files), {"deploy/production/compose.yaml", "deploy/production/Caddyfile"})
if __name__ == "__main__":
unittest.main()