Skip to content

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 62 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
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 Jul 14, 2025
48ec86b
gofumpt
shaun-nx Jul 14, 2025
c7129bd
Add tests for targetRefGroup
shaun-nx Jul 14, 2025
87a4e0e
Rename tests
shaun-nx Jul 14, 2025
29f201e
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 15, 2025
d921ed2
Move tests into clientsettingspolicy_test.go
shaun-nx Jul 15, 2025
f22269a
Update tests to create a ClientSettingsPolicy resource during tests
shaun-nx Jul 15, 2025
7fddffa
make lint in tests
shaun-nx Jul 15, 2025
dd66a79
Update tests to create a ClientSettingsPolicy object during validation
shaun-nx Jul 17, 2025
d664f35
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 17, 2025
cd9bada
Update TargetRegGroup tests
shaun-nx Jul 17, 2025
7d9989d
Fix lint errors
shaun-nx Jul 17, 2025
9274e57
Group valid and invalid test cases into single test function
shaun-nx Jul 21, 2025
506b01c
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 21, 2025
f99ab29
Revert dependency version
shaun-nx Jul 21, 2025
ad80b70
Move imports
shaun-nx Jul 21, 2025
e5479fb
Define constants for Kinds and Groups
shaun-nx Jul 21, 2025
1789701
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 22, 2025
aa4a6b2
Use controller-runtime library to get cluster information
shaun-nx Jul 22, 2025
8f74214
Add Makefile targets to run CEL test and re format test
shaun-nx Jul 22, 2025
4486bd5
Ensure TestClientSettingsPoliciesTargetRefKind and TestClientSettings…
shaun-nx Jul 22, 2025
1adf168
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 22, 2025
7c6dba4
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 23, 2025
69f485b
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 23, 2025
1ad24c4
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 25, 2025
aa5adc6
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 28, 2025
29f26ef
Update test to use `gomega` test library
shaun-nx Jul 28, 2025
f26eea3
Add ci workflow job for cel tests
shaun-nx Jul 28, 2025
efa4a81
Add new line
shaun-nx Jul 28, 2025
d25661a
Add `Deploy Kubernetes` step
shaun-nx Jul 28, 2025
c413895
Add step to apply CRDs in cel-tests CI job
shaun-nx Jul 28, 2025
9962e03
Add `--server-side` to apply command
shaun-nx Jul 28, 2025
feca4ed
Add `working-directory` to test run and remove code coverage steps
shaun-nx Jul 28, 2025
8d84e7b
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 28, 2025
ddce60f
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 29, 2025
d518ada
Add t.Parallel()
shaun-nx Jul 29, 2025
39a6b38
Use g.Expect for errors and ensure unique policy names
shaun-nx Jul 29, 2025
5079c21
Return and assert errors from `getKubernetesClient`
shaun-nx Jul 29, 2025
ce0960b
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 29, 2025
248df53
Remove nil error checks
shaun-nx Jul 29, 2025
c5860e2
Create and return client on same line
shaun-nx Jul 29, 2025
83139ce
Use `rand.Prime` to attempt to keep policy names unique for each para…
shaun-nx Jul 29, 2025
a4e58ee
Remove unused dependency
shaun-nx Jul 29, 2025
339e6b8
Fix imports
shaun-nx Jul 29, 2025
716d051
Add helper functions to gernated unique resource names
shaun-nx Jul 30, 2025
d273281
Move `getKubernetesClient` function to helpers
shaun-nx Jul 30, 2025
4a6e84e
Fix test failures
shaun-nx Jul 30, 2025
66b7105
Update `TestMustCreateKubernetesClient` to only assert client creation
shaun-nx Jul 30, 2025
510aac1
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 30, 2025
c45403a
Remove test for creating kubernetes cluster
shaun-nx Jul 30, 2025
e284740
Add common finctions for cel tests
shaun-nx Jul 30, 2025
06adc0e
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 30, 2025
cb43b52
Move common constatns to `common.go` and add v1alpha2 to client schema
shaun-nx Jul 30, 2025
9217339
Move helper functions to common.go and add tests
shaun-nx Jul 30, 2025
f30f117
unexport consnts and functions in cel common file
shaun-nx Jul 30, 2025
e18b9dc
unexport randomPrimeNumber and uniqueResourceName
shaun-nx Jul 30, 2025
869a091
Fix casing in comments
shaun-nx Jul 30, 2025
92caf12
Remove duplicate error checks
shaun-nx Jul 31, 2025
81fd868
Initialise ginko and k8sClient at the start of each test
shaun-nx Jul 31, 2025
94a8dd0
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Jul 31, 2025
801d2f3
Merge branch 'main' into tests/cel-clientsettingspolicies
shaun-nx Aug 6, 2025
37c85d2
Add timeout to k8sClient Create
shaun-nx Aug 6, 2025
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
176 changes: 176 additions & 0 deletions tests/cel/policies/clientsettingspolicies/targetRef_test.go
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)
}
}
})
}
}
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.24.2
replace github.com/nginx/nginx-gateway-fabric => ../

require (
github.com/nginx/nginx-gateway-fabric v0.0.0
github.com/nginx/nginx-gateway-fabric v1.6.2
github.com/onsi/ginkgo/v2 v2.23.4
github.com/onsi/gomega v1.37.0
github.com/prometheus/client_golang v1.22.0
Expand Down
Loading