Skip to content

Commit

Permalink
added fuzz tests for ValidateCPUs function
Browse files Browse the repository at this point in the history
Fuzz test for ValidateCPUs to verify it handles both valid and invalid inputs without panicking. This ensures that all potential errors are exercised and no panic occurs during execution.

Signed-off-by: Ronny Baturov <[email protected]>
  • Loading branch information
rbaturov authored and openshift-cherrypick-robot committed Dec 5, 2024
1 parent 930d955 commit 45bafd9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ endif
vet: $(BINDATA)
$(GO) vet ./...

test-unit: $(BINDATA)
test-unit: $(BINDATA) test-fuzz
$(GO) test ./cmd/... ./pkg/... -coverprofile cover.out

test-fuzz:
$(GO) test ./pkg/apis/performanceprofile/v2 -fuzz=FuzzValidateCPUs -fuzztime=10s

clean:
$(GO) clean $(PACKAGE_MAIN)
rm -rf $(BINDATA) $(OUT_DIR) tmp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v2

import (
"fmt"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -153,6 +154,33 @@ func NewPerformanceProfile(name string) *PerformanceProfile {
}
}

// Fuzz test for ValidateCPUs to ensure it handles invalid inputs and does not panic.
func FuzzValidateCPUs(f *testing.F) {
seeds := []string{"garbage", "a,b,c", "0-1"}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, input string) {
cpuFields := map[string]func(*PerformanceProfile, CPUSet){
"reserved": func(p *PerformanceProfile, input CPUSet) { p.Spec.CPU.Reserved = &input },
"isolated": func(p *PerformanceProfile, input CPUSet) { p.Spec.CPU.Isolated = &input },
"shared": func(p *PerformanceProfile, input CPUSet) { p.Spec.CPU.Shared = &input },
"offline": func(p *PerformanceProfile, input CPUSet) { p.Spec.CPU.Offlined = &input },
}

for fieldName, setField := range cpuFields {
t.Run(fieldName, func(t *testing.T) {
cpuSet := CPUSet(input)
profile := NewPerformanceProfile("test")

setField(profile, cpuSet)
// We don't care for the errors we got, only care about panics, which will cause a failure if they occur.
_ = profile.validateCPUs()
})
}
})
}

var _ = Describe("PerformanceProfile", func() {
var profile *PerformanceProfile

Expand Down

0 comments on commit 45bafd9

Please sign in to comment.