Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ut for ovn-nb-logical_router_policy #4693

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions pkg/ovs/ovn-nb-logical_router_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions pkg/ovs/ovn-nb-suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand Down
Loading