Skip to content

Commit 782fbff

Browse files
authored
Add CEL tests for snippetsFilter API (#3750)
Problem: As code owners we want to verify is API validation is working as expected for SnippetsFilter Solution: Added CEL tests for SnippetsFilter
1 parent 22f6954 commit 782fbff

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

tests/cel/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ const (
6161
expectedTargetRefNameUniqueError = `TargetRef Name must be unique`
6262
)
6363

64+
// SnippetsFilter validation errors.
65+
const (
66+
expectedSnippetsFilterContextError = `Only one snippet allowed per context`
67+
)
68+
6469
const (
6570
defaultNamespace = "default"
6671
)

tests/cel/snippetsfilter_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cel
2+
3+
import (
4+
"testing"
5+
6+
controllerruntime "sigs.k8s.io/controller-runtime"
7+
8+
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
9+
)
10+
11+
func TestSnippetsFilterValidation(t *testing.T) {
12+
t.Parallel()
13+
k8sClient := getKubernetesClient(t)
14+
15+
tests := []struct {
16+
name string
17+
wantErrors []string
18+
spec ngfAPIv1alpha1.SnippetsFilterSpec
19+
}{
20+
{
21+
name: "Validate single snippet with valid context",
22+
spec: ngfAPIv1alpha1.SnippetsFilterSpec{
23+
Snippets: []ngfAPIv1alpha1.Snippet{
24+
{
25+
Context: ngfAPIv1alpha1.NginxContextHTTP,
26+
Value: "limit_req zone=one burst=5 nodelay;",
27+
},
28+
},
29+
},
30+
},
31+
{
32+
name: "Validate multiple snippets with unique contexts",
33+
spec: ngfAPIv1alpha1.SnippetsFilterSpec{
34+
Snippets: []ngfAPIv1alpha1.Snippet{
35+
{
36+
Context: ngfAPIv1alpha1.NginxContextMain,
37+
Value: "worker_processes 4;",
38+
},
39+
{
40+
Context: ngfAPIv1alpha1.NginxContextHTTPServer,
41+
Value: "server_name example.com;",
42+
},
43+
},
44+
},
45+
},
46+
{
47+
name: "Validate duplicate contexts are not allowed",
48+
wantErrors: []string{expectedSnippetsFilterContextError},
49+
spec: ngfAPIv1alpha1.SnippetsFilterSpec{
50+
Snippets: []ngfAPIv1alpha1.Snippet{
51+
{
52+
Context: ngfAPIv1alpha1.NginxContextHTTP,
53+
Value: "limit_req zone=one burst=5 nodelay;",
54+
},
55+
{
56+
Context: ngfAPIv1alpha1.NginxContextHTTP,
57+
Value: "sendfile on;",
58+
},
59+
},
60+
},
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
t.Parallel()
67+
snippetsFilter := &ngfAPIv1alpha1.SnippetsFilter{
68+
ObjectMeta: controllerruntime.ObjectMeta{
69+
Name: uniqueResourceName(testResourceName),
70+
Namespace: defaultNamespace,
71+
},
72+
Spec: tt.spec,
73+
}
74+
validateCrd(t, tt.wantErrors, snippetsFilter, k8sClient)
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)