Skip to content

Commit

Permalink
add fake fuzz test that will quickly fail
Browse files Browse the repository at this point in the history
  • Loading branch information
rfratto committed Apr 27, 2024
1 parent 3dbc5cf commit 9e1752c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/fuzz-run-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,40 @@ jobs:
When opening a PR with the fix, please include the test case file in your PR to prevent regressions.
EOF
- name: Create new issue
if: ${{ failure() && steps.new-failure.outputs.file != '' }}
uses: actions/github-script@v7
with:
script: |
const failureName = "${{ steps.new-failure.outputs.name }}";
const issueTitle = `${{ inputs.package-path }}: ${{ inputs.test-name }} failed (${failureName})`;
// Look for existing issue first with the same title.
const issues = await github.rest.search.issuesAndPullRequests({
q: `is:issue is:open repo:${{ github.repository }} in:title "${failureName}"`
})
const issue = issues.data.items.find((issue) => issue.title === issue.title);
if (issue) {
return;
}
// Create a new issue.
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: issueTitle,
body: `
A new fuzz test failure was found in <code>${{ input.package-path }}</code>.
To reproduce the failure locally, run the following command using the GitHub CLI to download the corpus entry:
<pre lang="bash">gh run download --repo ${{ github.repository }} ${{ github.run_id }} -n failure-${{ steps.new-failure.outputs.package }}-${{ steps.new-failure.outputs.function }} --dir ${{ inputs.package-path }}/testdata/fuzz/${{ inputs.test-name }}</pre>
When opening a PR with the fix, please include in the corpus entry in your commit to prevent regressions.
[Link to failed run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
`,
labels: ['bug'],
})
27 changes: 27 additions & 0 deletions internal/fuzztests/fake_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func FuzzFails(f *testing.F) {
f.Add(0, 0)
f.Add(2, 4)

f.Fuzz(func(t *testing.T, a, b int) {
expect := a + b
actual := myAdd(a, b)

require.Equal(t, expect, actual, "Expect %d + %d = %d", a, b, expect)
})
}

func myAdd(a, b int) int {
// Force myAdd to return the wrong sum if a is an odd number.
if a%2 != 0 {
return a + b + 1
}
return a + b
}

0 comments on commit 9e1752c

Please sign in to comment.