Skip to content

Commit

Permalink
ut: add unit for ovn-nb-load_balancer (#4646)
Browse files Browse the repository at this point in the history
* add unit test for ip_range

* add unit test for ovn-nb-load_balancer

---------

Signed-off-by: zcq98 <[email protected]>
  • Loading branch information
zcq98 authored and zhangbingbing2 committed Nov 25, 2024
1 parent f3cf589 commit 718d748
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 7 deletions.
87 changes: 86 additions & 1 deletion pkg/ipam/ip_range_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func TestNewIPRangeList(t *testing.T) {
v4RangeEnd2, err := NewIP("10.0.0.18")
require.NoError(t, err)

v4, err := NewIPRangeList(v4RangeStart1, v4RangeEnd1, v4RangeStart2, v4RangeEnd2)
v4, err := NewIPRangeList(v4RangeStart1)
require.ErrorContains(t, err, "length of ips must be an even number")
require.Nil(t, v4)

v4, err = NewIPRangeList(v4RangeStart1, v4RangeEnd1, v4RangeStart2, v4RangeEnd2)
require.NoError(t, err)

fakeV4RangeItem1, err := NewIP("10.0.0.4")
Expand Down Expand Up @@ -837,6 +841,22 @@ func TestNewIPRangeListFrom(t *testing.T) {
require.True(t, ipList.Contains(end.Sub(1)))
}
}

ipList, err = NewIPRangeListFrom("192.168.1.2..192.168.1.1")
require.ErrorContains(t, err, "invalid ip range \"192.168.1.2..192.168.1.1\": 192.168.1.2 is greater than 192.168.1.1")
require.Nil(t, ipList)

ipList, err = NewIPRangeListFrom("invalidIP..192.168.1.1")
require.ErrorContains(t, err, "invalid IP address")
require.Nil(t, ipList)

ipList, err = NewIPRangeListFrom("192.168.1.2..invalidIP")
require.ErrorContains(t, err, "invalid IP address")
require.Nil(t, ipList)

ipList, err = NewIPRangeListFrom("invalidCIDR/24")
require.ErrorContains(t, err, "invalid CIDR address: invalidCIDR/24")
require.Nil(t, ipList)
}

func TestRemove(t *testing.T) {
Expand Down Expand Up @@ -1140,3 +1160,68 @@ func TestEqual(t *testing.T) {
v6RL5 := NewEmptyIPRangeList()
require.False(t, v6RangeList1.Equal(v6RL5))
}

func TestAllocate(t *testing.T) {
v4RangeStart, err := NewIP("10.0.0.1")
require.NoError(t, err)
require.NotNil(t, v4RangeStart)
v4RangeEnd, err := NewIP("10.0.0.4")
require.NoError(t, err)
require.NotNil(t, v4RangeEnd)
v4Range := NewIPRange(v4RangeStart, v4RangeEnd)
v4RangeList := NewEmptyIPRangeList().MergeRange(v4Range)

t.Run("Allocate from empty range", func(t *testing.T) {
emptyRange := NewEmptyIPRangeList()
allocated := emptyRange.Allocate(nil)
require.Nil(t, allocated)
})

t.Run("Allocate without skipped IPs", func(t *testing.T) {
allocated := v4RangeList.Allocate(nil)
require.Equal(t, "10.0.0.1", allocated.String())
require.False(t, v4RangeList.Contains(allocated))
})

t.Run("Allocate with skipped IPs", func(t *testing.T) {
skipped1, err := NewIP("10.0.0.2")
require.NoError(t, err)
skipped2, err := NewIP("10.0.0.3")
require.NoError(t, err)
allocated := v4RangeList.Allocate([]IP{skipped1, skipped2})
require.Equal(t, "10.0.0.4", allocated.String())
require.False(t, v4RangeList.Contains(allocated))
})

t.Run("Allocate all IPs", func(t *testing.T) {
skipped1, err := NewIP("10.0.0.1")
require.NoError(t, err)
skipped2, err := NewIP("10.0.0.2")
require.NoError(t, err)
skipped3, err := NewIP("10.0.0.3")
require.NoError(t, err)
skipped4, err := NewIP("10.0.0.4")
require.NoError(t, err)
allocated := v4RangeList.Allocate([]IP{skipped1, skipped2, skipped3, skipped4})
require.Nil(t, allocated)
})

t.Run("Allocate from IPv6 range", func(t *testing.T) {
v6RangeStart, err := NewIP("2001:db8::1")
require.NoError(t, err)
v6RangeEnd, err := NewIP("2001:db8::10")
require.NoError(t, err)
v6Range := NewIPRange(v6RangeStart, v6RangeEnd)
v6RangeList := NewEmptyIPRangeList().MergeRange(v6Range)

allocated := v6RangeList.Allocate(nil)
require.Equal(t, "2001:db8::1", allocated.String())
require.False(t, v6RangeList.Contains(allocated))

skipped, err := NewIP("2001:db8::2")
require.NoError(t, err)
allocated = v6RangeList.Allocate([]IP{skipped})
require.Equal(t, "2001:db8::3", allocated.String())
require.False(t, v6RangeList.Contains(allocated))
})
}
6 changes: 6 additions & 0 deletions pkg/ipam/ip_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func TestIPRangeRemove(t *testing.T) {
wantRanges: []*IPRange{NewIPRange(start, IP(net.ParseIP("192.168.1.9").To4()))},
wantOk: true,
},
{
name: "Remove IP that is not included",
ip: IP(net.ParseIP("192.168.1.254").To4()),
wantRanges: nil,
wantOk: false,
},
}

