knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/events_test.go (about) 1 /* 2 Copyright 2020 The Knative Authors 3 4 Licensed under the Apache License, Veroute.on 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 reconciler 18 19 import ( 20 "errors" 21 "io" 22 "net" 23 "testing" 24 25 "github.com/google/go-cmp/cmp" 26 corev1 "k8s.io/api/core/v1" 27 ) 28 29 const ( 30 exampleStatusFailed = "ExampleStatusFailed" 31 ) 32 33 func TestNil_Is(t *testing.T) { 34 var err error 35 if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) { 36 t.Error("Did not expect error to be a ReconcilerEvent") 37 } 38 } 39 40 func TestError_Is(t *testing.T) { 41 err := errors.New("some other error") 42 if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) { 43 t.Error("Did not expect error to be a ReconcilerEvent") 44 } 45 } 46 47 func TestNew_Is(t *testing.T) { 48 err := NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "this is an example error, %s", "yep") 49 if !EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) { 50 t.Error("Expected error to be a [Warn, ExampleStatusFailed]") 51 } 52 } 53 54 func TestNewOtherType_Is(t *testing.T) { 55 err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is an example error, %s", "yep") 56 if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) { 57 t.Error("Expected error to be a [Normal, ExampleStatusFailed], filtered by eventtype failed") 58 } 59 } 60 61 func TestNewWrappedErrors_Is(t *testing.T) { 62 err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is a wrapped error, %w", io.ErrUnexpectedEOF) 63 if !EventIs(err, io.ErrUnexpectedEOF) { 64 t.Error("Event expected to be a wrapped ErrUnexpectedEOF but was not") 65 } 66 } 67 68 func TestNewOtherReason_Is(t *testing.T) { 69 err := NewEvent(corev1.EventTypeWarning, "otherReason", "this is an example error, %s", "yep") 70 if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) { 71 t.Error("Did not expect event to be [Warn, ExampleStatusFailed]") 72 } 73 } 74 75 func TestNew_As(t *testing.T) { 76 err := NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "this is an example error, %s", "yep") 77 78 var event *ReconcilerEvent 79 if !EventAs(err, &event) { 80 t.Errorf("Expected error to be a ReconcilerEvent, is not") 81 } 82 83 if event.EventType != "Warning" { 84 t.Error("Mismatched EventType, expected Warning, got", event.EventType) 85 } 86 if event.Reason != exampleStatusFailed { 87 t.Error("Mismatched Reason, expected ExampleStatusFailed, got", event.Reason) 88 } 89 } 90 91 func TestNewWrappedErrors_As(t *testing.T) { 92 err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is a wrapped error, %w", &net.AddrError{}) 93 if netErr := new(net.Error); !EventAs(err, netErr) { 94 t.Error("Event expected to be a wrapped net.AddrError but was not") 95 } 96 } 97 98 func TestNil_As(t *testing.T) { 99 var err error 100 101 var event *ReconcilerEvent 102 if EventAs(err, &event) { 103 t.Error("Did not expect error to be a ReconcilerEvent") 104 } 105 } 106 107 func TestNew_Error(t *testing.T) { 108 err := NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "this is an example error, %s, %w", "yep", errors.New("indeed")) 109 110 const want = "this is an example error, yep, indeed" 111 got := err.Error() 112 if diff := cmp.Diff(want, got); diff != "" { 113 t.Error("Unexpected diff (-want, +got) =", diff) 114 } 115 }