|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/types" |
| 17 | + "k8s.io/apiserver/pkg/storage/names" |
| 18 | + |
| 19 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 20 | + routev1 "github.com/openshift/api/route/v1" |
| 21 | +) |
| 22 | + |
| 23 | +// Test_HTTPKeepAliveTimeout verifies that the HTTPKeepAliveTimeout tuning option correctly closes |
| 24 | +// a persistent HTTP connection after the configured duration, forcing the client to establish a new one. |
| 25 | +func Test_HTTPKeepAliveTimeout(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + // Use client timeout value lesser than the http keep alive timeout |
| 29 | + // to make sure we avoid https://github.com/haproxy/haproxy/issues/2334. |
| 30 | + httpKeepAliveTimeoutRunTest(t, 7, 3) |
| 31 | + // Use the default client timeout. |
| 32 | + httpKeepAliveTimeoutRunTest(t, 7, 0) |
| 33 | +} |
| 34 | + |
| 35 | +// httpKeepAliveTimeoutRunTest is a helper which runs a single test workflow. |
| 36 | +// The test workflow: |
| 37 | +// - Create an ingress controller with a set HTTPKeepAliveTimeout. |
| 38 | +// - Send multiple requests to confirm the connection reuse. |
| 39 | +// - Sleep for the duration of the timeout. |
| 40 | +// - Send a final request and verify a new connection is established. |
| 41 | +func httpKeepAliveTimeoutRunTest(t *testing.T, httpKATimeoutSeconds, clientTimeoutSeconds int64) { |
| 42 | + t.Helper() |
| 43 | + |
| 44 | + const ( |
| 45 | + webService = "web-service" |
| 46 | + // Number of requests to check the connection is reused. |
| 47 | + numConnReuseAttempts = 5 |
| 48 | + ) |
| 49 | + |
| 50 | + createIngressController := func(t *testing.T, testNs *corev1.Namespace) (*operatorv1.IngressController, error) { |
| 51 | + t.Helper() |
| 52 | + |
| 53 | + icName := types.NamespacedName{ |
| 54 | + Namespace: operatorNamespace, |
| 55 | + Name: testNs.Name, |
| 56 | + } |
| 57 | + |
| 58 | + t.Logf("Creating IngressController %q...", icName) |
| 59 | + |
| 60 | + ic := newLoadBalancerController(icName, icName.Name+"."+dnsConfig.Spec.BaseDomain) |
| 61 | + ic.Spec.EndpointPublishingStrategy.LoadBalancer = &operatorv1.LoadBalancerStrategy{ |
| 62 | + Scope: operatorv1.ExternalLoadBalancer, |
| 63 | + DNSManagementPolicy: operatorv1.ManagedLoadBalancerDNS, |
| 64 | + } |
| 65 | + ic.Spec.NamespaceSelector = &metav1.LabelSelector{ |
| 66 | + MatchLabels: testNs.Labels, |
| 67 | + } |
| 68 | + ic.Spec.TuningOptions.HTTPKeepAliveTimeout = &metav1.Duration{Duration: time.Duration(httpKATimeoutSeconds) * time.Second} |
| 69 | + if clientTimeoutSeconds != 0 { |
| 70 | + ic.Spec.TuningOptions.ClientTimeout = &metav1.Duration{Duration: time.Duration(clientTimeoutSeconds) * time.Second} |
| 71 | + } |
| 72 | + |
| 73 | + if err := kclient.Create(context.Background(), ic); err != nil { |
| 74 | + return nil, fmt.Errorf("failed to create IngressController: %w", err) |
| 75 | + } |
| 76 | + t.Cleanup(func() { |
| 77 | + t.Logf("Deleting IngressController %q...", icName) |
| 78 | + assertIngressControllerDeleted(t, kclient, ic) |
| 79 | + }) |
| 80 | + |
| 81 | + if err := waitForIngressControllerCondition(t, kclient, 5*time.Minute, icName, availableConditionsForIngressControllerWithLoadBalancer...); err != nil { |
| 82 | + return nil, fmt.Errorf("failed to observe expected conditions: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + return ic, nil |
| 86 | + } |
| 87 | + |
| 88 | + createTestServicesAndTestRoute := func(ns *corev1.Namespace, ic *operatorv1.IngressController) (string, error) { |
| 89 | + canaryImage, err := canaryImageReference(t) |
| 90 | + if err != nil { |
| 91 | + return "", fmt.Errorf("failed to get canary image reference: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + t.Logf("Creating test workload in namespace %q...", ns.Name) |
| 95 | + |
| 96 | + if err := idleConnectionCreateBackendService(context.Background(), t, ns.Name, webService, canaryImage); err != nil { |
| 97 | + return "", fmt.Errorf("failed to create service %s: %w", webService, err) |
| 98 | + } |
| 99 | + |
| 100 | + routeName := types.NamespacedName{Namespace: ns.Name, Name: webService} |
| 101 | + route := buildRoute(routeName.Name, routeName.Namespace, webService) |
| 102 | + if err := kclient.Create(context.Background(), route); err != nil { |
| 103 | + return "", fmt.Errorf("failed to create route %s: %w", routeName, err) |
| 104 | + } |
| 105 | + |
| 106 | + if err := waitForRouteIngressConditions(t, kclient, routeName, ic.Name, routev1.RouteIngressCondition{ |
| 107 | + Type: routev1.RouteAdmitted, |
| 108 | + Status: corev1.ConditionTrue, |
| 109 | + }); err != nil { |
| 110 | + return "", fmt.Errorf("failed to observe route admitted condition: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + if err := kclient.Get(context.Background(), routeName, route); err != nil { |
| 114 | + return "", fmt.Errorf("failed to get route %s: %w", routeName, err) |
| 115 | + } |
| 116 | + |
| 117 | + routeHost := getRouteHost(route, ic.Name) |
| 118 | + if routeHost == "" { |
| 119 | + return "", fmt.Errorf("route %s has no host assigned by IngressController %s", routeName, ic.Name) |
| 120 | + } |
| 121 | + |
| 122 | + return routeHost, nil |
| 123 | + } |
| 124 | + |
| 125 | + testName := names.SimpleNameGenerator.GenerateName("http-keep-alive-") |
| 126 | + testNs := createNamespace(t, testName) |
| 127 | + |
| 128 | + ic, err := createIngressController(t, testNs) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("Failed to create IngressController %s: %v", testName, err) |
| 131 | + } |
| 132 | + |
| 133 | + routeHost, err := createTestServicesAndTestRoute(testNs, ic) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("Test setup failed: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + elbAddress, err := resolveIngressControllerAddress(t, ic) |
| 139 | + if err != nil { |
| 140 | + t.Fatalf("Test setup failed: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + // Create an HTTP client that reuses connections by default. |
| 144 | + httpClient := &http.Client{ |
| 145 | + Timeout: time.Minute, // full request roundtrip (client-server-client) |
| 146 | + Transport: &addressCapturingRoundTripper{ |
| 147 | + BaseTransport: &http.Transport{ |
| 148 | + // Set client's idle timeout high to prevent it from closing the connection. |
| 149 | + IdleConnTimeout: 600 * time.Second, |
| 150 | + // Set connection pool sizes high to ensure the connection reuse. |
| 151 | + MaxConnsPerHost: 100, // active + idle per URL host |
| 152 | + MaxIdleConns: 100, // all idle |
| 153 | + MaxIdleConnsPerHost: 100, // idle per host |
| 154 | + }, |
| 155 | + }, |
| 156 | + } |
| 157 | + t.Logf("Checking connectivity to test route...") |
| 158 | + if err := checkRouteConnectivity(t, httpClient, elbAddress, routeHost); err != nil { |
| 159 | + t.Fatalf("Failed to check the connectivity to route %q: %v", routeHost, err) |
| 160 | + } |
| 161 | + |
| 162 | + t.Logf("Checking connection reuse by sending %d requests...", numConnReuseAttempts) |
| 163 | + // Send N requests to make sure the connection keep alive is working. |
| 164 | + prevLocalAddr := "" |
| 165 | + for i := 1; i < (numConnReuseAttempts + 1); i++ { |
| 166 | + response, localAddr, err := idleConnectionFetchResponse(t, httpClient, elbAddress, routeHost) |
| 167 | + if err != nil { |
| 168 | + t.Errorf("Request %d failed: %v", i, err) |
| 169 | + } |
| 170 | + if !strings.HasPrefix(response, webService) { |
| 171 | + t.Errorf("Request %d failed: wrong response: %s", i, response) |
| 172 | + } |
| 173 | + if prevLocalAddr == "" { |
| 174 | + prevLocalAddr = localAddr |
| 175 | + } else if prevLocalAddr != localAddr { |
| 176 | + t.Errorf("Request %d failed: unexpected new connection", i) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Don't send requests for longer than HTTPKeepAliveTimeout. |
| 181 | + sleepDuration := time.Duration(httpKATimeoutSeconds+1) * time.Second |
| 182 | + t.Logf("Going to sleep for %v...", sleepDuration) |
| 183 | + time.Sleep(sleepDuration) |
| 184 | + |
| 185 | + // Send the last request to see that a new connection was created by the client. |
| 186 | + // Old connection is supposed to be reset by the router due to expired http-keep-alive. |
| 187 | + t.Logf("Checking connection reset...") |
| 188 | + response, localAddr, err := idleConnectionFetchResponse(t, httpClient, elbAddress, routeHost) |
| 189 | + if err != nil { |
| 190 | + t.Errorf("Last request failed: %v", err) |
| 191 | + } |
| 192 | + if !strings.HasPrefix(response, webService) { |
| 193 | + t.Errorf("Last request failed: wrong response: %s", response) |
| 194 | + } |
| 195 | + if prevLocalAddr == localAddr { |
| 196 | + t.Errorf("Last request failed: unexpected reused connection after keep alive timeout expired") |
| 197 | + } |
| 198 | +} |
0 commit comments