k8s.io/kubernetes@v1.29.3/test/integration/utils.go (about)

     1  /*
     2  Copyright 2014 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 integration
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"google.golang.org/grpc"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/util/wait"
    28  	"k8s.io/apiserver/pkg/storage/storagebackend"
    29  	clientset "k8s.io/client-go/kubernetes"
    30  	coreclient "k8s.io/client-go/kubernetes/typed/core/v1"
    31  
    32  	"go.etcd.io/etcd/client/pkg/v3/transport"
    33  	clientv3 "go.etcd.io/etcd/client/v3"
    34  )
    35  
    36  // DeletePodOrErrorf deletes a pod or fails with a call to t.Errorf.
    37  func DeletePodOrErrorf(t *testing.T, c clientset.Interface, ns, name string) {
    38  	if err := c.CoreV1().Pods(ns).Delete(context.TODO(), name, metav1.DeleteOptions{}); err != nil {
    39  		t.Errorf("unable to delete pod %v: %v", name, err)
    40  	}
    41  }
    42  
    43  // Requests to try.  Each one should be forbidden or not forbidden
    44  // depending on the authentication and authorization setup of the API server.
    45  var (
    46  	Code200 = map[int]bool{200: true}
    47  	Code201 = map[int]bool{201: true}
    48  	Code400 = map[int]bool{400: true}
    49  	Code401 = map[int]bool{401: true}
    50  	Code403 = map[int]bool{403: true}
    51  	Code404 = map[int]bool{404: true}
    52  	Code405 = map[int]bool{405: true}
    53  	Code503 = map[int]bool{503: true}
    54  )
    55  
    56  // WaitForPodToDisappear polls the API server if the pod has been deleted.
    57  func WaitForPodToDisappear(podClient coreclient.PodInterface, podName string, interval, timeout time.Duration) error {
    58  	return wait.PollImmediate(interval, timeout, func() (bool, error) {
    59  		_, err := podClient.Get(context.TODO(), podName, metav1.GetOptions{})
    60  		if err == nil {
    61  			return false, nil
    62  		}
    63  		if apierrors.IsNotFound(err) {
    64  			return true, nil
    65  		}
    66  		return false, err
    67  	})
    68  }
    69  
    70  // GetEtcdClients returns an initialized  clientv3.Client and clientv3.KV.
    71  func GetEtcdClients(config storagebackend.TransportConfig) (*clientv3.Client, clientv3.KV, error) {
    72  	tlsInfo := transport.TLSInfo{
    73  		CertFile:      config.CertFile,
    74  		KeyFile:       config.KeyFile,
    75  		TrustedCAFile: config.TrustedCAFile,
    76  	}
    77  
    78  	tlsConfig, err := tlsInfo.ClientConfig()
    79  	if err != nil {
    80  		return nil, nil, err
    81  	}
    82  
    83  	cfg := clientv3.Config{
    84  		Endpoints:   config.ServerList,
    85  		DialTimeout: 20 * time.Second,
    86  		DialOptions: []grpc.DialOption{
    87  			grpc.WithBlock(), // block until the underlying connection is up
    88  		},
    89  		TLS: tlsConfig,
    90  	}
    91  
    92  	c, err := clientv3.New(cfg)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  
    97  	return c, clientv3.NewKV(c), nil
    98  }