field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

kotlin_smoke.py (11326B)


      1 """Run the test-only generated Kotlin/JNA boundary on the governed native host."""
      2 
      3 from __future__ import annotations
      4 
      5 import argparse
      6 import hashlib
      7 import json
      8 import os
      9 import platform
     10 import re
     11 import subprocess
     12 import sys
     13 import xml.etree.ElementTree as ET
     14 from pathlib import Path
     15 from typing import Any
     16 
     17 import ffi_artifacts as artifacts
     18 import ffi_build as builder
     19 import ffi_installed as installed
     20 import ffi_provenance as provenance
     21 import ffi_source as source
     22 import package_contract as contract
     23 
     24 HARNESS = "scripts/kotlin_smoke"
     25 HOST = "aarch64-apple-darwin"
     26 LIBRARY = f"native/{HOST}/libtera_ffi.dylib"
     27 WRAPPER_HASHES = {
     28     "gradlew": "ab5c0cad16305af2e619c159c1f58dd68d07fab9c11e36701e109c0277407f7a",
     29     "gradle/wrapper/gradle-wrapper.jar": "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7",
     30 }
     31 EXPECTED_CASES = {
     32     "tera.smoke.SearchAdmissionTests/installedSearchPreservesTypedByteAdmissionAndClosedPrecedence",
     33     "tera.smoke.LegacyInventoryTests/nativeLegacyPagesPreserveOperationsAcrossRestartAndRejectMalformedCursors",
     34     "tera.smoke.ScopeLifecycleTests/scopeAndUnsignedContextsCrossRustWithoutSignedNarrowing",
     35     "tera.smoke.ScopeLifecycleTests/cancelledCloseWaitRetainsNativeCallbackDrainAndClosedAdmission",
     36     "tera.smoke.ScopeLifecycleTests/independentSubscriptionDisposalStopsOnlyItsObserver",
     37     "tera.smoke.ScopeLifecycleTests/errorRecoveryAndProtectedDataFailureRemainTyped",
     38     "tera.smoke.ScopeLifecycleTests/cancellationHandleKeepsIdentityAndDisposesIdempotently",
     39     "tera.smoke.MediaOwnershipTests/callerCloseBeforeGeneratedConversionPreservesTheAdmittedBytes",
     40     "tera.smoke.MediaOwnershipTests/recycledCallerSlotAndDisposedSourceWrapperCannotSubstituteBytes",
     41     "tera.smoke.MediaOwnershipTests/admissionRejectsInvalidDescriptorsAndWrongTypesSynchronously",
     42     "tera.smoke.TodayReceiptTests/generatedNativeBufferRoundTripPreservesIncompleteAndUnknownTargetEvidence",
     43     "tera.smoke.CalendarTimingTests/generatedNativeBuffersPreserveCivilDatesAndExclusiveEnds",
     44     "tera.smoke.CalendarTimingTests/generatedPageBufferRetainsFrozenViewerCalendarContext",
     45     "tera.smoke.CalendarTimingTests/generatedNativeBuffersPreserveEveryUnsignedInstantBitAndSourceZone",
     46     "tera.smoke.CalendarTimingTests/unknownVariantsAndTruncatedGeneratedValuesFailWithoutFabricatingDates",
     47     "tera.smoke.ComposerPersistenceTests/actualNativeComposerPreservesPartialFormsExactUnsignedReceiptsAndRestart",
     48     "tera.smoke.ComposerPersistenceTests/unsupportedNestedVersionsAndMalformedVariantsFailClosed",
     49     "tera.smoke.SubmissionReservationTests/reservationRecoversOneHistoricalSourceAcrossConcurrencyAndRestart",
     50     "tera.smoke.SubmissionReservationTests/invalidReservationVersionsAndUnsignedWidthsFailClosed",
     51 }
     52 
     53 
     54 def require(condition: bool, message: str) -> None:
     55     if not condition:
     56         raise source.ProvenanceError(message)
     57 
     58 
     59 def output_root(root: Path) -> Path:
     60     require(
     61         bool(os.environ.get("EXT_BUILD_RUN_ACTIVE")), "Kotlin smoke requires extbuild"
     62     )
     63     project = os.environ.get("EXT_BUILD_PROJECT_DIR")
     64     require(bool(project), "Kotlin smoke output routing is absent")
     65     return provenance.output_path(
     66         root, HOST, str(Path(project) / "target/kotlin_smoke")
     67     )
     68 
     69 
     70 def verify_host(root: Path, *, offline: bool) -> str:
     71     require(
     72         platform.system() == "Darwin" and platform.machine() == "arm64",
     73         "Kotlin smoke host is unsupported",
     74     )
     75     java = subprocess.check_output(["java", "--version"], text=True, timeout=30).strip()
     76     require(
     77         bool(re.match(r"(?:openjdk|java) 21[. ]", java)),
     78         "Kotlin smoke requires the discovered JDK 21",
     79     )
     80     cache = os.environ.get("GRADLE_USER_HOME")
     81     require(bool(cache) and Path(cache).is_absolute(), "Gradle cache routing is absent")
     82     require(
     83         not Path(cache).resolve().is_relative_to(root),
     84         "Gradle cache is inside the source repository",
     85     )
     86     for name, digest in WRAPPER_HASHES.items():
     87         require(
     88             artifacts.file_record(root / HARNESS, name)["sha256"] == digest,
     89             "Gradle wrapper bytes differ",
     90         )
     91     properties = (
     92         root / HARNESS / "gradle/wrapper/gradle-wrapper.properties"
     93     ).read_text()
     94     require(
     95         "distributionUrl=https\\://services.gradle.org/distributions/gradle-9.5.0-bin.zip\n"
     96         in properties
     97         and "distributionSha256Sum=553c78f50dafcd54d65b9a444649057857469edf836431389695608536d6b746\n"
     98         in properties,
     99         "Gradle distribution pin differs",
    100     )
    101     if offline:
    102         # --offline governs Gradle dependency resolution; the wrapper can still
    103         # download its distribution on first use. Require its completed install.
    104         distributions = Path(cache) / "wrapper/dists/gradle-9.5.0-bin"
    105         require(
    106             any(
    107                 p.is_file() and (p.parent / "gradle-9.5.0/bin/gradle").is_file()
    108                 for p in distributions.glob("*/gradle-9.5.0-bin.zip.ok")
    109             ),
    110             "Gradle 9.5.0 is not bootstrapped; run make kotlin-smoke-bootstrap",
    111         )
    112     return java
    113 
    114 
    115 def native_input(
    116     root: Path, output: Path
    117 ) -> tuple[dict[str, Any], Path, dict[str, Any]]:
    118     manifest = installed.check(root)
    119     candidate = manifest["candidate"]
    120     records = {
    121         target: contract._read_json(root / "TeraFFI/source" / f"{target}.json")
    122         for target in artifacts.TARGETS
    123     }
    124     for target, record in records.items():
    125         data = provenance.encoded(record)
    126         require(
    127             {
    128                 "path": f"source/{target}.json",
    129                 "bytes": len(data),
    130                 "sha256": hashlib.sha256(data).hexdigest(),
    131             }
    132             in candidate["files"],
    133             "Kotlin smoke source record does not match the installed producer",
    134         )
    135     base = builder.candidate_path(output.parent.parent, records)
    136     require(base.resolve() == base, "Kotlin native candidate contains a symlink")
    137     record = artifacts.file_record(base, LIBRARY)
    138     require(
    139         record in candidate["files"],
    140         "Kotlin smoke native library does not match the installed producer",
    141     )
    142     return candidate, base / LIBRARY, record
    143 
    144 
    145 def generate(root: Path, output: Path, library: Path) -> Path:
    146     destination = provenance.output_path(root, HOST, str(output / "generated"))
    147     argv = [
    148         "cargo",
    149         "run",
    150         "-p",
    151         "tera_bindgen",
    152         "--locked",
    153         "--",
    154         "generate",
    155         str(library),
    156         "--language",
    157         "kotlin",
    158         "--metadata-no-deps",
    159         "--no-format",
    160         "--out-dir",
    161         str(destination),
    162     ]
    163     subprocess.run(argv, cwd=root, check=True, timeout=600)
    164     generated = destination / "uniffi/tera_core/tera_core.kt"
    165     require(
    166         generated.is_file() and not generated.is_symlink(),
    167         "Generated Kotlin source is absent",
    168     )
    169     require(
    170         list(destination.rglob("*.kt")) == [generated],
    171         "Generated Kotlin source inventory differs",
    172     )
    173     return generated
    174 
    175 
    176 def test_results(directory: Path) -> dict[str, Any]:
    177     files = sorted(directory.glob("TEST-*.xml"))
    178     require(bool(files), "Kotlin smoke produced no test results")
    179     cases = []
    180     for path in files:
    181         require(
    182             path.resolve() == path and not path.is_symlink(),
    183             "Kotlin test result contains a symlink",
    184         )
    185         require(
    186             path.stat().st_size <= 2_000_000,
    187             "Kotlin test result exceeds its byte bound",
    188         )
    189         suite = ET.fromstring(path.read_bytes())
    190         require(suite.tag == "testsuite", "Kotlin test result has an invalid root")
    191         require(
    192             all(
    193                 suite.attrib.get(key) == "0"
    194                 for key in ("failures", "errors", "skipped")
    195             ),
    196             "Kotlin smoke has failed or skipped tests",
    197         )
    198         observed = suite.findall("testcase")
    199         require(
    200             len(observed) == int(suite.attrib["tests"]) and bool(observed),
    201             "Kotlin test result count differs",
    202         )
    203         require(
    204             all(not list(case) for case in observed),
    205             "Kotlin test case contains a failure or skip",
    206         )
    207         cases.extend(
    208             f"{case.attrib['classname']}/{case.attrib['name']}" for case in observed
    209         )
    210     require(
    211         len(cases) == len(set(cases)) and set(cases) == EXPECTED_CASES,
    212         "Kotlin smoke inventory is incomplete",
    213     )
    214     return {"passed": len(cases), "failed": 0, "skipped": 0, "cases": sorted(cases)}
    215 
    216 
    217 def run(root: Path, *, offline: bool) -> None:
    218     output = output_root(root)
    219     java = verify_host(root, offline=offline)
    220     candidate, library, native = native_input(root, output)
    221     generated = generate(root, output, library)
    222     result_directory = output / "build/test-results/test"
    223     require(
    224         result_directory.resolve() == result_directory,
    225         "Kotlin result directory contains a symlink",
    226     )
    227     # Remove only prior XML for this exact owned task so stale results cannot pass.
    228     for path in result_directory.glob("TEST-*.xml"):
    229         require(not path.is_symlink(), "Kotlin result is a symlink")
    230         path.unlink()
    231     argv = [
    232         str(root / HARNESS / "gradlew"),
    233         "--no-daemon",
    234         "--console=plain",
    235         "--dependency-verification=strict",
    236         "--project-cache-dir",
    237         str(output / "project_cache"),
    238         f"-Pkotlin.project.persistent.dir={output / 'kotlin'}",
    239         "-p",
    240         str(root / HARNESS),
    241         "test",
    242     ]
    243     if offline:
    244         argv.append("--offline")
    245     environment = dict(
    246         os.environ,
    247         TERA_KOTLIN_SMOKE_ROOT=str(output),
    248         TERA_KOTLIN_NATIVE_DIR=str(library.parent),
    249     )
    250     subprocess.run(argv, cwd=root, env=environment, check=True, timeout=600)
    251     results = test_results(result_directory)
    252     require(
    253         installed.check(root)["candidate"] == candidate,
    254         "Producer changed during Kotlin smoke",
    255     )
    256     require(
    257         artifacts.file_record(library.parent.parent.parent, LIBRARY) == native,
    258         "Native library changed during Kotlin smoke",
    259     )
    260     report = {
    261         "schema": "tera.kotlin-binding-smoke.v1",
    262         "source": candidate["source"],
    263         "native": native,
    264         "generated_sha256": hashlib.sha256(generated.read_bytes()).hexdigest(),
    265         "jdk": java,
    266         "host": HOST,
    267         "dependency_resolution": "offline" if offline else "bootstrap",
    268         "gradle_argv": argv,
    269         "results": results,
    270         "disposition": "local_native_binding_smoke_only",
    271     }
    272     provenance.write_atomic(output / "result.json", provenance.encoded(report))
    273     print(json.dumps(report, indent=2))
    274 
    275 
    276 def main() -> int:
    277     parser = argparse.ArgumentParser(description=__doc__)
    278     parser.add_argument("mode", choices=("verify", "bootstrap"))
    279     args = parser.parse_args()
    280     try:
    281         run(Path(__file__).resolve().parent.parent, offline=args.mode == "verify")
    282     except (
    283         OSError,
    284         ValueError,
    285         ET.ParseError,
    286         subprocess.SubprocessError,
    287         source.ProvenanceError,
    288         contract.PackageContractError,
    289     ) as error:
    290         print(f"Kotlin binding smoke failed: {error}", file=sys.stderr)
    291         return 1
    292     return 0
    293 
    294 
    295 if __name__ == "__main__":
    296     raise SystemExit(main())