Skip to content

Commit 019d09c

Browse files
committed
Add unit test
Signed-off-by: Rick Brouwer <[email protected]>
1 parent bb97d40 commit 019d09c

File tree

1 file changed

+296
-0
lines changed

1 file changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
/*
2+
Copyright 2023 The KEDA Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
autoscalingv2 "k8s.io/api/autoscaling/v2"
24+
)
25+
26+
func TestCheckFallbackValid(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
scaledObject *ScaledObject
30+
expectedError bool
31+
errorContains string
32+
}{
33+
{
34+
name: "No fallback configured",
35+
scaledObject: &ScaledObject{
36+
Spec: ScaledObjectSpec{
37+
Fallback: nil,
38+
Triggers: []ScaleTriggers{
39+
{
40+
Type: "couchdb",
41+
},
42+
},
43+
},
44+
},
45+
expectedError: false,
46+
},
47+
{
48+
name: "Explicit AverageValue metricType - valid",
49+
scaledObject: &ScaledObject{
50+
Spec: ScaledObjectSpec{
51+
Fallback: &Fallback{
52+
FailureThreshold: 3,
53+
Replicas: 1,
54+
},
55+
Triggers: []ScaleTriggers{
56+
{
57+
Type: "couchdb",
58+
MetricType: autoscalingv2.AverageValueMetricType,
59+
},
60+
},
61+
},
62+
},
63+
expectedError: false,
64+
},
65+
{
66+
name: "Implicit AverageValue metricType (empty string) - valid",
67+
scaledObject: &ScaledObject{
68+
Spec: ScaledObjectSpec{
69+
Fallback: &Fallback{
70+
FailureThreshold: 3,
71+
Replicas: 1,
72+
},
73+
Triggers: []ScaleTriggers{
74+
{
75+
Type: "couchdb",
76+
MetricType: "", // Empty string should default to AverageValue
77+
},
78+
},
79+
},
80+
},
81+
expectedError: false,
82+
},
83+
{
84+
name: "Value metricType - invalid",
85+
scaledObject: &ScaledObject{
86+
Spec: ScaledObjectSpec{
87+
Fallback: &Fallback{
88+
FailureThreshold: 3,
89+
Replicas: 1,
90+
},
91+
Triggers: []ScaleTriggers{
92+
{
93+
Type: "couchdb",
94+
MetricType: autoscalingv2.ValueMetricType,
95+
},
96+
},
97+
},
98+
},
99+
expectedError: true,
100+
errorContains: "type for the fallback to be enabled",
101+
},
102+
{
103+
name: "Multiple triggers with one valid AverageValue - valid",
104+
scaledObject: &ScaledObject{
105+
Spec: ScaledObjectSpec{
106+
Fallback: &Fallback{
107+
FailureThreshold: 3,
108+
Replicas: 1,
109+
},
110+
Triggers: []ScaleTriggers{
111+
{
112+
Type: "prometheus",
113+
MetricType: autoscalingv2.ValueMetricType,
114+
},
115+
{
116+
Type: "couchdb",
117+
MetricType: autoscalingv2.AverageValueMetricType,
118+
},
119+
},
120+
},
121+
},
122+
expectedError: false,
123+
},
124+
{
125+
name: "CPU trigger - invalid",
126+
scaledObject: &ScaledObject{
127+
Spec: ScaledObjectSpec{
128+
Fallback: &Fallback{
129+
FailureThreshold: 3,
130+
Replicas: 1,
131+
},
132+
Triggers: []ScaleTriggers{
133+
{
134+
Type: "cpu",
135+
MetricType: autoscalingv2.AverageValueMetricType,
136+
},
137+
},
138+
},
139+
},
140+
expectedError: true,
141+
errorContains: "type for the fallback to be enabled",
142+
},
143+
{
144+
name: "Memory trigger - invalid",
145+
scaledObject: &ScaledObject{
146+
Spec: ScaledObjectSpec{
147+
Fallback: &Fallback{
148+
FailureThreshold: 3,
149+
Replicas: 1,
150+
},
151+
Triggers: []ScaleTriggers{
152+
{
153+
Type: "memory",
154+
MetricType: autoscalingv2.AverageValueMetricType,
155+
},
156+
},
157+
},
158+
},
159+
expectedError: true,
160+
errorContains: "type for the fallback to be enabled",
161+
},
162+
{
163+
name: "Multiple triggers with one CPU and one valid - valid",
164+
scaledObject: &ScaledObject{
165+
Spec: ScaledObjectSpec{
166+
Fallback: &Fallback{
167+
FailureThreshold: 3,
168+
Replicas: 1,
169+
},
170+
Triggers: []ScaleTriggers{
171+
{
172+
Type: "cpu",
173+
MetricType: autoscalingv2.UtilizationMetricType,
174+
},
175+
{
176+
Type: "couchdb",
177+
MetricType: autoscalingv2.AverageValueMetricType,
178+
},
179+
},
180+
},
181+
},
182+
expectedError: false,
183+
},
184+
{
185+
name: "Negative FailureThreshold - invalid",
186+
scaledObject: &ScaledObject{
187+
Spec: ScaledObjectSpec{
188+
Fallback: &Fallback{
189+
FailureThreshold: -1,
190+
Replicas: 1,
191+
},
192+
Triggers: []ScaleTriggers{
193+
{
194+
Type: "couchdb",
195+
MetricType: autoscalingv2.AverageValueMetricType,
196+
},
197+
},
198+
},
199+
},
200+
expectedError: true,
201+
errorContains: "must both be greater than or equal to 0",
202+
},
203+
{
204+
name: "Negative Replicas - invalid",
205+
scaledObject: &ScaledObject{
206+
Spec: ScaledObjectSpec{
207+
Fallback: &Fallback{
208+
FailureThreshold: 3,
209+
Replicas: -1,
210+
},
211+
Triggers: []ScaleTriggers{
212+
{
213+
Type: "couchdb",
214+
MetricType: autoscalingv2.AverageValueMetricType,
215+
},
216+
},
217+
},
218+
},
219+
expectedError: true,
220+
errorContains: "must both be greater than or equal to 0",
221+
},
222+
{
223+
name: "Using ScalingModifiers with AverageValue MetricType - valid",
224+
scaledObject: &ScaledObject{
225+
Spec: ScaledObjectSpec{
226+
Fallback: &Fallback{
227+
FailureThreshold: 3,
228+
Replicas: 1,
229+
},
230+
Advanced: &AdvancedConfig{
231+
ScalingModifiers: ScalingModifiers{
232+
MetricType: autoscalingv2.AverageValueMetricType,
233+
Formula: "x * 2",
234+
},
235+
},
236+
Triggers: []ScaleTriggers{
237+
{
238+
Type: "couchdb",
239+
},
240+
},
241+
},
242+
},
243+
expectedError: false,
244+
},
245+
{
246+
name: "Using ScalingModifiers with Value MetricType - invalid",
247+
scaledObject: &ScaledObject{
248+
Spec: ScaledObjectSpec{
249+
Fallback: &Fallback{
250+
FailureThreshold: 3,
251+
Replicas: 1,
252+
},
253+
Advanced: &AdvancedConfig{
254+
ScalingModifiers: ScalingModifiers{
255+
MetricType: autoscalingv2.ValueMetricType,
256+
Formula: "x * 2",
257+
},
258+
},
259+
Triggers: []ScaleTriggers{
260+
{
261+
Type: "couchdb",
262+
},
263+
},
264+
},
265+
},
266+
expectedError: true,
267+
errorContains: "ScaledObject.Spec.Advanced.ScalingModifiers.MetricType must be AverageValue",
268+
},
269+
}
270+
271+
for _, test := range tests {
272+
t.Run(test.name, func(t *testing.T) {
273+
err := CheckFallbackValid(test.scaledObject)
274+
275+
if test.expectedError && err == nil {
276+
t.Error("Expected error but got nil")
277+
}
278+
279+
if !test.expectedError && err != nil {
280+
t.Errorf("Expected no error but got: %v", err)
281+
}
282+
283+
if test.expectedError && err != nil && test.errorContains != "" {
284+
if !contains(err.Error(), test.errorContains) {
285+
t.Errorf("Error message does not contain expected text.\nExpected to contain: %s\nActual: %s",
286+
test.errorContains, err.Error())
287+
}
288+
}
289+
})
290+
}
291+
}
292+
293+
// Helper function to check if a string contains a substring
294+
func contains(s, substr string) bool {
295+
return strings.Contains(s, substr)
296+
}

0 commit comments

Comments
 (0)