|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate a conformance results.json (ADR-0032). |
| 3 | +
|
| 4 | +usage: check-conformance-results.py <results.json> <vendor-dir> <expected-conformance-version> |
| 5 | +
|
| 6 | +Fails (exit 1) when results.json |
| 7 | + * does not conform to schema/results.schema.json, |
| 8 | + * reports conformance_version other than the pinned one, |
| 9 | + * has a `fail` case, |
| 10 | + * omits a binding whose code is present in this repository (bindings are derived from the |
| 11 | + source tree, never hand-written), or lists one whose code is absent, |
| 12 | + * omits a section (or a case id) that the manifest puts in scope for the reported bindings. |
| 13 | +""" |
| 14 | +import json |
| 15 | +import os |
| 16 | +import sys |
| 17 | + |
| 18 | +from jsonschema import Draft202012Validator |
| 19 | + |
| 20 | +PACKAGE_ROOT = "src/main/java/com/babelqueue" |
| 21 | + |
| 22 | +# Sub-package of the core -> binding it carries (schema enum). Any transport package added to |
| 23 | +# the core must be listed here, or the check below fails on the unknown package. |
| 24 | +PACKAGE_BINDINGS = { |
| 25 | + "otel": "otel", |
| 26 | + "redis": "redis", |
| 27 | + "amqp": "amqp", |
| 28 | + "rabbitmq": "amqp", |
| 29 | + "sqs": "sqs", |
| 30 | + "azureservicebus": "asb", |
| 31 | + "asb": "asb", |
| 32 | + "pulsar": "pulsar", |
| 33 | + "kafka": "kafka", |
| 34 | + "artemis": "artemis", |
| 35 | +} |
| 36 | + |
| 37 | +# Sub-packages that are not bindings. |
| 38 | +NON_BINDING_PACKAGES = {"gdpr", "idempotency", "outbox", "schema"} |
| 39 | + |
| 40 | + |
| 41 | +def case_ids(section, body): |
| 42 | + """The case-id rule of the conformance README ("Sections, scopes and exceptions").""" |
| 43 | + def named(prefix, fallback, items): |
| 44 | + # `fallback` maps the 0-based index of an unnamed case to its id. |
| 45 | + return [prefix + (c["name"] if isinstance(c, dict) and isinstance(c.get("name"), str) else fallback(i)) |
| 46 | + for i, c in enumerate(items)] |
| 47 | + |
| 48 | + if isinstance(body, list): |
| 49 | + return named("", lambda i: f"{section}#{i}", body) |
| 50 | + if not isinstance(body, dict): |
| 51 | + return [] |
| 52 | + ids = [] |
| 53 | + if isinstance(body.get("cases"), list): |
| 54 | + ids += named("", lambda i: f"{section}#{i}", body["cases"]) |
| 55 | + for key, value in body.items(): |
| 56 | + if key in ("description", "schema", "cases") or not isinstance(value, dict): |
| 57 | + continue |
| 58 | + if isinstance(value.get("cases"), list): |
| 59 | + ids += named(key + "/", str, value["cases"]) |
| 60 | + else: |
| 61 | + ids.append(key) |
| 62 | + return ids |
| 63 | + |
| 64 | + |
| 65 | +def derive_bindings(): |
| 66 | + """Bindings carried by the source tree; fails on a package it cannot classify.""" |
| 67 | + errors = [] |
| 68 | + bindings = set() |
| 69 | + for entry in sorted(os.listdir(PACKAGE_ROOT)): |
| 70 | + if not os.path.isdir(os.path.join(PACKAGE_ROOT, entry)) or entry in NON_BINDING_PACKAGES: |
| 71 | + continue |
| 72 | + if entry in PACKAGE_BINDINGS: |
| 73 | + bindings.add(PACKAGE_BINDINGS[entry]) |
| 74 | + else: |
| 75 | + errors.append(f"unknown package {PACKAGE_ROOT}/{entry}: add it to PACKAGE_BINDINGS (a transport) " |
| 76 | + "or NON_BINDING_PACKAGES in this script") |
| 77 | + return sorted(bindings), errors |
| 78 | + |
| 79 | + |
| 80 | +def main(): |
| 81 | + results_path, vendor, pinned = sys.argv[1:4] |
| 82 | + results = json.load(open(results_path)) |
| 83 | + manifest = json.load(open(os.path.join(vendor, "manifest.json"))) |
| 84 | + schema = json.load(open(os.path.join(vendor, "schema", "results.schema.json"))) |
| 85 | + errors = [e.message for e in Draft202012Validator(schema).iter_errors(results)] |
| 86 | + |
| 87 | + if results.get("conformance_version") != pinned: |
| 88 | + errors.append(f"conformance_version {results.get('conformance_version')!r} != pinned {pinned!r}") |
| 89 | + |
| 90 | + derived, derive_errors = derive_bindings() |
| 91 | + errors += derive_errors |
| 92 | + if sorted(results.get("bindings", [])) != derived: |
| 93 | + errors.append(f"bindings {sorted(results.get('bindings', []))} != bindings derived from the source tree {derived}") |
| 94 | + |
| 95 | + carried = {"core", *derived} |
| 96 | + sections = results.get("sections", {}) |
| 97 | + for name, meta in manifest["sections"].items(): |
| 98 | + if meta["scope"] not in carried: |
| 99 | + if name in sections: |
| 100 | + errors.append(f"section {name!r} (scope {meta['scope']}) is out of scope and must be absent") |
| 101 | + continue |
| 102 | + if name not in sections: |
| 103 | + errors.append(f"in-scope section {name!r} is missing from results.json") |
| 104 | + continue |
| 105 | + missing = [i for i in case_ids(name, manifest.get(name)) if i not in sections[name]] |
| 106 | + if missing: |
| 107 | + errors.append(f"section {name!r} is missing cases: {', '.join(missing)}") |
| 108 | + for name in sections: |
| 109 | + if name not in manifest["sections"]: |
| 110 | + errors.append(f"section {name!r} is not defined by the manifest") |
| 111 | + |
| 112 | + failed = [f"{s}/{i}" for s, cs in sections.items() for i, r in cs.items() if r.get("status") == "fail"] |
| 113 | + if failed: |
| 114 | + errors.append("failing cases: " + ", ".join(failed)) |
| 115 | + |
| 116 | + if errors: |
| 117 | + print("results.json check FAILED:", file=sys.stderr) |
| 118 | + for e in errors: |
| 119 | + print(" - " + e, file=sys.stderr) |
| 120 | + return 1 |
| 121 | + total = sum(len(cs) for cs in sections.values()) |
| 122 | + print(f"results.json OK: {len(sections)} sections, {total} cases, bindings={results['bindings']}") |
| 123 | + return 0 |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + sys.exit(main()) |
0 commit comments