github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/errors_test.go (about)

     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 meta
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  )
    26  
    27  func TestErrorMatching(t *testing.T) {
    28  	testCases := []struct {
    29  		name string
    30  		// input should contain an error that is _not_ empty, otherwise the naive reflectlite.DeepEqual matching of
    31  		// the errors lib will always succeed, but for all of these we want to verify that the matching is based on
    32  		// type.
    33  		input       error
    34  		new         func() error
    35  		matcherFunc func(error) bool
    36  	}{
    37  		{
    38  			name:        "AmbiguousResourceError",
    39  			input:       &AmbiguousResourceError{MatchingResources: []schema.GroupVersionResource{{}}},
    40  			new:         func() error { return &AmbiguousResourceError{} },
    41  			matcherFunc: IsAmbiguousError,
    42  		},
    43  		{
    44  			name:        "AmbiguousKindError",
    45  			input:       &AmbiguousKindError{MatchingResources: []schema.GroupVersionResource{{}}},
    46  			new:         func() error { return &AmbiguousKindError{} },
    47  			matcherFunc: IsAmbiguousError,
    48  		},
    49  		{
    50  			name:        "NoResourceMatchError",
    51  			input:       &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: "foo"}},
    52  			new:         func() error { return &NoResourceMatchError{} },
    53  			matcherFunc: IsNoMatchError,
    54  		},
    55  		{
    56  			name:        "NoKindMatchError",
    57  			input:       &NoKindMatchError{SearchedVersions: []string{"foo"}},
    58  			new:         func() error { return &NoKindMatchError{} },
    59  			matcherFunc: IsNoMatchError,
    60  		},
    61  	}
    62  
    63  	for _, tc := range testCases {
    64  		t.Run(tc.name, func(t *testing.T) {
    65  			if !errors.Is(tc.input, tc.new()) {
    66  				t.Error("error doesn't match itself directly")
    67  			}
    68  			if !errors.Is(fmt.Errorf("wrapepd: %w", tc.input), tc.new()) {
    69  				t.Error("error doesn't match itself when wrapped")
    70  			}
    71  			if !tc.matcherFunc(tc.input) {
    72  				t.Errorf("error doesn't get matched by matcherfunc")
    73  			}
    74  			if errors.Is(tc.input, errors.New("foo")) {
    75  				t.Error("error incorrectly matches other error")
    76  			}
    77  		})
    78  	}
    79  }