84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
import hashlib
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
BACKUP_SCRIPT = REPO_ROOT / "scripts/backup.sh"
|
|
|
|
|
|
class BackupRestoreTests(unittest.TestCase):
|
|
def run_backup(self, env):
|
|
return subprocess.run(
|
|
["bash", str(BACKUP_SCRIPT)],
|
|
env={**os.environ, **env},
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
|
|
def test_backup_requires_explicit_external_paths_and_opt_in(self):
|
|
completed = self.run_backup({})
|
|
|
|
self.assertNotEqual(completed.returncode, 0)
|
|
self.assertIn("BACKUP_ROOT", completed.stderr)
|
|
|
|
def test_synthetic_backup_excludes_secrets_and_restores_in_temp(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
source = root / "knowledge-source"
|
|
output = root / "backups"
|
|
source.mkdir()
|
|
(source / "subdir").mkdir()
|
|
expected = b"Conteudo totalmente sintetico.\n"
|
|
(source / "subdir" / "fixture.md").write_bytes(expected)
|
|
(source / "credentials.txt").write_text("synthetic-secret", encoding="utf-8")
|
|
(source / ".env").write_text("TOKEN=synthetic-secret", encoding="utf-8")
|
|
(source / "notes.pdf").write_bytes(b"fake-pdf")
|
|
(source / "ignored.csv").write_text("not allowlisted", encoding="utf-8")
|
|
try:
|
|
(source / "linked.md").symlink_to(source / "subdir" / "fixture.md")
|
|
except OSError:
|
|
pass
|
|
|
|
completed = self.run_backup({
|
|
"BACKUP_ROOT": str(output),
|
|
"ATIVA_KNOWLEDGE_ROOT": str(source),
|
|
"INCLUDE_KNOWLEDGE": "YES",
|
|
})
|
|
self.assertEqual(completed.returncode, 0, completed.stderr)
|
|
backups = list(output.iterdir())
|
|
self.assertEqual(len(backups), 1)
|
|
backup = backups[0]
|
|
stored = backup / "knowledge/subdir/fixture.md"
|
|
self.assertEqual(stored.read_bytes(), expected)
|
|
self.assertFalse((backup / "knowledge/credentials.txt").exists())
|
|
self.assertFalse((backup / "knowledge/.env").exists())
|
|
self.assertFalse((backup / "knowledge/ignored.csv").exists())
|
|
|
|
restore = root / "restored-isolated"
|
|
shutil.copytree(backup / "knowledge", restore)
|
|
self.assertEqual((restore / "subdir/fixture.md").read_bytes(), expected)
|
|
digest = hashlib.sha256(expected).hexdigest()
|
|
manifest = (backup / "SHA256SUMS").read_text(encoding="utf-8")
|
|
self.assertIn(digest, manifest)
|
|
self.assertEqual((backup / "MANIFESTO.txt").stat().st_mode & 0o777, 0o600)
|
|
|
|
def test_backup_refuses_checkout_destination(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
source = Path(temporary) / "source"
|
|
source.mkdir()
|
|
completed = self.run_backup({
|
|
"BACKUP_ROOT": str(REPO_ROOT),
|
|
"ATIVA_KNOWLEDGE_ROOT": str(source),
|
|
"INCLUDE_KNOWLEDGE": "YES",
|
|
})
|
|
|
|
self.assertNotEqual(completed.returncode, 0)
|
|
self.assertIn("não pode ser o checkout", completed.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|