knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/testing/reactions_test.go (about) 1 /* 2 Copyright 2020 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 var ( 28 imc = schema.GroupVersionResource{ 29 Group: "messaging.knative.dev", 30 Version: "v1", 31 Resource: "inmemorychannels", 32 } 33 34 revision = schema.GroupVersionResource{ 35 Group: "serving.knative.dev", 36 Version: "v1", 37 Resource: "revisions", 38 } 39 ) 40 41 func TestInduceFailure(t *testing.T) { 42 tests := []struct { 43 name string 44 // These set up the InduceFailure function 45 verb string 46 resource string 47 // This is the resource fed to the function 48 testSource schema.GroupVersionResource 49 // This is the subresource fed to the function 50 testSubresource string 51 wantHandled bool 52 }{{ 53 name: "resource", 54 verb: "patch", 55 testSource: imc, 56 resource: "inmemorychannels", 57 wantHandled: true, 58 }, { 59 name: "resource, wrong verb", 60 verb: "create", 61 testSource: imc, 62 resource: "inmemorychannels", 63 }, { 64 name: "wrong resource", 65 verb: "patch", 66 testSource: revision, 67 resource: "inmemorychannels", 68 }, { 69 name: "resource and subresource", 70 verb: "patch", 71 testSource: imc, 72 resource: "inmemorychannels/status", 73 testSubresource: "status", 74 wantHandled: true, 75 }, { 76 name: "resource and subresource, wrong verb", 77 verb: "get", 78 testSource: imc, 79 resource: "inmemorychannels/status", 80 testSubresource: "status", 81 }, { 82 name: "resource and subresource, subresource does not match", 83 verb: "patch", 84 testSource: imc, 85 resource: "inmemorychannels/status", 86 testSubresource: "finalizers", 87 }} 88 for _, tc := range tests { 89 t.Run(tc.name, func(t *testing.T) { 90 f := InduceFailure(tc.verb, tc.resource) 91 var patchAction clientgotesting.PatchActionImpl 92 if tc.testSubresource != "" { 93 patchAction = clientgotesting.NewPatchSubresourceAction(tc.testSource, "testns", "test", types.JSONPatchType, []byte{}, tc.testSubresource) 94 } else { 95 patchAction = clientgotesting.NewPatchAction(tc.testSource, "testns", "test", types.JSONPatchType, []byte{}) 96 } 97 if handled, _, _ := f(patchAction); handled != tc.wantHandled { 98 t.Errorf("Handled got = %v, want: %v", tc.wantHandled, handled) 99 } 100 }) 101 } 102 }