From 97b4e7a6349b8b8ed11707d4ddc567bc1e3df787 Mon Sep 17 00:00:00 2001 From: Tate Date: Thu, 11 Jul 2024 15:37:48 -0600 Subject: [PATCH] [TT-1252] Make tools/bin/go_core_* tools reusable from other repositories (#13827) * [TT-1252] Make tools used in ci reusable for chainlink-integrations * Make fuzz reusable in other reops * Make the fuzz python arg for the root of the go project more readable as to its meaning --- fuzz/fuzz_all_native.py | 17 ++++++++--------- tools/bin/go_core_fuzz | 7 ++++++- tools/bin/go_core_race_tests | 6 +++++- tools/bin/go_core_tests | 5 ++++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/fuzz/fuzz_all_native.py b/fuzz/fuzz_all_native.py index 41588b090b7..aa191fc5e8d 100755 --- a/fuzz/fuzz_all_native.py +++ b/fuzz/fuzz_all_native.py @@ -7,8 +7,6 @@ import subprocess import sys -LIBROOT = "../" - def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, @@ -19,6 +17,7 @@ def main(): ) parser.add_argument("--ci", required=False, help="In CI mode we run each parser only briefly once", action="store_true") parser.add_argument("--seconds", required=False, help="Run for this many seconds of total fuzz time before exiting") + parser.add_argument("--go_module_root", required=True, help="Path to the root of the go module to fuzz") args = parser.parse_args() # use float for remaining_seconds so we can represent infinity @@ -27,7 +26,7 @@ def main(): else: remaining_seconds = float("inf") - fuzzers = discover_fuzzers() + fuzzers = discover_fuzzers(args.go_module_root) print(f"🐝 Discovered fuzzers:", file=sys.stderr) for fuzzfn, path in fuzzers.items(): print(f"{fuzzfn} in {path}", file=sys.stderr) @@ -50,12 +49,12 @@ def main(): remaining_seconds -= next_duration_seconds print(f"🐝 Running {fuzzfn} in {path} for {next_duration_seconds}s before switching to next fuzzer", file=sys.stderr) - run_fuzzer(fuzzfn, path, next_duration_seconds) + run_fuzzer(fuzzfn, path, next_duration_seconds, args.go_module_root) print(f"🐝 Completed running {fuzzfn} in {path} for {next_duration_seconds}s. Total remaining time is {remaining_seconds}s", file=sys.stderr) -def discover_fuzzers(): +def discover_fuzzers(go_module_root): fuzzers = {} - for root, dirs, files in os.walk(LIBROOT): + for root, dirs, files in os.walk(go_module_root): for file in files: if not file.endswith("test.go"): continue with open(os.path.join(root, file), "r") as f: @@ -68,11 +67,11 @@ def discover_fuzzers(): for fuzzfn in re.findall(r"func\s+(Fuzz\w+)", text): if fuzzfn in fuzzers: raise Exception(f"Duplicate fuzz function: {fuzzfn}") - fuzzers[fuzzfn] = os.path.relpath(root, LIBROOT) + fuzzers[fuzzfn] = os.path.relpath(root, go_module_root) return fuzzers -def run_fuzzer(fuzzfn, dir, duration_seconds): - subprocess.check_call(["go", "test", "-run=^$", f"-fuzz=^{fuzzfn}$", f"-fuzztime={duration_seconds}s", f"./{dir}"], cwd=LIBROOT) +def run_fuzzer(fuzzfn, dir, duration_seconds, go_module_root): + subprocess.check_call(["go", "test", "-run=^$", f"-fuzz=^{fuzzfn}$", f"-fuzztime={duration_seconds}s", f"./{dir}"], cwd=go_module_root) if __name__ == "__main__": main() \ No newline at end of file diff --git a/tools/bin/go_core_fuzz b/tools/bin/go_core_fuzz index c3119f4beb4..d81e6909300 100755 --- a/tools/bin/go_core_fuzz +++ b/tools/bin/go_core_fuzz @@ -6,6 +6,11 @@ SCRIPT_PATH=`dirname "$0"`; SCRIPT_PATH=`eval "cd \"$SCRIPT_PATH\" && pwd"` OUTPUT_FILE=${OUTPUT_FILE:-"./output.txt"} USE_TEE="${USE_TEE:-true}" +# To allow reuse in CI from other repositories +FUZZ_TOOL_PATH=${FUZZ_TOOL_PATH:-"./fuzz"} +GO_MODULE_ROOT_PATH=${GO_MODULE_ROOT_PATH:-"./"} +FUZZ_TIMEOUT=${FUZZ_TIMEOUT:-10m} + echo "Failed fuzz tests and panics: ---------------------" echo "" use_tee() { @@ -19,7 +24,7 @@ use_tee() { # the amount of --seconds here is subject to change based on how long the CI job takes in the future # as we add more fuzz tests, we should take into consideration increasing this timelapse, so we can have enough coverage. # We are timing out after ~10mins in case the tests hang. (Current CI duration is ~8m, modify if needed) -cd ./fuzz && timeout 10m ./fuzz_all_native.py --ci --seconds 420 | use_tee $OUTPUT_FILE +timeout "${FUZZ_TIMEOUT}" "${FUZZ_TOOL_PATH}"/fuzz_all_native.py --ci --seconds 420 --go_module_root "${GO_MODULE_ROOT_PATH}" | use_tee $OUTPUT_FILE EXITCODE=${PIPESTATUS[0]} # Assert no known sensitive strings present in test logger output diff --git a/tools/bin/go_core_race_tests b/tools/bin/go_core_race_tests index aa6510c1127..d0fcb6cae41 100755 --- a/tools/bin/go_core_race_tests +++ b/tools/bin/go_core_race_tests @@ -4,7 +4,11 @@ OUTPUT_FILE=${OUTPUT_FILE:-"./output.txt"} USE_TEE="${USE_TEE:-true}" TIMEOUT="${TIMEOUT:-30s}" COUNT="${COUNT:-10}" -GO_LDFLAGS=$(bash tools/bin/ldflags) + +# To allow reuse in CI from other repositories +TOOLS_PATH=${TOOLS_PATH:-"./tools"} + +GO_LDFLAGS=$(bash ${TOOLS_PATH}/bin/ldflags) use_tee() { if [ "$USE_TEE" = "true" ]; then tee "$@" diff --git a/tools/bin/go_core_tests b/tools/bin/go_core_tests index 074527698b3..f7c1dfaf231 100755 --- a/tools/bin/go_core_tests +++ b/tools/bin/go_core_tests @@ -6,9 +6,12 @@ SCRIPT_PATH=`dirname "$0"`; SCRIPT_PATH=`eval "cd \"$SCRIPT_PATH\" && pwd"` OUTPUT_FILE=${OUTPUT_FILE:-"./output.txt"} USE_TEE="${USE_TEE:-true}" +# To allow reuse in CI from other repositories +TOOLS_PATH=${TOOLS_PATH:-"./tools"} + echo "Failed tests and panics: ---------------------" echo "" -GO_LDFLAGS=$(bash tools/bin/ldflags) +GO_LDFLAGS=$(bash ${TOOLS_PATH}/bin/ldflags) use_tee() { if [ "$USE_TEE" = "true" ]; then tee "$@"