Skip to content
Merged
13 changes: 13 additions & 0 deletions .evergreen/generated_configs/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ tasks:
TEST_NAME: fuzz
SUB_TEST_NAME: fuzz
UV_PYTHON: "3.13"
- command: subprocess.exec
params:
binary: bash
args:
- .evergreen/scripts/capture-fuzz-corpus.sh
working_dir: src
include_expansions_in_env:
- FUZZ_CORPUS_MAX_UNITS
- FUZZ_CORPUS_MAX_UNIT_BYTES
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_SESSION_TOKEN
type: test
tags: [fuzz, fuzz-nightly]

# Getdata tests
Expand Down
32 changes: 32 additions & 0 deletions .evergreen/scripts/capture-fuzz-corpus.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
# PYTHON-6178: capture coverage-increasing inputs from a fuzz run and open
# an automated PR growing fuzz/corpus/. Runs as the last command of the
# nightly "fuzz" task, while the scratch corpus exists; manual
# `evergreen patch --variant fuzz-rhel8 --task fuzz` runs capture too.
set -eu

# Same environment as just.sh: venv on PATH for a modern Python.
. "$(dirname "${BASH_SOURCE:-$0}")/setup-dev-env.sh"

# No new units means no PR, and the github secrets flow is never touched.
uv run --no-sync python fuzz/capture_corpus.py
if [ -z "$(ls -A fuzz-scratch/capture 2>/dev/null || true)" ]; then
echo "No new fuzz corpus units; nothing to do."
exit 0
fi

