|
| 1 | +//go:build experimental |
| 2 | +// +build experimental |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2024 The Kubernetes Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 28 | + |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | +) |
| 31 | + |
| 32 | +//////////////////////////////////////////////////////////////////////////////// |
| 33 | +//////////////////////////////////////////////////////////////////////////////// |
| 34 | +// |
| 35 | +// How are tests named? Where to add new tests? |
| 36 | +// |
| 37 | +// Ensure that tests for newly added CEL validations are added in the correctly |
| 38 | +// named test function. For example, if you added a test at the |
| 39 | +// `GRPCRouteFilter` hierarchy (i.e. either at the struct level, or on one of |
| 40 | +// the immediate descendent fields), then the test will go in the |
| 41 | +// TestGRPCRouteFilter function. If the appropriate test function does not |
| 42 | +// exist, please create one. |
| 43 | +// |
| 44 | +//////////////////////////////////////////////////////////////////////////////// |
| 45 | +//////////////////////////////////////////////////////////////////////////////// |
| 46 | + |
| 47 | +func TestGRPCRequestMirrorFilterExperimental(t *testing.T) { |
| 48 | + var percent int32 = 42 |
| 49 | + var denominator int32 = 1000 |
| 50 | + var bad_denominator int32 = 0 |
| 51 | + testService := gatewayv1.ObjectName("test-service") |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + wantErrors []string |
| 55 | + rules []gatewayv1.GRPCRouteRule |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "GRPCRoute - Invalid because both percent and fraction are specified", |
| 59 | + wantErrors: []string{"Only one of percent or fraction may be specified in HTTPRequestMirrorFilter"}, |
| 60 | + rules: []gatewayv1.GRPCRouteRule{{ |
| 61 | + Filters: []gatewayv1.GRPCRouteFilter{{ |
| 62 | + Type: gatewayv1.GRPCRouteFilterRequestMirror, |
| 63 | + RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{ |
| 64 | + BackendRef: gatewayv1.BackendObjectReference{ |
| 65 | + Name: testService, |
| 66 | + Port: ptrTo(gatewayv1.PortNumber(8081)), |
| 67 | + }, |
| 68 | + Percent: &percent, |
| 69 | + Fraction: &gatewayv1.Fraction{ |
| 70 | + Numerator: 83, |
| 71 | + Denominator: &denominator, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }}, |
| 75 | + }}, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "GRPCRoute - Invalid fraction - numerator greater than denominator", |
| 79 | + wantErrors: []string{"numerator must be less than or equal to denominator"}, |
| 80 | + rules: []gatewayv1.GRPCRouteRule{{ |
| 81 | + Filters: []gatewayv1.GRPCRouteFilter{{ |
| 82 | + Type: gatewayv1.GRPCRouteFilterRequestMirror, |
| 83 | + RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{ |
| 84 | + BackendRef: gatewayv1.BackendObjectReference{ |
| 85 | + Name: testService, |
| 86 | + Port: ptrTo(gatewayv1.PortNumber(8081)), |
| 87 | + }, |
| 88 | + Fraction: &gatewayv1.Fraction{ |
| 89 | + Numerator: 1001, |
| 90 | + Denominator: &denominator, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }}, |
| 94 | + }}, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "GRPCRoute - Invalid fraction - denominator is 0", |
| 98 | + wantErrors: []string{"spec.rules[0].filters[0].requestMirror.fraction.denominator in body should be greater than or equal to 1"}, |
| 99 | + rules: []gatewayv1.GRPCRouteRule{{ |
| 100 | + Filters: []gatewayv1.GRPCRouteFilter{{ |
| 101 | + Type: gatewayv1.GRPCRouteFilterRequestMirror, |
| 102 | + RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{ |
| 103 | + BackendRef: gatewayv1.BackendObjectReference{ |
| 104 | + Name: testService, |
| 105 | + Port: ptrTo(gatewayv1.PortNumber(8081)), |
| 106 | + }, |
| 107 | + Fraction: &gatewayv1.Fraction{ |
| 108 | + Numerator: 0, |
| 109 | + Denominator: &bad_denominator, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }}, |
| 113 | + }}, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "GRPCRoute - Invalid fraction - numerator is negative", |
| 117 | + wantErrors: []string{"spec.rules[0].filters[0].requestMirror.fraction.numerator in body should be greater than or equal to 0"}, |
| 118 | + rules: []gatewayv1.GRPCRouteRule{{ |
| 119 | + Filters: []gatewayv1.GRPCRouteFilter{{ |
| 120 | + Type: gatewayv1.GRPCRouteFilterRequestMirror, |
| 121 | + RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{ |
| 122 | + BackendRef: gatewayv1.BackendObjectReference{ |
| 123 | + Name: testService, |
| 124 | + Port: ptrTo(gatewayv1.PortNumber(8081)), |
| 125 | + }, |
| 126 | + Fraction: &gatewayv1.Fraction{ |
| 127 | + Numerator: -1, |
| 128 | + Denominator: &denominator, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }}, |
| 132 | + }}, |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "GRPCRoute - Valid with percent", |
| 136 | + rules: []gatewayv1.GRPCRouteRule{{ |
| 137 | + Filters: []gatewayv1.GRPCRouteFilter{{ |
| 138 | + Type: gatewayv1.GRPCRouteFilterRequestMirror, |
| 139 | + RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{ |
| 140 | + BackendRef: gatewayv1.BackendObjectReference{ |
| 141 | + Name: testService, |
| 142 | + Port: ptrTo(gatewayv1.PortNumber(8081)), |
| 143 | + }, |
| 144 | + Percent: &percent, |
| 145 | + }, |
| 146 | + }}, |
| 147 | + }}, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "GRPCRoute - Valid with fraction", |
| 151 | + rules: []gatewayv1.GRPCRouteRule{{ |
| 152 | + Filters: []gatewayv1.GRPCRouteFilter{{ |
| 153 | + Type: gatewayv1.GRPCRouteFilterRequestMirror, |
| 154 | + RequestMirror: &gatewayv1.HTTPRequestMirrorFilter{ |
| 155 | + BackendRef: gatewayv1.BackendObjectReference{ |
| 156 | + Name: testService, |
| 157 | + Port: ptrTo(gatewayv1.PortNumber(8081)), |
| 158 | + }, |
| 159 | + Fraction: &gatewayv1.Fraction{ |
| 160 | + Numerator: 83, |
| 161 | + Denominator: &denominator, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }}, |
| 165 | + }}, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + for _, tc := range tests { |
| 170 | + t.Run(tc.name, func(t *testing.T) { |
| 171 | + route := &gatewayv1.GRPCRoute{ |
| 172 | + ObjectMeta: metav1.ObjectMeta{ |
| 173 | + Name: fmt.Sprintf("foo-%v", time.Now().UnixNano()), |
| 174 | + Namespace: metav1.NamespaceDefault, |
| 175 | + }, |
| 176 | + Spec: gatewayv1.GRPCRouteSpec{Rules: tc.rules}, |
| 177 | + } |
| 178 | + validateGRPCRoute(t, route, tc.wantErrors) |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments