Skip to content

Commit 81e05e3

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

File tree

9 files changed

+1815
-0
lines changed

9 files changed

+1815
-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

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
err := fs.Parse([]string{"--retry-duration=1s", "--retry-factor=2.0", "--retry-jitter=0.2", "--retry-steps=10"})
39+
if err != nil {
40+
t.Errorf("Expected no error, but got %v", err)
41+
}
42+
43+
if o.Duration != 1*time.Second {
44+
t.Errorf("Expected Duration to be 1s, got %v", o.Duration)
45+
}
46+
if o.Factor != 2.0 {
47+
t.Errorf("Expected Factor to be 2.0, got %v", o.Factor)
48+
}
49+
if o.Jitter != 0.2 {
50+
t.Errorf("Expected Jitter to be 0.2, got %v", o.Jitter)
51+
}
52+
if o.Steps != 10 {
53+
t.Errorf("Expected Steps to be 10, got %v", o.Steps)
54+
}
55+
})
56+
57+
t.Run("AddFlags with zero values", func(t *testing.T) {
58+
o := &BackoffOptions{}
59+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
60+
o.AddFlags(fs)
61+
62+
// Check setting values
63+
err := fs.Parse([]string{"--retry-duration=0", "--retry-factor=0.0", "--retry-jitter=0.0", "--retry-steps=0"})
64+
if err != nil {
65+
t.Errorf("Expected no error, but got %v", err)
66+
}
67+
68+
if o.Duration != 0 {
69+
t.Errorf("Expected Duration to be 0, got %v", o.Duration)
70+
}
71+
if o.Factor != 0.0 {
72+
t.Errorf("Expected Factor to be 0.0, got %v", o.Factor)
73+
}
74+
if o.Jitter != 0.0 {
75+
t.Errorf("Expected Jitter to be 0.0, got %v", o.Jitter)
76+
}
77+
if o.Steps != 0 {
78+
t.Errorf("Expected Steps to be 0, got %v", o.Steps)
79+
}
80+
})
81+
}
82+
83+
func TestDefaultUpdateRetryBackoff(t *testing.T) {
84+
t.Run("Test with zero values", func(t *testing.T) {
85+
opts := BackoffOptions{}
86+
backoff := DefaultUpdateRetryBackoff(opts)
87+
88+
if backoff.Duration != 5*time.Millisecond {
89+
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
90+
}
91+
if backoff.Factor != 1.0 {
92+
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
93+
}
94+
if backoff.Jitter != 0.1 {
95+
t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter)
96+
}
97+
if backoff.Steps != 5 {
98+
t.Errorf("Expected steps to be 5, got %v", backoff.Steps)
99+
}
100+
})
101+
102+
t.Run("Test with custom values", func(t *testing.T) {
103+
opts := BackoffOptions{
104+
Duration: 10 * time.Millisecond,
105+
Factor: 2.0,
106+
Jitter: 0.2,
107+
Steps: 10,
108+
}
109+
backoff := DefaultUpdateRetryBackoff(opts)
110+
111+
if backoff.Duration != 10*time.Millisecond {
112+
t.Errorf("Expected duration to be 10ms, got %v", backoff.Duration)
113+
}
114+
if backoff.Factor != 2.0 {
115+
t.Errorf("Expected factor to be 2.0, got %v", backoff.Factor)
116+
}
117+
if backoff.Jitter != 0.2 {
118+
t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter)
119+
}
120+
if backoff.Steps != 10 {
121+
t.Errorf("Expected steps to be 10, got %v", backoff.Steps)
122+
}
123+
})
124+
125+
t.Run("Test with negative values", func(t *testing.T) {
126+
opts := BackoffOptions{
127+
Duration: -1 * time.Millisecond,
128+
Factor: -2.0,
129+
Jitter: -0.2,
130+
Steps: -10,
131+
}
132+
backoff := DefaultUpdateRetryBackoff(opts)
133+
134+
if backoff.Duration != 5*time.Millisecond {
135+
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
136+
}
137+
if backoff.Factor != 1.0 {
138+
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
139+
}
140+
if backoff.Jitter != 0.1 {
141+
t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter)
142+
}
143+
if backoff.Steps != 5 {
144+
t.Errorf("Expected steps to be 5, got %v", backoff.Steps)
145+
}
146+
})
147+
148+
t.Run("Test with part values", func(t *testing.T) {
149+
opts := BackoffOptions{
150+
Jitter: 0.2,
151+
Steps: 10,
152+
}
153+
backoff := DefaultUpdateRetryBackoff(opts)
154+
155+
if backoff.Duration != 5*time.Millisecond {
156+
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
157+
}
158+
if backoff.Factor != 1.0 {
159+
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
160+
}
161+
if backoff.Jitter != 0.2 {
162+
t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter)
163+
}
164+
if backoff.Steps != 10 {
165+
t.Errorf("Expected steps to be 10, got %v", backoff.Steps)
166+
}
167+
})
168+
}

0 commit comments

Comments
 (0)