-
Notifications
You must be signed in to change notification settings - Fork 9
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
Support set-local-pref and set-med #238
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12c87e3
Support set-local-ref and set-med
wenovus fc04546
Update BUILD file
wenovus 8c8ed41
Refactor
wenovus 8c6d9c6
Merge branch 'refactor-policy-tests' into local-ref-med
wenovus 5e8259a
Merge branch 'refactor-policy-tests' into local-ref-med
wenovus 58f3e52
Merge branch 'refactor-policy-tests' into local-ref-med
wenovus cb4ca46
Merge branch 'refactor-policy-tests' into local-ref-med
wenovus 54bad46
Merge branch 'refactor-policy-tests' into local-ref-med
wenovus 7fcfb21
Add comment on switch
wenovus 9012f81
miinor comment fix
wenovus 259dabe
Merge branch 'main' into local-ref-med
wenovus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package local_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/openconfig/lemming/bgp" | ||
"github.com/openconfig/lemming/gnmi/oc" | ||
"github.com/openconfig/lemming/gnmi/oc/ocpath" | ||
"github.com/openconfig/ygot/ygot" | ||
|
||
valpb "github.com/openconfig/lemming/bgp/tests/proto/policyval" | ||
) | ||
|
||
const ( | ||
acceptedCommunitySet = oc.UnionString("23456:23456") | ||
rejectedCommunitySet = oc.UnionString("12345:12345") | ||
lowerLocalPref = 41 | ||
higherLocalPref = 42 | ||
lowerMED = oc.UnionUint32(10) | ||
higherMED = oc.UnionUint32(11) | ||
) | ||
|
||
// TestAttributes tests BGP attributes. | ||
// - set-community and community set. | ||
// - set-local-pref and local pref. | ||
// | ||
// DUT2's import policy from DUT1 sets the attribute values. | ||
// DUT2's export policy to DUT3 filters the prefix with the attribute value. | ||
func TestAttributes(t *testing.T) { | ||
routeList := []string{ | ||
"10.1.0.0/16", | ||
"10.2.0.0/16", | ||
"10.10.0.0/16", | ||
"10.11.0.0/16", | ||
"10.12.0.0/16", | ||
} | ||
testPolicy(t, PolicyTestCase{ | ||
spec: &valpb.PolicyTestCase{ | ||
Description: "Test that one NLRI gets accepted and the otheris rejected via various attribute values.", | ||
RouteTests: []*valpb.RouteTestCase{{ | ||
Description: "Accepted route with no attributes", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[0], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
}, { | ||
Description: "Accepted route with some attributes", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[1], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
}, { | ||
Description: "Rejected route due to community set", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[2], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_DISCARD, | ||
}, { | ||
Description: "Unpreferred route due to local-pref", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[3], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_NOT_PREFERRED, | ||
}, { | ||
Description: "Unpreferred route due to MED", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[4], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_NOT_PREFERRED, | ||
}}, | ||
LongerPathRouteTests: []*valpb.RouteTestCase{{ | ||
Description: "Accepted route due to higher local-pref", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[3], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_NOT_PREFERRED, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
}}, | ||
AlternatePathRouteTests: []*valpb.RouteTestCase{{ | ||
Description: "Accepted route due to lower MED", | ||
Input: &valpb.TestRoute{ | ||
ReachPrefix: routeList[4], | ||
}, | ||
ExpectedResultBeforePolicy: valpb.RouteTestResult_ROUTE_TEST_RESULT_NOT_PREFERRED, | ||
ExpectedResult: valpb.RouteTestResult_ROUTE_TEST_RESULT_ACCEPT, | ||
}}, | ||
}, | ||
installPolicies: func(t *testing.T, dut1, dut2, dut3, dut4, dut5 *Device) { | ||
if debug { | ||
fmt.Println("Installing test policies") | ||
} | ||
|
||
dut1PolicyName := "set-attributes-dut1" | ||
dut1Policy := &oc.RoutingPolicy_PolicyDefinition_Statement_OrderedMap{} | ||
dut5PolicyName := "set-attributes-dut5" | ||
dut5Policy := &oc.RoutingPolicy_PolicyDefinition_Statement_OrderedMap{} | ||
rejectPolicyName := "export-policy" | ||
rejectPolicy := &oc.RoutingPolicy_PolicyDefinition_Statement_OrderedMap{} | ||
|
||
for i, route := range routeList { | ||
// Create prefix set | ||
prefixSetName := "accept-" + route | ||
prefixPath := ocpath.Root().RoutingPolicy().DefinedSets().PrefixSet(prefixSetName).Prefix(route, "exact").IpPrefix() | ||
Replace(t, dut1, prefixPath.Config(), route) | ||
Replace(t, dut5, prefixPath.Config(), route) | ||
|
||
var installDut1Stmt bool | ||
dut1Stmt := &oc.RoutingPolicy_PolicyDefinition_Statement{Name: ygot.String(route + "-setattr-policy-dut1")} | ||
dut1Stmt.GetOrCreateConditions().GetOrCreateMatchPrefixSet().SetPrefixSet(prefixSetName) | ||
dut1Stmt.GetOrCreateConditions().GetOrCreateMatchPrefixSet().SetMatchSetOptions(oc.RoutingPolicy_MatchSetOptionsRestrictedType_ANY) | ||
|
||
var installDut5Stmt bool | ||
dut5Stmt := &oc.RoutingPolicy_PolicyDefinition_Statement{Name: ygot.String(route + "-setattr-policy-dut5")} | ||
dut5Stmt.GetOrCreateConditions().GetOrCreateMatchPrefixSet().SetPrefixSet(prefixSetName) | ||
dut5Stmt.GetOrCreateConditions().GetOrCreateMatchPrefixSet().SetMatchSetOptions(oc.RoutingPolicy_MatchSetOptionsRestrictedType_ANY) | ||
|
||
var installRejectStmt bool | ||
rejectStmt := &oc.RoutingPolicy_PolicyDefinition_Statement{Name: ygot.String(route + "-reject-policy")} | ||
|
||
// Create the corresponding set and filter policies for each test route. | ||
switch i { | ||
case 1: | ||
// Set communities | ||
installDut1Stmt = true | ||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().SetOptions(oc.BgpPolicy_BgpSetCommunityOptionType_ADD) | ||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().GetOrCreateInline().SetCommunities( | ||
[]oc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{ | ||
acceptedCommunitySet, | ||
}, | ||
) | ||
|
||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetLocalPref(higherLocalPref) | ||
case 2: | ||
// Set communities | ||
installDut1Stmt = true | ||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().SetOptions(oc.BgpPolicy_BgpSetCommunityOptionType_ADD) | ||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().GetOrCreateInline().SetCommunities( | ||
[]oc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{ | ||
rejectedCommunitySet, | ||
}, | ||
) | ||
|
||
// Create community set | ||
installRejectStmt = true | ||
rejectCommSetName := "reject-community-set" | ||
rejCommMemberPath := ocpath.Root().RoutingPolicy().DefinedSets().BgpDefinedSets().CommunitySet(rejectCommSetName).CommunityMember() | ||
Replace(t, dut2, rejCommMemberPath.Config(), []oc.RoutingPolicy_DefinedSets_BgpDefinedSets_CommunitySet_CommunityMember_Union{ | ||
rejectedCommunitySet, | ||
}) | ||
Replace(t, dut2, ocpath.Root().RoutingPolicy().DefinedSets().BgpDefinedSets().CommunitySet(rejectCommSetName).MatchSetOptions().Config(), oc.RoutingPolicy_MatchSetOptionsType_ANY) | ||
|
||
// Match on given list of community set members. | ||
rejectStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetCommunitySet(rejectCommSetName) | ||
case 3: | ||
// Set local-pref | ||
installDut1Stmt = true | ||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetLocalPref(lowerLocalPref) | ||
|
||
installDut5Stmt = true | ||
dut5Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetLocalPref(higherLocalPref) | ||
case 4: | ||
// Set MED | ||
installDut1Stmt = true | ||
dut1Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetMed(higherMED) | ||
|
||
installDut5Stmt = true | ||
dut5Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetMed(lowerMED) | ||
} | ||
if installDut1Stmt { | ||
dut1Stmt.GetOrCreateActions().SetPolicyResult(oc.RoutingPolicy_PolicyResultType_ACCEPT_ROUTE) | ||
if err := dut1Policy.Append(dut1Stmt); err != nil { | ||
t.Fatalf("Cannot append new BGP policy statement: %v", err) | ||
} | ||
} | ||
if installDut5Stmt { | ||
dut5Stmt.GetOrCreateActions().SetPolicyResult(oc.RoutingPolicy_PolicyResultType_ACCEPT_ROUTE) | ||
if err := dut5Policy.Append(dut5Stmt); err != nil { | ||
t.Fatalf("Cannot append new BGP policy statement: %v", err) | ||
} | ||
} | ||
if installRejectStmt { | ||
rejectStmt.GetOrCreateActions().SetPolicyResult(oc.RoutingPolicy_PolicyResultType_REJECT_ROUTE) | ||
if err := rejectPolicy.Append(rejectStmt); err != nil { | ||
t.Fatalf("Cannot append new BGP policy statement: %v", err) | ||
} | ||
} | ||
} | ||
// Install export policies | ||
Replace(t, dut1, ocpath.Root().RoutingPolicy().PolicyDefinition(dut1PolicyName).Config(), &oc.RoutingPolicy_PolicyDefinition{Name: ygot.String(dut1PolicyName), Statement: dut1Policy}) | ||
Replace(t, dut1, bgp.BGPPath.Neighbor(dut2.RouterID).ApplyPolicy().ExportPolicy().Config(), []string{dut1PolicyName}) | ||
Replace(t, dut5, ocpath.Root().RoutingPolicy().PolicyDefinition(dut5PolicyName).Config(), &oc.RoutingPolicy_PolicyDefinition{Name: ygot.String(dut5PolicyName), Statement: dut5Policy}) | ||
Replace(t, dut5, bgp.BGPPath.Neighbor(dut2.RouterID).ApplyPolicy().ExportPolicy().Config(), []string{dut5PolicyName}) | ||
// Install import policies | ||
Replace(t, dut2, ocpath.Root().RoutingPolicy().PolicyDefinition(rejectPolicyName).Config(), &oc.RoutingPolicy_PolicyDefinition{Name: ygot.String(rejectPolicyName), Statement: rejectPolicy}) | ||
Replace(t, dut2, bgp.BGPPath.Neighbor(dut1.RouterID).ApplyPolicy().ImportPolicy().Config(), []string{rejectPolicyName}) | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't very clear. i don't see why i == 1 means set communites
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment. Each index refers to an input test route, and each one has its corresponding policies that applies to it. Each one is essentially a subtest that re-uses the topology.