Skip to content

Commit b318753

Browse files
committed
Add tests for ObservabilityPolicy CEL validation
1 parent 003eccc commit b318753

File tree

2 files changed

+187
-1
lines changed

2 files changed

+187
-1
lines changed

tests/cel/common.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333
// ClientSettingsPolicy validation errors.
3434
const (
3535
expectedTargetRefKindError = `TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute`
36-
expectedTargetRefGroupError = `TargetRef Group must be gateway.networking.k8s.io.`
36+
expectedTargetRefGroupError = `TargetRef Group must be gateway.networking.k8s.io`
3737
expectedHeaderWithoutServerError = `header can only be specified if server is specified`
3838
)
3939

@@ -44,6 +44,10 @@ const (
4444
expectedMinReplicasLessThanOrEqualError = `minReplicas must be less than or equal to maxReplicas`
4545
)
4646

47+
const (
48+
expectedTargetRefMustBeHTTPRouteOrGrpcRouteError = `TargetRef Kind must be: HTTPRoute or GRPCRoute`
49+
)
50+
4751
const (
4852
defaultNamespace = "default"
4953
)

tests/cel/observabilitypolicy_test.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package cel
2+
3+
import (
4+
"testing"
5+
6+
controllerruntime "sigs.k8s.io/controller-runtime"
7+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
8+
9+
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
10+
)
11+
12+
func TestObservabilityPoliciesTargetRefKind(t *testing.T) {
13+
t.Parallel()
14+
k8sClient := getKubernetesClient(t)
15+
16+
tests := []struct {
17+
spec ngfAPIv1alpha1.ObservabilityPolicySpec
18+
name string
19+
wantErrors []string
20+
}{
21+
{
22+
name: "Validate TargetRef of kind HTTPRoute is allowed",
23+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
24+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
25+
{
26+
Kind: httpRouteKind,
27+
Group: gatewayGroup,
28+
},
29+
},
30+
},
31+
},
32+
{
33+
name: "Validate TargetRef of kind GRPCRoute is allowed",
34+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
35+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
36+
{
37+
Kind: grpcRouteKind,
38+
Group: gatewayGroup,
39+
},
40+
},
41+
},
42+
},
43+
{
44+
name: "Validate Invalid TargetRef Kind is not allowed",
45+
wantErrors: []string{expectedTargetRefMustBeHTTPRouteOrGrpcRouteError},
46+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
47+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
48+
{
49+
Kind: invalidKind,
50+
Group: gatewayGroup,
51+
},
52+
},
53+
},
54+
},
55+
{
56+
name: "Validate TCPRoute TargetRef Kind is not allowed",
57+
wantErrors: []string{expectedTargetRefMustBeHTTPRouteOrGrpcRouteError},
58+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
59+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
60+
{
61+
Kind: tcpRouteKind,
62+
Group: gatewayGroup,
63+
},
64+
},
65+
},
66+
},
67+
{
68+
name: "Validate TargetRef of kind Gateway is allowed",
69+
wantErrors: []string{expectedTargetRefMustBeHTTPRouteOrGrpcRouteError},
70+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
71+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
72+
{
73+
Kind: gatewayKind,
74+
Group: gatewayGroup,
75+
},
76+
},
77+
},
78+
},
79+
{
80+
name: "Validate ObservabilityPolicy is applied when one TargetRef is valid and another is invalid",
81+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
82+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
83+
{
84+
Kind: gatewayKind,
85+
Group: gatewayGroup,
86+
},
87+
{
88+
Kind: grpcRouteKind,
89+
Group: gatewayGroup,
90+
},
91+
},
92+
},
93+
},
94+
}
95+
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
t.Parallel()
99+
spec := tt.spec
100+
101+
for i := range spec.TargetRefs {
102+
spec.TargetRefs[i].Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
103+
}
104+
105+
observabilityPolicy := &ngfAPIv1alpha1.ObservabilityPolicy{
106+
ObjectMeta: controllerruntime.ObjectMeta{
107+
Name: uniqueResourceName(testResourceName),
108+
Namespace: defaultNamespace,
109+
},
110+
Spec: spec,
111+
}
112+
validateCrd(t, tt.wantErrors, observabilityPolicy, k8sClient)
113+
})
114+
}
115+
}
116+
117+
func TestObservabilityPoliciesTargetRefGroup(t *testing.T) {
118+
t.Parallel()
119+
k8sClient := getKubernetesClient(t)
120+
121+
tests := []struct {
122+
spec ngfAPIv1alpha1.ObservabilityPolicySpec
123+
name string
124+
wantErrors []string
125+
}{
126+
{
127+
name: "Validate gateway.networking.k8s.io TargetRef Group is allowed",
128+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
129+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
130+
{
131+
Kind: httpRouteKind,
132+
Group: gatewayGroup,
133+
},
134+
},
135+
},
136+
},
137+
{
138+
name: "Validate invalid.networking.k8s.io TargetRef Group is not allowed",
139+
wantErrors: []string{expectedTargetRefGroupError},
140+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
141+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
142+
{
143+
Kind: httpRouteKind,
144+
Group: invalidGroup,
145+
},
146+
},
147+
},
148+
},
149+
{
150+
name: "Validate discovery.k8s.io/v1 TargetRef Group is not allowed",
151+
wantErrors: []string{expectedTargetRefGroupError},
152+
spec: ngfAPIv1alpha1.ObservabilityPolicySpec{
153+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
154+
{
155+
Kind: httpRouteKind,
156+
Group: discoveryGroup,
157+
},
158+
},
159+
},
160+
},
161+
}
162+
163+
for _, tt := range tests {
164+
t.Run(tt.name, func(t *testing.T) {
165+
t.Parallel()
166+
spec := tt.spec
167+
168+
for i := range spec.TargetRefs {
169+
spec.TargetRefs[i].Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
170+
}
171+
172+
observabilityPolicy := &ngfAPIv1alpha1.ObservabilityPolicy{
173+
ObjectMeta: controllerruntime.ObjectMeta{
174+
Name: uniqueResourceName(testResourceName),
175+
Namespace: defaultNamespace,
176+
},
177+
Spec: spec,
178+
}
179+
validateCrd(t, tt.wantErrors, observabilityPolicy, k8sClient)
180+
})
181+
}
182+
}

0 commit comments

Comments
 (0)