118 lines
5.8 KiB
Python
118 lines
5.8 KiB
Python
import hashlib
|
|
import io
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from contextlib import nullcontext
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "backup"))
|
|
|
|
import external_rollback
|
|
from external_session import read_challenge, write_request
|
|
from release_state import read_json, validate_state
|
|
|
|
|
|
class ExternalRollbackTests(unittest.TestCase):
|
|
def test_pending_journal_is_durable_before_rollback_service_stop(self):
|
|
manifest = {"git_revision": "a" * 40}
|
|
manifest_sha = hashlib.sha256(
|
|
json.dumps(manifest, sort_keys=True, separators=(",", ":")).encode()
|
|
).hexdigest()
|
|
identity = {
|
|
"backup_id": "akyldash-20261031T120000Z-12345678", "size": 64,
|
|
"archive_sha256": "b" * 64, "manifest_sha256": manifest_sha, "manifest": manifest,
|
|
}
|
|
current = {
|
|
"backend": {"configured": "registry/backend:current", "image_id": "sha256:" + "1" * 64},
|
|
"opensearch": {"configured": "registry/search:current", "image_id": "sha256:" + "2" * 64},
|
|
}
|
|
state = {
|
|
"schema_version": 2, "commit": "c" * 40, "manifest_sha256": "d" * 64,
|
|
"current": current, "previous": current, "previous_backup": identity,
|
|
}
|
|
request = io.BytesIO()
|
|
write_request(request, {"version": 1, "action": "rollback", "data_compatible": True})
|
|
request.seek(0)
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
journal = Path(directory) / "journal.json"
|
|
with (
|
|
patch.object(external_rollback.os, "geteuid", return_value=0),
|
|
patch.object(external_rollback, "STATE_FILE", Path(directory) / "state.json"),
|
|
patch.object(external_rollback, "JOURNAL_FILE", journal),
|
|
patch.object(external_rollback, "read_json", return_value=state),
|
|
patch.object(external_rollback, "deployment_lock", return_value=nullcontext()),
|
|
patch.object(external_rollback, "nonce", return_value="e" * 64),
|
|
patch.object(external_rollback, "receive_archive", return_value=(64, "b" * 64)),
|
|
patch.object(external_rollback, "read_ack"),
|
|
patch.object(external_rollback, "image_ids", return_value=current),
|
|
patch.object(external_rollback, "compose", side_effect=KeyboardInterrupt),
|
|
):
|
|
with self.assertRaises(KeyboardInterrupt):
|
|
external_rollback.execute(reader=request, writer=io.BytesIO())
|
|
self.assertEqual(read_json(journal)["status"], "external-backup-pending")
|
|
|
|
def test_release_state_rejects_boolean_schema_version(self):
|
|
with self.assertRaisesRegex(ValueError, "unsupported schema"):
|
|
validate_state({
|
|
"schema_version": True, "commit": "a" * 40,
|
|
"manifest_sha256": "b" * 64, "current": {}, "previous": {},
|
|
})
|
|
|
|
def test_release_state_rejects_duplicate_identity_fields(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "state.json"
|
|
path.write_text('{"schema_version":1,"schema_version":2}', encoding="utf-8")
|
|
with self.assertRaisesRegex(ValueError, "duplicate release state field"):
|
|
read_json(path)
|
|
|
|
def test_unpinned_archive_stream_fails_before_service_operations(self):
|
|
manifest = {"git_revision": "a" * 40}
|
|
identity = {
|
|
"backup_id": "akyldash-20261031T120000Z-12345678", "size": 64,
|
|
"archive_sha256": "b" * 64,
|
|
"manifest_sha256": hashlib.sha256(
|
|
json.dumps(manifest, sort_keys=True, separators=(",", ":")).encode()
|
|
).hexdigest(),
|
|
"manifest": manifest,
|
|
}
|
|
state = {
|
|
"schema_version": 2, "commit": "c" * 40, "manifest_sha256": "d" * 64,
|
|
"current": {
|
|
"backend": {"configured": "registry/backend:current", "image_id": "sha256:" + "1" * 64},
|
|
"opensearch": {"configured": "registry/search:current", "image_id": "sha256:" + "2" * 64},
|
|
},
|
|
"previous": {
|
|
"backend": {"configured": "registry/backend:old", "image_id": "sha256:" + "3" * 64},
|
|
"opensearch": {"configured": "registry/search:old", "image_id": "sha256:" + "4" * 64},
|
|
},
|
|
"previous_backup": identity,
|
|
}
|
|
request = io.BytesIO()
|
|
write_request(request, {"version": 1, "action": "rollback", "data_compatible": True})
|
|
request.seek(0)
|
|
response = io.BytesIO()
|
|
with tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(external_rollback.os, "geteuid", return_value=0), \
|
|
patch.object(external_rollback, "STATE_FILE", Path(directory) / "state.json"), \
|
|
patch.object(external_rollback, "JOURNAL_FILE", Path(directory) / "journal.json"), \
|
|
patch.object(external_rollback, "read_json", return_value=state), \
|
|
patch.object(external_rollback, "deployment_lock", return_value=nullcontext()), \
|
|
patch.object(external_rollback, "nonce", return_value="e" * 64), \
|
|
patch.object(external_rollback, "receive_archive", return_value=(64, "f" * 64)), \
|
|
patch.object(external_rollback, "image_ids") as image_ids, \
|
|
patch.object(external_rollback, "compose") as compose:
|
|
with self.assertRaisesRegex(ValueError, "root-pinned"):
|
|
external_rollback.execute(reader=request, writer=response)
|
|
response.seek(0)
|
|
challenge = read_challenge(response)
|
|
self.assertEqual(challenge["backup_id"], identity["backup_id"])
|
|
image_ids.assert_not_called()
|
|
compose.assert_not_called()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|