Skip to content

Commit

Permalink
add unit test for net.go (#4659)
Browse files Browse the repository at this point in the history
Signed-off-by: zcq98 <[email protected]>
  • Loading branch information
zcq98 authored Oct 25, 2024
1 parent 8e9f947 commit 44cd6e5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
40 changes: 35 additions & 5 deletions pkg/util/arp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,24 @@ func TestArpResolve(t *testing.T) {
var defaultGW string
var nicIndex int
for _, r := range routes {
if r.Dst != nil && r.Dst.IP.String() == "0.0.0.0" {
if r.Dst == nil || r.Dst.IP.String() == "0.0.0.0" {
defaultGW = r.Gw.String()
nicIndex = r.LinkIndex
}
}
if defaultGW == "" {
t.Fatalf("failed to get default gateway")
return
}
if nicIndex == 0 {
t.Fatalf("failed to get nic")
return
}

link, err := netlink.LinkByIndex(nicIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
return
}
maxRetry := 3
done := make(chan struct{})
Expand Down Expand Up @@ -140,9 +143,8 @@ func TestDetectIPConflict(t *testing.T) {
var nicIndex int
var inMac, outMac net.HardwareAddr
for _, r := range routes {
if r.Dst != nil && r.Src != nil && r.Dst.IP.String() == "0.0.0.0" {
if r.Dst == nil || r.Dst.IP.String() == "0.0.0.0" {
nicIndex = r.LinkIndex
validIP = r.Src.String()
}
}

Expand All @@ -154,7 +156,21 @@ func TestDetectIPConflict(t *testing.T) {
link, err := netlink.LinkByIndex(nicIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
return
}

addrs, err := netlink.AddrList(link, unix.AF_INET)
if err != nil {
t.Fatalf("Failed to get addresses: %v", err)
return
}

if len(addrs) > 0 {
validIP = addrs[0].IP.String()
} else {
return
}

linkName := link.Attrs().Name
inMac = link.Attrs().HardwareAddr
outMac, err = ArpDetectIPConflict(linkName, validIP, inMac)
Expand Down Expand Up @@ -215,19 +231,33 @@ func TestAnnounceArpAddress(t *testing.T) {
var nicIndex int
var inMac net.HardwareAddr
for _, r := range routes {
if r.Dst != nil && r.Dst.IP.String() == "0.0.0.0" {
if r.Dst == nil || r.Dst.IP.String() == "0.0.0.0" {
nicIndex = r.LinkIndex
validIP = r.Src.String()
}
}
if nicIndex == 0 {
t.Fatalf("failed to get nic")
return
}

link, err := netlink.LinkByIndex(nicIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
return
}

addrs, err := netlink.AddrList(link, unix.AF_INET)
if err != nil {
t.Fatalf("Failed to get addresses: %v", err)
return
}

if len(addrs) > 0 {
validIP = addrs[0].IP.String()
} else {
return
}

maxRetry := 1
linkName := link.Attrs().Name
inMac = link.Attrs().HardwareAddr
Expand Down
44 changes: 40 additions & 4 deletions pkg/util/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ func TestGetNodeInternalIP(t *testing.T) {
}
}

func TestGetPodIPs(t *testing.T) {
func TestPodIPs(t *testing.T) {
tests := []struct {
name string
pod v1.Pod
exp []string
}{
{
name: "pod_with_one_pod_ip",
name: "pod_with_one_pod_ipv4_ip",
pod: v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Expand All @@ -222,7 +222,7 @@ func TestGetPodIPs(t *testing.T) {
exp: []string{"192.168.1.100"},
},
{
name: "pod_with_one_pod_ip",
name: "pod_with_one_pod_dual_ip",
pod: v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Expand All @@ -234,7 +234,6 @@ func TestGetPodIPs(t *testing.T) {
},
exp: []string{"192.168.1.100", "fd00:10:16::8"},
},

{
name: "pod_with_no_pod_ip",
pod: v1.Pod{
Expand All @@ -248,6 +247,19 @@ func TestGetPodIPs(t *testing.T) {
},
exp: []string{},
},
{
name: "pod_with_podip",
pod: v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.PodSpec{},
Status: v1.PodStatus{
PodIPs: []v1.PodIP{},
PodIP: "192.168.1.100",
},
},
exp: []string{"192.168.1.100"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -300,6 +312,30 @@ func TestServiceClusterIPs(t *testing.T) {
},
exp: []string{},
},
{
name: "service_with_no_clusterips",
svc: v1.Service{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.ServiceSpec{
ClusterIP: "10.96.0.1",
ClusterIPs: []string{},
},
},
exp: []string{"10.96.0.1"},
},
{
name: "service_with_invalid_cluster_ip",
svc: v1.Service{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.ServiceSpec{
ClusterIP: "",
ClusterIPs: []string{"10.96.0.1", "invalid ip"},
},
},
exp: []string{"10.96.0.1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ func TestCIDRContainIP(t *testing.T) {
ipStr: "ffff:ffff:ffff:ffff:ffff:0:ffff:4,ffff:ffff:ffff:ffff:ffff:0:ffff:5",
want: true,
},
{
name: "empty cidr",
cidrStr: "",
ipStr: "192.168.0.1",
want: false,
},
}
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
Expand Down

0 comments on commit 44cd6e5

Please sign in to comment.