feat: add managed database capture and operator restore
PostgreSQL and MySQL backups run through paired outposts, with operator sign-in, off-site copies, and legacy import. Retention stays on each orbit, so the unused retention console is removed. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
135
tests/mbu/capture-mysql.py
Normal file
135
tests/mbu/capture-mysql.py
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python3
|
||||
"""MySQL direct and pinned SSH capture with view/routine/event fixtures."""
|
||||
|
||||
from contextlib import ExitStack
|
||||
from pathlib import Path
|
||||
from subprocess import DEVNULL
|
||||
from tempfile import TemporaryDirectory
|
||||
from uuid import uuid4
|
||||
|
||||
from fixture import PASSWORD, bastion, command, mysql_container, pair, start_agent, start_hub, terminal, wait_until
|
||||
|
||||
|
||||
def main():
|
||||
with ExitStack() as cleanup:
|
||||
root = Path(cleanup.enter_context(TemporaryDirectory(prefix="tessera-mysql-")))
|
||||
mysql, defaults, mysql_port = mysql_container(cleanup, root)
|
||||
password = root / "mysql-password"
|
||||
password.write_text(PASSWORD + "\n")
|
||||
password.chmod(0o600)
|
||||
command(*mysql, "-D", "fixture", "-e",
|
||||
"CREATE TABLE sample(id INT PRIMARY KEY, label VARCHAR(40)) CHARSET utf8mb4;"
|
||||
"INSERT INTO sample VALUES(1, 'backup 🛰');"
|
||||
"CREATE VIEW sample_view AS SELECT label FROM sample;"
|
||||
"CREATE PROCEDURE sample_routine() SELECT id FROM sample;"
|
||||
"CREATE EVENT sample_event ON SCHEDULE EVERY 1 DAY DISABLE DO SELECT 1;")
|
||||
command(*mysql, "-e", """
|
||||
CREATE DATABASE restored;
|
||||
CREATE DATABASE managed_target;
|
||||
CREATE TABLE managed_target.previous(id INT);
|
||||
INSERT INTO managed_target.previous VALUES (84);
|
||||
CREATE DATABASE limited_target;
|
||||
CREATE TABLE limited_target.previous(id INT);
|
||||
INSERT INTO limited_target.previous VALUES (91);
|
||||
CREATE USER 'restore_limited'@'%' IDENTIFIED BY 'fixture-limited-password';
|
||||
GRANT SELECT, SHOW VIEW, EVENT ON limited_target.* TO 'restore_limited'@'%';
|
||||
GRANT SHOW_ROUTINE ON *.* TO 'restore_limited'@'%';
|
||||
""")
|
||||
instance = command(*mysql, "-NB", "-e", "SELECT LOWER(@@server_uuid)",
|
||||
capture_output=True, text=True).stdout.strip()
|
||||
limited_password = root / "limited-password"
|
||||
limited_password.write_text("fixture-limited-password\n")
|
||||
limited_password.chmod(0o600)
|
||||
api, hub_url, _, hub_log = start_hub(cleanup, root)
|
||||
agent = pair(api, "MySQL fixture")
|
||||
token = agent["upload_token"]
|
||||
pinned, wrong_key = bastion(cleanup, root)
|
||||
direct = {"engine": "mysql", "transport": "direct", "host": "127.0.0.1",
|
||||
"port": mysql_port, "user": "root", "password_file": str(password),
|
||||
"database": "fixture", "tls": {"mode": "required"}}
|
||||
remote = {"engine": "mysql", "transport": "ssh", "bastion": pinned,
|
||||
"defaults_file": str(defaults), "database": "fixture"}
|
||||
profiles, jobs, targets = {}, {}, {}
|
||||
for name, source in [
|
||||
("direct", direct), ("remote", remote),
|
||||
("engine-error", {**direct, "database": "does_not_exist"}),
|
||||
("wrong-key", {**remote, "bastion": wrong_key}),
|
||||
]:
|
||||
orbit = api("orbits", {"name": name, "provider": "wormhole", "provider_config": {}})
|
||||
app = str(uuid4())
|
||||
api("capture-profiles", {"application_id": app, "orbit_id": orbit["id"],
|
||||
"outpost_id": agent["outpost_id"], "format": "mysql_sql"}, expected=201)
|
||||
jobs[name] = api(f"capture-profiles/{app}/jobs", {}, expected=202)["id"]
|
||||
profiles[app] = {"source": source, "timeout_seconds": 45}
|
||||
if name in ("direct", "remote"):
|
||||
database = "managed_target" if name == "remote" else "limited_target"
|
||||
target = str(uuid4())
|
||||
api("restore-targets", {"id": target, "application_id": app, "outpost_id": agent["outpost_id"],
|
||||
"name": database, "engine": "mysql", "identity": {"instance": instance, "database": database}},
|
||||
expected=201)
|
||||
targets[name] = target
|
||||
target_connection = {key: value for key, value in source.items() if key != "engine"}
|
||||
target_connection["database"] = database
|
||||
if name == "direct":
|
||||
target_connection.update(user="restore_limited", password_file=str(limited_password))
|
||||
profiles[app]["targets"] = {target: {
|
||||
"engine": "mysql", "connection": target_connection,
|
||||
"recreate_database": True, "use_target_definer": True,
|
||||
}}
|
||||
_, agent_log = start_agent(cleanup, root, hub_url, token, profiles)
|
||||
for name, job in jobs.items():
|
||||
try:
|
||||
result = wait_until(lambda: terminal(api, job))
|
||||
except AssertionError as error:
|
||||
for log in (hub_log, agent_log):
|
||||
log.seek(0)
|
||||
print(log.read().replace(PASSWORD, "<redacted>"))
|
||||
raise AssertionError((name, api(f"capture-jobs/{job}"))) from error
|
||||
if name in ("engine-error", "wrong-key"):
|
||||
assert result["phase"] == "dump_failed" and result["snapshot_id"] is None, result
|
||||
continue
|
||||
assert result["status"] == "completed", (name, result)
|
||||
target = root / f"{name}.sql"
|
||||
session = api("reconstitute", {"snapshot_id": result["snapshot_id"], "target_path": str(target)})
|
||||
wait_until(lambda: api(f"reconstitute/{session['session_id']}")["state"] == "completed")
|
||||
sql = target.read_text()
|
||||
assert all(marker in sql for marker in ("sample_view", "sample_routine", "sample_event", "backup 🛰"))
|
||||
if name == "remote":
|
||||
with target.open("rb") as dump:
|
||||
command(*mysql, "-D", "restored", stdin=dump)
|
||||
output = command(*mysql, "-NB", "-D", "restored", "-e", "SELECT label FROM sample_view",
|
||||
capture_output=True, text=True)
|
||||
assert output.stdout.strip() == "backup 🛰", output.stdout
|
||||
assert len(api("snapshots")) == 2
|
||||
for name, target in targets.items():
|
||||
snap = jobs[name]
|
||||
job = api("restore-jobs", {"snapshot_id": snap, "target_id": target,
|
||||
"confirmation": f"RESTORE {snap} TO {target}"}, expected=202)["id"]
|
||||
|
||||
def restored_job():
|
||||
result = api(f"restore-jobs/{job}")
|
||||
return result if result["status"] in ("completed", "failed", "recovery_required") else None
|
||||
|
||||
result = wait_until(restored_job, seconds=65)
|
||||
if name == "direct":
|
||||
assert result["status"] == "recovery_required" and result["safety_snapshot_id"], result
|
||||
output = command(*mysql, "-NB", "-e", "SELECT id FROM limited_target.previous",
|
||||
capture_output=True, text=True)
|
||||
assert output.stdout.strip() == "91"
|
||||
assert b"previous" in api(f"snapshots/{result['safety_snapshot_id']}/download", raw=True)
|
||||
else:
|
||||
assert result["status"] == "completed" and result["safety_snapshot_id"], result
|
||||
output = command(*mysql, "-NB", "-D", "managed_target", "-e",
|
||||
"SELECT label FROM sample_view; CALL sample_routine();"
|
||||
"SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA='managed_target';",
|
||||
capture_output=True, text=True)
|
||||
assert output.stdout.strip().splitlines() == ["backup 🛰", "1", "sample_event"], output.stdout
|
||||
assert b"previous" in api(f"snapshots/{result['safety_snapshot_id']}/download", raw=True)
|
||||
for log in (hub_log, agent_log):
|
||||
log.seek(0)
|
||||
assert PASSWORD not in log.read()
|
||||
print("MySQL direct/SSH capture, SSH apply, utf8mb4, view/routine/event, and limited-role failure with safety verified")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user