Skip to content

Commit

Permalink
Merge pull request #211 from litmuschaos/CHAOS-4611
Browse files Browse the repository at this point in the history
chore: [CHAOS-4611]: writing initial fuzz testing for chaos runner
  • Loading branch information
Saranya-jena authored Mar 11, 2024
2 parents c5e46ee + fa7f5ed commit f7b569a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/litmuschaos/chaos-runner
go 1.20

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef
github.com/litmuschaos/chaos-operator v0.0.0-20240301085554-ba4d2f704cfa
github.com/litmuschaos/elves v0.0.0-20230607095010-c7119636b529
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest/autorest v0.11.12/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func buildSideCarSpec(experiment *ExperimentDetails) ([]*container.Builder, erro
}

containerSpec := container.NewBuilder().
WithName(experiment.JobName + "-sidecar-" + RandomString()).
WithName(experiment.JobName + "-sidecar-" + RandomString(6)).
WithImage(sidecar.Image).
WithImagePullPolicy(sidecar.ImagePullPolicy).
WithEnvsNew(sidecar.ENV)
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

// RandomString will generate a random string of length 6
func RandomString() string {
func RandomString(length int) string {
rand.Seed(time.Now().UnixNano())

Check failure on line 11 in pkg/utils/common.go

View workflow job for this annotation

GitHub Actions / pre-checks

SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: As of Go 1.20 there is no reason to call Seed with a random value. Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator. (staticcheck)
chars := []rune("abcdefghijklmnopqrstuvwxyz" + "0123456789")
length := 6

var b strings.Builder
for i := 0; i < length; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
Expand Down
38 changes: 38 additions & 0 deletions pkg/utils/common_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utils

import (
"strings"
"testing"
)

func FuzzRandomString(f *testing.F) {
f.Add(6)
f.Fuzz(func(t *testing.T, n int) {
randomString := RandomString(n)
// Perform checks on the generated string
// Check if the length matches the expected length
if n >= 0 && len(randomString) != n {
t.Errorf("Generated string length doesn't match expected length")
}

// Check if the string contains only valid characters
if !isValidString(randomString) {
t.Errorf("Generated string contains invalid characters")
}
})

}

func isValidString(s string) bool {
// Define the set of valid characters
validChars := "abcdefghijklmnopqrstuvwxyz0123456789"

// Iterate over each character in the string
for _, char := range s {
// Check if the character is not in the set of valid characters
if !strings.ContainsRune(validChars, char) {
return false
}
}
return true
}
2 changes: 1 addition & 1 deletion pkg/utils/experimentHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (engineDetails *EngineDetails) NewExperimentDetails(i int) ExperimentDetail
experimentDetails.SvcAccount = engineDetails.SvcAccount
experimentDetails.Namespace = engineDetails.EngineNamespace
// Setting the JobName in Experiment related struct
experimentDetails.JobName = experimentDetails.Name + "-" + RandomString()
experimentDetails.JobName = experimentDetails.Name + "-" + RandomString(6)
return experimentDetails
}

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

0 comments on commit f7b569a

Please sign in to comment.