Initial ABSDEV deployment framework development preview
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,399 @@
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from core.database import get_adapter
|
||||
|
||||
from core.backup import (
|
||||
BackupError,
|
||||
BackupEngine,
|
||||
)
|
||||
|
||||
|
||||
class BackupEngineTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
|
||||
self.root = Path(self.temp.name) / "backups"
|
||||
self.root.mkdir(mode=0o700)
|
||||
|
||||
self.config = json.loads(
|
||||
Path("config/site.example.json").read_text()
|
||||
)
|
||||
|
||||
self.adapter = get_adapter(self.config)
|
||||
|
||||
self.payload = b"-- SQL backup fixture\nSELECT 1;\n"
|
||||
|
||||
self.commands = []
|
||||
|
||||
self.engine = BackupEngine(
|
||||
self.adapter,
|
||||
self.root,
|
||||
self.runner,
|
||||
)
|
||||
|
||||
def runner(self, command, output):
|
||||
|
||||
self.commands.append(command)
|
||||
|
||||
if command == self.adapter.identity_command():
|
||||
|
||||
output.write(
|
||||
b"example_db\texample_app@%\t"
|
||||
b"11111111-2222-3333-4444-555555555555\n"
|
||||
)
|
||||
|
||||
elif command == self.adapter.backup_command():
|
||||
|
||||
output.write(self.payload)
|
||||
|
||||
else:
|
||||
|
||||
raise AssertionError("Unexpected command")
|
||||
|
||||
def create(self):
|
||||
|
||||
return self.engine.create()
|
||||
|
||||
def test_mysql_backup_created(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
self.assertTrue(path.is_dir())
|
||||
self.assertTrue((path / "database.sql").is_file())
|
||||
|
||||
def test_backup_contents_preserved(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
self.assertEqual(
|
||||
(path / "database.sql").read_bytes(),
|
||||
self.payload,
|
||||
)
|
||||
|
||||
def test_backup_checksum_verified(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
result = self.engine.verify(path)
|
||||
|
||||
self.assertEqual(
|
||||
result["bytes"],
|
||||
len(self.payload),
|
||||
)
|
||||
|
||||
def test_manifest_created(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
self.assertTrue(
|
||||
(path / "manifest.json").is_file()
|
||||
)
|
||||
|
||||
def test_manifest_database_identity(self):
|
||||
|
||||
result = self.engine.verify(self.create())
|
||||
|
||||
self.assertEqual(
|
||||
result["database"],
|
||||
"example_db",
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
result["driver"],
|
||||
"mysql",
|
||||
)
|
||||
|
||||
def test_unique_backup_names(self):
|
||||
|
||||
self.assertNotEqual(
|
||||
self.create(),
|
||||
self.create(),
|
||||
)
|
||||
|
||||
def test_directory_permissions(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
self.assertEqual(
|
||||
path.stat().st_mode & 0o777,
|
||||
0o700,
|
||||
)
|
||||
|
||||
def test_backup_permissions(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
self.assertEqual(
|
||||
(path / "database.sql").stat().st_mode
|
||||
& 0o777,
|
||||
0o600,
|
||||
)
|
||||
|
||||
def test_manifest_permissions(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
self.assertEqual(
|
||||
(path / "manifest.json").stat().st_mode
|
||||
& 0o777,
|
||||
0o600,
|
||||
)
|
||||
|
||||
def test_corrupted_backup_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
(path / "database.sql").write_bytes(b"corrupt")
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_missing_backup_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
(path / "database.sql").unlink()
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_missing_manifest_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
(path / "manifest.json").unlink()
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_empty_backup_rejected(self):
|
||||
|
||||
self.payload = b""
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.create()
|
||||
|
||||
self.assertEqual(list(self.root.iterdir()), [])
|
||||
|
||||
def test_symlink_backup_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
backup = path / "database.sql"
|
||||
backup.unlink()
|
||||
backup.symlink_to("/etc/passwd")
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_symlink_manifest_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
manifest = path / "manifest.json"
|
||||
manifest.unlink()
|
||||
manifest.symlink_to("/etc/passwd")
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_wrong_database_rejected(self):
|
||||
|
||||
def wrong_identity(command, output):
|
||||
|
||||
output.write(
|
||||
b"another_db\texample_app@%\tserver\n"
|
||||
)
|
||||
|
||||
self.engine.runner = wrong_identity
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.create()
|
||||
|
||||
self.assertEqual(list(self.root.iterdir()), [])
|
||||
|
||||
def test_wrong_account_rejected(self):
|
||||
|
||||
def wrong_identity(command, output):
|
||||
|
||||
output.write(
|
||||
b"example_db\troot@localhost\tserver\n"
|
||||
)
|
||||
|
||||
self.engine.runner = wrong_identity
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.create()
|
||||
|
||||
def test_runner_failure_cleaned_up(self):
|
||||
|
||||
def failing_runner(command, output):
|
||||
|
||||
if command == self.adapter.identity_command():
|
||||
|
||||
self.runner(command, output)
|
||||
return
|
||||
|
||||
output.write(b"partial output")
|
||||
|
||||
raise RuntimeError("Simulated database failure")
|
||||
|
||||
self.engine.runner = failing_runner
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.create()
|
||||
|
||||
self.assertEqual(list(self.root.iterdir()), [])
|
||||
|
||||
def test_insecure_directory_rejected(self):
|
||||
|
||||
self.root.chmod(0o777)
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.create()
|
||||
|
||||
def test_unexpected_file_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
(path / "unexpected").touch()
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_external_directory_rejected(self):
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(Path(self.temp.name))
|
||||
|
||||
def test_hardlinked_backup_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
os.link(
|
||||
path / "database.sql",
|
||||
self.root / "external-link",
|
||||
)
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_manifest_path_traversal_rejected(self):
|
||||
|
||||
path = self.create()
|
||||
|
||||
manifest = path / "manifest.json"
|
||||
|
||||
data = json.loads(manifest.read_text())
|
||||
data["filename"] = "../../etc/passwd"
|
||||
|
||||
manifest.write_text(json.dumps(data))
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.engine.verify(path)
|
||||
|
||||
def test_identity_checked_after_backup(self):
|
||||
|
||||
self.create()
|
||||
|
||||
self.assertEqual(
|
||||
self.commands,
|
||||
[
|
||||
self.adapter.identity_command(),
|
||||
self.adapter.backup_command(),
|
||||
self.adapter.identity_command(),
|
||||
],
|
||||
)
|
||||
|
||||
def test_database_identity_change_rejected(self):
|
||||
|
||||
calls = 0
|
||||
|
||||
def changing_identity(command, output):
|
||||
|
||||
nonlocal calls
|
||||
|
||||
if command == self.adapter.identity_command():
|
||||
|
||||
calls += 1
|
||||
|
||||
output.write(
|
||||
f"example_db\texample_app@%\t"
|
||||
f"server-{calls}\n".encode()
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
output.write(self.payload)
|
||||
|
||||
self.engine.runner = changing_identity
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
|
||||
self.create()
|
||||
|
||||
self.assertEqual(list(self.root.iterdir()), [])
|
||||
|
||||
def test_postgresql_backup_created(self):
|
||||
|
||||
self.config["database"].update({
|
||||
"driver": "pgsql",
|
||||
"container": "platform-postgres",
|
||||
"host": "platform-postgres",
|
||||
"port": 5432,
|
||||
})
|
||||
|
||||
adapter = get_adapter(self.config)
|
||||
|
||||
def runner(command, output):
|
||||
|
||||
if command == adapter.identity_command():
|
||||
|
||||
output.write(b"example_db|example_app\n")
|
||||
|
||||
else:
|
||||
|
||||
output.write(b"PGDMP simulated archive")
|
||||
|
||||
engine = BackupEngine(
|
||||
adapter,
|
||||
self.root,
|
||||
runner,
|
||||
)
|
||||
|
||||
path = engine.create()
|
||||
|
||||
self.assertTrue(
|
||||
(path / "database.dump").is_file()
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
engine.verify(path)["driver"],
|
||||
"pgsql",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user