Skip to content

Commit eb6a37a

Browse files
Added unit tests for status and cronfederatedhpa helper utilities
Signed-off-by: Anuj Agrawal <[email protected]> Added unit tests for status and cronfederatedhpa helper utilities Signed-off-by: Anuj Agrawal <[email protected]> Added unit tests for status and cronfederatedhpa helper utilities Signed-off-by: Anuj Agrawal <[email protected]>
1 parent 057cf86 commit eb6a37a

File tree

2 files changed

+540
-0
lines changed

2 files changed

+540
-0
lines changed

Diff for: pkg/util/helper/cronfederatedhpa_test.go

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
Copyright 2024 The Karmada 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 helper
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/utils/ptr"
25+
26+
autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1"
27+
)
28+
29+
func TestIsCronFederatedHPARuleSuspend(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
rule autoscalingv1alpha1.CronFederatedHPARule
33+
expected bool
34+
}{
35+
{
36+
name: "suspend is nil",
37+
rule: autoscalingv1alpha1.CronFederatedHPARule{},
38+
expected: false,
39+
},
40+
{
41+
name: "suspend is true",
42+
rule: autoscalingv1alpha1.CronFederatedHPARule{
43+
Suspend: ptr.To(true),
44+
},
45+
expected: true,
46+
},
47+
{
48+
name: "suspend is false",
49+
rule: autoscalingv1alpha1.CronFederatedHPARule{
50+
Suspend: ptr.To(false),
51+
},
52+
expected: false,
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
result := IsCronFederatedHPARuleSuspend(tt.rule)
59+
assert.Equal(t, tt.expected, result)
60+
})
61+
}
62+
}
63+
64+
func TestGetCronFederatedHPASuccessHistoryLimits(t *testing.T) {
65+
tests := []struct {
66+
name string
67+
rule autoscalingv1alpha1.CronFederatedHPARule
68+
expected int
69+
}{
70+
{
71+
name: "successful history limit is nil",
72+
rule: autoscalingv1alpha1.CronFederatedHPARule{},
73+
expected: 3,
74+
},
75+
{
76+
name: "successful history limit is set to 5",
77+
rule: autoscalingv1alpha1.CronFederatedHPARule{
78+
SuccessfulHistoryLimit: ptr.To[int32](5),
79+
},
80+
expected: 5,
81+
},
82+
{
83+
name: "successful history limit is set to 0",
84+
rule: autoscalingv1alpha1.CronFederatedHPARule{
85+
SuccessfulHistoryLimit: ptr.To[int32](0),
86+
},
87+
expected: 0,
88+
},
89+
}
90+
91+
for _, tt := range tests {
92+
t.Run(tt.name, func(t *testing.T) {
93+
result := GetCronFederatedHPASuccessHistoryLimits(tt.rule)
94+
assert.Equal(t, tt.expected, result)
95+
})
96+
}
97+
}
98+
99+
func TestGetCronFederatedHPAFailedHistoryLimits(t *testing.T) {
100+
tests := []struct {
101+
name string
102+
rule autoscalingv1alpha1.CronFederatedHPARule
103+
expected int
104+
}{
105+
{
106+
name: "failed history limit is nil",
107+
rule: autoscalingv1alpha1.CronFederatedHPARule{},
108+
expected: 3,
109+
},
110+
{
111+
name: "failed history limit is set to 5",
112+
rule: autoscalingv1alpha1.CronFederatedHPARule{
113+
FailedHistoryLimit: ptr.To[int32](5),
114+
},
115+
expected: 5,
116+
},
117+
{
118+
name: "failed history limit is set to 0",
119+
rule: autoscalingv1alpha1.CronFederatedHPARule{
120+
FailedHistoryLimit: ptr.To[int32](0),
121+
},
122+
expected: 0,
123+
},
124+
}
125+
126+
for _, tt := range tests {
127+
t.Run(tt.name, func(t *testing.T) {
128+
result := GetCronFederatedHPAFailedHistoryLimits(tt.rule)
129+
assert.Equal(t, tt.expected, result)
130+
})
131+
}
132+
}
133+
134+
func TestGetCronFederatedHPAKey(t *testing.T) {
135+
tests := []struct {
136+
name string
137+
cronFHPA *autoscalingv1alpha1.CronFederatedHPA
138+
expected string
139+
}{
140+
{
141+
name: "default namespace",
142+
cronFHPA: &autoscalingv1alpha1.CronFederatedHPA{
143+
ObjectMeta: metav1.ObjectMeta{
144+
Name: "test-hpa",
145+
Namespace: "default",
146+
},
147+
},
148+
expected: "default/test-hpa",
149+
},
150+
{
151+
name: "custom namespace",
152+
cronFHPA: &autoscalingv1alpha1.CronFederatedHPA{
153+
ObjectMeta: metav1.ObjectMeta{
154+
Name: "custom-hpa",
155+
Namespace: "karmada-system",
156+
},
157+
},
158+
expected: "karmada-system/custom-hpa",
159+
},
160+
}
161+
162+
for _, tt := range tests {
163+
t.Run(tt.name, func(t *testing.T) {
164+
result := GetCronFederatedHPAKey(tt.cronFHPA)
165+
assert.Equal(t, tt.expected, result)
166+
})
167+
}
168+
}

0 commit comments

Comments
 (0)