Complete MySQL disaster recovery qualification with persistent cross-instance restoration tests
ABSDEV Framework CI / framework-ci (push) Successful in 21s
ABSDEV Framework CI / framework-ci (push) Successful in 21s
This commit is contained in:
@@ -1239,6 +1239,274 @@ class MySQLIntegrationTests(unittest.TestCase):
|
|||||||
|
|
||||||
evidence["verified_checks"] = list(queries)
|
evidence["verified_checks"] = list(queries)
|
||||||
|
|
||||||
|
print("\n=== CROSS-INSTANCE MYSQL RECOVERY ===")
|
||||||
|
|
||||||
|
cross_checks = []
|
||||||
|
|
||||||
|
def cross_pass(label):
|
||||||
|
cross_checks.append(label)
|
||||||
|
print("PASS:", label)
|
||||||
|
|
||||||
|
# Record the original server and storage identity.
|
||||||
|
|
||||||
|
original_uuid = sql(
|
||||||
|
"SELECT @@server_uuid",
|
||||||
|
SOURCE_DATABASE,
|
||||||
|
).strip().decode()
|
||||||
|
|
||||||
|
original_item = verify_isolation()
|
||||||
|
|
||||||
|
original_mounts = {
|
||||||
|
mount["Source"]
|
||||||
|
for mount in original_item["Mounts"]
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assertTrue(original_uuid)
|
||||||
|
self.assertTrue(original_mounts)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
manifest["identity"].split(chr(9))[2],
|
||||||
|
original_uuid,
|
||||||
|
)
|
||||||
|
|
||||||
|
engine.verify(backup)
|
||||||
|
|
||||||
|
cross_pass("original_server_identity_recorded")
|
||||||
|
|
||||||
|
# Remove the original disposable instance.
|
||||||
|
# Recovery must not depend on its availability.
|
||||||
|
|
||||||
|
old_cid = cid
|
||||||
|
|
||||||
|
command("docker", "rm", "-f", "-v", cid)
|
||||||
|
|
||||||
|
cid = None
|
||||||
|
|
||||||
|
remaining = command(
|
||||||
|
"docker", "ps", "-aq", "--no-trunc"
|
||||||
|
).decode().splitlines()
|
||||||
|
|
||||||
|
self.assertNotIn(old_cid, remaining)
|
||||||
|
|
||||||
|
cross_pass("original_database_server_removed")
|
||||||
|
|
||||||
|
# Create a fresh independent server using the same
|
||||||
|
# verified immutable image and isolation restrictions.
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory(dir=work) as directory:
|
||||||
|
|
||||||
|
envfile = Path(directory) / "mysql.env"
|
||||||
|
|
||||||
|
envfile.write_text(
|
||||||
|
f"MYSQL_ROOT_PASSWORD={password}\n"
|
||||||
|
f"MYSQL_DATABASE={RESTORE_DATABASE}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
envfile.chmod(0o600)
|
||||||
|
|
||||||
|
cid = command(
|
||||||
|
"docker", "create",
|
||||||
|
"--pull=never",
|
||||||
|
"--name", name,
|
||||||
|
"--label", f"{LABEL}=true",
|
||||||
|
"--label", f"absdev.integration.token={token}",
|
||||||
|
"--network", "none",
|
||||||
|
"--restart", "no",
|
||||||
|
"--memory", "1024m",
|
||||||
|
"--memory-swap", "1024m",
|
||||||
|
"--cpus", "1.0",
|
||||||
|
"--pids-limit", "256",
|
||||||
|
"--security-opt", "no-new-privileges=true",
|
||||||
|
"--env-file", str(envfile),
|
||||||
|
IMAGE_REF,
|
||||||
|
"--innodb-buffer-pool-size=64M",
|
||||||
|
"--max-connections=20",
|
||||||
|
"--performance-schema=OFF",
|
||||||
|
"--skip-log-bin",
|
||||||
|
).decode().strip()
|
||||||
|
|
||||||
|
self.assertNotEqual(cid, old_cid)
|
||||||
|
|
||||||
|
replacement = verify_isolation()
|
||||||
|
|
||||||
|
replacement_mounts = {
|
||||||
|
mount["Source"]
|
||||||
|
for mount in replacement["Mounts"]
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assertTrue(replacement_mounts)
|
||||||
|
|
||||||
|
self.assertFalse(
|
||||||
|
original_mounts.intersection(replacement_mounts)
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("independent_container_and_storage")
|
||||||
|
|
||||||
|
command("docker", "start", cid)
|
||||||
|
|
||||||
|
verify_isolation()
|
||||||
|
|
||||||
|
command(
|
||||||
|
"docker", "exec", "-i", cid,
|
||||||
|
"sh", "-c",
|
||||||
|
"umask 077; "
|
||||||
|
"mkdir -p /run/secrets; "
|
||||||
|
"cat > /run/secrets/absdev-client.cnf",
|
||||||
|
input_data=client,
|
||||||
|
)
|
||||||
|
|
||||||
|
deadline = time.monotonic() + 90
|
||||||
|
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
|
||||||
|
state = verify_isolation()["State"]
|
||||||
|
|
||||||
|
require(
|
||||||
|
state["Running"] and not state["OOMKilled"],
|
||||||
|
"Replacement MySQL server stopped unexpectedly",
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if sql(
|
||||||
|
"SELECT 1",
|
||||||
|
RESTORE_DATABASE,
|
||||||
|
).strip() == b"1":
|
||||||
|
break
|
||||||
|
|
||||||
|
except (RuntimeError, subprocess.TimeoutExpired):
|
||||||
|
pass
|
||||||
|
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Replacement MySQL readiness timed out"
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("replacement_authenticated_readiness")
|
||||||
|
|
||||||
|
replacement_uuid = sql(
|
||||||
|
"SELECT @@server_uuid",
|
||||||
|
RESTORE_DATABASE,
|
||||||
|
).strip().decode()
|
||||||
|
|
||||||
|
self.assertTrue(replacement_uuid)
|
||||||
|
|
||||||
|
self.assertNotEqual(
|
||||||
|
replacement_uuid,
|
||||||
|
original_uuid,
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("independent_mysql_server_uuid")
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
sql(
|
||||||
|
"SELECT COUNT(*) FROM information_schema.tables "
|
||||||
|
"WHERE TABLE_SCHEMA=DATABASE()",
|
||||||
|
RESTORE_DATABASE,
|
||||||
|
).strip(),
|
||||||
|
b"0",
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("replacement_restore_target_empty")
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
sql(
|
||||||
|
"SELECT COUNT(*) FROM information_schema.schemata "
|
||||||
|
"WHERE schema_name='example_db'"
|
||||||
|
).strip(),
|
||||||
|
b"0",
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("original_source_database_absent")
|
||||||
|
|
||||||
|
# Rebuild the restoration command to bind it to the
|
||||||
|
# replacement container ID, not the removed instance.
|
||||||
|
|
||||||
|
cross_restore = bounded(
|
||||||
|
adapter.restore_command(RESTORE_DATABASE)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIn(cid, cross_restore)
|
||||||
|
self.assertNotIn(old_cid, cross_restore)
|
||||||
|
|
||||||
|
engine.verify(backup)
|
||||||
|
|
||||||
|
verify_isolation()
|
||||||
|
|
||||||
|
with (backup / "database.sql").open("rb") as handle:
|
||||||
|
|
||||||
|
restored = subprocess.run(
|
||||||
|
cross_restore,
|
||||||
|
stdin=handle,
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
timeout=75,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
restored.returncode,
|
||||||
|
0,
|
||||||
|
"Independent server restoration failed",
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("cross_instance_restore_completed")
|
||||||
|
|
||||||
|
for label, query in queries.items():
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
sql(query, RESTORE_DATABASE),
|
||||||
|
source_snapshot[label],
|
||||||
|
f"Independent server restoration mismatch: {label}",
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("all_six_cross_instance_comparisons")
|
||||||
|
|
||||||
|
cross_fk = [
|
||||||
|
"docker", "exec", cid,
|
||||||
|
"timeout", "-k", "5s", "60s",
|
||||||
|
"mysql",
|
||||||
|
adapter.client_configuration,
|
||||||
|
"--database=" + RESTORE_DATABASE,
|
||||||
|
"--execute",
|
||||||
|
"INSERT INTO orders VALUES (999,999,1.00,NULL)",
|
||||||
|
]
|
||||||
|
|
||||||
|
expect_mysql_error(
|
||||||
|
cross_fk,
|
||||||
|
1452,
|
||||||
|
"23000",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
sql(
|
||||||
|
"SELECT COUNT(*) FROM orders",
|
||||||
|
RESTORE_DATABASE,
|
||||||
|
).strip(),
|
||||||
|
b"2",
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("cross_instance_foreign_key_enforcement")
|
||||||
|
|
||||||
|
self.assertEqual(engine.verify(backup), manifest)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
engine.verify(recovery_backup),
|
||||||
|
recovery_manifest,
|
||||||
|
)
|
||||||
|
|
||||||
|
cross_pass("original_and_recovery_backups_preserved")
|
||||||
|
|
||||||
|
self.assertEqual(len(cross_checks), 11)
|
||||||
|
|
||||||
|
evidence["cross_instance_checks"] = cross_checks
|
||||||
|
|
||||||
|
evidence["original_server_uuid"] = original_uuid
|
||||||
|
|
||||||
|
evidence["restored_server_uuid"] = replacement_uuid
|
||||||
|
|
||||||
|
print("PASS: All 11 persistent cross-instance checks")
|
||||||
|
|
||||||
evidence["result"] = "MYSQL_RESTORE_VERIFIED"
|
evidence["result"] = "MYSQL_RESTORE_VERIFIED"
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user