Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>
  • Loading branch information
Zbynek Roubalik committed Aug 17, 2021
1 parent 11e71a4 commit 5de3bb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
12 changes: 6 additions & 6 deletions pkg/provider/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p *KedaProvider) getMetricsWithFallback(scaler scalers.Scaler, metricName

initHealthStatus(status)
metrics, err := scaler.GetMetrics(context.TODO(), metricName, metricSelector)
healthStatus := getHealthStatus(scaledObject, metricName)
healthStatus := getHealthStatus(status, metricName)

if err == nil {
zero := int32(0)
Expand Down Expand Up @@ -104,18 +104,18 @@ func (p *KedaProvider) updateStatus(scaledObject *kedav1alpha1.ScaledObject, sta
}
}

func getHealthStatus(scaledObject *kedav1alpha1.ScaledObject, metricName string) *kedav1alpha1.HealthStatus {
func getHealthStatus(status *kedav1alpha1.ScaledObjectStatus, metricName string) *kedav1alpha1.HealthStatus {
// Get health status for a specific metric
_, healthStatusExists := scaledObject.Status.Health[metricName]
_, healthStatusExists := status.Health[metricName]
if !healthStatusExists {
zero := int32(0)
status := kedav1alpha1.HealthStatus{
healthStatus := kedav1alpha1.HealthStatus{
NumberOfFailures: &zero,
Status: kedav1alpha1.HealthStatusHappy,
}
scaledObject.Status.Health[metricName] = status
status.Health[metricName] = healthStatus
}
healthStatus := scaledObject.Status.Health[metricName]
healthStatus := status.Health[metricName]
return &healthStatus
}

Expand Down
31 changes: 16 additions & 15 deletions pkg/provider/fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (

"github.com/go-logr/logr"
"github.com/golang/mock/gomock"
kedav1alpha1 "github.com/kedacore/keda/v2/api/v1alpha1"
"github.com/kedacore/keda/v2/pkg/mock/mock_client"
mock_scalers "github.com/kedacore/keda/v2/pkg/mock/mock_scaler"
"github.com/kedacore/keda/v2/pkg/mock/mock_scaling"
"github.com/kubernetes-sigs/custom-metrics-apiserver/pkg/provider"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -20,6 +16,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/metrics/pkg/apis/external_metrics"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"

kedav1alpha1 "github.com/kedacore/keda/v2/api/v1alpha1"
"github.com/kedacore/keda/v2/pkg/mock/mock_client"
mock_scalers "github.com/kedacore/keda/v2/pkg/mock/mock_scaler"
"github.com/kedacore/keda/v2/pkg/mock/mock_scaling"
)

const metricName = "some_metric_name"
Expand Down Expand Up @@ -67,7 +68,7 @@ var _ = Describe("fallback", func() {
primeGetMetrics(scaler, expectedMetricValue)
so := buildScaledObject(nil, nil)
metricSpec := createMetricSpec(3)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

metrics, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)

Expand Down Expand Up @@ -97,7 +98,7 @@ var _ = Describe("fallback", func() {
)

metricSpec := createMetricSpec(3)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

metrics, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)

Expand All @@ -112,7 +113,7 @@ var _ = Describe("fallback", func() {

so := buildScaledObject(nil, nil)
metricSpec := createMetricSpec(3)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

_, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)

Expand Down Expand Up @@ -140,7 +141,7 @@ var _ = Describe("fallback", func() {
)

metricSpec := createMetricSpec(10)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

_, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)

Expand Down Expand Up @@ -169,7 +170,7 @@ var _ = Describe("fallback", func() {
},
)
metricSpec := createMetricSpec(10)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

metrics, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)

Expand Down Expand Up @@ -223,7 +224,7 @@ var _ = Describe("fallback", func() {
metricSpec := createMetricSpec(10)

statusWriter := mock_client.NewMockStatusWriter(ctrl)
statusWriter.EXPECT().Update(gomock.Any(), gomock.Any()).Return(errors.New("Some error"))
statusWriter.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("Some error"))
client.EXPECT().Status().Return(statusWriter)

metrics, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)
Expand Down Expand Up @@ -253,7 +254,7 @@ var _ = Describe("fallback", func() {
},
)
metricSpec := createMetricSpec(10)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

_, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)

Expand Down Expand Up @@ -286,7 +287,7 @@ var _ = Describe("fallback", func() {
},
)
metricSpec := createMetricSpec(10)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

_, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -319,7 +320,7 @@ var _ = Describe("fallback", func() {
},
)
metricSpec := createMetricSpec(10)
expectStatusUpdate(ctrl, client)
expectStatusPatch(ctrl, client)

_, err := providerUnderTest.getMetricsWithFallback(scaler, metricName, nil, so, metricSpec)
Expect(err).ShouldNot(BeNil())
Expand Down Expand Up @@ -365,9 +366,9 @@ func (h *healthStatusMatcher) NegatedFailureMessage(actual interface{}) (message
}
}

func expectStatusUpdate(ctrl *gomock.Controller, client *mock_client.MockClient) {
func expectStatusPatch(ctrl *gomock.Controller, client *mock_client.MockClient) {
statusWriter := mock_client.NewMockStatusWriter(ctrl)
statusWriter.EXPECT().Update(gomock.Any(), gomock.Any())
statusWriter.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any())
client.EXPECT().Status().Return(statusWriter)
}

Expand Down

0 comments on commit 5de3bb8

Please sign in to comment.