|
| 1 | +/* |
| 2 | +Copyright 2016 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 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 apimachinery |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "time" |
| 23 | + |
| 24 | + v1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/util/wait" |
| 27 | + "k8s.io/kubernetes/test/e2e/framework" |
| 28 | + |
| 29 | + "github.com/onsi/ginkgo" |
| 30 | + "k8s.io/apimachinery/pkg/types" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + eventRetryPeriod = 1 * time.Second |
| 35 | + eventRetryTimeout = 1 * time.Minute |
| 36 | +) |
| 37 | + |
| 38 | +var _ = ginkgo.Describe("[sig-api-machinery] Events", func() { |
| 39 | + f := framework.NewDefaultFramework("events") |
| 40 | + |
| 41 | + /* |
| 42 | + Release : v1.19 |
| 43 | + Testname: Event resource lifecycle |
| 44 | + Description: Create an event, the event MUST exist. |
| 45 | + The event is patched with a new message, the check MUST have the update message. |
| 46 | + The event is deleted and MUST NOT show up when listing all events. |
| 47 | + */ |
| 48 | + framework.ConformanceIt("should ensure that an event can be fetched, patched, deleted, and listed", func() { |
| 49 | + eventTestName := "event-test" |
| 50 | + |
| 51 | + ginkgo.By("creating a test event") |
| 52 | + // create a test event in test namespace |
| 53 | + _, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Create(context.TODO(), &v1.Event{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Name: eventTestName, |
| 56 | + Labels: map[string]string{ |
| 57 | + "testevent-constant": "true", |
| 58 | + }, |
| 59 | + }, |
| 60 | + Message: "This is a test event", |
| 61 | + Reason: "Test", |
| 62 | + Type: "Normal", |
| 63 | + Count: 1, |
| 64 | + InvolvedObject: v1.ObjectReference{ |
| 65 | + Namespace: f.Namespace.Name, |
| 66 | + }, |
| 67 | + }, metav1.CreateOptions{}) |
| 68 | + framework.ExpectNoError(err, "failed to create test event") |
| 69 | + |
| 70 | + ginkgo.By("listing all events in all namespaces") |
| 71 | + // get a list of Events in all namespaces to ensure endpoint coverage |
| 72 | + eventsList, err := f.ClientSet.CoreV1().Events("").List(context.TODO(), metav1.ListOptions{ |
| 73 | + LabelSelector: "testevent-constant=true", |
| 74 | + }) |
| 75 | + framework.ExpectNoError(err, "failed list all events") |
| 76 | + |
| 77 | + foundCreatedEvent := false |
| 78 | + var eventCreatedName string |
| 79 | + for _, val := range eventsList.Items { |
| 80 | + if val.ObjectMeta.Name == eventTestName && val.ObjectMeta.Namespace == f.Namespace.Name { |
| 81 | + foundCreatedEvent = true |
| 82 | + eventCreatedName = val.ObjectMeta.Name |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + framework.ExpectEqual(foundCreatedEvent, true, "unable to find the test event") |
| 87 | + |
| 88 | + ginkgo.By("patching the test event") |
| 89 | + // patch the event's message |
| 90 | + eventPatchMessage := "This is a test event - patched" |
| 91 | + eventPatch, err := json.Marshal(map[string]interface{}{ |
| 92 | + "message": eventPatchMessage, |
| 93 | + }) |
| 94 | + framework.ExpectNoError(err, "failed to marshal the patch JSON payload") |
| 95 | + |
| 96 | + _, err = f.ClientSet.CoreV1().Events(f.Namespace.Name).Patch(context.TODO(), eventTestName, types.StrategicMergePatchType, []byte(eventPatch), metav1.PatchOptions{}) |
| 97 | + framework.ExpectNoError(err, "failed to patch the test event") |
| 98 | + |
| 99 | + ginkgo.By("fetching the test event") |
| 100 | + // get event by name |
| 101 | + event, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Get(context.TODO(), eventCreatedName, metav1.GetOptions{}) |
| 102 | + framework.ExpectNoError(err, "failed to fetch the test event") |
| 103 | + framework.ExpectEqual(event.Message, eventPatchMessage, "test event message does not match patch message") |
| 104 | + |
| 105 | + ginkgo.By("deleting the test event") |
| 106 | + // delete original event |
| 107 | + err = f.ClientSet.CoreV1().Events(f.Namespace.Name).Delete(context.TODO(), eventCreatedName, metav1.DeleteOptions{}) |
| 108 | + framework.ExpectNoError(err, "failed to delete the test event") |
| 109 | + |
| 110 | + ginkgo.By("listing all events in all namespaces") |
| 111 | + // get a list of Events list namespace |
| 112 | + eventsList, err = f.ClientSet.CoreV1().Events("").List(context.TODO(), metav1.ListOptions{ |
| 113 | + LabelSelector: "testevent-constant=true", |
| 114 | + }) |
| 115 | + framework.ExpectNoError(err, "fail to list all events") |
| 116 | + foundCreatedEvent = false |
| 117 | + for _, val := range eventsList.Items { |
| 118 | + if val.ObjectMeta.Name == eventTestName && val.ObjectMeta.Namespace == f.Namespace.Name { |
| 119 | + foundCreatedEvent = true |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + framework.ExpectEqual(foundCreatedEvent, false, "should not have found test event after deletion") |
| 124 | + }) |
| 125 | + |
| 126 | + ginkgo.It("should delete a collection of events", func() { |
| 127 | + eventTestNames := []string{"test-event-1", "test-event-2", "test-event-3"} |
| 128 | + |
| 129 | + ginkgo.By("Create set of events") |
| 130 | + // create a test event in test namespace |
| 131 | + for _, eventTestName := range eventTestNames { |
| 132 | + eventMessage := "This is " + eventTestName |
| 133 | + _, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).Create(context.TODO(), &v1.Event{ |
| 134 | + |
| 135 | + ObjectMeta: metav1.ObjectMeta{ |
| 136 | + Name: eventTestName, |
| 137 | + Labels: map[string]string{"testevent-set": "true"}, |
| 138 | + }, |
| 139 | + Message: eventMessage, |
| 140 | + Reason: "Test", |
| 141 | + Type: "Normal", |
| 142 | + Count: 1, |
| 143 | + InvolvedObject: v1.ObjectReference{ |
| 144 | + Namespace: f.Namespace.Name, |
| 145 | + }, |
| 146 | + }, metav1.CreateOptions{}) |
| 147 | + framework.ExpectNoError(err, "failed to create event") |
| 148 | + framework.Logf("created %v", eventTestName) |
| 149 | + } |
| 150 | + |
| 151 | + ginkgo.By("get a list of Events with a label in the current namespace") |
| 152 | + // get a list of events |
| 153 | + eventList, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ |
| 154 | + LabelSelector: "testevent-set=true", |
| 155 | + }) |
| 156 | + framework.ExpectNoError(err, "failed to get a list of events") |
| 157 | + |
| 158 | + framework.ExpectEqual(len(eventList.Items), len(eventTestNames), "looking for expected number of pod templates events") |
| 159 | + |
| 160 | + ginkgo.By("delete collection of events") |
| 161 | + // delete collection |
| 162 | + |
| 163 | + framework.Logf("requesting DeleteCollection of events") |
| 164 | + err = f.ClientSet.CoreV1().Events(f.Namespace.Name).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{ |
| 165 | + LabelSelector: "testevent-set=true"}) |
| 166 | + framework.ExpectNoError(err, "failed to delete the test event") |
| 167 | + |
| 168 | + ginkgo.By("check that the list of events matches the requested quantity") |
| 169 | + |
| 170 | + err = wait.PollImmediate(eventRetryPeriod, eventRetryTimeout, checkEventListQuantity(f, "testevent-set=true", 0)) |
| 171 | + framework.ExpectNoError(err, "failed to count required events") |
| 172 | + }) |
| 173 | + |
| 174 | +}) |
| 175 | + |
| 176 | +func checkEventListQuantity(f *framework.Framework, label string, quantity int) func() (bool, error) { |
| 177 | + return func() (bool, error) { |
| 178 | + var err error |
| 179 | + |
| 180 | + framework.Logf("requesting list of events to confirm quantity") |
| 181 | + |
| 182 | + eventList, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ |
| 183 | + LabelSelector: label}) |
| 184 | + |
| 185 | + if err != nil { |
| 186 | + return false, err |
| 187 | + } |
| 188 | + |
| 189 | + if len(eventList.Items) != quantity { |
| 190 | + return false, err |
| 191 | + } |
| 192 | + return true, nil |
| 193 | + } |
| 194 | +} |
0 commit comments