sigs.k8s.io/kueue@v0.6.2/pkg/util/testing/not_found_error_cmp.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 testing
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/onsi/gomega/format"
    23  	"github.com/onsi/gomega/types"
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  )
    26  
    27  func BeNotFoundError() types.GomegaMatcher {
    28  	return &notFoundErrorMatch{}
    29  }
    30  
    31  type notFoundErrorMatch struct {
    32  }
    33  
    34  func (matcher *notFoundErrorMatch) Match(actual interface{}) (success bool, err error) {
    35  	if actual == nil {
    36  		return false, nil
    37  	}
    38  
    39  	err, ok := actual.(error)
    40  	if !ok {
    41  		return false, fmt.Errorf("NotFoundError expects an error")
    42  	}
    43  
    44  	return err != nil && apierrors.IsNotFound(err), nil
    45  }
    46  
    47  func (matcher *notFoundErrorMatch) FailureMessage(actual interface{}) (message string) {
    48  	return format.Message(actual, "to be a NotFound error")
    49  }
    50  
    51  func (matcher *notFoundErrorMatch) NegatedFailureMessage(actual interface{}) (message string) {
    52  	return format.Message(actual, "not to be NotFound error")
    53  }
    54  
    55  func BeForbiddenError() types.GomegaMatcher {
    56  	return &isForbiddenErrorMatch{}
    57  }
    58  
    59  type isForbiddenErrorMatch struct {
    60  }
    61  
    62  func (matcher *isForbiddenErrorMatch) Match(actual interface{}) (success bool, err error) {
    63  	err, ok := actual.(error)
    64  	if !ok {
    65  		return false, fmt.Errorf("ForbiddenError expects an error")
    66  	}
    67  
    68  	return err != nil && apierrors.IsForbidden(err), nil
    69  }
    70  
    71  func (matcher *isForbiddenErrorMatch) FailureMessage(actual interface{}) (message string) {
    72  	return format.Message(actual, "to be a Forbidden error")
    73  }
    74  
    75  func (matcher *isForbiddenErrorMatch) NegatedFailureMessage(actual interface{}) (message string) {
    76  	return format.Message(actual, "not to be Forbidden error")
    77  }