-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
event_test.go
137 lines (130 loc) · 4.14 KB
/
event_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package watchevents
import (
"context"
"regexp"
"testing"
"time"
"github.com/openshift/origin/pkg/monitor"
"github.com/openshift/origin/pkg/monitor/monitorapi"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func Test_recordAddOrUpdateEvent(t *testing.T) {
type args struct {
ctx context.Context
m monitorapi.Recorder
client kubernetes.Interface
reMatchFirstQuote *regexp.Regexp
significantlyBeforeNow time.Time
kubeEvent *corev1.Event
}
now := time.Now()
tests := []struct {
name string
args args
skip bool
kubeEvent *corev1.Event
expectedLocator string
expectedMessage string
}{
{
name: "simple event",
args: args{
ctx: context.TODO(),
m: monitor.NewRecorder(),
kubeEvent: &corev1.Event{
Count: 2,
Reason: "SomethingHappened",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Namespace: "openshift-authentication",
Name: "testpod-927947",
},
Message: "sample message",
LastTimestamp: metav1.Now(),
},
},
expectedLocator: "ns/openshift-authentication pod/testpod-927947",
expectedMessage: "reason/SomethingHappened sample message (2 times)",
},
{
name: "unknown pathological event",
args: args{
ctx: context.TODO(),
m: monitor.NewRecorder(),
kubeEvent: &corev1.Event{
Count: 40,
Reason: "SomethingHappened",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Namespace: "openshift-authentication",
Name: "testpod-927947",
},
Message: "sample message",
LastTimestamp: metav1.Now(),
},
},
expectedLocator: "ns/openshift-authentication pod/testpod-927947 hmsg/72c78c2ba1",
expectedMessage: "pathological/true reason/SomethingHappened sample message (40 times)",
},
{
name: "allowed pathological event",
args: args{
ctx: context.TODO(),
m: monitor.NewRecorder(),
kubeEvent: &corev1.Event{
Count: 40,
Reason: "SomethingHappened",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Namespace: "openshift-e2e-loki",
Name: "loki-promtail-982739",
},
Message: "Readiness probe failed",
LastTimestamp: metav1.Now(),
},
significantlyBeforeNow: now.UTC().Add(-15 * time.Minute),
},
expectedLocator: "ns/openshift-e2e-loki pod/loki-promtail-982739 hmsg/04cd2d7fbb",
expectedMessage: "pathological/true interesting/true reason/SomethingHappened Readiness probe failed (40 times)",
},
{
name: "allowed pathological event with known bug (BZ 2000234)",
args: args{
ctx: context.TODO(),
m: monitor.NewRecorder(),
kubeEvent: &corev1.Event{
Count: 40,
Reason: "ns/openshift-etcd pod/etcd-quorum-guard-42 node/worker-42 - reason/Unhealthy",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Namespace: "openshift-etcd",
Name: "etcd-quorum-guard-42",
},
Message: "Readiness probe failed:",
LastTimestamp: metav1.Now(),
},
significantlyBeforeNow: now.UTC().Add(-15 * time.Minute),
},
// hmsg in expectedLocator is the hash of the entire expectedMessage except the number of times
expectedLocator: "ns/openshift-etcd pod/etcd-quorum-guard-42 hmsg/9100aa725d",
expectedMessage: "pathological/true interesting/true reason/ns/openshift-etcd pod/etcd-quorum-guard-42 node/worker-42 - reason/Unhealthy Readiness probe failed: (40 times)",
},
}
for _, tt := range tests {
if tt.skip {
continue
}
t.Run(tt.name, func(t *testing.T) {
significantlyBeforeNow := now.UTC().Add(-15 * time.Minute)
recordAddOrUpdateEvent(tt.args.ctx, tt.args.m, nil, nil, significantlyBeforeNow, tt.args.kubeEvent)
intervals := tt.args.m.Intervals(now.Add(-10*time.Minute), now.Add(10*time.Minute))
assert.Equal(t, 1, len(intervals))
interval := intervals[0]
assert.Equal(t, tt.expectedLocator, interval.Locator)
assert.Equal(t, tt.expectedMessage, interval.Message)
})
}
}