From 44cd6e5c2ff111e6f75ac4ace8df129e70a8e087 Mon Sep 17 00:00:00 2001 From: Congqi Zhao Date: Fri, 25 Oct 2024 13:59:42 +0800 Subject: [PATCH] add unit test for net.go (#4659) Signed-off-by: zcq98 --- pkg/util/arp_test.go | 40 +++++++++++++++++++++++++++++++++++----- pkg/util/k8s_test.go | 44 ++++++++++++++++++++++++++++++++++++++++---- pkg/util/net_test.go | 6 ++++++ 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/pkg/util/arp_test.go b/pkg/util/arp_test.go index 91138387bab..76feec3348b 100644 --- a/pkg/util/arp_test.go +++ b/pkg/util/arp_test.go @@ -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{}) @@ -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() } } @@ -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) @@ -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 diff --git a/pkg/util/k8s_test.go b/pkg/util/k8s_test.go index ab7c9ae29c6..f103b8849bf 100644 --- a/pkg/util/k8s_test.go +++ b/pkg/util/k8s_test.go @@ -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{}, @@ -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{}, @@ -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{ @@ -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) { @@ -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) { diff --git a/pkg/util/net_test.go b/pkg/util/net_test.go index a9ea6021f5d..aa0cccd08c0 100644 --- a/pkg/util/net_test.go +++ b/pkg/util/net_test.go @@ -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) {