knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/testing/reactions.go (about)

     1  /*
     2  Copyright 2019 The Knative 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  	"context"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	clientgotesting "k8s.io/client-go/testing"
    25  
    26  	"knative.dev/pkg/apis"
    27  )
    28  
    29  // InduceFailure is used in conjunction with TableTest's WithReactors field.
    30  // Tests that want to induce a failure in a row of a TableTest would add:
    31  //
    32  //	WithReactors: []clientgotesting.ReactionFunc{
    33  //	   // Makes calls to create revisions return an error.
    34  //	   InduceFailure("create", "revisions"),
    35  //	},
    36  //
    37  // Or to target a subresource, say a patch to InMemoryChannel.Status, you would add:
    38  //
    39  //	WithReactors: []clientgotesting.ReactionFunc{
    40  //	   // Makes calls to patch inmemorychannels status subresource return an error.
    41  //	   InduceFailure("patch", "inmemorychannels/status"),
    42  //	},
    43  func InduceFailure(verb, resource string) clientgotesting.ReactionFunc {
    44  	return func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) {
    45  		if !action.Matches(verb, resource) {
    46  			return false, nil, nil
    47  		}
    48  		return true, nil, fmt.Errorf("inducing failure for %s %s", action.GetVerb(), action.GetResource().Resource)
    49  	}
    50  }
    51  
    52  func ValidateCreates(ctx context.Context, action clientgotesting.Action) (handled bool, ret runtime.Object, err error) {
    53  	got := action.(clientgotesting.CreateAction).GetObject()
    54  	obj, ok := got.(apis.Validatable)
    55  	if !ok {
    56  		return false, nil, nil
    57  	}
    58  	// Only return error-level errors; warnings should not block API calls.
    59  	if err := obj.Validate(ctx).Filter(apis.ErrorLevel); err != nil {
    60  		return true, nil, err
    61  	}
    62  	return false, nil, nil
    63  }
    64  
    65  func ValidateUpdates(ctx context.Context, action clientgotesting.Action) (handled bool, ret runtime.Object, err error) {
    66  	got := action.(clientgotesting.UpdateAction).GetObject()
    67  	obj, ok := got.(apis.Validatable)
    68  	if !ok {
    69  		return false, nil, nil
    70  	}
    71  	// Only return error-level errors; warnings should not block API calls.
    72  	if err := obj.Validate(ctx).Filter(apis.ErrorLevel); err != nil {
    73  		return true, nil, err
    74  	}
    75  	return false, nil, nil
    76  }