-
Notifications
You must be signed in to change notification settings - Fork 130
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
62 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 9274e57
Group valid and invalid test cases into single test function
shaun-nx 506b01c
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx f99ab29
Revert dependency version
shaun-nx ad80b70
Move imports
shaun-nx e5479fb
Define constants for Kinds and Groups
shaun-nx 1789701
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx aa4a6b2
Use controller-runtime library to get cluster information
shaun-nx 8f74214
Add Makefile targets to run CEL test and re format test
shaun-nx 4486bd5
Ensure TestClientSettingsPoliciesTargetRefKind and TestClientSettings…
shaun-nx 1adf168
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 7c6dba4
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 69f485b
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 1ad24c4
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx aa5adc6
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 29f26ef
Update test to use `gomega` test library
shaun-nx f26eea3
Add ci workflow job for cel tests
shaun-nx efa4a81
Add new line
shaun-nx d25661a
Add `Deploy Kubernetes` step
shaun-nx c413895
Add step to apply CRDs in cel-tests CI job
shaun-nx 9962e03
Add `--server-side` to apply command
shaun-nx feca4ed
Add `working-directory` to test run and remove code coverage steps
shaun-nx 8d84e7b
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx ddce60f
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx d518ada
Add t.Parallel()
shaun-nx 39a6b38
Use g.Expect for errors and ensure unique policy names
shaun-nx 5079c21
Return and assert errors from `getKubernetesClient`
shaun-nx ce0960b
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 248df53
Remove nil error checks
shaun-nx c5860e2
Create and return client on same line
shaun-nx 83139ce
Use `rand.Prime` to attempt to keep policy names unique for each para…
shaun-nx a4e58ee
Remove unused dependency
shaun-nx 339e6b8
Fix imports
shaun-nx 716d051
Add helper functions to gernated unique resource names
shaun-nx d273281
Move `getKubernetesClient` function to helpers
shaun-nx 4a6e84e
Fix test failures
shaun-nx 66b7105
Update `TestMustCreateKubernetesClient` to only assert client creation
shaun-nx 510aac1
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx c45403a
Remove test for creating kubernetes cluster
shaun-nx e284740
Add common finctions for cel tests
shaun-nx 06adc0e
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx cb43b52
Move common constatns to `common.go` and add v1alpha2 to client schema
shaun-nx 9217339
Move helper functions to common.go and add tests
shaun-nx f30f117
unexport consnts and functions in cel common file
shaun-nx e18b9dc
unexport randomPrimeNumber and uniqueResourceName
shaun-nx 869a091
Fix casing in comments
shaun-nx 92caf12
Remove duplicate error checks
shaun-nx 81fd868
Initialise ginko and k8sClient at the start of each test
shaun-nx 94a8dd0
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 801d2f3
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx 37c85d2
Add timeout to k8sClient Create
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.
Uh oh!
There was an error while loading. Please reload this page.