forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_test.go
115 lines (94 loc) · 3.54 KB
/
events_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Veroute.on 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconciler
import (
"errors"
"io"
"net"
"testing"
"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
)
const (
exampleStatusFailed = "ExampleStatusFailed"
)
func TestNil_Is(t *testing.T) {
var err error
if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) {
t.Error("Did not expect error to be a ReconcilerEvent")
}
}
func TestError_Is(t *testing.T) {
err := errors.New("some other error")
if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) {
t.Error("Did not expect error to be a ReconcilerEvent")
}
}
func TestNew_Is(t *testing.T) {
err := NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "this is an example error, %s", "yep")
if !EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) {
t.Error("Expected error to be a [Warn, ExampleStatusFailed]")
}
}
func TestNewOtherType_Is(t *testing.T) {
err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is an example error, %s", "yep")
if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) {
t.Error("Expected error to be a [Normal, ExampleStatusFailed], filtered by eventtype failed")
}
}
func TestNewWrappedErrors_Is(t *testing.T) {
err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is a wrapped error, %w", io.ErrUnexpectedEOF)
if !EventIs(err, io.ErrUnexpectedEOF) {
t.Error("Event expected to be a wrapped ErrUnexpectedEOF but was not")
}
}
func TestNewOtherReason_Is(t *testing.T) {
err := NewEvent(corev1.EventTypeWarning, "otherReason", "this is an example error, %s", "yep")
if EventIs(err, NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "")) {
t.Error("Did not expect event to be [Warn, ExampleStatusFailed]")
}
}
func TestNew_As(t *testing.T) {
err := NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "this is an example error, %s", "yep")
var event *ReconcilerEvent
if !EventAs(err, &event) {
t.Errorf("Expected error to be a ReconcilerEvent, is not")
}
if event.EventType != "Warning" {
t.Error("Mismatched EventType, expected Warning, got", event.EventType)
}
if event.Reason != exampleStatusFailed {
t.Error("Mismatched Reason, expected ExampleStatusFailed, got", event.Reason)
}
}
func TestNewWrappedErrors_As(t *testing.T) {
err := NewEvent(corev1.EventTypeNormal, exampleStatusFailed, "this is a wrapped error, %w", &net.AddrError{})
if netErr := new(net.Error); !EventAs(err, netErr) {
t.Error("Event expected to be a wrapped net.AddrError but was not")
}
}
func TestNil_As(t *testing.T) {
var err error
var event *ReconcilerEvent
if EventAs(err, &event) {
t.Error("Did not expect error to be a ReconcilerEvent")
}
}
func TestNew_Error(t *testing.T) {
err := NewEvent(corev1.EventTypeWarning, exampleStatusFailed, "this is an example error, %s, %w", "yep", errors.New("indeed"))
const want = "this is an example error, yep, indeed"
got := err.Error()
if diff := cmp.Diff(want, got); diff != "" {
t.Error("Unexpected diff (-want, +got) =", diff)
}
}