130 lines
6.2 KiB
Python
130 lines
6.2 KiB
Python
import json
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
import rollback
|
|
from rollback import image_restore_plan, require_tag_rollback, tag_previous_images, verify_previous_images
|
|
|
|
|
|
class RollbackPlanTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.manifest = {
|
|
"images": {
|
|
"backend": {"configured": "akyldash/backend:local", "image_id": "sha256:" + "a" * 64},
|
|
"opensearch": {"configured": "akyldash/opensearch:local", "image_id": "sha256:" + "b" * 64},
|
|
}
|
|
}
|
|
self.configured = {
|
|
"backend": "akyldash/backend:local",
|
|
"opensearch": "akyldash/opensearch:local",
|
|
}
|
|
|
|
def test_plans_only_recorded_local_images(self):
|
|
plan = image_restore_plan(self.manifest, self.configured)
|
|
self.assertEqual(plan["backend"][1], "akyldash/backend:local")
|
|
self.assertEqual(plan["opensearch"][1], "akyldash/opensearch:local")
|
|
|
|
def test_refuses_changed_compose_image_reference(self):
|
|
with self.assertRaisesRegex(ValueError, "reference changed"):
|
|
image_restore_plan(self.manifest, {**self.configured, "backend": "other/backend:tag"})
|
|
|
|
def test_refuses_digest_reference_that_cannot_be_retagged(self):
|
|
self.manifest["images"]["backend"]["configured"] = "registry/backend@sha256:" + "c" * 64
|
|
self.configured["backend"] = self.manifest["images"]["backend"]["configured"]
|
|
plan = image_restore_plan(self.manifest, self.configured)
|
|
self.assertEqual(plan["backend"], ("sha256:" + "a" * 64, self.configured["backend"]))
|
|
with self.assertRaisesRegex(ValueError, "not supported safely"):
|
|
require_tag_rollback(plan)
|
|
|
|
def test_refuses_malformed_digest_reference(self):
|
|
self.manifest["images"]["backend"]["configured"] = "registry/backend@sha256:xyz"
|
|
self.configured["backend"] = self.manifest["images"]["backend"]["configured"]
|
|
with self.assertRaisesRegex(ValueError, "invalid image reference"):
|
|
image_restore_plan(self.manifest, self.configured)
|
|
|
|
def test_refuses_invalid_recorded_image_id(self):
|
|
self.manifest["images"]["backend"]["image_id"] = "sha256:not-a-digest"
|
|
with self.assertRaisesRegex(ValueError, "invalid previous image ID"):
|
|
image_restore_plan(self.manifest, self.configured)
|
|
|
|
def test_digest_reference_must_resolve_to_recorded_image_id(self):
|
|
digest = "registry/backend@sha256:" + "c" * 64
|
|
self.manifest["images"]["backend"]["configured"] = digest
|
|
self.configured["backend"] = digest
|
|
plan = image_restore_plan(self.manifest, self.configured)
|
|
with patch("rollback.run", side_effect=["sha256:" + "a" * 64, "sha256:" + "a" * 64,
|
|
"sha256:" + "b" * 64]):
|
|
verify_previous_images(plan)
|
|
with patch("rollback.run", side_effect=["sha256:" + "a" * 64, "sha256:" + "d" * 64]):
|
|
with self.assertRaisesRegex(ValueError, "does not match its recorded image"):
|
|
verify_previous_images(plan)
|
|
|
|
def test_missing_previous_image_fails_closed(self):
|
|
plan = image_restore_plan(self.manifest, self.configured)
|
|
with patch("rollback.run", side_effect=subprocess.CalledProcessError(1, "docker")):
|
|
with self.assertRaisesRegex(ValueError, "previous image is unavailable"):
|
|
verify_previous_images(plan)
|
|
|
|
def test_digest_reference_is_never_tagged(self):
|
|
digest = "registry/backend@sha256:" + "c" * 64
|
|
plan = {"backend": ("sha256:" + "a" * 64, digest)}
|
|
with patch("rollback.run") as run_mock:
|
|
with self.assertRaisesRegex(ValueError, "not supported safely"):
|
|
tag_previous_images(plan)
|
|
run_mock.assert_not_called()
|
|
|
|
def test_digest_rollback_fails_before_stopping_or_changing_references(self):
|
|
backend_id = "sha256:" + "a" * 64
|
|
opensearch_id = "sha256:" + "b" * 64
|
|
digest = "registry/backend@sha256:" + "c" * 64
|
|
manifest = {
|
|
"images": {
|
|
"backend": {"configured": digest, "image_id": backend_id},
|
|
"opensearch": self.manifest["images"]["opensearch"],
|
|
},
|
|
"deployment_files": {
|
|
"deploy/production/compose.yaml": "compose-hash",
|
|
"deploy/production/Caddyfile": "caddy-hash",
|
|
},
|
|
}
|
|
rendered = {
|
|
"services": {
|
|
"backend": {"image": digest},
|
|
"opensearch": {"image": self.configured["opensearch"]},
|
|
}
|
|
}
|
|
fake_archive = SimpleNamespace(
|
|
digest=lambda path: "compose-hash" if path.name == "compose.yaml" else "caddy-hash",
|
|
verify_archive=lambda _backup: manifest,
|
|
)
|
|
backup = rollback.BACKUP_DIR / "synthetic.tar.gz"
|
|
with (
|
|
patch("rollback.Path.is_file", return_value=True),
|
|
patch("rollback.Path.is_symlink", return_value=False),
|
|
patch("rollback.Path.stat", return_value=SimpleNamespace(st_uid=0, st_mode=0o100600)),
|
|
patch("rollback.compose", return_value=json.dumps(rendered)) as compose_mock,
|
|
patch("rollback.run", side_effect=[backend_id, backend_id, opensearch_id]) as run_mock,
|
|
patch.dict(sys.modules, {"archive": fake_archive}),
|
|
):
|
|
with self.assertRaisesRegex(ValueError, "not supported safely"):
|
|
rollback.rollback(backup, data_compatible=True)
|
|
compose_mock.assert_called_once_with("config", "--format", "json", capture=True)
|
|
self.assertEqual(run_mock.call_count, 3)
|
|
self.assertTrue(all(call.args[0][:3] == ["docker", "image", "inspect"]
|
|
for call in run_mock.call_args_list))
|
|
|
|
def test_tag_rollback_keeps_existing_image_tag_operation(self):
|
|
plan = image_restore_plan(self.manifest, self.configured)
|
|
with patch("rollback.run") as run_mock:
|
|
tag_previous_images(plan)
|
|
self.assertEqual(run_mock.call_count, 2)
|
|
run_mock.assert_any_call(["docker", "image", "tag", "sha256:" + "a" * 64,
|
|
"akyldash/backend:local"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|