|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package tests |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + stdhttp "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + |
| 26 | + "sigs.k8s.io/gateway-api/conformance/utils/http" |
| 27 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 28 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 29 | + "sigs.k8s.io/gateway-api/pkg/features" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + ConformanceTests = append(ConformanceTests, HTTPRouteSessionPersistenceCookie) |
| 34 | +} |
| 35 | + |
| 36 | +var HTTPRouteSessionPersistenceCookie = suite.ConformanceTest{ |
| 37 | + ShortName: "HTTPRouteSessionPersistenceCookie", |
| 38 | + Description: "An HTTPRoute with cookie-based session persistence routes requests with the same cookie to the same backend", |
| 39 | + Manifests: []string{"tests/httproute-session-persistence-cookie.yaml"}, |
| 40 | + Features: []features.FeatureName{ |
| 41 | + features.SupportGateway, |
| 42 | + features.SupportHTTPRoute, |
| 43 | + features.SupportHTTPRouteSessionPersistenceCookie, |
| 44 | + }, |
| 45 | + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { |
| 46 | + const ( |
| 47 | + ns = "gateway-conformance-infra" |
| 48 | + path = "/session-persistence" |
| 49 | + ) |
| 50 | + routeNN := types.NamespacedName{Name: "session-persistence-cookie", Namespace: ns} |
| 51 | + gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns} |
| 52 | + gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN) |
| 53 | + kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN) |
| 54 | + |
| 55 | + expected := http.ExpectedResponse{ |
| 56 | + Request: http.Request{ |
| 57 | + Path: path, |
| 58 | + }, |
| 59 | + Response: http.Response{ |
| 60 | + StatusCode: 200, |
| 61 | + }, |
| 62 | + Namespace: ns, |
| 63 | + } |
| 64 | + |
| 65 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expected) |
| 66 | + |
| 67 | + req := http.MakeRequest(t, &expected, gwAddr, "HTTP", "http") |
| 68 | + initialPod := "" |
| 69 | + for i := 0; i < 10; i++ { |
| 70 | + cReq, cRes, err := suite.RoundTripper.CaptureRoundTrip(req) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("request %d with cookie failed: %v", i+1, err) |
| 73 | + } |
| 74 | + if err := http.CompareRoundTrip(t, &req, cReq, cRes, expected); err != nil { |
| 75 | + t.Fatalf("request %d with cookie failed expectations: %v", i+1, err) |
| 76 | + } |
| 77 | + |
| 78 | + if i == 0 { |
| 79 | + if cReq.Pod == "" { |
| 80 | + t.Fatalf("expected pod to be set") |
| 81 | + } |
| 82 | + cookie, err := parseCookie(cRes.Headers) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("failed to parse session persistence cookie: %v", err) |
| 85 | + } |
| 86 | + t.Logf("session persistence cookie: %s=%s", cookie.Name, cookie.Value) |
| 87 | + req.Headers["Cookie"] = []string{fmt.Sprintf("%s=%s", cookie.Name, cookie.Value)} |
| 88 | + initialPod = cReq.Pod |
| 89 | + continue |
| 90 | + } |
| 91 | + if cReq.Pod != initialPod { |
| 92 | + t.Fatalf("expected session persistence to keep routing to pod %q, got %q", initialPod, cReq.Pod) |
| 93 | + } |
| 94 | + } |
| 95 | + }, |
| 96 | +} |
| 97 | + |
| 98 | +func parseCookie(headers map[string][]string) (*stdhttp.Cookie, error) { |
| 99 | + parser := &stdhttp.Response{Header: stdhttp.Header(headers)} |
| 100 | + cookies := parser.Cookies() |
| 101 | + if len(cookies) == 0 { |
| 102 | + return nil, fmt.Errorf("cookie not found: headers: %v", headers) |
| 103 | + } |
| 104 | + return cookies[0], nil |
| 105 | +} |
0 commit comments