Skip to content

Commit a478f1d

Browse files
committed
test: add testcase for util
Signed-off-by: qiuming520 <[email protected]>
1 parent afb3cab commit a478f1d

File tree

9 files changed

+1806
-0
lines changed

9 files changed

+1806
-0
lines changed

pkg/utils/convertpolicy/pod_test.go

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// nolint:dupl
2+
package convertpolicy
3+
4+
import (
5+
"testing"
6+
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
9+
kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
10+
)
11+
12+
func TestGetMatchPodConvertPolicy(t *testing.T) {
13+
t.Run("Test with empty policies", func(t *testing.T) {
14+
policies := kosmosv1alpha1.PodConvertPolicyList{}
15+
podLabels := map[string]string{
16+
"app": "test",
17+
}
18+
nodeLabels := map[string]string{
19+
"node": "test",
20+
}
21+
22+
matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels)
23+
if err != nil {
24+
t.Errorf("Expected no error, got %v", err)
25+
}
26+
if len(matched) != 0 {
27+
t.Errorf("Expected no matched policies, got %v", matched)
28+
}
29+
})
30+
31+
t.Run("Test with policies that do not match", func(t *testing.T) {
32+
policies := kosmosv1alpha1.PodConvertPolicyList{
33+
Items: []kosmosv1alpha1.PodConvertPolicy{
34+
{
35+
Spec: kosmosv1alpha1.PodConvertPolicySpec{
36+
LabelSelector: metav1.LabelSelector{
37+
MatchLabels: map[string]string{
38+
"app": "not-test",
39+
},
40+
},
41+
},
42+
},
43+
},
44+
}
45+
46+
podLabels := map[string]string{
47+
"app": "test",
48+
}
49+
nodeLabels := map[string]string{
50+
"node": "test",
51+
}
52+
matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels)
53+
54+
if err != nil {
55+
t.Errorf("Expected no error, got %v", err)
56+
}
57+
if len(matched) != 0 {
58+
t.Errorf("Expected no matched policies, got %v", matched)
59+
}
60+
})
61+
62+
t.Run("Test with policies that match", func(t *testing.T) {
63+
policies := kosmosv1alpha1.PodConvertPolicyList{
64+
Items: []kosmosv1alpha1.PodConvertPolicy{
65+
{
66+
Spec: kosmosv1alpha1.PodConvertPolicySpec{
67+
LabelSelector: metav1.LabelSelector{
68+
MatchLabels: map[string]string{
69+
"app": "test",
70+
},
71+
},
72+
},
73+
},
74+
},
75+
}
76+
77+
podLabels := map[string]string{
78+
"app": "test",
79+
}
80+
nodeLabels := map[string]string{
81+
"node": "test",
82+
}
83+
matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels)
84+
85+
if err != nil {
86+
t.Errorf("Expected no error, got %v", err)
87+
}
88+
if len(matched) != 1 {
89+
t.Errorf("Expected 1 matched policy, got %v", len(matched))
90+
}
91+
})
92+
}
93+
94+
func TestGetMatchClusterPodConvertPolicy(t *testing.T) {
95+
t.Run("Test with empty policies", func(t *testing.T) {
96+
policies := kosmosv1alpha1.ClusterPodConvertPolicyList{}
97+
podLabels := map[string]string{
98+
"app": "test",
99+
}
100+
nodeLabels := map[string]string{
101+
"node": "test",
102+
}
103+
104+
matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels)
105+
if err != nil {
106+
t.Errorf("Expected no error, got %v", err)
107+
}
108+
if len(matched) != 0 {
109+
t.Errorf("Expected no matched policies, got %v", matched)
110+
}
111+
})
112+
113+
t.Run("Test with policies that do not match", func(t *testing.T) {
114+
policies := kosmosv1alpha1.ClusterPodConvertPolicyList{
115+
Items: []kosmosv1alpha1.ClusterPodConvertPolicy{
116+
{
117+
Spec: kosmosv1alpha1.ClusterPodConvertPolicySpec{
118+
LabelSelector: metav1.LabelSelector{
119+
MatchLabels: map[string]string{"app": "not-test"},
120+
},
121+
},
122+
},
123+
},
124+
}
125+
126+
podLabels := map[string]string{
127+
"app": "test",
128+
}
129+
nodeLabels := map[string]string{
130+
"node": "test",
131+
}
132+
matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels)
133+
134+
if err != nil {
135+
t.Errorf("Expected no error, got %v", err)
136+
}
137+
if len(matched) != 0 {
138+
t.Errorf("Expected no matched policies, got %v", matched)
139+
}
140+
})
141+
142+
t.Run("Test with policies that match", func(t *testing.T) {
143+
policies := kosmosv1alpha1.ClusterPodConvertPolicyList{
144+
Items: []kosmosv1alpha1.ClusterPodConvertPolicy{
145+
{
146+
Spec: kosmosv1alpha1.ClusterPodConvertPolicySpec{
147+
LabelSelector: metav1.LabelSelector{
148+
MatchLabels: map[string]string{"app": "test"},
149+
},
150+
},
151+
},
152+
},
153+
}
154+
155+
podLabels := map[string]string{
156+
"app": "test",
157+
}
158+
nodeLabels := map[string]string{
159+
"node": "test",
160+
}
161+
matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels)
162+
163+
if err != nil {
164+
t.Errorf("Expected no error, got %v", err)
165+
}
166+
if len(matched) != 1 {
167+
t.Errorf("more than one matched policies, got %v", matched)
168+
}
169+
})
170+
}

