Skip to content

Commit

Permalink
Add tests for GetLitmusChaosMetrics
Browse files Browse the repository at this point in the history
Signed-off-by: nagesh bansal <[email protected]>
  • Loading branch information
Nageshbansal committed Jun 27, 2023
1 parent 8fa3066 commit f6fa308
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 99 deletions.
12 changes: 7 additions & 5 deletions controller/collect-data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
clientTypes "k8s.io/apimachinery/pkg/types"
)

// interface for the both functions GetResultList and getExperimentMetricsFromResult for being dependent on the clientsets
//go:generate mockgen -destination=mocks/mock_collect-data.go -package=mocks github.com/litmuschaos/chaos-exporter/controller CollectResult

// CollectResult interface for the both functions GetResultList and getExperimentMetricsFromResult
type CollectResult interface {
GetResultList(chaosNamespace string, monitoringEnabled *MonitoringEnabled) (litmuschaosv1alpha1.ChaosResultList, error)
getExperimentMetricsFromResult(chaosResult *litmuschaosv1alpha1.ChaosResult, clients clients.ClientSets) (bool, error)
GetResultList(clients clients.ClientSets, chaosNamespace string, monitoringEnabled *MonitoringEnabled) (litmuschaosv1alpha1.ChaosResultList, error)
GetExperimentMetricsFromResult(chaosResult *litmuschaosv1alpha1.ChaosResult, clients clients.ClientSets) (bool, error)
}

// GetResultList return the result list correspond to the monitoring enabled chaosengine
Expand Down Expand Up @@ -47,8 +49,8 @@ func GetResultList(clients clients.ClientSets, chaosNamespace string, monitoring
return *chaosResultList, nil
}

