Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
Signed-off-by: nagesh bansal <[email protected]>
  • Loading branch information
Nageshbansal committed Jul 25, 2023
1 parent 27a7ae2 commit c3ebd2c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.1
github.com/sirupsen/logrus v1.9.3
k8s.io/api v0.22.2
k8s.io/apimachinery v0.22.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
k8s.io/api v0.26.0
k8s.io/apimachinery v0.26.0
k8s.io/client-go v12.0.0+incompatible
Expand Down Expand Up @@ -48,6 +46,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
105 changes: 60 additions & 45 deletions tests/controller_test/collect-data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/litmuschaos/chaos-exporter/pkg/clients"
"github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
litmusFakeClientSet "github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/fake"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
Expand All @@ -17,13 +19,23 @@ func TestGetResultList(t *testing.T) {
FakeChaosNameSpace := "Fake Namespace"
FakeEngineName := "Fake Engine"

tests := map[string]struct {
chaosresult *v1alpha1.ChaosResult
tests := []struct {
name string
execFunc func(client clients.ClientSets, chaosResult *v1alpha1.ChaosResult)
chaosResult *v1alpha1.ChaosResult
monitoring *controller.MonitoringEnabled
isErr bool
}{
"Test Positive-1": {
chaosresult: &v1alpha1.ChaosResult{
{
name: "success:chaos result found",
execFunc: func(client clients.ClientSets, chaosResult *v1alpha1.ChaosResult) {
_, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosResults(chaosResult.Namespace).Create(context.Background(), chaosResult, metav1.CreateOptions{})
if err != nil {
t.Fatalf("chaosresult not created")
}
},

chaosResult: &v1alpha1.ChaosResult{
ObjectMeta: metav1.ObjectMeta{
Name: FakeEngineName,
Namespace: FakeChaosNameSpace,
Expand All @@ -33,39 +45,34 @@ func TestGetResultList(t *testing.T) {
EngineName: FakeEngineName,
},
},

isErr: false,
monitoring: &controller.MonitoringEnabled{
IsChaosResultsAvailable: true,
},
},
"Test Negative-1": {
chaosresult: &v1alpha1.ChaosResult{},
isErr: true,
{
name: "success:empty chaosResult",
chaosResult: &v1alpha1.ChaosResult{},
execFunc: func(client clients.ClientSets, chaosResult *v1alpha1.ChaosResult) {},
isErr: false,
monitoring: &controller.MonitoringEnabled{
IsChaosResultsAvailable: true,
},
},
"Test Negative-2": {
isErr: true,
monitoring: &controller.MonitoringEnabled{},
},
}

for name, mock := range tests {
t.Run(name, func(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := CreateFakeClient(t)
if !mock.isErr {
_, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosResults(mock.chaosresult.Namespace).Create(context.Background(), mock.chaosresult, metav1.CreateOptions{})
if err != nil {
t.Fatalf("chaosresult not created for %v test, err: %v", name, err)
}
}
tt.execFunc(client, tt.chaosResult)
resultDetails := &controller.ResultDetails{}
_, err := resultDetails.GetResultList(client, FakeChaosNameSpace, mock.monitoring)
if !mock.isErr && err != nil {
t.Fatalf("test Failed as not able to get the Chaos result list")
_, err := resultDetails.GetResultList(client, FakeChaosNameSpace, tt.monitoring)
if tt.isErr {
require.Error(t, err)
return
}

require.NoError(t, err)
})
}
}
Expand All @@ -82,8 +89,10 @@ func TestGetExperimentMetricsFromResult(t *testing.T) {
chaosresult *v1alpha1.ChaosResult
expectedVerdict bool
isErr bool
verdict bool
execFunc func(client clients.ClientSets, engine *v1alpha1.ChaosEngine, result *v1alpha1.ChaosResult)
}{
"Test Positive-1": {
"success": {
chaosengine: &v1alpha1.ChaosEngine{
ObjectMeta: metav1.ObjectMeta{
Name: FakeEngineName,
Expand Down Expand Up @@ -130,36 +139,42 @@ func TestGetExperimentMetricsFromResult(t *testing.T) {
History: &v1alpha1.HistoryDetails{},
},
},
isErr: false,

execFunc: func(client clients.ClientSets, engine *v1alpha1.ChaosEngine, result *v1alpha1.ChaosResult) {
_, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosEngines(engine.Namespace).Create(context.Background(), engine, metav1.CreateOptions{})
if err != nil {
t.Fatalf("engine not created for test, err: %v", err)
}

_, err = client.LitmusClient.LitmuschaosV1alpha1().ChaosResults(result.Namespace).Create(context.Background(), result, metav1.CreateOptions{})
if err != nil {
t.Fatalf("chaosresult not created fortest, err: %v", err)
}
},
isErr: false,
verdict: true,
},
"Test Negative-1": {
"failure: No Chaos Engine": {
chaosresult: &v1alpha1.ChaosResult{},
isErr: true,
isErr: false,
verdict: true,
execFunc: func(client clients.ClientSets, engine *v1alpha1.ChaosEngine, result *v1alpha1.ChaosResult) {
},
},
}

for name, mock := range tests {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {

client := CreateFakeClient(t)
resultDetails := &controller.ResultDetails{}
if !mock.isErr {
_, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosEngines(mock.chaosengine.Namespace).Create(context.Background(), mock.chaosengine, metav1.CreateOptions{})
if err != nil {
t.Fatalf("engine not created for %v test, err: %v", name, err)
}

_, err = client.LitmusClient.LitmuschaosV1alpha1().ChaosResults(mock.chaosresult.Namespace).Create(context.Background(), mock.chaosresult, metav1.CreateOptions{})
if err != nil {
t.Fatalf("chaosresult not created for %v test, err: %v", name, err)
}
}
var err error
_, err = resultDetails.GetExperimentMetricsFromResult(mock.chaosresult, client)
if !mock.isErr && err != nil {
t.Fatalf("Test %q failed: expected error to be nil", name)
tt.execFunc(client, tt.chaosengine, tt.chaosresult)
verdict, err := resultDetails.GetExperimentMetricsFromResult(tt.chaosresult, client)
assert.Equal(t, tt.verdict, verdict)
if tt.isErr {
require.Error(t, err)
return
}

require.NoError(t, err)
})
}
}
Expand Down
23 changes: 8 additions & 15 deletions tests/controller_test/scrap_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package controller_test

import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/litmuschaos/chaos-exporter/controller"
"github.com/litmuschaos/chaos-exporter/controller/mocks"
v1alpha1 "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"testing"
)

Expand All @@ -18,22 +16,16 @@ func TestGetLitmusChaosMetrics(t *testing.T) {

//FakeEngineName := "Fake Engine"
//FakeNamespace := "Fake Namespace"
//fakeServiceAcc := "Fake Service Account"
//fakeAppLabel := "Fake Label"
//FakeAppName := "Fake App"
//FakeClusterName := "Fake Cluster"

tests := []struct {
name string
execFunc func()
chaosengine *v1alpha1.ChaosEngine
chaosresult *v1alpha1.ChaosResult
isErr bool
monitoring *controller.MonitoringEnabled
overallChaosResult *v1alpha1.ChaosResultList
}{
{
name: "Test Positive-1",
name: "TestGetLitmusChaosMetrics_Success",
execFunc: func() {
mockCollectData.EXPECT().GetResultList(gomock.Any(), gomock.Any(), gomock.Any()).
Return(v1alpha1.ChaosResultList{
Expand All @@ -46,11 +38,10 @@ func TestGetLitmusChaosMetrics(t *testing.T) {
},
}, nil).Times(1)
mockCollectData.EXPECT().GetExperimentMetricsFromResult(gomock.Any(), gomock.Any()).Return(false, nil).Times(1)

os.Setenv("AWS_CLOUDWATCH_METRIC_NAMESPACE", "")
os.Setenv("CLUSTER_NAME", "")
os.Setenv("APP_NAME", "")
os.Setenv("WATCH_NAMESPACE", "")
mockCollectData.EXPECT().SetResultDetails()
mockCollectData.EXPECT().GetResultDetails().Return(controller.ChaosResultDetails{
UID: "FAKE-UID",
}).Times(1)
},
overallChaosResult: &v1alpha1.ChaosResultList{
Items: []v1alpha1.ChaosResult{
Expand All @@ -75,7 +66,9 @@ func TestGetLitmusChaosMetrics(t *testing.T) {

r.GaugeMetrics.InitializeGaugeMetrics().RegisterFixedMetrics()
err := r.GetLitmusChaosMetrics(client, tt.overallChaosResult, tt.monitoring)
fmt.Print(err)
if !tt.isErr && err != nil {
t.Fatalf("test Failed as not able to get the Chaos result list")
}
})
}

Expand Down

0 comments on commit c3ebd2c

Please sign in to comment.