k8s.io/kubernetes@v1.29.3/test/e2e/framework/service/util.go (about)

     1  /*
     2  Copyright 2019 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 service
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/util/wait"
    24  	"k8s.io/kubernetes/test/e2e/framework"
    25  	e2enetwork "k8s.io/kubernetes/test/e2e/framework/network"
    26  )
    27  
    28  // TestReachableHTTP tests that the given host serves HTTP on the given port.
    29  func TestReachableHTTP(ctx context.Context, host string, port int, timeout time.Duration) {
    30  	TestReachableHTTPWithRetriableErrorCodes(ctx, host, port, []int{}, timeout)
    31  }
    32  
    33  // TestReachableHTTPWithRetriableErrorCodes tests that the given host serves HTTP on the given port with the given retriableErrCodes.
    34  func TestReachableHTTPWithRetriableErrorCodes(ctx context.Context, host string, port int, retriableErrCodes []int, timeout time.Duration) {
    35  	pollfn := func(ctx context.Context) (bool, error) {
    36  		result := e2enetwork.PokeHTTP(host, port, "/echo?msg=hello",
    37  			&e2enetwork.HTTPPokeParams{
    38  				BodyContains:   "hello",
    39  				RetriableCodes: retriableErrCodes,
    40  			})
    41  		if result.Status == e2enetwork.HTTPSuccess {
    42  			return true, nil
    43  		}
    44  		return false, nil // caller can retry
    45  	}
    46  
    47  	if err := wait.PollUntilContextTimeout(ctx, framework.Poll, timeout, true, pollfn); err != nil {
    48  		if wait.Interrupted(err) {
    49  			framework.Failf("Could not reach HTTP service through %v:%v after %v", host, port, timeout)
    50  		} else {
    51  			framework.Failf("Failed to reach HTTP service through %v:%v: %v", host, port, err)
    52  		}
    53  	}
    54  }