Skip to content

Commit

Permalink
[TT-1252] Make tools/bin/go_core_* tools reusable from other reposito…
Browse files Browse the repository at this point in the history
…ries (#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
  • Loading branch information
tateexon authored Jul 11, 2024
1 parent c8b9879 commit 97b4e7a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
17 changes: 8 additions & 9 deletions fuzz/fuzz_all_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import subprocess
import sys

LIBROOT = "../"

def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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()
7 changes: 6 additions & 1 deletion tools/bin/go_core_fuzz
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tools/bin/go_core_race_tests
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
Expand Down
5 changes: 4 additions & 1 deletion tools/bin/go_core_tests
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
Expand Down

0 comments on commit 97b4e7a

Please sign in to comment.