-
Notifications
You must be signed in to change notification settings - Fork 39k
/
core_events.go
243 lines (203 loc) · 8.9 KB
/
core_events.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright 2016 The Kubernetes Authors.
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 instrumentation
import (
"context"
"encoding/json"
"time"
v1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/instrumentation/common"
admissionapi "k8s.io/pod-security-admission/api"
"github.com/onsi/ginkgo/v2"
"k8s.io/apimachinery/pkg/types"
)
const (
eventRetryPeriod = 1 * time.Second
eventRetryTimeout = 1 * time.Minute
)
var _ = common.SIGDescribe("Events", func() {
f := framework.NewDefaultFramework("events")
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
/*
Release: v1.25
Testname: Event, manage lifecycle of an Event
Description: Attempt to create an event which MUST succeed.
Attempt to list all namespaces with a label selector which MUST
succeed. One list MUST be found. The event is patched with a
new message, the check MUST have the update message. The event
is updated with a new series of events, the check MUST confirm
this update. The event is deleted and MUST NOT show up when
listing all events.
*/
framework.ConformanceIt("should manage the lifecycle of an event", func() {
// As per SIG-Arch meeting 14 July 2022 this e2e test now supersede
// e2e test "Event resource lifecycle", which has been removed.
eventTestName := "event-test"
ginkgo.By("creating a test event")
// create a test event in test namespace
_, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Create(context.TODO(), &v1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: eventTestName,
Labels: map[string]string{
"testevent-constant": "true",
},
},
Message: "This is a test event",
Reason: "Test",
Type: "Normal",
Count: 1,
InvolvedObject: v1.ObjectReference{
Namespace: f.Namespace.Name,
},
}, metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create test event")
ginkgo.By("listing all events in all namespaces")
// get a list of Events in all namespaces to ensure endpoint coverage
eventsList, err := f.ClientSet.CoreV1().Events("").List(context.TODO(), metav1.ListOptions{
LabelSelector: "testevent-constant=true",
})
framework.ExpectNoError(err, "failed list all events")
foundCreatedEvent := false
var eventCreatedName string
for _, val := range eventsList.Items {
if val.ObjectMeta.Name == eventTestName && val.ObjectMeta.Namespace == f.Namespace.Name {
foundCreatedEvent = true
eventCreatedName = val.ObjectMeta.Name
break
}
}
if !foundCreatedEvent {
framework.Failf("unable to find test event %s in namespace %s, full list of events is %+v", eventTestName, f.Namespace.Name, eventsList.Items)
}
ginkgo.By("patching the test event")
// patch the event's message
eventPatchMessage := "This is a test event - patched"
eventPatch, err := json.Marshal(map[string]interface{}{
"message": eventPatchMessage,
})
framework.ExpectNoError(err, "failed to marshal the patch JSON payload")
_, err = f.ClientSet.CoreV1().Events(f.Namespace.Name).Patch(context.TODO(), eventTestName, types.StrategicMergePatchType, []byte(eventPatch), metav1.PatchOptions{})
framework.ExpectNoError(err, "failed to patch the test event")
ginkgo.By("fetching the test event")
// get event by name
event, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Get(context.TODO(), eventCreatedName, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to fetch the test event")
framework.ExpectEqual(event.Message, eventPatchMessage, "test event message does not match patch message")
ginkgo.By("updating the test event")
testEvent, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Get(context.TODO(), event.Name, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to get test event")
testEvent.Series = &v1.EventSeries{
Count: 100,
LastObservedTime: metav1.MicroTime{Time: time.Unix(1505828956, 0)},
}
// clear ResourceVersion and ManagedFields which are set by control-plane
testEvent.ObjectMeta.ResourceVersion = ""
testEvent.ObjectMeta.ManagedFields = nil
_, err = f.ClientSet.CoreV1().Events(f.Namespace.Name).Update(context.TODO(), testEvent, metav1.UpdateOptions{})
framework.ExpectNoError(err, "failed to update the test event")
ginkgo.By("getting the test event")
event, err = f.ClientSet.CoreV1().Events(f.Namespace.Name).Get(context.TODO(), testEvent.Name, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to get test event")
// clear ResourceVersion and ManagedFields which are set by control-plane
event.ObjectMeta.ResourceVersion = ""
event.ObjectMeta.ManagedFields = nil
if !apiequality.Semantic.DeepEqual(testEvent, event) {
framework.Failf("test event wasn't properly updated: %v", diff.ObjectReflectDiff(testEvent, event))
}
ginkgo.By("deleting the test event")
// delete original event
err = f.ClientSet.CoreV1().Events(f.Namespace.Name).Delete(context.TODO(), eventCreatedName, metav1.DeleteOptions{})
framework.ExpectNoError(err, "failed to delete the test event")
ginkgo.By("listing all events in all namespaces")
// get a list of Events list namespace
eventsList, err = f.ClientSet.CoreV1().Events("").List(context.TODO(), metav1.ListOptions{
LabelSelector: "testevent-constant=true",
})
framework.ExpectNoError(err, "fail to list all events")
foundCreatedEvent = false
for _, val := range eventsList.Items {
if val.ObjectMeta.Name == eventTestName && val.ObjectMeta.Namespace == f.Namespace.Name {
foundCreatedEvent = true
break
}
}
if foundCreatedEvent {
framework.Failf("Should not have found test event %s in namespace %s, full list of events %+v", eventTestName, f.Namespace.Name, eventsList.Items)
}
})
/*
Release: v1.20
Testname: Event, delete a collection
Description: A set of events is created with a label selector which MUST be found when listed.
The set of events is deleted and MUST NOT show up when listed by its label selector.
*/
framework.ConformanceIt("should delete a collection of events", func() {
eventTestNames := []string{"test-event-1", "test-event-2", "test-event-3"}
ginkgo.By("Create set of events")
// create a test event in test namespace
for _, eventTestName := range eventTestNames {
eventMessage := "This is " + eventTestName
_, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Create(context.TODO(), &v1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: eventTestName,
Labels: map[string]string{"testevent-set": "true"},
},
Message: eventMessage,
Reason: "Test",
Type: "Normal",
Count: 1,
InvolvedObject: v1.ObjectReference{
Namespace: f.Namespace.Name,
},
}, metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create event")
framework.Logf("created %v", eventTestName)
}
ginkgo.By("get a list of Events with a label in the current namespace")
// get a list of events
eventList, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{
LabelSelector: "testevent-set=true",
})
framework.ExpectNoError(err, "failed to get a list of events")
framework.ExpectEqual(len(eventList.Items), len(eventTestNames), "looking for expected number of pod templates events")
ginkgo.By("delete collection of events")
// delete collection
framework.Logf("requesting DeleteCollection of events")
err = f.ClientSet.CoreV1().Events(f.Namespace.Name).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: "testevent-set=true"})
framework.ExpectNoError(err, "failed to delete the test event")
ginkgo.By("check that the list of events matches the requested quantity")
err = wait.PollImmediate(eventRetryPeriod, eventRetryTimeout, checkEventListQuantity(f, "testevent-set=true", 0))
framework.ExpectNoError(err, "failed to count required events")
})
})
func checkEventListQuantity(f *framework.Framework, label string, quantity int) func() (bool, error) {
return func() (bool, error) {
var err error
framework.Logf("requesting list of events to confirm quantity")
eventList, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{
LabelSelector: label})
if err != nil {
return false, err
}
if len(eventList.Items) != quantity {
return false, err
}
return true, nil
}
}