diff --git a/pkg/ovs/ovn-nb-logical_router_policy_test.go b/pkg/ovs/ovn-nb-logical_router_policy_test.go index b571db7c4bd..7c712a97a66 100644 --- a/pkg/ovs/ovn-nb-logical_router_policy_test.go +++ b/pkg/ovs/ovn-nb-logical_router_policy_test.go @@ -49,6 +49,10 @@ func (suite *OvnClientTestSuite) testAddLogicalRouterPolicy() { err = nbClient.AddLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) require.NoError(t, err) + + action = ovnnb.LogicalRouterPolicyActionDrop + err = nbClient.AddLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) + require.NoError(t, err) } func (suite *OvnClientTestSuite) testCreateLogicalRouterPolicies() { @@ -248,6 +252,7 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterPolicy() { lrName := "test_get_policy_lr" priority := 11000 match := "ip4.src == $ovn.default.lm2_ip4" + fakeMatch := "ip4.dst == $ovn.default.lm2_ip4" err := nbClient.CreateLogicalRouter(lrName) require.NoError(t, err) @@ -282,6 +287,14 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterPolicy() { require.NoError(t, err) }) + t.Run("should no err and no policy for ignoreNotFound=true", func(t *testing.T) { + t.Parallel() + + plcList, err := nbClient.GetLogicalRouterPolicy(lrName, priority, fakeMatch, true) + require.Nil(t, plcList) + require.NoError(t, err) + }) + t.Run("no policy belongs to parent exist", func(t *testing.T) { t.Parallel() @@ -290,6 +303,92 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterPolicy() { }) } +func (suite *OvnClientTestSuite) testGetLogicalRouterPolicyByUUID() { + t := suite.T() + t.Parallel() + + nbClient := suite.ovnNBClient + lrName := "test-get-policy-by-uuid-lr" + priority := 11011 + match := "ip4.src == $ovn.default.lm2_ip4" + action := ovnnb.LogicalRouterPolicyActionAllow + nextHops := []string{"100.64.0.2"} + + err := nbClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + + err = nbClient.AddLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) + require.NoError(t, err) + + lr, err := nbClient.GetLogicalRouter(lrName, false) + require.NoError(t, err) + + policyList, err := nbClient.GetLogicalRouterPolicy(lrName, priority, match, false) + require.NoError(t, err) + require.Len(t, policyList, 1) + require.Contains(t, lr.Policies, policyList[0].UUID) + + t.Run("get lrp with right uuid", func(t *testing.T) { + t.Parallel() + + _, err = nbClient.GetLogicalRouterPolicyByUUID(policyList[0].UUID) + require.NoError(t, err) + }) + + t.Run("get lrp with wrong uuid", func(t *testing.T) { + t.Parallel() + + policy, err := nbClient.GetLogicalRouterPolicyByUUID("1234334") + require.Nil(t, policy) + require.NotNil(t, err) + }) +} + +func (suite *OvnClientTestSuite) testGetLogicalRouterPolicyByExtID() { + t := suite.T() + t.Parallel() + + nbClient := suite.ovnNBClient + lrName := "test-get-policy-by-extid-lr" + priority := 11011 + match := "ip4.src == $ovn.default.lm2_ip4" + action := ovnnb.LogicalRouterPolicyActionAllow + nextHops := []string{"100.64.0.2"} + extID := map[string]string{ + "vendor": "kube-ovn", + } + + err := nbClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + + err = nbClient.AddLogicalRouterPolicy(lrName, priority, match, action, nextHops, extID) + require.NoError(t, err) + + lr, err := nbClient.GetLogicalRouter(lrName, false) + require.NoError(t, err) + + policyList, err := nbClient.GetLogicalRouterPolicy(lrName, priority, match, false) + require.NoError(t, err) + require.Len(t, policyList, 1) + require.Contains(t, lr.Policies, policyList[0].UUID) + + t.Run("get lrp with right extID", func(t *testing.T) { + t.Parallel() + + pList, err := nbClient.GetLogicalRouterPoliciesByExtID(lrName, "vendor", "kube-ovn") + require.NoError(t, err) + require.Len(t, pList, 1) + }) + + t.Run("get lrp with wrong extID", func(t *testing.T) { + t.Parallel() + + pList, err := nbClient.GetLogicalRouterPoliciesByExtID(lrName, "vendor", "other") + require.NoError(t, err) + require.Len(t, pList, 0) + }) +} + func (suite *OvnClientTestSuite) testNewLogicalRouterPolicy() { t := suite.T() t.Parallel() @@ -385,3 +484,64 @@ func (suite *OvnClientTestSuite) testPolicyFilter() { require.False(t, filterFunc(policy)) }) } + +func (suite *OvnClientTestSuite) testDeleteRouterPolicy() { + t := suite.T() + t.Parallel() + + nbClient := suite.ovnNBClient + lrName := "test-delete-policy-lr" + priority := 11011 + match := "ip4.src == $ovn.default.lm2_ip4" + action := ovnnb.LogicalRouterPolicyActionAllow + nextHops := []string{"100.64.0.2"} + + err := nbClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + + err = nbClient.AddLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) + require.NoError(t, err) + + lr, err := nbClient.GetLogicalRouter(lrName, false) + require.NoError(t, err) + + policyList, err := nbClient.GetLogicalRouterPolicy(lrName, priority, match, false) + require.NoError(t, err) + require.Len(t, policyList, 1) + require.Contains(t, lr.Policies, policyList[0].UUID) + + err = nbClient.DeleteRouterPolicy(lr, policyList[0].UUID) + require.NoError(t, err) +} + +func (suite *OvnClientTestSuite) testDeleteLogicalRouterPolicyByNexthop() { + t := suite.T() + t.Parallel() + + nbClient := suite.ovnNBClient + lrName := "test-delete-policy-by-next-hop-lr" + priority := 11011 + match := "ip4.src == $ovn.default.lm2_ip4" + action := ovnnb.LogicalRouterPolicyActionAllow + nextHops := []string{"100.64.0.2"} + + err := nbClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + + err = nbClient.AddLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) + require.NoError(t, err) + + lr, err := nbClient.GetLogicalRouter(lrName, false) + require.NoError(t, err) + + policyList, err := nbClient.GetLogicalRouterPolicy(lrName, priority, match, false) + require.NoError(t, err) + require.Len(t, policyList, 1) + require.Contains(t, lr.Policies, policyList[0].UUID) + + err = nbClient.DeleteLogicalRouterPolicyByNexthop(lrName, priority, nextHops[0]) + require.NoError(t, err) + + err = nbClient.DeleteLogicalRouterPolicyByNexthop(lrName, priority+1, nextHops[0]) + require.NoError(t, err) +} diff --git a/pkg/ovs/ovn-nb-suite_test.go b/pkg/ovs/ovn-nb-suite_test.go index bcaba2144ea..014fbb96a2d 100644 --- a/pkg/ovs/ovn-nb-suite_test.go +++ b/pkg/ovs/ovn-nb-suite_test.go @@ -786,6 +786,14 @@ func (suite *OvnClientTestSuite) Test_DeleteLogicalRouterPolicies() { suite.testDeleteLogicalRouterPolicies() } +func (suite *OvnClientTestSuite) Test_DeleteLogicalRouterPoliciesByNexthop() { + suite.testDeleteLogicalRouterPolicyByNexthop() +} + +func (suite *OvnClientTestSuite) Test_DeleteRouterPolicy() { + suite.testDeleteRouterPolicy() +} + func (suite *OvnClientTestSuite) Test_ClearLogicalRouterPolicy() { suite.testClearLogicalRouterPolicy() } @@ -794,6 +802,14 @@ func (suite *OvnClientTestSuite) Test_GetLogicalRouterPolicy() { suite.testGetLogicalRouterPolicy() } +func (suite *OvnClientTestSuite) Test_GetLogicalRouterPolicyByUUID() { + suite.testGetLogicalRouterPolicyByUUID() +} + +func (suite *OvnClientTestSuite) Test_GetLogicalRouterPolicyByExtID() { + suite.testGetLogicalRouterPolicyByExtID() +} + func (suite *OvnClientTestSuite) Test_NewLogicalRouterPolicy() { suite.testNewLogicalRouterPolicy() }