Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Adding fuzz testing for common util #688

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.7.0
google.golang.org/api v0.48.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.26.0
Expand Down Expand Up @@ -56,6 +57,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
Expand Down
18 changes: 13 additions & 5 deletions pkg/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"os/signal"
"reflect"
"regexp"
"strconv"
"strings"
"syscall"
Expand All @@ -29,13 +30,17 @@ type ENVDetails struct {
ENV []apiv1.EnvVar
}

//WaitForDuration waits for the given time duration (in seconds)
// WaitForDuration waits for the given time duration (in seconds)
func WaitForDuration(duration int) {
time.Sleep(time.Duration(duration) * time.Second)
}

// RandomInterval wait for the random interval lies between lower & upper bounds
func RandomInterval(interval string) error {
re := regexp.MustCompile(`^\d+(-\d+)?$`)
if re.MatchString(interval) == false {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "could not parse CHAOS_INTERVAL env, bad input"}
}
intervals := strings.Split(interval, "-")
var lowerBound, upperBound int
switch len(intervals) {
Expand All @@ -49,6 +54,9 @@ func RandomInterval(interval string) error {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "could not parse CHAOS_INTERVAL env, invalid format"}
}
rand.Seed(time.Now().UnixNano())
if upperBound < 1 {
return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "invalid CHAOS_INTERVAL env value, value below lower limit"}
}
waitTime := lowerBound + rand.Intn(upperBound-lowerBound)
log.Infof("[Wait]: Wait for the random chaos interval %vs", waitTime)
WaitForDuration(waitTime)
Expand Down Expand Up @@ -98,7 +106,7 @@ func AbortWatcherWithoutExit(expname string, clients clients.ClientSets, resultD
}
}

//FilterBasedOnPercentage return the slice of list based on the the provided percentage
// FilterBasedOnPercentage return the slice of list based on the the provided percentage
func FilterBasedOnPercentage(percentage int, list []string) []string {

var finalList []string
Expand Down Expand Up @@ -175,7 +183,7 @@ func GetStatusMessage(defaultCheck bool, defaultMsg, probeStatus string) string
return "Probes: " + probeStatus
}

//GetRandomSequence will gives a random value for sequence
// GetRandomSequence will gives a random value for sequence
func GetRandomSequence(sequence string) string {
if strings.ToLower(sequence) == "random" {
rand.Seed(time.Now().UnixNano())
Expand All @@ -186,7 +194,7 @@ func GetRandomSequence(sequence string) string {
return sequence
}

//ValidateRange validates the given range of numbers
// ValidateRange validates the given range of numbers
func ValidateRange(a string) string {
var lb, ub int
intervals := strings.Split(a, "-")
Expand All @@ -204,7 +212,7 @@ func ValidateRange(a string) string {
}
}

//getRandomValue gives a random value between two integers
// getRandomValue gives a random value between two integers
func getRandomValue(a, b int) int {
rand.Seed(time.Now().Unix())
return (a + rand.Intn(b-a+1))
Expand Down
40 changes: 40 additions & 0 deletions pkg/utils/common/common_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import (
"github.com/stretchr/testify/assert"
"regexp"
"strconv"
"strings"
"testing"
)

func FuzzRandomInterval(f *testing.F) {
testCases := []struct {
interval string
}{
{
interval: "13",
},
}

for _, tc := range testCases {
f.Add(tc.interval)
}

f.Fuzz(func(t *testing.T, interval string) {
re := regexp.MustCompile(`^\d+(-\d+)?$`)
intervals := strings.Split(interval, "-")
err := RandomInterval(interval)

if re.MatchString(interval) == false {
assert.Error(t, err, "{\"errorCode\":\"GENERIC_ERROR\",\"reason\":\"could not parse CHAOS_INTERVAL env, bad input\"}")
}

num, _ := strconv.Atoi(intervals[0])
if num < 1 && err != nil {
assert.Error(t, err, "{\"errorCode\":\"GENERIC_ERROR\",\"reason\":\"invalid CHAOS_INTERVAL env value, value below lower limit\"}")
} else if num > 1 && err != nil {
t.Errorf("Unexpected Error: %v", err)
}
})
ispeakc0de marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("2778")
Loading