Skip to content

Commit

Permalink
added fuzzy test for subscriber (#4518)
Browse files Browse the repository at this point in the history
* added fuzzy test for subscriber

Signed-off-by: Amit Kumar Das <amit.das@harness.io>

* removed print statements

Signed-off-by: Amit Kumar Das <amit.das@harness.io>

---------

Signed-off-by: Amit Kumar Das <amit.das@harness.io>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
  • Loading branch information
amityt and Saranya-jena committed Apr 4, 2024
1 parent 328bd26 commit cc74f22
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
3 changes: 2 additions & 1 deletion chaoscenter/subscriber/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/litmuschaos/chaos-operator v0.0.0-20230718113617-6819a4be12e4
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.2
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.26.0
k8s.io/apimachinery v0.26.0
Expand Down Expand Up @@ -40,8 +41,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.2 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.7.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions chaoscenter/subscriber/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
Expand Down
2 changes: 1 addition & 1 deletion chaoscenter/subscriber/pkg/events/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func getNameFromLog(log string) string {
return name[1]
}

// converts unix timestamp to string
// StrConvTime converts unix timestamp to string
func StrConvTime(time int64) string {
if time < 0 {
return ""
Expand Down
50 changes: 48 additions & 2 deletions chaoscenter/subscriber/pkg/events/utils_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package events

import (
"fmt"
"subscriber/pkg/graphql"
"subscriber/pkg/k8s"
"subscriber/pkg/types"
"testing"

"github.com/stretchr/testify/assert"

fuzz "github.com/AdaLogics/go-fuzz-headers"
"github.com/golang/mock/gomock"
)
Expand All @@ -32,11 +33,56 @@ func FuzzGenerateWorkflowPayload(f *testing.F) {

event, err := subscriberEvents.GenerateWorkflowPayload(targetStruct.cid, targetStruct.accessKey, targetStruct.version, targetStruct.completed, targetStruct.wfEvent)
if err != nil {
fmt.Println(event)
t.Errorf("Unexpected error: %v", err)
}
if event == nil {
t.Errorf("Returned payload is nil")
}
})
}

func FuzzStrConvTime(f *testing.F) {

targetStruct := []int64{
12345, 54353, -12345,
}

for _, v := range targetStruct {
f.Add(v)
}

f.Fuzz(func(t *testing.T, data int64) {

ctrl := gomock.NewController(t)
defer ctrl.Finish()

resp := StrConvTime(data)
if resp == "" {
t.Log("result is in negative")
} else {
t.Log("converted successfully")
}
})
}

func FuzzGetExperimentStatus(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)

targetStruct := &struct {
wfEvent types.WorkflowEvent
}{}
err := fuzzConsumer.GenerateStruct(targetStruct)
if err != nil {
return
}

// Call the getExperimentStatus function
_, err = getExperimentStatus(targetStruct.wfEvent)
if err != nil {
assert.Equal(t, "status is invalid", "status is invalid")
} else {
t.Log("status returned")
}
})
}
17 changes: 12 additions & 5 deletions chaoscenter/subscriber/pkg/events/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ func (ev *subscriberEvents) SendWorkflowUpdates(infraData map[string]string, eve
// Setting up the experiment status
// based on different probes results
// present in the experiment
event.Phase = getExperimentStatus(event)
status, err := getExperimentStatus(event)
if err != nil {
logrus.WithError(err)
}
event.Phase = status

eventMap[event.UID] = event

Expand Down Expand Up @@ -276,7 +280,7 @@ func updateWorkflowStatus(status v1alpha1.WorkflowPhase) string {

// getExperimentStatus is used to fetch the final experiment status
// based on the fault/probe status
func getExperimentStatus(experiment types.WorkflowEvent) string {
func getExperimentStatus(experiment types.WorkflowEvent) (string, error) {
var (
errorCount = 0
completedWithProbeFailureCount = 0
Expand All @@ -287,8 +291,12 @@ func getExperimentStatus(experiment types.WorkflowEvent) string {
// we will fetch the data based on the different
// node statuses(which are coming from the probe status
// of these faults)
if status == "" {
return "", errors.New("status is invalid")
}

if status == "Stopped" || experiment.FinishedAt == "" {
return status
return status, nil
}

for _, node := range experiment.Nodes {
Expand All @@ -313,6 +321,5 @@ func getExperimentStatus(experiment types.WorkflowEvent) string {
} else if completedWithProbeFailureCount > 0 {
status = string(types.FaultCompletedWithProbeFailure)
}

return status
return status, nil
}

0 comments on commit cc74f22

Please sign in to comment.