Skip to content

Commit

Permalink
make: add ut cover (#4490)
Browse files Browse the repository at this point in the history
* make: add ut cover

Signed-off-by: bobz965 <[email protected]>

* git ignore cover html

Signed-off-by: bobz965 <[email protected]>

* ut: add subnet

Signed-off-by: bobz965 <[email protected]>

* ut: arp add

Signed-off-by: bobz965 <[email protected]>

* fix: github ci arp request not permitted

Signed-off-by: bobz965 <[email protected]>

* fix: ut log

Signed-off-by: bobz965 <[email protected]>

---------

Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 authored Sep 11, 2024
1 parent 3e843e8 commit 486cde8
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ image-amd64-dpdk.tar
image-arm64.tar
test/**/*.test
cover.out
cover.html
kube-ovn-app-sa.yaml
kube-ovn-cni-sa.yaml
kube-ovn-sa.yaml
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,12 @@ ut:
ginkgo -mod=mod --show-node-events --poll-progress-after=60s $(GINKGO_OUTPUT_OPT) -v test/unittest
go test ./pkg/...

.PHONY: cover
cover:
go test ./pkg/ovs ./pkg/util ./pkg/ipam -gcflags=all=-l -p 1 -coverprofile=cover.out -covermode=atomic
go tool cover -func=cover.out | grep -v "100.0%"
go tool cover -html=cover.out -o cover.html

.PHONY: ipam-bench
ipam-bench:
go test -timeout 30m -bench='^BenchmarkIPAM' -benchtime=10000x test/unittest/ipam_bench/ipam_test.go -args -logtostderr=false
Expand Down
82 changes: 81 additions & 1 deletion pkg/ipam/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func TestNewSubnetDualStack(t *testing.T) {
require.Len(t, subnet.V6IPToPod, 0)
require.NotNil(t, subnet.V6Available)
require.True(t, subnet.V6Available.Equal(subnet.V6Free))
// TODO: check pool
}

func TestGetV4StaticAddress(t *testing.T) {
Expand Down Expand Up @@ -1019,3 +1018,84 @@ func TestSubnetIPPoolStatistics(t *testing.T) {
require.Empty(t, v6as)
require.Empty(t, v6us)
}

func TestSubnetReleaseAddr(t *testing.T) {
v4ExcludeIps := []string{
"10.0.0.2", "10.0.0.4", "10.0.0.100",
"10.0.0.252", "10.0.0.253", "10.0.0.254",
}
subnet, err := NewSubnet("v4Subnet", "10.0.0.0/24", v4ExcludeIps)
require.NoError(t, err)
require.NotNil(t, subnet)
// 1.1 two different pod get the same v4 ip
pod41Name := "pod41.default"
nic41Name := "pod41.default"
// release not exist ip
subnet.releaseAddr(pod41Name, nic41Name)
var mac *string
v4 := "10.0.0.3"
v4IP, err := NewIP(v4)
require.NoError(t, err)
ip1, macStr1, err := subnet.GetStaticAddress(pod41Name, nic41Name, v4IP, mac, false, true)
require.NoError(t, err)
require.Equal(t, v4, ip1.String())
require.NotEmpty(t, macStr1)
pod42Name := "pod42.default"
nic42Name := "pod42.default"
ip2, macStr2, err := subnet.GetStaticAddress(pod42Name, nic42Name, v4IP, mac, false, false)
require.NoError(t, err)
require.Equal(t, v4, ip2.String())
require.NotEmpty(t, macStr2)
subnet.releaseAddr(pod41Name, nic41Name)
subnet.releaseAddr(pod42Name, nic42Name)
pod43Name := "pod43.default"
nic43Name := "pod43.default"
// 1.2 release from exclude ip
v43 := "10.0.0.100"
v43IP, err := NewIP(v43)
require.NoError(t, err)
ip3, macStr3, err := subnet.GetStaticAddress(pod43Name, nic43Name, v43IP, mac, false, false)
require.NoError(t, err)
require.Equal(t, v43, ip3.String())
require.NotEmpty(t, macStr3)
subnet.releaseAddr(pod43Name, nic43Name)

// 2. two different pod get the same v6 ip
v6ExcludeIps := []string{
"2001:db8::2", "2001:db8::4", "2001:db8::100",
"2001:db8::252", "2001:db8::253", "2001:db8::254",
}
subnet, err = NewSubnet("v6Subnet", "2001:db8::/64", v6ExcludeIps)
require.NoError(t, err)
require.NotNil(t, subnet)
pod61Name := "pod61.default"
nic61Name := "pod61.default"
// release not exist ip
subnet.releaseAddr(pod61Name, nic61Name)
v6 := "2001:db8::3"
v6IP, err := NewIP(v6)
require.NoError(t, err)
ip1, macStr1, err = subnet.GetStaticAddress(pod61Name, nic61Name, v6IP, nil, false, true)
require.NoError(t, err)
require.Equal(t, v6, ip1.String())
require.NotEmpty(t, macStr1)
pod62Name := "pod2.default"
nic62Name := "pod2.default"
ip2, macStr2, err = subnet.GetStaticAddress(pod62Name, nic62Name, v6IP, mac, false, false)
require.NoError(t, err)
require.Equal(t, v6, ip2.String())
require.NotEmpty(t, macStr2)
subnet.releaseAddr(pod61Name, nic61Name)
subnet.releaseAddr(pod62Name, nic62Name)
// 2.2 release from exclude ip
pod63Name := "pod63.default"
nic63Name := "pod63.default"
v63 := "2001:db8::100"
v63IP, err := NewIP(v63)
require.NoError(t, err)
ip3, macStr3, err = subnet.GetStaticAddress(pod63Name, nic63Name, v63IP, nil, false, false)
require.NoError(t, err)
require.Equal(t, v63, ip3.String())
require.NotEmpty(t, macStr3)
subnet.releaseAddr(pod63Name, nic63Name)
}
58 changes: 58 additions & 0 deletions pkg/util/arp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package util

import (
"net"
"strings"
"testing"
"time"

"github.com/vishvananda/netlink"
)

func TestMacEqual(t *testing.T) {
Expand Down Expand Up @@ -47,3 +51,57 @@ func TestMacEqual(t *testing.T) {
})
}
}

