From 892c6518b47ed451e12c189759984bef69e5a972 Mon Sep 17 00:00:00 2001 From: zcq98 Date: Tue, 27 Aug 2024 17:47:38 +0800 Subject: [PATCH 1/3] add unit test for ovn-nb-address_set.go Signed-off-by: zcq98 --- pkg/ovs/ovn-nb-address_set_test.go | 94 ++++++++++++++++++++++++++++++ pkg/ovs/ovn-nb-suite_test.go | 4 ++ 2 files changed, 98 insertions(+) diff --git a/pkg/ovs/ovn-nb-address_set_test.go b/pkg/ovs/ovn-nb-address_set_test.go index eb29d06fc25..5bd6af7b7dd 100644 --- a/pkg/ovs/ovn-nb-address_set_test.go +++ b/pkg/ovs/ovn-nb-address_set_test.go @@ -44,6 +44,27 @@ func (suite *OvnClientTestSuite) testCreateAddressSet() { }) require.Error(t, err) }) + + t.Run("create address set that already exists", func(t *testing.T) { + asName := "existing_address_set" + err := ovnClient.CreateAddressSet(asName, nil) + require.NoError(t, err) + + // Attempt to create the same address set again + err = ovnClient.CreateAddressSet(asName, nil) + require.NoError(t, err) + + // Verify that only one address set exists + ass, err := ovnClient.ListAddressSets(nil) + require.NoError(t, err) + count := 0 + for _, as := range ass { + if as.Name == asName { + count++ + } + } + require.Equal(t, 1, count) + }) } func (suite *OvnClientTestSuite) testAddressSetUpdateAddress() { @@ -86,6 +107,62 @@ func (suite *OvnClientTestSuite) testAddressSetUpdateAddress() { require.NoError(t, err) require.Empty(t, as.Addresses) }) + + t.Run("update with mixed IPv4 and IPv6 addresses", func(t *testing.T) { + addresses := []string{"192.168.1.1", "2001:db8::1", "10.0.0.1", "fe80::1"} + err := ovnClient.AddressSetUpdateAddress(asName, addresses...) + require.NoError(t, err) + + as, err := ovnClient.GetAddressSet(asName, false) + require.NoError(t, err) + require.ElementsMatch(t, addresses, as.Addresses) + }) + + t.Run("update with CIDR notation", func(t *testing.T) { + addresses := []string{"192.168.1.0/24", "2001:db8::/64"} + err := ovnClient.AddressSetUpdateAddress(asName, addresses...) + require.NoError(t, err) + + as, err := ovnClient.GetAddressSet(asName, false) + require.NoError(t, err) + require.ElementsMatch(t, []string{"192.168.1.0/24", "2001:db8::/64"}, as.Addresses) + }) + + t.Run("update with duplicate addresses", func(t *testing.T) { + addresses := []string{"192.168.1.1", "192.168.1.1", "2001:db8::1", "2001:db8::1"} + err := ovnClient.AddressSetUpdateAddress(asName, addresses...) + require.NoError(t, err) + + as, err := ovnClient.GetAddressSet(asName, false) + require.NoError(t, err) + require.ElementsMatch(t, []string{"192.168.1.1", "2001:db8::1"}, as.Addresses) + }) + + t.Run("update with invalid CIDR", func(t *testing.T) { + addresses := []string{"192.168.1.1", "invalid_cidr", "2001:db8::1"} + err := ovnClient.AddressSetUpdateAddress(asName, addresses...) + require.NoError(t, err) + + as, err := ovnClient.GetAddressSet(asName, false) + require.NoError(t, err) + require.ElementsMatch(t, []string{"192.168.1.1", "invalid_cidr", "2001:db8::1"}, as.Addresses) + }) + + t.Run("update with empty address list", func(t *testing.T) { + err := ovnClient.AddressSetUpdateAddress(asName) + require.NoError(t, err) + + as, err := ovnClient.GetAddressSet(asName, false) + require.NoError(t, err) + require.Empty(t, as.Addresses) + }) + + t.Run("update non-existent address set", func(t *testing.T) { + nonExistentAS := "non_existent_as" + err := ovnClient.AddressSetUpdateAddress(nonExistentAS, "192.168.1.1") + require.Error(t, err) + require.Contains(t, err.Error(), "get address set") + }) } func (suite *OvnClientTestSuite) testDeleteAddressSet() { @@ -146,6 +223,10 @@ func (suite *OvnClientTestSuite) testDeleteAddressSets() { ass, err := ovnClient.ListAddressSets(externalIDs) require.NoError(t, err) require.Empty(t, ass) + + // delete address sets with empty externalIDs + err = ovnClient.DeleteAddressSets(map[string]string{}) + require.NoError(t, err) } func (suite *OvnClientTestSuite) testListAddressSets() { @@ -239,3 +320,16 @@ func (suite *OvnClientTestSuite) testAddressSetFilter() { require.False(t, out) }) } + +func (suite *OvnClientTestSuite) testUpdateAddressSet() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + t.Run("update with nil address set", func(t *testing.T) { + err := ovnClient.UpdateAddressSet(nil) + require.Error(t, err) + require.Contains(t, err.Error(), "address_set is nil") + }) +} diff --git a/pkg/ovs/ovn-nb-suite_test.go b/pkg/ovs/ovn-nb-suite_test.go index e8f1442e2b3..210bd23e425 100644 --- a/pkg/ovs/ovn-nb-suite_test.go +++ b/pkg/ovs/ovn-nb-suite_test.go @@ -469,6 +469,10 @@ func (suite *OvnClientTestSuite) Test_addressSetFilter() { suite.testAddressSetFilter() } +func (suite *OvnClientTestSuite) Test_UpdateAddressSet() { + suite.testUpdateAddressSet() +} + /* acl unit test */ func (suite *OvnClientTestSuite) Test_testUpdateIngressAclOps() { suite.testUpdateIngressACLOps() From d34662f1ba154dd7f649a462aa9907ffc4cbc550 Mon Sep 17 00:00:00 2001 From: zcq98 Date: Wed, 28 Aug 2024 14:13:04 +0800 Subject: [PATCH 2/3] add unit test for ovn-nb-acl.go Signed-off-by: zcq98 --- pkg/ovs/ovn-nb-acl_test.go | 131 +++++++++++++++++++++++++++++++---- pkg/ovs/ovn-nb-suite_test.go | 12 ++++ 2 files changed, 131 insertions(+), 12 deletions(-) diff --git a/pkg/ovs/ovn-nb-acl_test.go b/pkg/ovs/ovn-nb-acl_test.go index f1c29a7924b..cdd98ea061c 100644 --- a/pkg/ovs/ovn-nb-acl_test.go +++ b/pkg/ovs/ovn-nb-acl_test.go @@ -360,9 +360,6 @@ func (suite *OvnClientTestSuite) testCreateNodeACL() { t.Parallel() ovnClient := suite.ovnClient - pgName := "test_create_node_acl_pg" - nodeIP := "192.168.20.3" - joinIP := "100.64.0.2,fd00:100:64::2" checkACL := func(pg *ovnnb.PortGroup, direction, priority, match string, options map[string]string) { acl, err := ovnClient.GetACL(pg.Name, direction, priority, match, false) @@ -376,7 +373,7 @@ func (suite *OvnClientTestSuite) testCreateNodeACL() { require.Contains(t, pg.ACLs, acl.UUID) } - expect := func(pg *ovnnb.PortGroup, _ string) { + expect := func(pg *ovnnb.PortGroup, nodeIP, pgName string) { for _, ip := range strings.Split(nodeIP, ",") { protocol := util.CheckProtocol(ip) ipSuffix := "ip4" @@ -396,17 +393,41 @@ func (suite *OvnClientTestSuite) testCreateNodeACL() { } } - err := ovnClient.CreatePortGroup(pgName, nil) - require.NoError(t, err) + t.Run("create node ACL with single stack nodeIP and dual stack joinIP", func(t *testing.T) { + pgName := "test_create_node_acl_pg" + nodeIP := "192.168.20.3" + joinIP := "100.64.0.2,fd00:100:64::2" - err = ovnClient.CreateNodeACL(pgName, nodeIP, joinIP) - require.NoError(t, err) + err := ovnClient.CreatePortGroup(pgName, nil) + require.NoError(t, err) - pg, err := ovnClient.GetPortGroup(pgName, false) - require.NoError(t, err) - require.Len(t, pg.ACLs, 2) + err = ovnClient.CreateNodeACL(pgName, nodeIP, joinIP) + require.NoError(t, err) + + pg, err := ovnClient.GetPortGroup(pgName, false) + require.NoError(t, err) + require.Len(t, pg.ACLs, 2) + + expect(pg, nodeIP, pgName) + }) + + t.Run("create node ACL with dual stack nodeIP and join IP", func(t *testing.T) { + pgName := "test-pg-overlap" + nodeIP := "192.168.20.4,fd00::4" + joinIP := "100.64.0.3,fd00:100:64::3" + + err := ovnClient.CreatePortGroup(pgName, nil) + require.NoError(t, err) - expect(pg, nodeIP) + err = ovnClient.CreateNodeACL(pgName, nodeIP, joinIP) + require.NoError(t, err) + + pg, err := ovnClient.GetPortGroup(pgName, false) + require.NoError(t, err) + require.Len(t, pg.ACLs, 4) + + expect(pg, nodeIP, pgName) + }) } func (suite *OvnClientTestSuite) testCreateSgDenyAllACL() { @@ -2145,3 +2166,89 @@ func (suite *OvnClientTestSuite) testNewAnpACLMatch() { }) } } + +func (suite *OvnClientTestSuite) testCreateBareACL() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + t.Run("create bare ACL successfully", func(t *testing.T) { + err := ovnClient.CreateBareACL("test-parent", "from-lport", "1000", "ip4.src == 10.0.0.1", "allow") + require.NoError(t, err) + }) + + t.Run("create bare ACL with empty match", func(t *testing.T) { + err := ovnClient.CreateBareACL("test-parent", "from-lport", "1000", "", "allow") + require.Error(t, err) + require.Contains(t, err.Error(), "new acl direction from-lport priority 1000 match") + }) +} + +func (suite *OvnClientTestSuite) testUpdateAnpRuleACLOps() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + expect := func(row ovsdb.Row, action, direction, match, priority string) { + intPriority, err := strconv.Atoi(priority) + require.NoError(t, err) + require.Equal(t, action, row["action"]) + require.Equal(t, direction, row["direction"]) + require.Equal(t, match, row["match"]) + require.Equal(t, intPriority, row["priority"]) + } + + t.Run("ingress ACL for ANP", func(t *testing.T) { + pgName := "test-pg-ingress" + asName := "test-as-ingress" + protocol := "tcp" + aclName := "test-acl" + priority := 1000 + aclAction := ovnnb.ACLActionAllow + logACLActions := []ovnnb.ACLAction{ovnnb.ACLActionAllow} + rulePorts := []v1alpha1.AdminNetworkPolicyPort{} + isIngress := true + isBanp := false + + err := ovnClient.CreatePortGroup(pgName, nil) + require.NoError(t, err) + ops, err := ovnClient.UpdateAnpRuleACLOps(pgName, asName, protocol, aclName, priority, aclAction, logACLActions, rulePorts, isIngress, isBanp) + require.NoError(t, err) + require.NotEmpty(t, ops) + expect(ops[0].Row, ovnnb.ACLActionAllow, ovnnb.ACLDirectionToLport, fmt.Sprintf("outport == @%s && ip && ip4.src == $%s", pgName, asName), "1000") + }) + + t.Run("egress ACL for BANP", func(t *testing.T) { + pgName := "test-pg-egress" + asName := "test-as-egress" + protocol := "udp" + aclName := "test-acl" + priority := 2000 + aclAction := ovnnb.ACLActionDrop + logACLActions := []ovnnb.ACLAction{ovnnb.ACLActionDrop} + rulePorts := []v1alpha1.AdminNetworkPolicyPort{} + isIngress := false + isBanp := true + + err := ovnClient.CreatePortGroup(pgName, nil) + require.NoError(t, err) + ops, err := ovnClient.UpdateAnpRuleACLOps(pgName, asName, protocol, aclName, priority, aclAction, logACLActions, rulePorts, isIngress, isBanp) + require.NoError(t, err) + require.NotEmpty(t, ops) + expect(ops[0].Row, ovnnb.ACLActionDrop, ovnnb.ACLDirectionFromLport, fmt.Sprintf("inport == @%s && ip && ip4.dst == $%s", pgName, asName), "2000") + }) +} + +func (suite *OvnClientTestSuite) testUpdateACL() { + t := suite.T() + + ovnClient := suite.ovnClient + + t.Run("update ACL with nil input", func(t *testing.T) { + err := ovnClient.UpdateACL(nil) + require.Error(t, err) + require.Contains(t, err.Error(), "address_set is nil") + }) +} diff --git a/pkg/ovs/ovn-nb-suite_test.go b/pkg/ovs/ovn-nb-suite_test.go index 210bd23e425..1cd22923c51 100644 --- a/pkg/ovs/ovn-nb-suite_test.go +++ b/pkg/ovs/ovn-nb-suite_test.go @@ -562,6 +562,18 @@ func (suite *OvnClientTestSuite) Test_newAnpACLMatch() { suite.testNewAnpACLMatch() } +func (suite *OvnClientTestSuite) Test_CreateBareACL() { + suite.testCreateBareACL() +} + +func (suite *OvnClientTestSuite) Test_UpdateAnpRuleACLOps() { + suite.testUpdateAnpRuleACLOps() +} + +func (suite *OvnClientTestSuite) Test_UpdateACL() { + suite.testUpdateACL() +} + /* logical_router_policy unit test */ func (suite *OvnClientTestSuite) Test_AddLogicalRouterPolicy() { suite.testAddLogicalRouterPolicy() From 22cd0cc76e2ba8b3f9062b02fc28f56731d6487f Mon Sep 17 00:00:00 2001 From: zcq98 Date: Thu, 29 Aug 2024 19:21:41 +0800 Subject: [PATCH 3/3] add unit test for ovn-nb-bfd.go Signed-off-by: zcq98 --- pkg/ovs/ovn-nb-bfd_test.go | 531 ++++++++++++++++++++++++++++++++++- pkg/ovs/ovn-nb-suite_test.go | 25 ++ 2 files changed, 555 insertions(+), 1 deletion(-) diff --git a/pkg/ovs/ovn-nb-bfd_test.go b/pkg/ovs/ovn-nb-bfd_test.go index 441af1b101d..fd9df258cc6 100644 --- a/pkg/ovs/ovn-nb-bfd_test.go +++ b/pkg/ovs/ovn-nb-bfd_test.go @@ -3,6 +3,8 @@ package ovs import ( "testing" + "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" + "github.com/scylladb/go-set/strset" "github.com/stretchr/testify/require" ) @@ -12,13 +14,14 @@ func (suite *OvnClientTestSuite) testCreateBFD() { t.Parallel() ovnClient := suite.ovnClient - lrpName := "test-create-bfd" dstIP := "192.168.124.1" minRx, minTx, detectMult := 101, 102, 19 t.Run("create BFD", func(t *testing.T) { t.Parallel() + lrpName := "test-create-bfd" + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) require.NoError(t, err) require.NotNil(t, bfd) @@ -31,6 +34,25 @@ func (suite *OvnClientTestSuite) testCreateBFD() { require.Equal(t, minTx, *bfd.MinTx) require.Equal(t, detectMult, *bfd.DetectMult) }) + + t.Run("create BFD with existing entry", func(t *testing.T) { + t.Parallel() + + lrpName := "test-create-existing-bfd" + + bfd1, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd1) + + bfd2, err := ovnClient.CreateBFD(lrpName, dstIP, minRx+1, minTx+1, detectMult+1) + require.NoError(t, err) + require.NotNil(t, bfd2) + require.Equal(t, bfd1, bfd2) + require.Equal(t, bfd1.UUID, bfd2.UUID) + require.Equal(t, minRx, *bfd2.MinRx) + require.Equal(t, minTx, *bfd2.MinTx) + require.Equal(t, detectMult, *bfd2.DetectMult) + }) } func (suite *OvnClientTestSuite) testListBFD() { @@ -115,4 +137,511 @@ func (suite *OvnClientTestSuite) testDeleteBFD() { require.NoError(t, err) require.Len(t, bfdList, 0) }) + + t.Run("delete non-existent BFD", func(t *testing.T) { + t.Parallel() + + err := ovnClient.DeleteBFD(lrpName, "192.168.124.17") + require.NoError(t, err) + }) +} + +func (suite *OvnClientTestSuite) testListDownBFDs() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + lrpName := "test-list-down-bfd" + dstIP1 := "192.168.124.6" + dstIP2 := "192.168.124.7" + dstIP3 := "192.168.124.8" + minRx, minTx, detectMult := 101, 102, 19 + + t.Run("list down BFDs", func(t *testing.T) { + t.Parallel() + + bfd1, err := ovnClient.CreateBFD(lrpName, dstIP1, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd1) + + bfd2, err := ovnClient.CreateBFD(lrpName, dstIP2, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd2) + + bfd3, err := ovnClient.CreateBFD(lrpName, dstIP3, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd3) + + // Set BFD statuses + downStatus := ovnnb.BFDStatusDown + adminDownStatus := ovnnb.BFDStatusAdminDown + upStatus := ovnnb.BFDStatusUp + + bfd1.Status = &downStatus + bfd2.Status = &adminDownStatus + bfd3.Status = &upStatus + + err = ovnClient.UpdateBFD(bfd1) + require.NoError(t, err) + err = ovnClient.UpdateBFD(bfd2) + require.NoError(t, err) + err = ovnClient.UpdateBFD(bfd3) + require.NoError(t, err) + + // Test listing down BFDs for specific IP + downBFDs, err := ovnClient.ListDownBFDs(dstIP1) + require.NoError(t, err) + require.Len(t, downBFDs, 1) + require.Equal(t, bfd1.UUID, downBFDs[0].UUID) + + downBFDs, err = ovnClient.ListDownBFDs(dstIP2) + require.NoError(t, err) + require.Len(t, downBFDs, 1) + + downBFDs, err = ovnClient.ListDownBFDs(dstIP3) + require.NoError(t, err) + require.Len(t, downBFDs, 0) + + // Test listing down BFDs for non-existent IP + nonExistentBFDs, err := ovnClient.ListDownBFDs("192.168.124.9") + require.NoError(t, err) + require.Len(t, nonExistentBFDs, 0) + }) + + t.Run("list down BFDs with no down BFDs", func(t *testing.T) { + t.Parallel() + + // Create a BFD with UP status + bfd, err := ovnClient.CreateBFD(lrpName, "192.168.124.10", minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + upStatus := ovnnb.BFDStatusUp + bfd.Status = &upStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + // Try to list down BFDs + downBFDs, err := ovnClient.ListDownBFDs("192.168.124.10") + require.NoError(t, err) + require.Len(t, downBFDs, 0) + }) +} + +func (suite *OvnClientTestSuite) testListUpBFDs() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + lrpName := "test-list-up-bfd" + dstIP1 := "192.168.124.11" + dstIP2 := "192.168.124.12" + dstIP3 := "192.168.124.13" + minRx, minTx, detectMult := 101, 102, 19 + + t.Run("list up BFDs", func(t *testing.T) { + t.Parallel() + + bfd1, err := ovnClient.CreateBFD(lrpName, dstIP1, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd1) + + bfd2, err := ovnClient.CreateBFD(lrpName, dstIP2, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd2) + + bfd3, err := ovnClient.CreateBFD(lrpName, dstIP3, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd3) + + upStatus := ovnnb.BFDStatusUp + downStatus := ovnnb.BFDStatusDown + adminDownStatus := ovnnb.BFDStatusAdminDown + + bfd1.Status = &upStatus + bfd2.Status = &downStatus + bfd3.Status = &adminDownStatus + + err = ovnClient.UpdateBFD(bfd1) + require.NoError(t, err) + err = ovnClient.UpdateBFD(bfd2) + require.NoError(t, err) + err = ovnClient.UpdateBFD(bfd3) + require.NoError(t, err) + + upBFDs, err := ovnClient.ListUpBFDs(dstIP1) + require.NoError(t, err) + require.Len(t, upBFDs, 1) + require.Equal(t, bfd1.UUID, upBFDs[0].UUID) + + upBFDs, err = ovnClient.ListUpBFDs(dstIP2) + require.NoError(t, err) + require.Len(t, upBFDs, 0) + + upBFDs, err = ovnClient.ListUpBFDs(dstIP3) + require.NoError(t, err) + require.Len(t, upBFDs, 0) + }) + + t.Run("list up BFDs with non-existent IP", func(t *testing.T) { + t.Parallel() + + upBFDs, err := ovnClient.ListUpBFDs("192.168.124.14") + require.NoError(t, err) + require.Len(t, upBFDs, 0) + }) +} + +func (suite *OvnClientTestSuite) testIsLrpBfdUp() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + dstIP := "192.168.124.15" + minRx, minTx, detectMult := 101, 102, 19 + + t.Run("BFD is up", func(t *testing.T) { + t.Parallel() + + lrpName := "test-is-lrp-bfd-up" + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + upStatus := ovnnb.BFDStatusUp + bfd.Status = &upStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + isUp, err := ovnClient.isLrpBfdUp(lrpName, dstIP) + require.NoError(t, err) + require.True(t, isUp) + }) + + t.Run("BFD is down", func(t *testing.T) { + t.Parallel() + + lrpName := "test-is-lrp-bfd-down" + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + downStatus := ovnnb.BFDStatusDown + bfd.Status = &downStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + isUp, err := ovnClient.isLrpBfdUp(lrpName, dstIP) + require.Error(t, err) + require.False(t, isUp) + require.Contains(t, err.Error(), "status is down") + }) + + t.Run("BFD status is nil", func(t *testing.T) { + t.Parallel() + + lrpName := "test-is-lrp-bfd-status-nil" + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + bfd.Status = nil + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + isUp, err := ovnClient.isLrpBfdUp(lrpName, dstIP) + require.Error(t, err) + require.False(t, isUp) + require.Contains(t, err.Error(), "status is nil") + }) + + t.Run("No BFD exists", func(t *testing.T) { + t.Parallel() + + lrpName := "test-is-lrp-bfd-none" + isUp, err := ovnClient.isLrpBfdUp(lrpName, "192.168.124.16") + require.NoError(t, err) + require.True(t, isUp) + }) +} + +func (suite *OvnClientTestSuite) testBfdAddL3HAHandler() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + t.Run("BFD status is nil", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-add-l3ha-handler-nil" + dstIP := "192.168.124.19" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + bfd.Status = nil + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + ovnClient.bfdAddL3HAHandler(ovnnb.BFDTable, bfd) + + updatedBfd, err := ovnClient.ListBFDs(lrpName, dstIP) + require.NoError(t, err) + require.Nil(t, updatedBfd[0].Status) + }) + + t.Run("BFD status is down", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-add-l3ha-handler-down" + dstIP := "192.168.124.20" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + downStatus := ovnnb.BFDStatusDown + bfd.Status = &downStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + ovnClient.bfdAddL3HAHandler(ovnnb.BFDTable, bfd) + + updatedBfd, err := ovnClient.ListBFDs(lrpName, dstIP) + require.NoError(t, err) + require.NotNil(t, updatedBfd[0].Status) + require.Equal(t, ovnnb.BFDStatusDown, *updatedBfd[0].Status) + }) + + t.Run("BFD status is already up", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-add-l3ha-handler-up" + dstIP := "192.168.124.21" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + upStatus := ovnnb.BFDStatusUp + bfd.Status = &upStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + ovnClient.bfdAddL3HAHandler(ovnnb.BFDTable, bfd) + + updatedBfd, err := ovnClient.ListBFDs(lrpName, dstIP) + require.NoError(t, err) + require.NotNil(t, updatedBfd[0].Status) + require.Equal(t, ovnnb.BFDStatusUp, *updatedBfd[0].Status) + }) + + t.Run("Wrong table", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-add-l3ha-handler-up-wrong-table" + dstIP := "192.168.124.22" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + ovnClient.bfdAddL3HAHandler("WrongTable", bfd) + + updatedBfd, err := ovnClient.ListBFDs(lrpName, dstIP) + require.NoError(t, err) + require.Nil(t, updatedBfd[0].Status) + }) +} + +func (suite *OvnClientTestSuite) testBfdUpdateL3HAHandler() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + t.Run("BFD status change with wrong table", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-update-l3ha-handler-wrong-table" + dstIP := "192.168.124.26" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + upStatus := ovnnb.BFDStatusUp + bfd.Status = &upStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + newBfd := *bfd + downStatus := ovnnb.BFDStatusDown + newBfd.Status = &downStatus + + ovnClient.bfdUpdateL3HAHandler("WrongTable", bfd, &newBfd) + }) + + t.Run("BFD status change with nil status", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-update-l3ha-handler-nil-status" + dstIP := "192.168.124.27" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + bfd.Status = nil + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + newBfd := *bfd + downStatus := ovnnb.BFDStatusDown + newBfd.Status = &downStatus + + ovnClient.bfdUpdateL3HAHandler(ovnnb.BFDTable, bfd, &newBfd) + + updatedBfd, err := ovnClient.ListBFDs(lrpName, dstIP) + require.NoError(t, err) + require.Nil(t, updatedBfd[0].Status) + }) + + t.Run("BFD status not changed", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-update-l3ha-handler-same-status" + dstIP := "192.168.124.28" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + upStatus := ovnnb.BFDStatusUp + bfd.Status = &upStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + newBfd := *bfd + newBfd.Status = &upStatus + + ovnClient.bfdUpdateL3HAHandler(ovnnb.BFDTable, bfd, &newBfd) + }) + + t.Run("BFD status change from AdminDown to Down", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-update-l3ha-handler-admin-down-to-down" + dstIP := "192.168.124.23" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + adminDownStatus := ovnnb.BFDStatusAdminDown + bfd.Status = &adminDownStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + newBfd := *bfd + downStatus := ovnnb.BFDStatusDown + newBfd.Status = &downStatus + + ovnClient.bfdUpdateL3HAHandler(ovnnb.BFDTable, bfd, &newBfd) + }) + + t.Run("BFD status change from Down to Up", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-update-l3ha-handler-down-to-up" + dstIP := "192.168.124.24" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + downStatus := ovnnb.BFDStatusDown + bfd.Status = &downStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + newBfd := *bfd + upStatus := ovnnb.BFDStatusUp + newBfd.Status = &upStatus + + ovnClient.bfdUpdateL3HAHandler(ovnnb.BFDTable, bfd, &newBfd) + }) + + t.Run("BFD status change from Up to Down", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-update-l3ha-handler-up-to-down" + dstIP := "192.168.124.25" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + upStatus := ovnnb.BFDStatusUp + bfd.Status = &upStatus + err = ovnClient.UpdateBFD(bfd) + require.NoError(t, err) + + newBfd := *bfd + downStatus := ovnnb.BFDStatusDown + newBfd.Status = &downStatus + + ovnClient.bfdUpdateL3HAHandler(ovnnb.BFDTable, bfd, &newBfd) + }) +} + +func (suite *OvnClientTestSuite) testBfdDelL3HAHandler() { + t := suite.T() + t.Parallel() + + ovnClient := suite.ovnClient + + t.Run("BFD deletion with wrong table", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-del-l3ha-handler-wrong-table" + dstIP := "192.168.124.30" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + ovnClient.bfdDelL3HAHandler("WrongTable", bfd) + + // Verify that the BFD is not deleted + bfdList, err := ovnClient.ListBFDs(lrpName, dstIP) + require.NoError(t, err) + require.Len(t, bfdList, 1) + }) + + t.Run("BFD deletion with correct table", func(t *testing.T) { + t.Parallel() + + lrpName := "test-bfd-del-l3ha-handler-correct-table" + dstIP := "192.168.124.31" + minRx, minTx, detectMult := 101, 102, 19 + + bfd, err := ovnClient.CreateBFD(lrpName, dstIP, minRx, minTx, detectMult) + require.NoError(t, err) + require.NotNil(t, bfd) + + ovnClient.bfdDelL3HAHandler(ovnnb.BFDTable, bfd) + }) } diff --git a/pkg/ovs/ovn-nb-suite_test.go b/pkg/ovs/ovn-nb-suite_test.go index 1cd22923c51..f7fc289b717 100644 --- a/pkg/ovs/ovn-nb-suite_test.go +++ b/pkg/ovs/ovn-nb-suite_test.go @@ -288,6 +288,7 @@ func (suite *OvnClientTestSuite) Test_logicalRouterPortFilter() { suite.testlogicalRouterPortFilter() } +/* bfd unit test */ func (suite *OvnClientTestSuite) Test_CreateBFD() { suite.testCreateBFD() } @@ -300,6 +301,30 @@ func (suite *OvnClientTestSuite) Test_DeleteBFD() { suite.testDeleteBFD() } +func (suite *OvnClientTestSuite) Test_ListDownBFDs() { + suite.testListDownBFDs() +} + +func (suite *OvnClientTestSuite) Test_ListUpBFDs() { + suite.testListUpBFDs() +} + +func (suite *OvnClientTestSuite) Test_isLrpBfdUp() { + suite.testIsLrpBfdUp() +} + +func (suite *OvnClientTestSuite) Test_BfdAddL3HAHandler() { + suite.testBfdAddL3HAHandler() +} + +func (suite *OvnClientTestSuite) Test_BfdUpdateL3HAHandler() { + suite.testBfdUpdateL3HAHandler() +} + +func (suite *OvnClientTestSuite) Test_BfdDelL3HAHandler() { + suite.testBfdDelL3HAHandler() +} + /* gateway_chassis unit test */ func (suite *OvnClientTestSuite) Test_CreateGatewayChassises() { suite.testCreateGatewayChassises()