pkg/utils/flags/backoffflag_test.go

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// nolint:dupl
2+
package flags
3+
4+
import (
5+
"testing"
6+
"time"
7+
8+
"github.com/spf13/pflag"
9+
)
10+
11+
func TestAddFlags(t *testing.T) {
12+
t.Run("AddFlags with default values", func(t *testing.T) {
13+
o := &BackoffOptions{}
14+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
15+
o.AddFlags(fs)
16+
17+
// Check default values
18+
if o.Duration != 5*time.Millisecond {
19+
t.Errorf("Expected default Duration to be 5ms, got %v", o.Duration)
20+
}
21+
if o.Factor != 1.0 {
22+
t.Errorf("Expected default Factor to be 1.0, got %v", o.Factor)
23+
}
24+
if o.Jitter != 0.1 {
25+
t.Errorf("Expected default Jitter to be 0.1, got %v", o.Jitter)
26+
}
27+
if o.Steps != 5 {
28+
t.Errorf("Expected default Steps to be 5, got %v", o.Steps)
29+
}
30+
})
31+
32+
t.Run("AddFlags with custom values", func(t *testing.T) {
33+
o := &BackoffOptions{}
34+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
35+
o.AddFlags(fs)
36+
37+
// Check setting values
38+
fs.Parse([]string{"--retry-duration=1s", "--retry-factor=2.0", "--retry-jitter=0.2", "--retry-steps=10"})
39+
if o.Duration != 1*time.Second {
40+
t.Errorf("Expected Duration to be 1s, got %v", o.Duration)
41+
}
42+
if o.Factor != 2.0 {
43+
t.Errorf("Expected Factor to be 2.0, got %v", o.Factor)
44+
}
45+
if o.Jitter != 0.2 {
46+
t.Errorf("Expected Jitter to be 0.2, got %v", o.Jitter)
47+
}
48+
if o.Steps != 10 {
49+
t.Errorf("Expected Steps to be 10, got %v", o.Steps)
50+
}
51+
})
52+
53+
t.Run("AddFlags with zero values", func(t *testing.T) {
54+
o := &BackoffOptions{}
55+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
56+
o.AddFlags(fs)
57+
58+
// Check setting values
59+
fs.Parse([]string{"--retry-duration=0", "--retry-factor=0.0", "--retry-jitter=0.0", "--retry-steps=0"})
60+
if o.Duration != 0 {
61+
t.Errorf("Expected Duration to be 0, got %v", o.Duration)
62+
}
63+
if o.Factor != 0.0 {
64+
t.Errorf("Expected Factor to be 0.0, got %v", o.Factor)
65+
}
66+
if o.Jitter != 0.0 {
67+
t.Errorf("Expected Jitter to be 0.0, got %v", o.Jitter)
68+
}
69+
if o.Steps != 0 {
70+
t.Errorf("Expected Steps to be 0, got %v", o.Steps)
71+
}
72+
})
73+
}
74+
75+
func TestDefaultUpdateRetryBackoff(t *testing.T) {
76+
t.Run("Test with zero values", func(t *testing.T) {
77+
opts := BackoffOptions{}
78+
backoff := DefaultUpdateRetryBackoff(opts)
79+
80+
if backoff.Duration != 5*time.Millisecond {
81+
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
82+
}
83+
if backoff.Factor != 1.0 {
84+
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
85+
}
86+
if backoff.Jitter != 0.1 {
87+
t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter)
88+
}
89+
if backoff.Steps != 5 {
90+
t.Errorf("Expected steps to be 5, got %v", backoff.Steps)
91+
}
92+
})
93+
94+
t.Run("Test with custom values", func(t *testing.T) {
95+
opts := BackoffOptions{
96+
Duration: 10 * time.Millisecond,
97+
Factor: 2.0,
98+
Jitter: 0.2,
99+
Steps: 10,
100+
}
101+
backoff := DefaultUpdateRetryBackoff(opts)
102+
103+
if backoff.Duration != 10*time.Millisecond {
104+
t.Errorf("Expected duration to be 10ms, got %v", backoff.Duration)
105+
}
106+
if backoff.Factor != 2.0 {
107+
t.Errorf("Expected factor to be 2.0, got %v", backoff.Factor)
108+
}
109+
if backoff.Jitter != 0.2 {
110+
t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter)
111+
}
112+
if backoff.Steps != 10 {
113+
t.Errorf("Expected steps to be 10, got %v", backoff.Steps)
114+
}
115+
})
116+
117+
t.Run("Test with negative values", func(t *testing.T) {
118+
opts := BackoffOptions{
119+
Duration: -1 * time.Millisecond,
120+
Factor: -2.0,
121+
Jitter: -0.2,
122+
Steps: -10,
123+
}
124+
backoff := DefaultUpdateRetryBackoff(opts)
125+
126+
if backoff.Duration != 5*time.Millisecond {
127+
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
128+
}
129+
if backoff.Factor != 1.0 {
130+
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
131+
}
132+
if backoff.Jitter != 0.1 {
133+
t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter)
134+
}
135+
if backoff.Steps != 5 {
136+
t.Errorf("Expected steps to be 5, got %v", backoff.Steps)
137+
}
138+
})
139+
140+
t.Run("Test with part values", func(t *testing.T) {
141+
opts := BackoffOptions{
142+
Jitter: 0.2,
143+
Steps: 10,
144+
}
145+
backoff := DefaultUpdateRetryBackoff(opts)
146+
147+
if backoff.Duration != 5*time.Millisecond {
148+
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
149+
}
150+
if backoff.Factor != 1.0 {
151+
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
152+
}
153+
if backoff.Jitter != 0.2 {
154+
t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter)
155+
}
156+
if backoff.Steps != 10 {
157+
t.Errorf("Expected steps to be 10, got %v", backoff.Steps)
158+
}
159+
})
160+
}

0 commit comments

Comments
 (0)