...

Source file src/sigs.k8s.io/gateway-api/conformance/utils/tls/tls.go

Documentation: sigs.k8s.io/gateway-api/conformance/utils/tls

     1  /*
     2  Copyright 2022 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 tls
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"sigs.k8s.io/gateway-api/conformance/utils/config"
    24  	"sigs.k8s.io/gateway-api/conformance/utils/http"
    25  	"sigs.k8s.io/gateway-api/conformance/utils/roundtripper"
    26  )
    27  
    28  // MakeTLSRequestAndExpectEventuallyConsistentResponse makes a request with the given parameters,
    29  // understanding that the request may fail for some amount of time.
    30  //
    31  // Once the request succeeds consistently with the response having the expected status code, make
    32  // additional assertions on the response body using the provided ExpectedResponse.
    33  func MakeTLSRequestAndExpectEventuallyConsistentResponse(t *testing.T, r roundtripper.RoundTripper, timeoutConfig config.TimeoutConfig, gwAddr string, cPem, keyPem []byte, server string, expected http.ExpectedResponse) {
    34  	t.Helper()
    35  
    36  	req := http.MakeRequest(t, &expected, gwAddr, "HTTPS", "https")
    37  
    38  	WaitForConsistentTLSResponse(t, r, req, expected, timeoutConfig.RequiredConsecutiveSuccesses, timeoutConfig.MaxTimeToConsistency, cPem, keyPem, server)
    39  }
    40  
    41  // WaitForConsistentTLSResponse - repeats the provided request until it completes with a response having
    42  // the expected response consistently. The provided threshold determines how many times in
    43  // a row this must occur to be considered "consistent".
    44  func WaitForConsistentTLSResponse(t *testing.T, r roundtripper.RoundTripper, req roundtripper.Request, expected http.ExpectedResponse, threshold int, maxTimeToConsistency time.Duration, cPem, keyPem []byte, server string) {
    45  	http.AwaitConvergence(t, threshold, maxTimeToConsistency, func(elapsed time.Duration) bool {
    46  		req.KeyPem = keyPem
    47  		req.CertPem = cPem
    48  		req.Server = server
    49  
    50  		cReq, cRes, err := r.CaptureRoundTrip(req)
    51  		if err != nil {
    52  			t.Logf("Request failed, not ready yet: %v (after %v)", err.Error(), elapsed)
    53  			return false
    54  		}
    55  
    56  		if err := http.CompareRequest(t, &req, cReq, cRes, expected); err != nil {
    57  			t.Logf("Response expectation failed for request: %+v  not ready yet: %v (after %v)", req, err, elapsed)
    58  			return false
    59  		}
    60  
    61  		return true
    62  	})
    63  	t.Logf("Request passed")
    64  }
    65  

View as plain text