Skip to content

Commit c2df910

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

File tree

9 files changed

+1793
-0
lines changed

9 files changed

+1793
-0
lines changed

pkg/utils/convertpolicy/pod_test.go

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

pkg/utils/flags/backoffflag_test.go

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package flags
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/spf13/pflag"
8+
)
9+
10+
func TestAddFlags(t *testing.T) {
11+
t.Run("AddFlags with default values", func(t *testing.T) {
12+
o := &BackoffOptions{}
13+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
14+
o.AddFlags(fs)
15+
16+
// Check default values
17+
if o.Duration != 5*time.Millisecond {
18+
t.Errorf("Expected default Duration to be 5ms, got %v", o.Duration)
19+
}
20+
if o.Factor != 1.0 {
21+
t.Errorf("Expected default Factor to be 1.0, got %v", o.Factor)
22+
}
23+
if o.Jitter != 0.1 {
24+
t.Errorf("Expected default Jitter to be 0.1, got %v", o.Jitter)
25+
}
26+
if o.Steps != 5 {
27+
t.Errorf("Expected default Steps to be 5, got %v", o.Steps)
28+
}
29+
})
30+
31+
t.Run("AddFlags with custom values", func(t *testing.T) {
32+
o := &BackoffOptions{}
33+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
34+
o.AddFlags(fs)
35+
36+
// Check setting values
37+
fs.Parse([]string{"--retry-duration=1s", "--retry-factor=2.0", "--retry-jitter=0.2", "--retry-steps=10"})
38+
if o.Duration != 1*time.Second {
39+
t.Errorf("Expected Duration to be 1s, got %v", o.Duration)
40+
}
41+
if o.Factor != 2.0 {
42+
t.Errorf("Expected Factor to be 2.0, got %v", o.Factor)
43+
}
44+
if o.Jitter != 0.2 {
45+
t.Errorf("Expected Jitter to be 0.2, got %v", o.Jitter)
46+
}
47+
if o.Steps != 10 {
48+
t.Errorf("Expected Steps to be 10, got %v", o.Steps)
49+
}
50+
})
51+
52+
t.Run("AddFlags with zero values", func(t *testing.T) {
53+
o := &BackoffOptions{}
54+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
55+
o.AddFlags(fs)
56+
57+
// Check setting values
58+
fs.Parse([]string{"--retry-duration=0", "--retry-factor=0.0", "--retry-jitter=0.0", "--retry-steps=0"})
59+
if o.Duration != 0 {
60+
t.Errorf("Expected Duration to be 0, got %v", o.Duration)
61+
}
62+
if o.Factor != 0.0 {
63+
t.Errorf("Expected Factor to be 0.0, got %v", o.Factor)
64+
}
65+
if o.Jitter != 0.0 {
66+
t.Errorf("Expected Jitter to be 0.0, got %v", o.Jitter)
67+
}
68+
if o.Steps != 0 {
69+
t.Errorf("Expected Steps to be 0, got %v", o.Steps)
70+
}
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)