|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Rebuild WAT fixtures exactly and test freshly compiled legacy Rust guests.""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +import shutil |
| 20 | +import subprocess |
| 21 | +import tempfile |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +ROOT = Path(__file__).resolve().parents[2] |
| 25 | +PLUGIN = ROOT / "shenyu-plugin" |
| 26 | +API = PLUGIN / "shenyu-plugin-wasm-api" / "src/test" |
| 27 | +BASE = PLUGIN / "shenyu-plugin-wasm-base" / "src/test" |
| 28 | +RUST_TOOLCHAIN = "1.90.0" |
| 29 | +RUST_GUESTS = [ |
| 30 | + (API, "rust-wasm-plugin", "org.apache.shenyu.plugin.wasm.api.AbstractWasmPluginTest$RustWasmPlugin.wasm"), |
| 31 | + (BASE, "rust-shenyu-wasm-plugin", "org.apache.shenyu.plugin.wasm.base.AbstractShenyuWasmPluginTest$TestShenyuWasmPlugin.wasm"), |
| 32 | + (BASE, "rust-plugin-data-handler", "org.apache.shenyu.plugin.wasm.base.handler.AbstractWasmPluginDataHandlerTest$TestWasmPluginDataHandler.wasm"), |
| 33 | + (BASE, "rust-meta-data-handler", "org.apache.shenyu.plugin.wasm.base.handler.AbstractWasmMetaDataHandlerTest$TestWasmMetaDataHandler.wasm"), |
| 34 | + (BASE, "rust-discovery-handler", "org.apache.shenyu.plugin.wasm.base.handler.AbstractWasmDiscoveryHandlerTest$TestWasmPluginDiscoveryHandler.wasm"), |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +def run(command, **kwargs): |
| 39 | + subprocess.run(command, check=True, **kwargs) |
| 40 | + |
| 41 | + |
| 42 | +def verify_wat(): |
| 43 | + version = subprocess.check_output(["wat2wasm", "--version"], text=True).strip() |
| 44 | + if version != "1.0.42": |
| 45 | + raise RuntimeError("WAT verification requires WABT 1.0.42, found " + version) |
| 46 | + count = 0 |
| 47 | + for family in ["wasm-bridge", "wasm-gateway"]: |
| 48 | + directory = BASE / family |
| 49 | + if not directory.exists(): |
| 50 | + continue |
| 51 | + with tempfile.TemporaryDirectory(prefix="shenyu-wat-") as temporary: |
| 52 | + run(["make", "-C", str(directory), "build", "RESOURCES=" + temporary]) |
| 53 | + binaries = list(Path(temporary).glob("*.wasm")) |
| 54 | + if not binaries: |
| 55 | + raise RuntimeError("No fixtures generated for " + family) |
| 56 | + for rebuilt in binaries: |
| 57 | + committed = BASE / "resources" / rebuilt.name |
| 58 | + if rebuilt.read_bytes() != committed.read_bytes(): |
| 59 | + raise RuntimeError("WAT fixture differs from committed binary: " + rebuilt.name) |
| 60 | + count += 1 |
| 61 | + if not count: |
| 62 | + raise RuntimeError("No WAT fixture families found") |
| 63 | + print("Verified " + str(count) + " byte-identical WAT fixtures", flush=True) |
| 64 | + |
| 65 | + |
| 66 | +def verify_rust(): |
| 67 | + originals = {} |
| 68 | + try: |
| 69 | + with tempfile.TemporaryDirectory(prefix="shenyu-rust-") as temporary: |
| 70 | + for source_root, crate, resource_name in RUST_GUESTS: |
| 71 | + source = source_root / crate |
| 72 | + copied = Path(temporary) / crate |
| 73 | + copied.mkdir() |
| 74 | + shutil.copyfile(source / "Cargo.toml", copied / "Cargo.toml") |
| 75 | + shutil.copytree(source / "src", copied / "src") |
| 76 | + run(["cargo", "+" + RUST_TOOLCHAIN, "build", "--manifest-path", str(copied / "Cargo.toml"), |
| 77 | + "--target", "wasm32-wasip1", "--release", "--offline"]) |
| 78 | + binary = copied / "target/wasm32-wasip1/release" / (crate.replace("-", "_") + ".wasm") |
| 79 | + resource = source_root / "resources" / resource_name |
| 80 | + originals[resource] = resource.read_bytes() |
| 81 | + shutil.copyfile(binary, resource) |
| 82 | + # Maven copies the freshly rebuilt guests before the tests load them. |
| 83 | + # Legacy compiler versions were not recorded, so assert behavior instead of bytes. |
| 84 | + run(["./mvnw", "-B", "-ntp", "-pl", "shenyu-plugin/shenyu-plugin-wasm-base", "-am", "test", |
| 85 | + "-Dtest=*Wasm*Test", "-Dsurefire.failIfNoSpecifiedTests=false", |
| 86 | + "-DskipRemoteResources=true", "-Djacoco.skip=true", "-Dmaven.javadoc.skip=true"], cwd=ROOT) |
| 87 | + finally: |
| 88 | + for resource, original in originals.items(): |
| 89 | + resource.write_bytes(original) |
| 90 | + print("Verified five rebuilt Rust guests through the WASM Java suites", flush=True) |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + parser = argparse.ArgumentParser(description=__doc__) |
| 95 | + mode = parser.add_mutually_exclusive_group() |
| 96 | + mode.add_argument("--wat-only", action="store_true") |
| 97 | + mode.add_argument("--rust-only", action="store_true") |
| 98 | + options = parser.parse_args() |
| 99 | + if not options.rust_only: |
| 100 | + verify_wat() |
| 101 | + if not options.wat_only: |
| 102 | + verify_rust() |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments