forked from longtao.wu/droboticflow
248 lines
13 KiB
Python
248 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Supplemental real POC/BOS acceptance; no mocked responses or SQLite writes."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import csv
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
import zipfile
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location("real_bos_matrix", REPO / "scripts/drosync-real-bos-matrix.py")
|
|
matrix = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = matrix
|
|
spec.loader.exec_module(matrix)
|
|
sys.path.insert(0, str(REPO / "scripts/lib"))
|
|
from drosync_real_evidence import _record
|
|
from drosync_real_order import analyze_order_and_metadata
|
|
|
|
|
|
class ExtraNative(matrix.Native):
|
|
def __init__(self, *args, **kwargs):
|
|
try:
|
|
super().__init__(*args, **kwargs)
|
|
except Exception:
|
|
# A constructor failure happens before the case's finally block.
|
|
# Stop only the child daemon this instance created.
|
|
if hasattr(self, "process"):
|
|
self.stop()
|
|
raise
|
|
|
|
|
|
def put_records(text):
|
|
records = []
|
|
for line in text.splitlines():
|
|
_, _, fields = _record(line)
|
|
if fields.get("event") == "request_completed" and fields.get("endpoint_kind") == "bos_put":
|
|
records.append(fields)
|
|
return records
|
|
|
|
|
|
def completed(c, count):
|
|
result = c.sync(timeout=600)
|
|
session = result.get("session") or {}
|
|
if session.get("status") != "completed":
|
|
# Automatic scheduling may win the same mapping lease. Verify its real
|
|
# terminal session rather than accepting a CLI busy result as success.
|
|
terminal = matrix.wait_for(lambda: next((s for s in c.status().get("sessions", [])
|
|
if s.get("status") == "completed"
|
|
and s.get("document", {}).get("fileCount") == count), None), 180)
|
|
assert terminal["mappingId"] == c.mapping
|
|
assert result.get("confirmedFilesSkipped") == count or result.get("errorCode") in ("busy", "conflict") or "active sync" in str(result.get("error", "")), result
|
|
return {"cliResult": result, "confirmedSession": terminal}
|
|
assert session.get("counts", {}).get("manifest") == count or result.get("itemCount") == count, result
|
|
return result
|
|
|
|
|
|
def order_case(suite, concurrency):
|
|
c = ExtraNative(suite, f"extra-order-{concurrency}", concurrency=concurrency, level="debug")
|
|
try:
|
|
y, t = suite.yesterday, suite.today
|
|
paths = [
|
|
f"{y}/order/episode_day/data_001.bin",
|
|
f"{y}/order/episode_day/data_002.bin",
|
|
f"{y}-08_00_00/order/episode_morning/data.bin",
|
|
f"{y}-16_00_00/order/episode_afternoon/data.bin",
|
|
f"{t}/order/episode_day/data_001.bin",
|
|
f"{t}-08_00_00/order/episode_morning/data.bin",
|
|
f"{t}-16_00_00/order/episode_afternoon/data.bin",
|
|
"unknown-time/order/episode_unknown/data.bin",
|
|
]
|
|
# Creation order intentionally opposes capture-time order.
|
|
for index, relative in reversed(list(enumerate(paths))):
|
|
c.add(relative, 1 << 20, 700 + index)
|
|
result = completed(c, len(paths))
|
|
c.call("pause")
|
|
verification = suite.verify(c)
|
|
assert verification["verifiedCount"] == 8 and verification["verifiedBytes"] == 8 << 20
|
|
assert verification["listedObjects"] == 8 and verification["unexpectedObjects"] == 0
|
|
c.snapshot()
|
|
expected = [c.name + "/" + path for path in paths]
|
|
analysis = analyze_order_and_metadata(c.text(), concurrency, expected)
|
|
matrix.save(c.evidence / "expected-paths.json", expected)
|
|
matrix.save(c.evidence / "order-metadata.json", analysis)
|
|
ordering_runs = [r for r in analysis["runs"] if r["ordering"]["observedStartCount"]]
|
|
assert len(ordering_runs) == 1, analysis
|
|
assert ordering_runs[0]["ordering"]["status"] == "PASS", analysis
|
|
assert ordering_runs[0]["metadata"]["status"] == "PASS", analysis
|
|
assert ordering_runs[0]["ordering"]["uniqueStartCount"] == 8
|
|
return {"concurrency": concurrency, "result": result,
|
|
"verification": {k: verification[k] for k in ("status", "verifiedCount", "verifiedBytes", "listedObjects")},
|
|
"analysis": analysis}
|
|
finally:
|
|
c.close()
|
|
|
|
|
|
def mapping_case(suite):
|
|
c = ExtraNative(suite, "extra-mapping-recreate", level="debug")
|
|
try:
|
|
for index in range(2):
|
|
c.add(f"{suite.today}/mapping/episode_{index:02d}/data.bin", 1 << 20, 810 + index)
|
|
first = completed(c, 2)
|
|
suite.verify(c, suffix="before-recreate-bos")
|
|
c.call("pause")
|
|
before = len(put_records(c.text()))
|
|
old_mapping = c.mapping
|
|
removed = c.call("mapping", "remove", c.mapping)
|
|
mapping = c.call("mapping", "add", str(c.source), "--space", suite.access["spaceId"],
|
|
"--remote-prefix", c.name, "--require-done")
|
|
c.mapping = mapping.get("id", mapping.get("mapping", {}).get("id"))
|
|
assert c.mapping and c.mapping != old_mapping, mapping
|
|
assert all((c.source / path).is_file() for path in c.expected)
|
|
repeated = c.sync(timeout=600)
|
|
assert repeated.get("session", {}).get("status") == "completed" or repeated.get("confirmedFilesSkipped") == 2, repeated
|
|
if repeated.get("session"):
|
|
counts = repeated["session"].get("counts", {})
|
|
assert counts.get("unchanged") == 2 and counts.get("committed", 0) == 0, repeated
|
|
c.call("pause")
|
|
c.snapshot()
|
|
after = len(put_records(c.text()))
|
|
assert after == before, {"beforePutCalls": before, "afterPutCalls": after}
|
|
verification = suite.verify(c, suffix="after-recreate-bos")
|
|
assert verification["verifiedCount"] == 2 and verification["listedObjects"] == 2
|
|
return {"oldMappingId": old_mapping, "newMappingId": c.mapping, "first": first,
|
|
"removed": removed, "repeat": repeated, "additionalCompletedPutCalls": after - before,
|
|
"verification": {k: verification[k] for k in ("status", "verifiedCount", "verifiedBytes", "listedObjects")}}
|
|
finally:
|
|
c.close()
|
|
|
|
|
|
def auth_case(suite):
|
|
c = ExtraNative(suite, "extra-wrong-secret", level="debug")
|
|
try:
|
|
before = c.call("doctor")
|
|
rejected = c.call("auth", "--server", suite.access["baseURL"],
|
|
"--access-key-id", suite.access["accessKeyId"], "--secret-stdin",
|
|
stdin="deliberately-wrong-acceptance-secret", accepted=(0, 10, 20, 30, 40, 50))
|
|
assert c.commands[-1]["exit"] != 0 and rejected.get("authenticated") is not True, rejected
|
|
assert rejected.get("error") or rejected.get("errorCode"), rejected
|
|
after = c.call("doctor")
|
|
assert before["credentials"] == after["credentials"] == "valid"
|
|
assert before["controlPlane"] == after["controlPlane"] == "ok"
|
|
for field in ("credentialId", "deviceId"):
|
|
assert before["capabilities"][field] == after["capabilities"][field]
|
|
c.snapshot()
|
|
assert len(put_records(c.text())) == 0
|
|
failed_auth = []
|
|
for line in c.text().splitlines():
|
|
_, _, fields = _record(line)
|
|
if fields.get("event") == "request_failed" and str(fields.get("endpoint", "")).endswith("/auth/token"):
|
|
failed_auth.append({key: fields.get(key) for key in ("status", "error_code", "retryable")})
|
|
assert failed_auth, "actual rejected authentication request missing from diagnostics"
|
|
return {"rejected": rejected, "beforeCredentialId": before["capabilities"]["credentialId"],
|
|
"afterCredentialId": after["capabilities"]["credentialId"],
|
|
"doctorAfter": {key: after[key] for key in ("credentials", "controlPlane", "compatible")},
|
|
"actualFailedAuthenticationRequests": failed_auth, "completedPutCalls": 0}
|
|
finally:
|
|
c.close()
|
|
|
|
|
|
def report_exports_case(path):
|
|
"""Compare exports with the real snapshot captured at export time."""
|
|
path = Path(path)
|
|
web = json.loads((path / "web-conflict-report.json").read_text())
|
|
episodes = json.loads((path / "deleted-report.json").read_text())
|
|
assert [web[k] for k in ("totalLocalEpisodes", "totalCloudEpisodes", "totalMissingEpisodes", "totalCleanedEpisodes")] == [episodes["counts"][k] for k in ("total", "completed", "missing", "cleaned")]
|
|
assert web["totalSize"] == sum(item["localSize"] for item in episodes["items"])
|
|
summary_file = next((path / "csv").glob("*_summary.csv"))
|
|
missing_file = next((path / "csv").glob("*_missing.csv"))
|
|
with summary_file.open(encoding="utf-8-sig", newline="") as stream:
|
|
summary_rows = list(csv.reader(stream))[1:]
|
|
with missing_file.open(encoding="utf-8-sig", newline="") as stream:
|
|
missing_rows = list(csv.reader(stream))[1:]
|
|
|
|
def size(value):
|
|
if value < 1024:
|
|
return f"{value} B"
|
|
for unit in ("KB", "MB", "GB", "TB"):
|
|
value /= 1024
|
|
if value < 1024 or unit == "TB":
|
|
return f"{value:.2f} {unit}"
|
|
|
|
expected_summary = [[item["timestamp"], item["taskType"], str(item["localEpisodes"]),
|
|
str(item["cloudEpisodes"]), item["totalSizeStr"], item["status"],
|
|
str(item["cleanedEpisodes"]), size(item["cleanedSize"])] for item in web["summaries"]]
|
|
expected_missing = [[item["timestamp"], item["taskType"], item["episode"], item["status"],
|
|
item["reason"], item["localSizeStr"], item["cloudSizeStr"]] for item in web["missingEpisodes"]]
|
|
assert summary_rows == expected_summary, "CSV summary rows differ from real report snapshot"
|
|
assert missing_rows == expected_missing, "CSV missing rows differ from real report snapshot"
|
|
ns = {"m": "http://schemas.openxmlformats.org/spreadsheetml/2006/main"}
|
|
with zipfile.ZipFile(next((path / "xlsx").glob("*.xlsx"))) as archive:
|
|
assert archive.testzip() is None
|
|
strings = ["".join(element.itertext()) for element in ET.fromstring(archive.read("xl/sharedStrings.xml"))]
|
|
sheets = {}
|
|
for name in ("sheet1", "sheet2"):
|
|
cells = {}
|
|
for cell in ET.fromstring(archive.read(f"xl/worksheets/{name}.xml")).findall(".//m:c", ns):
|
|
value = cell.find("m:v", ns)
|
|
if value is not None:
|
|
cells[cell.attrib["r"]] = strings[int(value.text)] if cell.attrib.get("t") == "s" else value.text
|
|
sheets[name] = cells
|
|
summary, missing = sheets["sheet1"], sheets["sheet2"]
|
|
assert summary["B3"] == web["date"] and summary["B4"] == web["localRoot"]
|
|
assert summary["E4"] == web["totalSizeStr"]
|
|
assert [int(summary[cell]) for cell in ("B5", "E5", "B6", "B7")] == [web[key] for key in ("totalLocalEpisodes", "totalCloudEpisodes", "totalMissingEpisodes", "totalCleanedEpisodes")]
|
|
assert int(missing["E3"]) == web["totalMissingEpisodes"]
|
|
for index, expected in enumerate(expected_summary, 9):
|
|
assert [summary[column + str(index)] for column in "ABCDEFGH"] == expected
|
|
for index, expected in enumerate(expected_missing, 6):
|
|
assert [missing[column + str(index)] for column in "ABCDEFG"] == expected
|
|
return {"sourceDirectory": str(path), "sourceSnapshot": "web-conflict-report.json and deleted-report.json",
|
|
"csvSummaryRows": len(summary_rows), "csvMissingRows": len(missing_rows),
|
|
"xlsxSummaryRows": len(expected_summary), "xlsxMissingRows": len(expected_missing),
|
|
"counts": episodes["counts"], "totalBytes": web["totalSize"],
|
|
"checks": ["web/episodes counts and bytes", "CSV complete rows", "XLSX date/root/totals/bytes/complete rows"],
|
|
"note": "Later restored report.json is not the earlier export snapshot"}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--access-file", required=True)
|
|
parser.add_argument("--workdir", required=True)
|
|
parser.add_argument("--evidence-dir", required=True)
|
|
parser.add_argument("--verify-bin", required=True)
|
|
parser.add_argument("--kubeconfig", default=str(Path.home() / ".config/droboticflow/kubeconfigs/poc.config"))
|
|
parser.add_argument("--suffix", default="")
|
|
parser.add_argument("--case", action="append", choices=("order-1", "order-4", "mapping", "auth"))
|
|
parser.add_argument("--report-dir", action="append", help="Read-only CSV/XLSX audit; when set alone, no network cases run")
|
|
args = parser.parse_args()
|
|
suite = matrix.Suite(args)
|
|
local_exclusions = "127.0.0.1,localhost,::1"
|
|
suite.env["NO_PROXY"] = ",".join(filter(None, (suite.env.get("NO_PROXY", ""), local_exclusions)))
|
|
suite.env["no_proxy"] = ",".join(filter(None, (suite.env.get("no_proxy", ""), local_exclusions)))
|
|
cases = {"order-1": lambda: order_case(suite, 1), "order-4": lambda: order_case(suite, 4),
|
|
"mapping": lambda: mapping_case(suite), "auth": lambda: auth_case(suite)}
|
|
for name in args.case or ([] if args.report_dir else cases):
|
|
suite.case(name, cases[name])
|
|
for directory in args.report_dir or []:
|
|
suite.case("report-exports-" + Path(directory).name, lambda directory=directory: report_exports_case(directory))
|
|
return 1 if any(result["status"] == "FAIL" for result in suite.results) else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|