Skip to content

Commit f7b569a

Browse files
authored
Merge pull request #211 from litmuschaos/CHAOS-4611
chore: [CHAOS-4611]: writing initial fuzz testing for chaos runner
2 parents c5e46ee + fa7f5ed commit f7b569a

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/litmuschaos/chaos-runner
33
go 1.20
44

55
require (
6+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24
67
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef
78
github.com/litmuschaos/chaos-operator v0.0.0-20240301085554-ba4d2f704cfa
89
github.com/litmuschaos/elves v0.0.0-20230607095010-c7119636b529

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
3232
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
3333
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
3434
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
35+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
36+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
3537
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
3638
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
3739
github.com/Azure/go-autorest/autorest v0.11.12/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=

pkg/utils/builders.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func buildSideCarSpec(experiment *ExperimentDetails) ([]*container.Builder, erro
6565
}
6666

6767
containerSpec := container.NewBuilder().
68-
WithName(experiment.JobName + "-sidecar-" + RandomString()).
68+
WithName(experiment.JobName + "-sidecar-" + RandomString(6)).
6969
WithImage(sidecar.Image).
7070
WithImagePullPolicy(sidecar.ImagePullPolicy).
7171
WithEnvsNew(sidecar.ENV)

pkg/utils/common.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
// RandomString will generate a random string of length 6
10-
func RandomString() string {
10+
func RandomString(length int) string {
1111
rand.Seed(time.Now().UnixNano())
1212
chars := []rune("abcdefghijklmnopqrstuvwxyz" + "0123456789")
13-
length := 6
13+
1414
var b strings.Builder
1515
for i := 0; i < length; i++ {
1616
b.WriteRune(chars[rand.Intn(len(chars))])

pkg/utils/common_fuzz_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package utils
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func FuzzRandomString(f *testing.F) {
9+
f.Add(6)
10+
f.Fuzz(func(t *testing.T, n int) {
11+
randomString := RandomString(n)
12+
// Perform checks on the generated string
13+
// Check if the length matches the expected length
14+
if n >= 0 && len(randomString) != n {
15+
t.Errorf("Generated string length doesn't match expected length")
16+
}
17+
18+
// Check if the string contains only valid characters
19+
if !isValidString(randomString) {
20+
t.Errorf("Generated string contains invalid characters")
21+
}
22+
})
23+
24+
}
25+
26+
func isValidString(s string) bool {
27+
// Define the set of valid characters
28+
validChars := "abcdefghijklmnopqrstuvwxyz0123456789"
29+
30+
// Iterate over each character in the string
31+
for _, char := range s {
32+
// Check if the character is not in the set of valid characters
33+
if !strings.ContainsRune(validChars, char) {
34+
return false
35+
}
36+
}
37+
return true
38+
}

pkg/utils/experimentHelper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (engineDetails *EngineDetails) NewExperimentDetails(i int) ExperimentDetail
2828
experimentDetails.SvcAccount = engineDetails.SvcAccount
2929
experimentDetails.Namespace = engineDetails.EngineNamespace
3030
// Setting the JobName in Experiment related struct
31-
experimentDetails.JobName = experimentDetails.Name + "-" + RandomString()
31+
experimentDetails.JobName = experimentDetails.Name + "-" + RandomString(6)
3232
return experimentDetails
3333
}
3434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("0")

0 commit comments

Comments
 (0)