-
Notifications
You must be signed in to change notification settings - Fork 129
Add CEL validation test for targetRef in ClientSettingsPolicy #3623
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
Open
shaun-nx
wants to merge
12
commits into
main
Choose a base branch
from
tests/cel-clientsettingspolicies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dbb5b5c
Add CEL validation test for targetRef in ClientSettingsPolicy
shaun-nx 48ec86b
gofumpt
shaun-nx c7129bd
Add tests for targetRefGroup
shaun-nx 87a4e0e
Rename tests
shaun-nx 29f201e
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx d921ed2
Move tests into clientsettingspolicy_test.go
shaun-nx f22269a
Update tests to create a ClientSettingsPolicy resource during tests
shaun-nx 7fddffa
make lint in tests
shaun-nx dd66a79
Update tests to create a ClientSettingsPolicy object during validation
shaun-nx d664f35
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx cd9bada
Update TargetRegGroup tests
shaun-nx 7d9989d
Fix lint errors
shaun-nx 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
176 changes: 176 additions & 0 deletions
176
tests/cel/policies/clientsettingspolicies/targetRef_test.go
This file contains hidden or 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,176 @@ | ||
package clientsettingspolicies | ||
|
||
import ( | ||
"testing" | ||
|
||
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
) | ||
|
||
func TestClientSettingsPoliciesTargetRefKind(t *testing.T) { | ||
allowedKinds := map[string]bool{ | ||
"Gateway": true, | ||
"HTTPRoute": true, | ||
"GRPCRoute": true, | ||
} | ||
|
||
testValidTargetRefKind(t, allowedKinds) | ||
testInvalidTargetRefKind(t, allowedKinds) | ||
} | ||
|
||
func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) { | ||
testValidTargetRefGroup(t) | ||
testInvalidTargetRefGroup(t) | ||
} | ||
|
||
func testValidTargetRefKind(t *testing.T, allowedKinds map[string]bool) { | ||
t.Helper() | ||
|
||
tests := []struct { | ||
name string | ||
wantErrors string | ||
targetRef gatewayv1alpha2.LocalPolicyTargetReference | ||
}{ | ||
{ | ||
name: "Validate TargetRef is of an allowed kind", | ||
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", | ||
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Kind: "Gateway", | ||
Group: "gateway.networking.k8s.io", | ||
}, | ||
}, | ||
{ | ||
name: "Validate TargetRef is of an allowed kind", | ||
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", | ||
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Kind: "HTTPRoute", | ||
Group: "gateway.networking.k8s.io", | ||
}, | ||
}, | ||
{ | ||
name: "Validate TargetRef is of an allowed kind", | ||
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", | ||
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Kind: "GRPCRoute", | ||
Group: "gateway.networking.k8s.io", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok { | ||
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'" | ||
|
||
if tt.wantErrors == gotError { | ||
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testInvalidTargetRefKind(t *testing.T, allowedKinds map[string]bool) { | ||
t.Helper() | ||
|
||
tests := []struct { | ||
name string | ||
wantErrors string | ||
targetRef gatewayv1alpha2.LocalPolicyTargetReference | ||
}{ | ||
{ | ||
name: "Validate TargetRef is of an allowed kind", | ||
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", | ||
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Kind: "InvalidKind", | ||
Group: "gateway.networking.k8s.io", | ||
}, | ||
}, | ||
{ | ||
name: "Validate TargetRef is of an allowed kind", | ||
wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", | ||
targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Kind: "TCPRoute", | ||
Group: "gateway.networking.k8s.io", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok { | ||
gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'" | ||
|
||
if tt.wantErrors != gotError { | ||
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testValidTargetRefGroup(t *testing.T) { | ||
t.Helper() | ||
|
||
tests := []struct { | ||
name string | ||
wantErrors string | ||
targetRefGroup gatewayv1alpha2.LocalPolicyTargetReference | ||
}{ | ||
{ | ||
name: "Validate TargetRef group is gateway.networking.k8s.io", | ||
wantErrors: "TargetRef Group must be gateway.networking.k8s.io", | ||
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Group: "gateway.networking.k8s.io", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.targetRefGroup.Group != "gateway.networking.k8s.io" { | ||
gotError := "TargetRef Group must be gateway.networking.k8s.io" | ||
|
||
if tt.wantErrors == gotError { | ||
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testInvalidTargetRefGroup(t *testing.T) { | ||
t.Helper() | ||
|
||
tests := []struct { | ||
name string | ||
wantErrors string | ||
targetRefGroup gatewayv1alpha2.LocalPolicyTargetReference | ||
}{ | ||
{ | ||
name: "Validate TargetRef group is gateway.networking.k8s.io", | ||
wantErrors: "TargetRef Group must be gateway.networking.k8s.io", | ||
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Group: "invalid.networking.k8s.io", | ||
}, | ||
}, | ||
{ | ||
name: "Validate TargetRef is of an allowed kind", | ||
wantErrors: "TargetRef Group must be gateway.networking.k8s.io", | ||
targetRefGroup: gatewayv1alpha2.LocalPolicyTargetReference{ | ||
Group: "discovery.k8s.io/v1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.targetRefGroup.Group != "gateway.networking.k8s.io" { | ||
gotError := "TargetRef Group must be gateway.networking.k8s.io" | ||
|
||
if tt.wantErrors != gotError { | ||
t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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
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 should stay as-is.
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.
Thanks @sjberman
I'm not sure why that updated originally. Just so I know, what is the impact of this version being
v1.6.2
vsv0.0.0
?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.
Functionally it may not matter since we perform the
replace
above, but it's a little confusing IMO versus just pointing to an empty version to show that we're just importing whatever the same version is that we have checked out.