#!/usr/bin/env python3 """Independent application-side fixtures; only uses the lab's existing FUSE client.""" import hashlib import json import os import pathlib import shutil import stat import struct import subprocess import sys import time import uuid import cephfs def ceph(cluster, *args): return subprocess.check_output([ "ceph", "-c", f"/clusters/{cluster}/ceph.conf", "-k", f"/clusters/{cluster}/ceph.client.admin.keyring", *args], text=True).strip() def volume(cluster, *args): return ceph(cluster, "fs", "subvolume", *args, "--group_name", "migration") def snapshot(cluster, *args): return volume(cluster, "snapshot", *args) def claim_path(): claim = hashlib.sha256("\0".join([ceph("target", "fsid"), "cephfs", "migration", name]).encode()).hexdigest() return "/mnt/target-fs/.cephmig/claims/" + claim def snapshot_names(cluster): return {x["name"] for x in json.loads(snapshot(cluster, "ls", "cephfs", name))} def acl(): entries = [(1, 6, 0xffffffff), (2, 4, 12345), (4, 0, 0xffffffff), (16, 4, 0xffffffff), (32, 0, 0xffffffff)] return struct.pack(" 10 assert snapshot("source", "metadata", "ls", "cephfs", name, snap["name"]) == snapshot( "target", "metadata", "ls", "cephfs", name, snap["name"]) h2 = next(x for x in s["snapshots"] if x["name"] == "history-2")["transfer"] assert h2["mode"] == "snapdiff" and h2["blockdiff_files"] > 0, h2 assert h2["bytes_copied"] < 64 << 20, h2 assert volume("source", "metadata", "ls", "cephfs", name) == volume( "target", "metadata", "ls", "cephfs", name) if action == "final": assert s["phase"] == "ready-for-cutover" and len(s["snapshots"]) == 5, s assert s["snapshots"][-1]["transfer"]["bytes_copied"] == 0, s["snapshots"][-1] print(json.dumps({"phase": s["phase"], "rounds": len(s["snapshots"]), "history_2": h2})) elif action == "cleaned": roots = json.load(open(fixture)) compare(roots["source"], roots["target"]) compare_times(roots["source"], roots["target"], atime=False) s = json.load(open(state)) assert s["phase"] == "ready-for-cutover" and not s.get("live_snapshot"), s assert all(x.get("removed") for x in s["snapshots"] if x["owned"]), s application = {x["name"] for x in s["snapshots"] if not x["owned"]} assert application == {"history-1", "history-2"}, application for c in ("source", "target"): assert snapshot_names(c) == application, (c, snapshot_names(c)) assert not os.path.exists(claim_path()), "claim not released" print(json.dumps({"remaining_snapshots": sorted(application)})) elif action == "busy": # An unrelated source subvolume remains writable throughout migration/finalization. p = "/mnt/source/" + name count = 0 try: with open(p, "wb", buffering=0) as f: while not os.path.exists(work + "/stop-busy"): f.seek(0) f.write(str(count).encode()) os.fsync(f.fileno()) count += 1 pathlib.Path(work + "/busy-count").write_text(str(count)) time.sleep(0.1) finally: os.unlink(p) elif action == "cleanup": for cluster in ("source", "target"): for snap in json.loads(snapshot(cluster, "ls", "cephfs", name)): snapshot(cluster, "rm", "cephfs", name, snap["name"]) volume(cluster, "rm", "cephfs", name, "--force") try: os.unlink(claim_path()) except FileNotFoundError: pass # cleanup released it shutil.rmtree(work) else: raise ValueError(action)