Skip to content

Commit 789910a

Browse files
committed
Add tests for tracing ratio strategy
1 parent 6dac6fa commit 789910a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tests/cel/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ const (
4444
expectedMinReplicasLessThanOrEqualError = `minReplicas must be less than or equal to maxReplicas`
4545
)
4646

47+
// ObservabilityPolicy validation errors.
4748
const (
4849
expectedTargetRefMustBeHTTPRouteOrGrpcRouteError = `TargetRef Kind must be: HTTPRoute or GRPCRoute`
4950
expectedTargetRefKindAndNameComboMustBeUnique = `TargetRef Kind and Name combination must be unique`
51+
expectedStrategyMustBeOfTypeRatio = `ratio can only be specified if strategy is of type ratio`
5052
)
5153

5254
const (

tests/cel/observabilitypolicy_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
88

99
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha2"
10+
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
1011
)
1112

1213
func TestObservabilityPoliciesTargetRefKind(t *testing.T) {
@@ -227,3 +228,66 @@ func TestObservabilityPoliciesTargetRefKindAndNameCombo(t *testing.T) {
227228
})
228229
}
229230
}
231+
232+
func TestObservabilityPoliciesTracing(t *testing.T) {
233+
t.Parallel()
234+
k8sClient := getKubernetesClient(t)
235+
236+
tests := []struct {
237+
spec ngfAPIv1alpha2.ObservabilityPolicySpec
238+
name string
239+
wantErrors []string
240+
}{
241+
{
242+
name: "Validate ObservabilityPolicy is applied when ratio is set and strategy is TraceStrategyRatio",
243+
spec: ngfAPIv1alpha2.ObservabilityPolicySpec{
244+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
245+
{
246+
Kind: httpRouteKind,
247+
Group: gatewayGroup,
248+
},
249+
},
250+
Tracing: &ngfAPIv1alpha2.Tracing{
251+
Strategy: ngfAPIv1alpha2.TraceStrategyRatio,
252+
Ratio: helpers.GetPointer[int32](50),
253+
},
254+
},
255+
},
256+
{
257+
name: "Validate ObservabilityPolicy is invalid when ratio is set and strategy is not TraceStrategyRatio",
258+
wantErrors: []string{expectedStrategyMustBeOfTypeRatio},
259+
spec: ngfAPIv1alpha2.ObservabilityPolicySpec{
260+
TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReference{
261+
{
262+
Kind: httpRouteKind,
263+
Group: gatewayGroup,
264+
},
265+
},
266+
Tracing: &ngfAPIv1alpha2.Tracing{
267+
Strategy: ngfAPIv1alpha2.TraceStrategyParent,
268+
Ratio: helpers.GetPointer[int32](50),
269+
},
270+
},
271+
},
272+
}
273+
274+
for _, tt := range tests {
275+
t.Run(tt.name, func(t *testing.T) {
276+
t.Parallel()
277+
spec := tt.spec
278+
279+
for i := range spec.TargetRefs {
280+
spec.TargetRefs[i].Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
281+
}
282+
283+
observabilityPolicy := &ngfAPIv1alpha2.ObservabilityPolicy{
284+
ObjectMeta: controllerruntime.ObjectMeta{
285+
Name: uniqueResourceName(testResourceName),
286+
Namespace: defaultNamespace,
287+
},
288+
Spec: spec,
289+
}
290+
validateCrd(t, tt.wantErrors, observabilityPolicy, k8sClient)
291+
})
292+
}
293+
}

0 commit comments

Comments
 (0)