Skip to content

Commit 7fbb040

Browse files
authored
test: fuzz weighted CI partitioning (#51)
* test: fuzz weighted CI partitioning Signed-off-by: IanHollow <72767437+IanHollow@users.noreply.github.com> * style: format fuzz workflow Signed-off-by: IanHollow <72767437+IanHollow@users.noreply.github.com> * Name fuzz CI job for workflow validation Signed-off-by: IanHollow <72767437+IanHollow@users.noreply.github.com> * Run fuzzing in the merge queue Signed-off-by: IanHollow <72767437+IanHollow@users.noreply.github.com> * Scope fuzz workflow token permissions Signed-off-by: IanHollow <72767437+IanHollow@users.noreply.github.com> --------- Signed-off-by: IanHollow <72767437+IanHollow@users.noreply.github.com>
1 parent 2de61ce commit 7fbb040

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎.github/workflows/fuzz.yml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Fuzz CI partitioning
2+
on:
3+
pull_request:
4+
merge_group:
5+
types: [checks_requested]
6+
schedule:
7+
- cron: '43 8 * * 1'
8+
workflow_dispatch:
9+
permissions: {}
10+
concurrency:
11+
group: fuzz-${{ github.ref }}
12+
cancel-in-progress: true
13+
jobs:
14+
partitioning:
15+
name: Fuzz check partitioning
16+
permissions:
17+
contents: read
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 15
20+
steps:
21+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
22+
with:
23+
persist-credentials: false
24+
- name: Install Atheris
25+
run: |
26+
python3 -m venv "$RUNNER_TEMP/fuzz-venv"
27+
"$RUNNER_TEMP/fuzz-venv/bin/pip" install atheris==3.1.0
28+
- name: Fuzz weighted partitions
29+
run: |
30+
mkdir -p "$RUNNER_TEMP/fuzz-crashes"
31+
"$RUNNER_TEMP/fuzz-venv/bin/python" fuzz/partition_fuzzer.py -max_total_time=180 -timeout=10 -artifact_prefix="$RUNNER_TEMP/fuzz-crashes/"
32+
- name: Save crashing inputs
33+
if: failure()
34+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
35+
with:
36+
name: fuzz-crashes
37+
path: ${{ runner.temp }}/fuzz-crashes/
38+
if-no-files-found: ignore
39+
retention-days: 7

‎fuzz/partition_fuzzer.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Check that weighted CI partitioning never drops or duplicates a check."""
2+
3+
import importlib.util
4+
import math
5+
import sys
6+
from pathlib import Path
7+
8+
import atheris
9+
10+
SCRIPT = Path(__file__).resolve().parents[1] / "scripts/ci_partitioning.py"
11+
with atheris.instrument_imports():
12+
spec = importlib.util.spec_from_file_location("ci_partitioning", SCRIPT)
13+
assert spec is not None and spec.loader is not None
14+
module = importlib.util.module_from_spec(spec)
15+
sys.modules[spec.name] = module
16+
spec.loader.exec_module(module)
17+
atheris.instrument_func(module.partition_names)
18+
atheris.instrument_func(module.partition_load)
19+
20+
21+
@atheris.instrument_func
22+
def test_one_input(data: bytes) -> None:
23+
provider = atheris.FuzzedDataProvider(data)
24+
count = provider.ConsumeIntInRange(1, 32)
25+
names = [f"{provider.ConsumeUnicodeNoSurrogates(12)}-{i}" for i in range(count)]
26+
partitions = provider.ConsumeIntInRange(1, count)
27+
weights = {name: provider.ConsumeIntInRange(1, 1000) / 10 for name in names}
28+
result = module.partition_names(names, partitions, weights=weights)
29+
assert len(result) == partitions
30+
assert all(result)
31+
assert sorted(name for group in result for name in group) == sorted(names)
32+
assert result == module.partition_names(names[::-1], partitions, weights=weights)
33+
loads = [module.partition_load(group, weights=weights) for group in result]
34+
assert math.isclose(sum(loads), sum(weights.values()), rel_tol=1e-12)
35+
assert max(loads) - min(loads) <= max(weights.values()) + 1e-9
36+
37+
38+
if __name__ == "__main__":
39+
atheris.Setup(sys.argv, test_one_input)
40+
atheris.Fuzz()

0 commit comments

Comments
 (0)