knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/testing/actions.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  	"fmt"
    21  
    22  	clientgotesting "k8s.io/client-go/testing"
    23  )
    24  
    25  // Actions stores list of Actions recorded by the reactors.
    26  type Actions struct {
    27  	Gets              []clientgotesting.GetAction
    28  	Creates           []clientgotesting.CreateAction
    29  	Updates           []clientgotesting.UpdateAction
    30  	Deletes           []clientgotesting.DeleteAction
    31  	DeleteCollections []clientgotesting.DeleteCollectionAction
    32  	Patches           []clientgotesting.PatchAction
    33  }
    34  
    35  // ActionRecorder contains list of K8s request actions.
    36  type ActionRecorder interface {
    37  	Actions() []clientgotesting.Action
    38  }
    39  
    40  // ActionRecorderList is a list of ActionRecorder objects.
    41  type ActionRecorderList []ActionRecorder
    42  
    43  // ActionsByVerb fills in Actions objects, sorting the actions
    44  // by verb.
    45  func (l ActionRecorderList) ActionsByVerb() (Actions, error) {
    46  	var a Actions
    47  
    48  	for _, recorder := range l {
    49  		for _, action := range recorder.Actions() {
    50  			switch action.GetVerb() {
    51  			case "get":
    52  				a.Gets = append(a.Gets,
    53  					action.(clientgotesting.GetAction))
    54  			case "create":
    55  				a.Creates = append(a.Creates,
    56  					action.(clientgotesting.CreateAction))
    57  			case "update":
    58  				a.Updates = append(a.Updates,
    59  					action.(clientgotesting.UpdateAction))
    60  			case "delete":
    61  				a.Deletes = append(a.Deletes,
    62  					action.(clientgotesting.DeleteAction))
    63  			case "delete-collection":
    64  				a.DeleteCollections = append(a.DeleteCollections,
    65  					action.(clientgotesting.DeleteCollectionAction))
    66  			case "patch":
    67  				a.Patches = append(a.Patches,
    68  					action.(clientgotesting.PatchAction))
    69  			case "list", "watch": // avoid 'unexpected verb list/watch' error
    70  			default:
    71  				return a, fmt.Errorf("unexpected verb %v: %+v", action.GetVerb(), action)
    72  			}
    73  		}
    74  	}
    75  	return a, nil
    76  }