Skip to content

Commit

Permalink
add e2e test for rate limit on multiple listeners
Browse files Browse the repository at this point in the history
Signed-off-by: huabing zhao <zhaohuabing@gmail.com>
  • Loading branch information
zhaohuabing committed Apr 4, 2024
1 parent 2b5a2e6 commit 42e0d34
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/e2e/testdata/ratelimit-multiple-listeners.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: eg-rate-limit
namespace: gateway-conformance-infra
spec:
gatewayClassName: "{GATEWAY_CLASS_NAME}"
listeners:
- name: http-80
protocol: HTTP
port: 80
- name: http-8080
protocol: HTTP
port: 8080
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: cidr-ratelimit
namespace: gateway-conformance-infra
spec:
parentRefs:
- name: eg-rate-limit
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: infra-backend-v1
port: 8080
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: ratelimit-all-ips
namespace: gateway-conformance-infra
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: cidr-ratelimit
namespace: gateway-conformance-infra
rateLimit:
type: Global
global:
rules:
- clientSelectors:
- sourceCIDR:
value: 0.0.0.0/0
type: distinct
limit:
requests: 3
unit: Hour
51 changes: 51 additions & 0 deletions test/e2e/tests/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,57 @@ var RateLimitBasedJwtClaimsTest = suite.ConformanceTest{
},
}

var RateLimitCIDRMatchTest = suite.ConformanceTest{

Check failure on line 288 in test/e2e/tests/ratelimit.go

View workflow job for this annotation

GitHub Actions / lint

RateLimitCIDRMatchTest redeclared in this block

Check failure on line 288 in test/e2e/tests/ratelimit.go

View workflow job for this annotation

GitHub Actions / lint

RateLimitCIDRMatchTest redeclared in this block

Check failure on line 288 in test/e2e/tests/ratelimit.go

View workflow job for this annotation

GitHub Actions / lint

RateLimitCIDRMatchTest redeclared in this block
ShortName: "RateLimitMultipleListeners",
Description: "Limit requests on multiple listeners",
Manifests: []string{"testdata/ratelimit-multiple-listeners.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
t.Run("block all ips on listener 80", func(t *testing.T) {
ns := "gateway-conformance-infra"
routeNN := types.NamespacedName{Name: "cidr-ratelimit", Namespace: ns}
gwNN := types.NamespacedName{Name: "gateway-conformance-infra", Namespace: ns}
gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)
ratelimitHeader := make(map[string]string)
expectOkResp := http.ExpectedResponse{
Request: http.Request{
Path: "/",
},
Response: http.Response{
StatusCode: 200,
Headers: ratelimitHeader,
},
Namespace: ns,
}
expectOkResp.Response.Headers["X-Ratelimit-Limit"] = "3, 3;w=3600"
expectOkReq := http.MakeRequest(t, &expectOkResp, gwAddr, "HTTP", "http")

expectLimitResp := http.ExpectedResponse{
Request: http.Request{
Path: "/",
},
Response: http.Response{
StatusCode: 429,
},
Namespace: ns,
}
expectLimitReq := http.MakeRequest(t, &expectLimitResp, gwAddr, "HTTP", "http")

// should just send exactly 4 requests, and expect 429

// keep sending requests till get 200 first, that will cost one 200
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectOkResp)

// fire the rest of requests
if err := GotExactExpectedResponse(t, 2, suite.RoundTripper, expectOkReq, expectOkResp); err != nil {
t.Errorf("failed to get expected response for the first three requests: %v", err)
}
if err := GotExactExpectedResponse(t, 1, suite.RoundTripper, expectLimitReq, expectLimitResp); err != nil {
t.Errorf("failed to get expected response for the last (fourth) request: %v", err)
}
})
},
}

func GotExactExpectedResponse(t *testing.T, n int, r roundtripper.RoundTripper, req roundtripper.Request, resp http.ExpectedResponse) error {
for i := 0; i < n; i++ {
cReq, cRes, err := r.CaptureRoundTrip(req)
Expand Down

0 comments on commit 42e0d34

Please sign in to comment.