Skip to content

Commit 9dc8e8b

Browse files
committed
test: add testcase for utils
Signed-off-by: qiuming520 <[email protected]>
1 parent 30ad591 commit 9dc8e8b

File tree

2 files changed

+1110
-0
lines changed

2 files changed

+1110
-0
lines changed

pkg/utils/calico_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package utils
2+
3+
import "testing"
4+
5+
// TestGetCIDRs tests the GetCIDRs function.
6+
func TestGetCIDRs(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
obj map[string]interface{}
10+
expected string
11+
}{
12+
{
13+
name: "nil input",
14+
obj: nil,
15+
expected: "",
16+
},
17+
{
18+
name: "empty map",
19+
obj: map[string]interface{}{},
20+
expected: "",
21+
},
22+
{
23+
name: "missing spec",
24+
obj: map[string]interface{}{
25+
"foo": "bar",
26+
},
27+
expected: "",
28+
},
29+
{
30+
name: "spec is not a map",
31+
obj: map[string]interface{}{
32+
"spec": "not-a-map",
33+
},
34+
expected: "",
35+
},
36+
{
37+
name: "spec without cidr",
38+
obj: map[string]interface{}{
39+
"spec": map[string]interface{}{
40+
"foo": "bar",
41+
},
42+
},
43+
expected: "",
44+
},
45+
{
46+
name: "spec with cidr",
47+
obj: map[string]interface{}{
48+
"spec": map[string]interface{}{
49+
"cidr": "192.168.1.0/24",
50+
},
51+
},
52+
expected: "192.168.1.0/24",
53+
},
54+
{
55+
name: "spec with cidr as empty string",
56+
obj: map[string]interface{}{
57+
"spec": map[string]interface{}{
58+
"cidr": "",
59+
},
60+
},
61+
expected: "",
62+
},
63+
{
64+
name: "cidr is not a string",
65+
obj: map[string]interface{}{
66+
"spec": map[string]interface{}{
67+
"cidr": 12345,
68+
},
69+
},
70+
expected: "",
71+
},
72+
}
73+
74+
for _, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
result := GetCIDRs(tt.obj)
77+
if result != tt.expected {
78+
t.Errorf("expected %q, got %q", tt.expected, result)
79+
}
80+
})
81+
}
82+
}

0 commit comments

Comments
 (0)