knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/testing/actions_test.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  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime/schema"
    23  	"k8s.io/apimachinery/pkg/types"
    24  	clientgotesting "k8s.io/client-go/testing"
    25  )
    26  
    27  func TestActionsByVerb(t *testing.T) {
    28  	list := ActionRecorderList{
    29  		fakeRecorder{
    30  			newGetAction(),
    31  		},
    32  		fakeRecorder{
    33  			newCreateAction(),
    34  			newUpdateAction(),
    35  			newDeleteAction(),
    36  			newPatchAction(),
    37  		},
    38  		fakeRecorder{
    39  			newCreateAction(),
    40  		},
    41  		fakeRecorder{
    42  			newUpdateAction(),
    43  		},
    44  		fakeRecorder{
    45  			newDeleteAction(),
    46  		},
    47  		fakeRecorder{
    48  			newPatchAction(),
    49  		},
    50  	}
    51  
    52  	actions, err := list.ActionsByVerb()
    53  	if err != nil {
    54  		t.Error("Unexpected error sorting actions by verb", err)
    55  	}
    56  
    57  	if got, want := len(actions.Creates), 2; got != want {
    58  		t.Errorf("Create action count = %d, want %d", got, want)
    59  	}
    60  
    61  	if got, want := len(actions.Updates), 2; got != want {
    62  		t.Errorf("Update action count = %d; want %d", got, want)
    63  	}
    64  
    65  	if got, want := len(actions.Deletes), 2; got != want {
    66  		t.Errorf("Delete action count is incorrect got %d - want %d", got, want)
    67  	}
    68  
    69  	if got, want := len(actions.Patches), 2; got != want {
    70  		t.Errorf("Patch action = %d; want %d", got, want)
    71  	}
    72  }
    73  
    74  func TestActionsByVerb_UnrecognizedVerb(t *testing.T) {
    75  	list := ActionRecorderList{
    76  		fakeRecorder{
    77  			clientgotesting.ActionImpl{Verb: "unknown"},
    78  		},
    79  	}
    80  
    81  	if _, err := list.ActionsByVerb(); err == nil {
    82  		t.Error("Expected an error to have occurred when grouping actions")
    83  	}
    84  }
    85  
    86  func newGetAction() clientgotesting.Action {
    87  	return clientgotesting.NewGetAction(schema.GroupVersionResource{}, "namespace", "name")
    88  }
    89  
    90  func newCreateAction() clientgotesting.Action {
    91  	return clientgotesting.NewCreateAction(schema.GroupVersionResource{}, "namespace", nil)
    92  }
    93  
    94  func newUpdateAction() clientgotesting.Action {
    95  	return clientgotesting.NewUpdateAction(schema.GroupVersionResource{}, "namespace", nil)
    96  }
    97  
    98  func newDeleteAction() clientgotesting.Action {
    99  	return clientgotesting.NewDeleteAction(schema.GroupVersionResource{}, "namespace", "name")
   100  }
   101  
   102  func newPatchAction() clientgotesting.Action {
   103  	return clientgotesting.NewPatchAction(schema.GroupVersionResource{}, "namespace", "name", types.JSONPatchType, nil)
   104  }
   105  
   106  type fakeRecorder []clientgotesting.Action
   107  
   108  func (f fakeRecorder) Actions() []clientgotesting.Action {
   109  	return f
   110  }
   111  
   112  var _ ActionRecorder = (fakeRecorder)(nil)