Skip to content

Commit 36e1186

Browse files
committed
test: Adding fuzz testing for common util
1 parent 96f6571 commit 36e1186

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/pkg/errors v0.9.1
1515
github.com/sirupsen/logrus v1.8.1
1616
github.com/spf13/cobra v1.1.1
17+
github.com/stretchr/testify v1.7.0
1718
google.golang.org/api v0.48.0
1819
gopkg.in/yaml.v2 v2.4.0
1920
k8s.io/api v0.26.0
@@ -56,6 +57,7 @@ require (
5657
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5758
github.com/modern-go/reflect2 v1.0.2 // indirect
5859
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
60+
github.com/pmezard/go-difflib v1.0.0 // indirect
5961
github.com/spf13/pflag v1.0.5 // indirect
6062
go.opencensus.io v0.23.0 // indirect
6163
golang.org/x/crypto v0.16.0 // indirect

pkg/utils/common/common.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"os/signal"
1212
"reflect"
13+
"regexp"
1314
"strconv"
1415
"strings"
1516
"syscall"
@@ -29,13 +30,17 @@ type ENVDetails struct {
2930
ENV []apiv1.EnvVar
3031
}
3132

32-
//WaitForDuration waits for the given time duration (in seconds)
33+
// WaitForDuration waits for the given time duration (in seconds)
3334
func WaitForDuration(duration int) {
3435
time.Sleep(time.Duration(duration) * time.Second)
3536
}
3637

3738
// RandomInterval wait for the random interval lies between lower & upper bounds
3839
func RandomInterval(interval string) error {
40+
re := regexp.MustCompile(`^\d+(-\d+)?$`)
41+
if re.MatchString(interval) == false {
42+
return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "could not parse CHAOS_INTERVAL env, bad input"}
43+
}
3944
intervals := strings.Split(interval, "-")
4045
var lowerBound, upperBound int
4146
switch len(intervals) {
@@ -49,6 +54,9 @@ func RandomInterval(interval string) error {
4954
return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "could not parse CHAOS_INTERVAL env, invalid format"}
5055
}
5156
rand.Seed(time.Now().UnixNano())
57+
if upperBound < 1 {
58+
return cerrors.Error{ErrorCode: cerrors.ErrorTypeGeneric, Reason: "invalid CHAOS_INTERVAL env value, value below lower limit"}
59+
}
5260
waitTime := lowerBound + rand.Intn(upperBound-lowerBound)
5361
log.Infof("[Wait]: Wait for the random chaos interval %vs", waitTime)
5462
WaitForDuration(waitTime)
@@ -98,7 +106,7 @@ func AbortWatcherWithoutExit(expname string, clients clients.ClientSets, resultD
98106
}
99107
}
100108

101-
//FilterBasedOnPercentage return the slice of list based on the the provided percentage
109+
// FilterBasedOnPercentage return the slice of list based on the the provided percentage
102110
func FilterBasedOnPercentage(percentage int, list []string) []string {
103111

104112
var finalList []string
@@ -175,7 +183,7 @@ func GetStatusMessage(defaultCheck bool, defaultMsg, probeStatus string) string
175183
return "Probes: " + probeStatus
176184
}
177185

178-
//GetRandomSequence will gives a random value for sequence
186+
// GetRandomSequence will gives a random value for sequence
179187
func GetRandomSequence(sequence string) string {
180188
if strings.ToLower(sequence) == "random" {
181189
rand.Seed(time.Now().UnixNano())
@@ -186,7 +194,7 @@ func GetRandomSequence(sequence string) string {
186194
return sequence
187195
}
188196

189-
//ValidateRange validates the given range of numbers
197+
// ValidateRange validates the given range of numbers
190198
func ValidateRange(a string) string {
191199
var lb, ub int
192200
intervals := strings.Split(a, "-")
@@ -204,7 +212,7 @@ func ValidateRange(a string) string {
204212
}
205213
}
206214

207-
//getRandomValue gives a random value between two integers
215+
// getRandomValue gives a random value between two integers
208216
func getRandomValue(a, b int) int {
209217
rand.Seed(time.Now().Unix())
210218
return (a + rand.Intn(b-a+1))

pkg/utils/common/common_fuzz_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package common
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"regexp"
6+
"strconv"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func FuzzRandomInterval(f *testing.F) {
12+
testCases := []struct {
13+
interval string
14+
}{
15+
{
16+
interval: "13",
17+
},
18+
}
19+
20+
for _, tc := range testCases {
21+
f.Add(tc.interval)
22+
}
23+
24+
f.Fuzz(func(t *testing.T, interval string) {
25+
re := regexp.MustCompile(`^\d+(-\d+)?$`)
26+
intervals := strings.Split(interval, "-")
27+
err := RandomInterval(interval)
28+
29+
if re.MatchString(interval) == false {
30+
assert.Error(t, err, "{\"errorCode\":\"GENERIC_ERROR\",\"reason\":\"could not parse CHAOS_INTERVAL env, bad input\"}")
31+
}
32+
33+
num, _ := strconv.Atoi(intervals[0])
34+
if num < 1 && err != nil {
35+
assert.Error(t, err, "{\"errorCode\":\"GENERIC_ERROR\",\"reason\":\"invalid CHAOS_INTERVAL env value, value below lower limit\"}")
36+
} else if num > 1 && err != nil {
37+
t.Errorf("Unexpected Error: %v", err)
38+
}
39+
})
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("2778")

0 commit comments

Comments
 (0)