From c3ebd2c407e610638751040a501e0ac9df722c18 Mon Sep 17 00:00:00 2001 From: nagesh bansal Date: Wed, 5 Jul 2023 12:43:20 +0530 Subject: [PATCH] Clean up tests Signed-off-by: nagesh bansal --- go.mod | 5 +- tests/controller_test/collect-data_test.go | 105 ++++++++++++--------- tests/controller_test/scrap_test.go | 23 ++--- 3 files changed, 70 insertions(+), 63 deletions(-) diff --git a/go.mod b/go.mod index e51b578..6ea59d9 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/tests/controller_test/collect-data_test.go b/tests/controller_test/collect-data_test.go index 4a2e255..7308b77 100644 --- a/tests/controller_test/collect-data_test.go +++ b/tests/controller_test/collect-data_test.go @@ -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" @@ -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, @@ -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) }) } } @@ -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, @@ -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) }) } } diff --git a/tests/controller_test/scrap_test.go b/tests/controller_test/scrap_test.go index 8212922..f0675d5 100644 --- a/tests/controller_test/scrap_test.go +++ b/tests/controller_test/scrap_test.go @@ -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" ) @@ -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{ @@ -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{ @@ -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") + } }) }