// getExperimentMetricsFromResult derive all the metrics data from the chaosresult and set into resultDetails struct
func (resultDetails *ChaosResultDetails) getExperimentMetricsFromResult(chaosResult *litmuschaosv1alpha1.ChaosResult, clients clients.ClientSets) (bool, error) {
// GetExperimentMetricsFromResult derive all the metrics data from the chaosresult and set into resultDetails struct
func (resultDetails *ChaosResultDetails) GetExperimentMetricsFromResult(chaosResult *litmuschaosv1alpha1.ChaosResult, clients clients.ClientSets) (bool, error) {
verdict := strings.ToLower(string(chaosResult.Status.ExperimentStatus.Verdict))
probeSuccesPercentage, err := getProbeSuccessPercentage(chaosResult)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func Exporter(clients clients.ClientSets) {
// Register the fixed (count) chaos metrics
log.Info("Registering Fixed Metrics")

gaugeMetrics := GaugeMetrics{}
r := ResultDetails{}
//gaugeMetrics := GaugeMetrics{}
overallChaosResults := litmuschaosv1alpha1.ChaosResultList{}

gaugeMetrics.InitializeGaugeMetrics().
r.GaugeMetrics.InitializeGaugeMetrics().
RegisterFixedMetrics()

monitoringEnabled := MonitoringEnabled{
Expand All @@ -43,7 +44,7 @@ func Exporter(clients clients.ClientSets) {
}

for {
if err := gaugeMetrics.GetLitmusChaosMetrics(clients, &overallChaosResults, &monitoringEnabled); err != nil {
if err := r.GetLitmusChaosMetrics(clients, &overallChaosResults, &monitoringEnabled); err != nil {
log.Errorf("err: %v", err)
}
time.Sleep(1000 * time.Millisecond)
Expand Down
67 changes: 67 additions & 0 deletions controller/mocks/mock_collect-data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 0 additions & 71 deletions controller/scrap_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions controller/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
var err error

// GetLitmusChaosMetrics derive and send the chaos metrics
func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSets, overallChaosResults *litmuschaosv1alpha1.ChaosResultList, monitoringEnabled *MonitoringEnabled) error {
func (r *ResultDetails) GetLitmusChaosMetrics(clients clients.ClientSets, overallChaosResults *litmuschaosv1alpha1.ChaosResultList, monitoringEnabled *MonitoringEnabled) error {
engineCount := 0

// initialising the parameters for the namespaced scope metrics
Expand All @@ -53,13 +53,13 @@ func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSe
}
watchNamespace := os.Getenv("WATCH_NAMESPACE")
// Getting list of all the chaosresults for the monitoring
resultList, err := GetResultList(clients, watchNamespace, monitoringEnabled)
resultList, err := r.CollectResult.GetResultList(clients, watchNamespace, monitoringEnabled)
if err != nil {
return err
}

// unset the metrics correspond to deleted chaosresults
gaugeMetrics.unsetDeletedChaosResults(overallChaosResults, &resultList)
r.GaugeMetrics.unsetDeletedChaosResults(overallChaosResults, &resultList)
// updating the overall chaosresults items to latest
overallChaosResults.Items = resultList.Items

Expand All @@ -75,7 +75,7 @@ func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSe
}

// deriving metrics data from the chaosresult
skip, err := resultDetails.getExperimentMetricsFromResult(&chaosresult, clients)
skip, err := r.CollectResult.GetExperimentMetricsFromResult(&chaosresult, clients)
if err != nil {
return err
}
Expand Down Expand Up @@ -110,8 +110,8 @@ func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSe
})

// setting chaosresult metrics for the given chaosresult
verdictValue := gaugeMetrics.unsetOutdatedMetrics(resultDetails)
gaugeMetrics.setResultChaosMetrics(resultDetails, verdictValue)
verdictValue := r.GaugeMetrics.unsetOutdatedMetrics(resultDetails)
r.GaugeMetrics.setResultChaosMetrics(resultDetails, verdictValue)
// setting chaosresult aws metrics for the given chaosresult, which can be used for cloudwatch
if awsConfig.Namespace != "" && awsConfig.ClusterName != "" && awsConfig.Service != "" {
awsConfig.setAwsResultChaosMetrics(resultDetails)
Expand All @@ -128,7 +128,7 @@ func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSe
}

//setting aggregate metrics from the all chaosresults
gaugeMetrics.setNamespacedChaosMetrics(namespacedScopeMetrics, watchNamespace)
r.GaugeMetrics.setNamespacedChaosMetrics(namespacedScopeMetrics, watchNamespace)
//setting aggregate aws metrics from the all chaosresults, which can be used for cloudwatch
if awsConfig.Namespace != "" && awsConfig.ClusterName != "" && awsConfig.Service != "" {
awsConfig.setAwsNamespacedChaosMetrics(namespacedScopeMetrics)
Expand Down
4 changes: 4 additions & 0 deletions controller/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ type GaugeMetrics struct {
ClusterScopedExperimentsInstalledCount *prometheus.GaugeVec
ClusterScopedExperimentsRunCount *prometheus.GaugeVec
}
type ResultDetails struct {
CollectResult CollectResult
GaugeMetrics GaugeMetrics
}

// MonitoringEnabled contains existence/availability of chaosEngines and chaosResults
type MonitoringEnabled struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package controller
package controller_test

import (
"context"
"github.com/litmuschaos/chaos-exporter/controller"
"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"
Expand All @@ -18,7 +19,7 @@ func TestGetResultList(t *testing.T) {

tests := map[string]struct {
chaosresult *v1alpha1.ChaosResult
monitoring *MonitoringEnabled
monitoring *controller.MonitoringEnabled
isErr bool
}{
"Test Positive-1": {
Expand All @@ -33,37 +34,36 @@ func TestGetResultList(t *testing.T) {
},
},
isErr: false,
monitoring: &MonitoringEnabled{
monitoring: &controller.MonitoringEnabled{
IsChaosResultsAvailable: true,
},
},
"Test Negative-1": {
chaosresult: &v1alpha1.ChaosResult{},
isErr: true,
monitoring: &MonitoringEnabled{
monitoring: &controller.MonitoringEnabled{
IsChaosResultsAvailable: true,
},
},
"Test Negative-2": {
isErr: true,
monitoring: &MonitoringEnabled{},
monitoring: &controller.MonitoringEnabled{},
},
}

for name, mock := range tests {
t.Run(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{})
_, 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)
}
}
_, err = GetResultList(client, FakeChaosNameSpace, mock.monitoring)
//if !mock.isErr && err != nil && mock.chaosresultlist != resultList {
// t.Fatalf("test Failed as not able to get the Chaos result list")
//}
_, err := controller.GetResultList(client, FakeChaosNameSpace, mock.monitoring)
if !mock.isErr && err != nil {
t.Fatalf("test Failed as not able to get the Chaos result list")
}

})
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestGetExperimentMetricsFromResult(t *testing.T) {
t.Run(name, func(t *testing.T) {

client := CreateFakeClient(t)
resultDetails := &ChaosResultDetails{}
resultDetails := &controller.ChaosResultDetails{}
if !mock.isErr {
_, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosEngines(mock.chaosengine.Namespace).Create(context.Background(), mock.chaosengine, metav1.CreateOptions{})
if err != nil {
Expand All @@ -153,7 +153,8 @@ func TestGetExperimentMetricsFromResult(t *testing.T) {
t.Fatalf("chaosresult not created for %v test, err: %v", name, err)
}
}
_, err = resultDetails.getExperimentMetricsFromResult(mock.chaosresult, client)
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)
}
Expand Down
Loading

0 comments on commit f6fa308

Please sign in to comment.