func TestArpResolve(t *testing.T) {
// get the default route gw and nic
routes, err := netlink.RouteList(nil, netlink.FAMILY_ALL)
if err != nil {
t.Fatalf("failed to get routes: %v", err)
}
var defaultGW string
var nicIndex int
for _, r := range routes {
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")
}
if nicIndex == 0 {
t.Fatalf("failed to get nic")
}

link, err := netlink.LinkByIndex(nicIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
}
maxRetry := 3
done := make(chan struct{})
linkName := link.Attrs().Name
if !strings.HasPrefix(linkName, "e") {
// default gw nic should be ethernet
t.Fatalf("invalid default gw nic link name: %s", linkName)
}
mac, count, err := ArpResolve(linkName, defaultGW, time.Second, maxRetry, done)
if err != nil {
if strings.Contains(err.Error(), "not permitted") {
t.Skipf("ARP request operation not permitted: try %d, link name %s, default gw %s", count, linkName, defaultGW)
return
}
t.Errorf("Error resolving ARP: %v: try %d, link name %s, default gw %s", err, count, linkName, defaultGW)
}
if mac == nil {
t.Errorf("ARP resolved MAC address is nil: try %d, link name %s, default gw %s", count, linkName, defaultGW)
}
// should failed
defaultGW = "xx.xx.xx.xx"
mac, count, err = ArpResolve(linkName, defaultGW, time.Second, maxRetry, done)
if err == nil {
t.Errorf("Expect error, but got nil: try %d, link name %s, default gw %s", count, linkName, defaultGW)
}
if mac != nil {
t.Errorf("Expect nil MAC address, but got %v: try %d, link name %s, default gw %s", mac, count, linkName, defaultGW)
}
}

0 comments on commit 486cde8

Please sign in to comment.