Skip to content

Commit

Permalink
Add Additional API Testing for GEP-3171 (#3302)
Browse files Browse the repository at this point in the history
* Make HTTPRequestMirrorFilter's fraction field a pointer

* Update and rename grcproute_experimental_test.go to grpcroute_experimental_test.go

* Add more tests to TestHTTPRequestMirrorFilterExperimental

* Ran hack/update-codegen.sh

* Fix typo

* Fix typo in httproute_experimental_test.go

* Fix boilerplate header for grpcroute_experimental_test.go
  • Loading branch information
jakebennert committed Aug 28, 2024
1 parent 650e404 commit d416ed4
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 95 deletions.
1 change: 0 additions & 1 deletion apis/applyconfiguration/internal/internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/v1/httproute_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ type HTTPRequestMirrorFilter struct {
//
// +optional
// <gateway:experimental>
Fraction Fraction `json:"fraction,omitempty"`
Fraction *Fraction `json:"fraction,omitempty"`
}

// HTTPBackendRef defines how a HTTPRoute forwards a HTTP request.
Expand Down
6 changes: 5 additions & 1 deletion apis/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 0 additions & 90 deletions pkg/test/cel/grcproute_experimental_test.go

This file was deleted.

181 changes: 181 additions & 0 deletions pkg/test/cel/grpcroute_experimental_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
//go:build experimental
// +build experimental

/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"testing"
"time"

gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// How are tests named? Where to add new tests?
//
// Ensure that tests for newly added CEL validations are added in the correctly
// named test function. For example, if you added a test at the
// `GRPCRouteFilter` hierarchy (i.e. either at the struct level, or on one of
// the immediate descendent fields), then the test will go in the
// TestGRPCRouteFilter function. If the appropriate test function does not
// exist, please create one.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
var percent int32 = 42
var denominator int32 = 1000
var bad_denominator int32 = 0
testService := gatewayv1.ObjectName("test-service")
tests := []struct {
name string
wantErrors []string
rules []gatewayv1.GRPCRouteRule
}{
{
name: "GRPCRoute - Invalid because both percent and fraction are specified",
wantErrors: []string{"Only one of percent or fraction may be specified in HTTPRequestMirrorFilter"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Type: gatewayv1.GRPCRouteFilterRequestMirror,
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{
BackendRef: gatewayv1.BackendObjectReference{
Name: testService,
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Percent: &percent,
Fraction: &gatewayv1.Fraction{
Numerator: 83,
Denominator: &denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Invalid fraction - numerator greater than denominator",
wantErrors: []string{"numerator must be less than or equal to denominator"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Type: gatewayv1.GRPCRouteFilterRequestMirror,
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{
BackendRef: gatewayv1.BackendObjectReference{
Name: testService,
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: 1001,
Denominator: &denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Invalid fraction - denominator is 0",
wantErrors: []string{"spec.rules[0].filters[0].requestMirror.fraction.denominator in body should be greater than or equal to 1"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Type: gatewayv1.GRPCRouteFilterRequestMirror,
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{
BackendRef: gatewayv1.BackendObjectReference{
Name: testService,
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: 0,
Denominator: &bad_denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Invalid fraction - numerator is negative",
wantErrors: []string{"spec.rules[0].filters[0].requestMirror.fraction.numerator in body should be greater than or equal to 0"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Type: gatewayv1.GRPCRouteFilterRequestMirror,
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{
BackendRef: gatewayv1.BackendObjectReference{
Name: testService,
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: -1,
Denominator: &denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Valid with percent",
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Type: gatewayv1.GRPCRouteFilterRequestMirror,
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{
BackendRef: gatewayv1.BackendObjectReference{
Name: testService,
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Percent: &percent,
},
}},
}},
},
{
name: "GRPCRoute - Valid with fraction",
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Type: gatewayv1.GRPCRouteFilterRequestMirror,
RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{
BackendRef: gatewayv1.BackendObjectReference{
Name: testService,
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: 83,
Denominator: &denominator,
},
},
}},
}},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
route := &gatewayv1.GRPCRoute{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("foo-%v", time.Now().UnixNano()),
Namespace: metav1.NamespaceDefault,
},
Spec: gatewayv1.GRPCRouteSpec{Rules: tc.rules},
}
validateGRPCRoute(t, route, tc.wantErrors)
})
}
}
Loading

0 comments on commit d416ed4

Please sign in to comment.