k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/framework/client/objects_test.go (about)

     1  /*
     2  Copyright 2020 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 client
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"testing"
    23  
    24  	apierrs "k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  )
    27  
    28  func TestIsResourceQuotaError(t *testing.T) {
    29  	testcases := []struct {
    30  		name string
    31  		err  error
    32  		want bool
    33  	}{
    34  		{
    35  			name: "resource-quota error",
    36  			err:  apierrs.NewConflict(schema.GroupResource{Resource: "resourcequotas"}, "gke-resource-quotas", fmt.Errorf("please apply your changes to the latest version and try again")),
    37  			want: true,
    38  		},
    39  		{
    40  			name: "non-resource-quota api error",
    41  			err:  apierrs.NewBadRequest("XXX"),
    42  			want: false,
    43  		},
    44  		{
    45  			name: "non-api error",
    46  			err:  fmt.Errorf("other resourcequotas error"),
    47  			want: false,
    48  		},
    49  	}
    50  
    51  	for _, tc := range testcases {
    52  		t.Run(tc.name, func(t *testing.T) {
    53  			if got := isResourceQuotaConflictError(tc.err); tc.want != got {
    54  				t.Errorf("want: %v, got: %v", tc.want, got)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestIsRetryableAPIError(t *testing.T) {
    61  	testcases := []struct {
    62  		name string
    63  		err  error
    64  		want bool
    65  	}{
    66  		{
    67  			name: "conflict resource-quota error",
    68  			err:  apierrs.NewConflict(schema.GroupResource{Resource: "resourcequotas"}, "gke-resource-quotas", fmt.Errorf("please apply your changes to the latest version and try again")),
    69  			want: true,
    70  		},
    71  		{
    72  			name: "conflict non-resource-quota error",
    73  			err:  apierrs.NewConflict(schema.GroupResource{Resource: "pods"}, "my-pod", fmt.Errorf("please apply your changes to the latest version and try again")),
    74  			want: false,
    75  		},
    76  		{
    77  			name: "non-conflict resource-quota error",
    78  			err:  apierrs.NewAlreadyExists(schema.GroupResource{Resource: "resourcequotas"}, "gke-resource-quotas"),
    79  			want: false,
    80  		},
    81  		{
    82  			name: "other error",
    83  			err:  fmt.Errorf("XXX"),
    84  			want: false,
    85  		},
    86  		{
    87  			name: "other resourcequotas error",
    88  			err:  fmt.Errorf("other resourcequotas error"),
    89  			want: false,
    90  		},
    91  	}
    92  
    93  	for _, tc := range testcases {
    94  		t.Run(tc.name, func(t *testing.T) {
    95  			if got := IsRetryableAPIError(tc.err); tc.want != got {
    96  				t.Errorf("want: %v, got: %v", tc.want, got)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  func TestIsRetryableNetError(t *testing.T) {
   103  	tests := []struct {
   104  		name string
   105  		err  error
   106  		want bool
   107  	}{
   108  		{
   109  			name: "http2: client connection lost",
   110  			err:  errors.New("http2: client connection lost"),
   111  			want: true,
   112  		},
   113  		{
   114  			name: "http2: some other error",
   115  			err:  errors.New("http2: some other error"),
   116  			want: false,
   117  		},
   118  	}
   119  	for _, tt := range tests {
   120  		t.Run(tt.name, func(t *testing.T) {
   121  			if got := IsRetryableNetError(tt.err); got != tt.want {
   122  				t.Errorf("IsRetryableNetError() = %v, want %v", got, tt.want)
   123  			}
   124  		})
   125  	}
   126  }