# A unit found by a later target never ran through the earlier ones, so
# replay every unit through all targets before publishing.
replay=(fuzz-scratch/capture/*)
for target in fuzz_decode fuzz_decode_all fuzz_is_valid; do
echo "Replaying captured units against ${target}..."
if ! uv run --no-sync python "fuzz/${target}.py" "${replay[@]}" \
-artifact_prefix="$PWD/fuzz-scratch/" -atheris_runs="${#replay[@]}"
then
echo "Captured unit fails ${target}; no corpus PR." >&2
exit 1
fi
done

bash .evergreen/scripts/create-fuzz-corpus-pr.sh \
fuzz-scratch/capture fuzz-scratch/capture-summary.md
31 changes: 31 additions & 0 deletions .evergreen/scripts/create-fuzz-corpus-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -eu

# Open (or update) the automated pull request that grows fuzz/corpus/
# with freshly captured units. Arguments: 1 = captured units directory,
# 2 = capture summary markdown to use as the PR body.

capture_dir="$1"
summary_file="$2"
today="$(date '+%m-%d-%Y')"

# Shared github-app authentication, git branch and PR plumbing.
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")/github-pr-util.sh"

acquire_github_token

echo "Creating the git checkout..."
branch="fuzz-corpus-${today}"
prepare_bot_branch "$branch"

mkdir -p fuzz/corpus
cp "$capture_dir"/* fuzz/corpus/
if ! stage_and_commit "PYTHON-6178 Grow the BSON fuzz corpus (${today})" fuzz/corpus; then
echo "No new corpus units to commit; skipping PR creation."
exit 0
fi
echo "Creating the git checkout... done."

push_branch "$branch"
create_or_update_pr "$branch" "[Fuzz Corpus] ${today}" "$summary_file"
129 changes: 16 additions & 113 deletions .evergreen/scripts/create-spec-pr.sh
Original file line number Diff line number Diff line change
@@ -1,127 +1,30 @@
#!/usr/bin/env bash
set -eu

# Default to the drivers-evergreen-tools submodule when unset; a
# caller-provided DRIVERS_TOOLS wins, as in the other consumers.
tools="$(realpath -s "${DRIVERS_TOOLS:-./drivers-evergreen-tools}")"
pushd $tools/.evergreen/github_app || exit
# Open (or update) the automated pull request resyncing the spec tests.
# Arguments: 1 = resync summary text to use as the PR body.

owner="mongodb"
repo="mongo-python-driver"
summary_file="$1"
today="$(date '+%m-%d-%Y')"

# Bootstrap the app.
echo "bootstrapping"
source utils.sh
bootstrap drivers/comment-bot
# Shared github-app authentication, git branch and PR plumbing.
# shellcheck disable=SC1091
. "$(dirname "${BASH_SOURCE[0]}")/github-pr-util.sh"

# Run the app.
source ./secrets-export.sh
acquire_github_token

# Get a github access token for the git checkout.
echo "Getting github token..."

token=$(bash ./get-access-token.sh $repo $owner)
if [ -z "${token}" ]; then
echo "Failed to get github access token!"
popd || exit
exit 1
fi
echo "Getting github token... done."
popd || exit

# Make the git checkout and create a new branch.
echo "Creating the git checkout..."
branch="spec-resync-"$(date '+%m-%d-%Y')

git remote set-url origin https://x-access-token:${token}@github.com/$owner/$repo.git
# -B resets the branch if a reused checkout already has it, keeping reruns idempotent.
git checkout -B "$branch" "origin/main"
branch="spec-resync-${today}"
prepare_bot_branch "$branch"

# Attribute the commit to the bot user; fall back to the host identity.
bot_login="mongodb-drivers-pr-bot[bot]"
bot_id=$(curl -gsm 10 "https://api.github.com/users/${bot_login}" | jq -r '.id // empty' || true)
if [ -n "$bot_id" ]; then
git config user.name "$bot_login"
git config user.email "${bot_id}+${bot_login}@users.noreply.github.com"
fi

git add ./test

if git diff --cached --quiet && git diff --quiet --ignore-submodules=dirty; then
# The spec sync also updates the vendored public suffix list (cp_psl in
# resync-specs.sh), a tracked file outside test/.
if ! stage_and_commit "resyncing specs ${today}" ./test pymongo/public_suffix_list.dat; then
echo "No spec changes to commit; skipping PR creation."
cat "$1"
cat "$summary_file"
exit 0
fi

git commit -am "resyncing specs $(date '+%m-%d-%Y')"
echo "Creating the git checkout... done."

# Date-stamped branch: force-push makes same-day reruns idempotent; retry
# absorbs GitHub's intermittent 403s on the receive-pack endpoint.
push_ok=false
for _attempt in 1 2 3; do
if git push --force origin $branch; then
push_ok=true
break
fi
if [ "$_attempt" -lt 3 ]; then
echo "Push attempt ${_attempt} failed; retrying in 30s..."
sleep 30
fi
done
if [ "$push_ok" != true ]; then
echo "Failed to push $branch after 3 attempts!" >&2
exit 1
fi

# Build the payload as a file so the body content is always properly
# JSON-escaped, rather than interpolated into the command line as text.
payload_file=$(mktemp)
trap 'rm -f "$payload_file"' EXIT
jq -n \
--arg title "[Spec Resync] $(date '+%m-%d-%Y')" \
--arg head "${branch}" \
--rawfile body "$1" \
'{title: $title, body: $body, head: $head, base: "main"}' > "$payload_file"

# Reuse an open PR for this branch (same-day rerun) instead of duplicating it.
existing_pr_json=$(curl -gs \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $token" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--url "https://api.github.com/repos/$owner/$repo/pulls?head=$owner:$branch&state=open")
existing_pr_url=$(echo "$existing_pr_json" | jq -r '.[0].html_url // empty')
if [ -n "$existing_pr_url" ]; then
existing_pr_number=$(echo "$existing_pr_json" | jq -r '.[0].number // empty')
if ! curl -sfX PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $token" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "{\"title\": \"[Spec Resync] $(date '+%m-%d-%Y')\", \"body\": $(jq -Rs . < "$1")}" \
--url "https://api.github.com/repos/$owner/$repo/pulls/${existing_pr_number}" > /dev/null
then
echo "Failed to update PR summary!" >&2
exit 1
fi
echo "$existing_pr_url"
echo "Creating the PR... done. (PR already existed; branch and summary were updated)"
rm -rf $tools
exit 0
fi

resp=$(curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $token" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "@${payload_file}" \
--url https://api.github.com/repos/$owner/$repo/pulls)

pr_url=$(echo "$resp" | jq -r '.html_url // empty')
if [ -z "$pr_url" ]; then
echo "Failed to create PR! API response:"
echo "$resp" | jq .
exit 1
fi
echo "$pr_url"
echo "Creating the PR... done."
push_branch "$branch"
create_or_update_pr "$branch" "[Spec Resync] ${today}" "$summary_file"
40 changes: 32 additions & 8 deletions .evergreen/scripts/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,21 @@ def create_doctests_variants():


def create_fuzz_variants():
# Two variants for the two fuzz flows: the regression reproducers run
# on every PR, the time-boxed fuzzing runs nightly. The nightly variant
# carries no "pr" tag, so PR patch builds never select it, while its
# default patchability keeps it manually runnable
# (`evergreen patch --variant fuzz-rhel8 --task fuzz`). The host needs
# CPython, which the rhel8 toolchain provides.
host = HOSTS["rhel8"]
# Regression test on every PR.
name = get_variant_name("Fuzz Regression", host)
pr_variant = create_variant([".fuzz-regression"], name, host=host, tags=["pr"])
# Fuzzing requires CPython on Linux/macOS; use a pinned CPython on RHEL8.
name = get_variant_name("Fuzz", host)
nightly = create_variant([".fuzz-nightly"], name, host=host, batchtime=BATCHTIME_DAY)
return [pr_variant, nightly]
return [
create_variant(
[".fuzz-regression"], get_variant_name("Fuzz Regression", host), host=host, tags=["pr"]
),
create_variant(
[".fuzz-nightly"], get_variant_name("Fuzz", host), host=host, batchtime=BATCHTIME_DAY
),
]


def create_sanitizer_variants() -> list[BuildVariant]:
Expand Down Expand Up @@ -960,7 +967,24 @@ def create_fuzz_tasks():
tags = ["fuzz", "fuzz-nightly"]
test_vars = dict(TEST_NAME="fuzz", SUB_TEST_NAME="fuzz", UV_PYTHON="3.13")
test_func = FunctionCall(func="run tests", vars=test_vars)
tasks.append(EvgTask(name="fuzz", tags=tags, commands=[test_func]))
# PYTHON-6178: while the scratch corpus still exists, capture the new
# coverage-increasing inputs, replay them through every target so a
# unit that fails one is never published, and open an automated PR
# growing fuzz/corpus/.
capture_cmd = get_subprocess_exec(
args=[".evergreen/scripts/capture-fuzz-corpus.sh"],
include_expansions_in_env=[
"FUZZ_CORPUS_MAX_UNITS",
"FUZZ_CORPUS_MAX_UNIT_BYTES",
# The capture bootstraps the comment-bot github app, whose
# secrets live in AWS Secrets Manager; pass the assumed-role
# credentials as resync_specs and backport-pr also do.
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
],
Comment thread
Copilot marked this conversation as resolved.
Resolved
)
tasks.append(EvgTask(name="fuzz", tags=tags, commands=[test_func, capture_cmd]))
return tasks


Expand Down
Loading
Loading