for _, tt := range tests {
Expand Down
107 changes: 107 additions & 0 deletions pkg/ovs/ovn-nb-load_balancer_health_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ovn-org/libovsdb/ovsdb"
"github.com/stretchr/testify/require"

ovsclient "github.com/kubeovn/kube-ovn/pkg/ovsdb/client"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
)

Expand Down Expand Up @@ -45,6 +46,14 @@ func (suite *OvnClientTestSuite) testAddLoadBalancerHealthCheck() {
require.NotEmpty(t, lbhcRepeat.UUID)

require.Equal(t, lbhc.UUID, lbhcRepeat.UUID)

// create lbhc with empty lbName
err = nbClient.AddLoadBalancerHealthCheck("", vip, map[string]string{})
require.ErrorContains(t, err, "the lb name is required")

// create lbhc with empty vip
err = nbClient.AddLoadBalancerHealthCheck(lbName, "", map[string]string{})
require.ErrorContains(t, err, "the vip endpoint is required")
}

func (suite *OvnClientTestSuite) testUpdateLoadBalancerHealthCheck() {
Expand Down Expand Up @@ -95,6 +104,10 @@ func (suite *OvnClientTestSuite) testDeleteLoadBalancerHealthCheck() {
err error
)

// delete lbhc for non-exist lb
err = nbClient.DeleteLoadBalancerHealthCheck(lbName, vip)
require.ErrorContains(t, err, "not found load balancer")

err = nbClient.CreateLoadBalancer(lbName, "tcp", "ip_dst")
require.NoError(t, err)

Expand Down Expand Up @@ -202,6 +215,43 @@ func (suite *OvnClientTestSuite) testGetLoadBalancerHealthCheck() {
require.NoError(t, err)
},
)

t.Run("should return err when health checks have the same vipEndpoint",
func(t *testing.T) {
t.Parallel()
err = nbClient.CreateLoadBalancer(lbName+"1", "tcp", "")
require.NoError(t, err)
lbhc1 := &ovnnb.LoadBalancerHealthCheck{
UUID: ovsclient.NamedUUID(),
ExternalIDs: nil,
Options: map[string]string{
"timeout": "20",
"interval": "5",
"success_count": "3",
"failure_count": "3",
},
Vip: vip,
}
lbhc2 := &ovnnb.LoadBalancerHealthCheck{
UUID: ovsclient.NamedUUID(),
ExternalIDs: nil,
Options: map[string]string{
"timeout": "20",
"interval": "5",
"success_count": "3",
"failure_count": "3",
},
Vip: vip,
}
err = nbClient.CreateLoadBalancerHealthCheck(lbName+"1", vip, lbhc1)
require.NoError(t, err)
err = nbClient.CreateLoadBalancerHealthCheck(lbName+"1", vip, lbhc2)
require.NoError(t, err)

_, _, err := nbClient.GetLoadBalancerHealthCheck(lbName+"1", vip, true)
require.ErrorContains(t, err, "lb test-get-lb-hc1 has more than one health check with the same vip 1.1.1.22:80")
},
)
}

func (suite *OvnClientTestSuite) testListLoadBalancerHealthChecks() {
Expand Down Expand Up @@ -368,3 +418,60 @@ func (suite *OvnClientTestSuite) testDeleteLoadBalancerHealthCheckOp() {
},
)
}

func (suite *OvnClientTestSuite) testNewLoadBalancerHealthCheck() {
t := suite.T()
t.Parallel()

var (
nbClient = suite.ovnNBClient
lbName = "test-new-lb-hc"
vip = "4.4.4.4:80"
)

t.Run("create new load balancer health check", func(t *testing.T) {
externals := map[string]string{"key1": "value1", "key2": "value2"}
lbhc, err := nbClient.newLoadBalancerHealthCheck(lbName, vip, externals)
require.ErrorContains(t, err, "not found load balancer")
require.Nil(t, lbhc)

err = nbClient.CreateLoadBalancer(lbName, "tcp", "")
require.NoError(t, err)

lbhc, err = nbClient.newLoadBalancerHealthCheck(lbName, vip, externals)
require.NoError(t, err)
require.NotNil(t, lbhc)
require.Equal(t, vip, lbhc.Vip)
require.Equal(t, externals, lbhc.ExternalIDs)
require.Equal(t, "20", lbhc.Options["timeout"])
require.Equal(t, "5", lbhc.Options["interval"])
require.Equal(t, "3", lbhc.Options["success_count"])
require.Equal(t, "3", lbhc.Options["failure_count"])
})

t.Run("create with empty lb name", func(t *testing.T) {
lbhc, err := nbClient.newLoadBalancerHealthCheck("", vip, nil)
require.Error(t, err)
require.Nil(t, lbhc)
require.Contains(t, err.Error(), "the lb name is required")
})

t.Run("create with empty vip", func(t *testing.T) {
lbhc, err := nbClient.newLoadBalancerHealthCheck(lbName, "", nil)
require.Error(t, err)
require.Nil(t, lbhc)
require.Contains(t, err.Error(), "the vip endpoint is required")
})

t.Run("create existing health check", func(t *testing.T) {
err := nbClient.CreateLoadBalancer(lbName, "tcp", "ip_dst")
require.NoError(t, err)

err = nbClient.AddLoadBalancerHealthCheck(lbName, vip, nil)
require.NoError(t, err)

lbhc, err := nbClient.newLoadBalancerHealthCheck(lbName, vip, nil)
require.NoError(t, err)
require.Nil(t, lbhc)
})
}
Loading

0 comments on commit 718d748

Please sign in to comment.