/
events_test.go
147 lines (124 loc) · 4.37 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright 2021 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 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 k8sutil for Kubernetes helpers.
package k8sutil
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
)
func getEventsOccurences(channel chan string) map[string]int {
foundEvents := make(map[string]int)
for len(channel) > 0 {
e := <-channel
foundEvents[e]++
}
return foundEvents
}
func TestReportIfNotPresent(t *testing.T) {
pod1 := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod1",
Namespace: "rook-ceph",
},
}
pod2 := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod2",
Namespace: "rook-ceph",
},
}
testCases := []struct {
eventReported int
changeTime bool
ReportAnotherEvent bool
ReportEventForDifferentObject bool
}{
{
// verify ReportIfNotPresent is called once and event is reported once
eventReported: 1,
},
{
// verify ReportIfNotPresent is called twice and event is reported once
eventReported: 2,
},
{
// verify ReportIfNotPresent report same event again when event is not present on cluster
eventReported: 1,
changeTime: true,
},
{
// verify ReportIfNotPresent report same event again when event is not present on cluster
eventReported: 2,
changeTime: true,
},
{
// verify it report event "a" both the times if events came like "a", "b", "a"
eventReported: 1,
ReportAnotherEvent: true,
},
{
// verify it report event "a" both the times if events came like "a", "b", "a"
eventReported: 2,
ReportAnotherEvent: true,
},
{
// verify it does not report the same event for same objects if multiple objects come into the picture
eventReported: 1,
ReportEventForDifferentObject: true,
},
{
// verify it does not report the same event for same objects if multiple objects come into the picture
eventReported: 2,
ReportEventForDifferentObject: true,
},
}
for _, tc := range testCases {
eventType, eventReason, eventMsg := corev1.EventTypeNormal, "Created", "Pod has been created"
frecorder := record.NewFakeRecorder(1024)
reporter := NewEventReporter(frecorder)
for i := 0; i < tc.eventReported; i++ {
reporter.ReportIfNotPresent(pod1, eventType, eventReason, eventMsg)
}
foundEvents := getEventsOccurences(frecorder.Events)
assert.Equal(t, 1, foundEvents[eventType+" "+eventReason+" "+eventMsg])
if tc.changeTime {
nameSpacedName, err := getNameSpacedName(pod1)
assert.NoError(t, err)
ftime := reporter.lastReportedEventTime[nameSpacedName].Add(time.Minute * -60)
reporter.lastReportedEventTime[nameSpacedName] = ftime
reporter.ReportIfNotPresent(pod1, eventType, eventReason, eventMsg)
foundEvents := getEventsOccurences(frecorder.Events)
assert.Equal(t, 1, foundEvents[eventType+" "+eventReason+" "+eventMsg])
}
if tc.ReportAnotherEvent {
reporter.ReportIfNotPresent(pod1, corev1.EventTypeWarning, eventReason, eventMsg)
foundEvents := getEventsOccurences(frecorder.Events)
assert.Equal(t, 1, foundEvents[corev1.EventTypeWarning+" "+eventReason+" "+eventMsg])
reporter.ReportIfNotPresent(pod1, eventType, eventReason, eventMsg)
foundEvents = getEventsOccurences(frecorder.Events)
assert.Equal(t, 1, foundEvents[eventType+" "+eventReason+" "+eventMsg])
}
if tc.ReportEventForDifferentObject {
reporter.ReportIfNotPresent(pod2, eventType, eventReason, eventMsg)
foundEvents := getEventsOccurences(frecorder.Events)
assert.Equal(t, 1, foundEvents[eventType+" "+eventReason+" "+eventMsg])
reporter.ReportIfNotPresent(pod1, eventType, eventReason, eventMsg)
foundEvents = getEventsOccurences(frecorder.Events)
assert.Equal(t, 0, foundEvents[eventType+" "+eventReason+" "+eventMsg])
}
}
}