"""Test benchmark export target selection + doc shaping (no network). Verifies: - BENCHMARK_EXPORT_TARGET parsing: default→noop, comma list→multiple, unknown ignored. - a misconfigured target (missing creds) is skipped, not fatal, and never empties the list. - CosmosExporter.build_doc shapes a Cosmos doc (string id = swarm_id; partition field present). - constructors do NOT connect (no creds touched until export()). Run: python scripts/test-benchmark-export.py """ import os import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from benchmark.export import get_exporters, NoopExporter, CosmosExporter, BlobExporter failures = [] _ENV = ("BENCHMARK_EXPORT_TARGET", "BENCHMARK_COSMOS_CONNECTION_STRING", "BENCHMARK_COSMOS_DATABASE", "BENCHMARK_COSMOS_CONTAINER", "AZURE_STORAGE_CONNECTION_STRING", "BENCHMARK_BLOB_ACCOUNT_URL", "BENCHMARK_BLOB_CONTAINER") def check(name, cond): print(("PASS" if cond else "FAIL"), "-", name) if not cond: failures.append(name) def reset(**env): for k in _ENV: os.environ.pop(k, None) os.environ.update(env) def names(exps): return [e.name for e in exps] def main(): # default → noop reset() check("default → [noop]", names(get_exporters()) == ["noop"]) # explicit none reset(BENCHMARK_EXPORT_TARGET="none") check("none → [noop]", names(get_exporters()) == ["noop"]) # cosmos + blob, both with creds present → both built reset(BENCHMARK_EXPORT_TARGET="cosmos,blob", BENCHMARK_COSMOS_CONNECTION_STRING="AccountEndpoint=https://x.documents.azure.com:443/;AccountKey=FAKE==;", AZURE_STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=https;AccountName=x;AccountKey=FAKE==;") check("cosmos,blob → [cosmos, blob]", names(get_exporters()) == ["cosmos", "blob"]) # cosmos requested but NO connection string → skipped (not fatal); blob still built reset(BENCHMARK_EXPORT_TARGET="cosmos,blob", AZURE_STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=https;AccountName=x;AccountKey=FAKE==;") check("misconfigured cosmos skipped, blob kept", names(get_exporters()) == ["blob"]) # all targets misconfigured → never empty, falls back to noop reset(BENCHMARK_EXPORT_TARGET="cosmos") check("all misconfigured → [noop] fallback", names(get_exporters()) == ["noop"]) # unknown target ignored → noop fallback reset(BENCHMARK_EXPORT_TARGET="clickhouse") check("unknown target → [noop] fallback", names(get_exporters()) == ["noop"]) # CosmosExporter.build_doc: string id = swarm_id, partition field swarm_id retained doc = CosmosExporter.build_doc({"swarm_id": "swarm-123", "scenario": "coding", "metrics": {"s_gain": None}}) check("cosmos doc id == swarm_id (str)", doc["id"] == "swarm-123" and isinstance(doc["id"], str)) check("cosmos doc keeps swarm_id partition field", doc["swarm_id"] == "swarm-123") check("cosmos doc preserves null metric (no fabrication)", doc["metrics"]["s_gain"] is None) reset() print() if failures: print(f"{len(failures)} export check(s) FAILED: {failures}") return 1 print("all benchmark export checks passed") return 0 if __name__ == "__main__": sys.exit(main())