forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaledobject_controller_test.go
179 lines (145 loc) · 5.69 KB
/
scaledobject_controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package controllers
import (
"fmt"
"github.com/golang/mock/gomock"
kedav1alpha1 "github.com/kedacore/keda/v2/api/v1alpha1"
"github.com/kedacore/keda/v2/pkg/mock/mock_client"
"github.com/kedacore/keda/v2/pkg/mock/mock_scaling"
"github.com/kedacore/keda/v2/pkg/scalers"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
type GinkgoTestReporter struct{}
func (g GinkgoTestReporter) Errorf(format string, args ...interface{}) {
Fail(fmt.Sprintf(format, args...))
}
func (g GinkgoTestReporter) Fatalf(format string, args ...interface{}) {
Fail(fmt.Sprintf(format, args...))
}
var _ = Describe("ScaledObjectController", func() {
var (
testLogger = zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter))
)
Describe("Metric Names", func() {
var (
metricNameTestReconciler ScaledObjectReconciler
mockScaleHandler *mock_scaling.MockScaleHandler
mockClient *mock_client.MockClient
mockStatusWriter *mock_client.MockStatusWriter
)
var triggerMeta []map[string]string = []map[string]string{
{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total", "threshold": "100", "query": "up", "disableScaleToZero": "true"},
{"serverAddress": "http://localhost:9090", "metricName": "http_requests_total2", "threshold": "100", "query": "up"},
}
BeforeEach(func() {
ctrl := gomock.NewController(GinkgoTestReporter{})
mockScaleHandler = mock_scaling.NewMockScaleHandler(ctrl)
mockClient = mock_client.NewMockClient(ctrl)
mockStatusWriter = mock_client.NewMockStatusWriter(ctrl)
metricNameTestReconciler = ScaledObjectReconciler{
scaleHandler: mockScaleHandler,
Client: mockClient,
}
})
Context("With Unique Values", func() {
var uniquelyNamedScaledObject = &kedav1alpha1.ScaledObject{}
It("should pass metric name validation", func() {
// Generate test data
testScalers := make([]scalers.Scaler, 0)
expectedExternalMetricNames := make([]string, 0)
for i, tm := range triggerMeta {
config := &scalers.ScalerConfig{
Name: fmt.Sprintf("test.%d", i),
Namespace: "test",
TriggerMetadata: tm,
ResolvedEnv: nil,
AuthParams: nil,
}
s, err := scalers.NewPrometheusScaler(config)
if err != nil {
Fail(err.Error())
}
testScalers = append(testScalers, s)
for _, metricSpec := range s.GetMetricSpecForScaling() {
if metricSpec.External != nil {
expectedExternalMetricNames = append(expectedExternalMetricNames, metricSpec.External.Metric.Name)
}
}
}
// Set up expectations
mockScaleHandler.EXPECT().GetScalers(uniquelyNamedScaledObject).Return(testScalers, nil)
mockClient.EXPECT().Status().Return(mockStatusWriter)
mockStatusWriter.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any())
// Call function to be tested
metricSpecs, err := metricNameTestReconciler.getScaledObjectMetricSpecs(testLogger, uniquelyNamedScaledObject)
// Test that the status was updated with metric names
Ω(uniquelyNamedScaledObject.Status.ExternalMetricNames).Should(Equal(expectedExternalMetricNames))
// Test returned values
Ω(len(metricSpecs)).Should(Equal(len(testScalers)))
Ω(err).Should(BeNil())
})
It("should pass metric name validation with single value", func() {
// Generate test data
expectedExternalMetricNames := make([]string, 0)
config := &scalers.ScalerConfig{
Name: "test",
Namespace: "test",
TriggerMetadata: triggerMeta[0],
ResolvedEnv: nil,
AuthParams: nil,
}
s, err := scalers.NewPrometheusScaler(config)
if err != nil {
Fail(err.Error())
}
for _, metricSpec := range s.GetMetricSpecForScaling() {
if metricSpec.External != nil {
expectedExternalMetricNames = append(expectedExternalMetricNames, metricSpec.External.Metric.Name)
}
}
// Set up expectations
mockScaleHandler.EXPECT().GetScalers(uniquelyNamedScaledObject).Return([]scalers.Scaler{s}, nil)
mockClient.EXPECT().Status().Return(mockStatusWriter)
mockStatusWriter.EXPECT().Patch(gomock.Any(), gomock.Any(), gomock.Any())
// Call function to be tested
metricSpecs, err := metricNameTestReconciler.getScaledObjectMetricSpecs(testLogger, uniquelyNamedScaledObject)
// Test that the status was updated
Ω(uniquelyNamedScaledObject.Status.ExternalMetricNames).Should(Equal(expectedExternalMetricNames))
// Test returned values
Ω(len(metricSpecs)).Should(Equal(1))
Ω(err).Should(BeNil())
})
})
Context("With Duplicate Values", func() {
var duplicateNamedScaledObject = &kedav1alpha1.ScaledObject{}
It("should pass metric name validation", func() {
// Generate test data
testScalers := make([]scalers.Scaler, 0)
for i := 0; i < 4; i++ {
config := &scalers.ScalerConfig{
Name: fmt.Sprintf("test.%d", i),
Namespace: "test",
TriggerMetadata: triggerMeta[0],
ResolvedEnv: nil,
AuthParams: nil,
}
s, err := scalers.NewPrometheusScaler(config)
if err != nil {
Fail(err.Error())
}
testScalers = append(testScalers, s)
}
// Set up expectations
mockScaleHandler.EXPECT().GetScalers(duplicateNamedScaledObject).Return(testScalers, nil)
// Call function tobe tested
metricSpecs, err := metricNameTestReconciler.getScaledObjectMetricSpecs(testLogger, duplicateNamedScaledObject)
// Test that the status was not updated
Ω(duplicateNamedScaledObject.Status.ExternalMetricNames).Should(BeNil())
// Test returned values
Ω(metricSpecs).Should(BeNil())
Ω(err).ShouldNot(BeNil())
})
})
})
})