|
| 1 | +/* |
| 2 | +Copyright 2020 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 instrumentation |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "time" |
| 24 | + |
| 25 | + "k8s.io/api/core/v1" |
| 26 | + eventsv1beta1 "k8s.io/api/events/v1beta1" |
| 27 | + apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/util/diff" |
| 30 | + typedeventsv1beta1 "k8s.io/client-go/kubernetes/typed/events/v1beta1" |
| 31 | + "k8s.io/kubernetes/test/e2e/framework" |
| 32 | + "k8s.io/kubernetes/test/e2e/instrumentation/common" |
| 33 | + |
| 34 | + "github.com/onsi/ginkgo" |
| 35 | + "k8s.io/apimachinery/pkg/types" |
| 36 | +) |
| 37 | + |
| 38 | +func newTestEvent(namespace, name, label string) *eventsv1beta1.Event { |
| 39 | + someTime := metav1.MicroTime{Time: time.Unix(1505828956, 0)} |
| 40 | + return &eventsv1beta1.Event{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: name, |
| 43 | + Labels: map[string]string{ |
| 44 | + label: "true", |
| 45 | + }, |
| 46 | + }, |
| 47 | + Regarding: v1.ObjectReference{ |
| 48 | + Namespace: namespace, |
| 49 | + }, |
| 50 | + EventTime: someTime, |
| 51 | + Note: "This is " + name, |
| 52 | + Action: "Do", |
| 53 | + Reason: "Test", |
| 54 | + Type: "Normal", |
| 55 | + ReportingController: "test-controller", |
| 56 | + ReportingInstance: "test-node", |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func eventExistsInList(client typedeventsv1beta1.EventInterface, namespace, name string) bool { |
| 61 | + eventsList, err := client.List(context.TODO(), metav1.ListOptions{ |
| 62 | + LabelSelector: "testevent-constant=true", |
| 63 | + }) |
| 64 | + framework.ExpectNoError(err, "failed to list events") |
| 65 | + |
| 66 | + for _, val := range eventsList.Items { |
| 67 | + if val.ObjectMeta.Name == name && val.ObjectMeta.Namespace == namespace { |
| 68 | + return true |
| 69 | + } |
| 70 | + } |
| 71 | + return false |
| 72 | +} |
| 73 | + |
| 74 | +var _ = common.SIGDescribe("Events API", func() { |
| 75 | + f := framework.NewDefaultFramework("events") |
| 76 | + var client typedeventsv1beta1.EventInterface |
| 77 | + var clientAllNamespaces typedeventsv1beta1.EventInterface |
| 78 | + |
| 79 | + ginkgo.BeforeEach(func() { |
| 80 | + client = f.ClientSet.EventsV1beta1().Events(f.Namespace.Name) |
| 81 | + clientAllNamespaces = f.ClientSet.EventsV1beta1().Events(metav1.NamespaceAll) |
| 82 | + }) |
| 83 | + |
| 84 | + /* |
| 85 | + Release : v1.19 |
| 86 | + Testname: Event resource lifecycle |
| 87 | + Description: Create an event, the event MUST exist. |
| 88 | + The event is patched with a new note, the check MUST have the update note. |
| 89 | + The event is deleted and MUST NOT show up when listing all events. |
| 90 | + */ |
| 91 | + ginkgo.It("should ensure that an event can be fetched, patched, deleted, and listed", func() { |
| 92 | + eventName := "event-test" |
| 93 | + |
| 94 | + ginkgo.By("creating a test event") |
| 95 | + _, err := client.Create(context.TODO(), newTestEvent(f.Namespace.Name, eventName, "testevent-constant"), metav1.CreateOptions{}) |
| 96 | + framework.ExpectNoError(err, "failed to create test event") |
| 97 | + |
| 98 | + ginkgo.By("listing events in all namespaces") |
| 99 | + foundCreatedEvent := eventExistsInList(clientAllNamespaces, f.Namespace.Name, eventName) |
| 100 | + framework.ExpectEqual(foundCreatedEvent, true, "failed to find test event in list with cluster scope") |
| 101 | + |
| 102 | + ginkgo.By("listing events in test namespace") |
| 103 | + foundCreatedEvent = eventExistsInList(client, f.Namespace.Name, eventName) |
| 104 | + framework.ExpectEqual(foundCreatedEvent, true, "failed to find test event in list with namespace scope") |
| 105 | + |
| 106 | + ginkgo.By("getting the test event") |
| 107 | + testEvent, err := client.Get(context.TODO(), eventName, metav1.GetOptions{}) |
| 108 | + framework.ExpectNoError(err, "failed to get test event") |
| 109 | + |
| 110 | + ginkgo.By("patching the test event") |
| 111 | + eventPatchNote := "This is a test event - patched" |
| 112 | + eventPatch, err := json.Marshal(map[string]interface{}{ |
| 113 | + "note": eventPatchNote, |
| 114 | + }) |
| 115 | + framework.ExpectNoError(err, "failed to marshal the patch JSON payload") |
| 116 | + |
| 117 | + _, err = client.Patch(context.TODO(), eventName, types.StrategicMergePatchType, []byte(eventPatch), metav1.PatchOptions{}) |
| 118 | + framework.ExpectNoError(err, "failed to patch the test event") |
| 119 | + |
| 120 | + ginkgo.By("getting the test event") |
| 121 | + event, err := client.Get(context.TODO(), eventName, metav1.GetOptions{}) |
| 122 | + framework.ExpectNoError(err, "failed to get test event") |
| 123 | + // clear ResourceVersion and ManagedFields which are set by control-plane |
| 124 | + event.ObjectMeta.ResourceVersion = "" |
| 125 | + testEvent.ObjectMeta.ResourceVersion = "" |
| 126 | + event.ObjectMeta.ManagedFields = nil |
| 127 | + testEvent.ObjectMeta.ManagedFields = nil |
| 128 | + testEvent.Note = eventPatchNote |
| 129 | + if !apiequality.Semantic.DeepEqual(testEvent, event) { |
| 130 | + framework.Failf("test event wasn't properly patched: %v", diff.ObjectReflectDiff(testEvent, event)) |
| 131 | + } |
| 132 | + |
| 133 | + ginkgo.By("deleting the test event") |
| 134 | + err = client.Delete(context.TODO(), eventName, metav1.DeleteOptions{}) |
| 135 | + framework.ExpectNoError(err, "failed to delete the test event") |
| 136 | + |
| 137 | + ginkgo.By("listing events in all namespaces") |
| 138 | + foundCreatedEvent = eventExistsInList(clientAllNamespaces, f.Namespace.Name, eventName) |
| 139 | + framework.ExpectEqual(foundCreatedEvent, false, "should not have found test event after deletion") |
| 140 | + |
| 141 | + ginkgo.By("listing events in test namespace") |
| 142 | + foundCreatedEvent = eventExistsInList(client, f.Namespace.Name, eventName) |
| 143 | + framework.ExpectEqual(foundCreatedEvent, false, "should not have found test event after deletion") |
| 144 | + }) |
| 145 | + |
| 146 | + ginkgo.It("should delete a collection of events", func() { |
| 147 | + eventNames := []string{"test-event-1", "test-event-2", "test-event-3"} |
| 148 | + |
| 149 | + ginkgo.By("Create set of events") |
| 150 | + for _, eventName := range eventNames { |
| 151 | + _, err := client.Create(context.TODO(), newTestEvent(f.Namespace.Name, eventName, "testevent-set"), metav1.CreateOptions{}) |
| 152 | + framework.ExpectNoError(err, "failed to create event") |
| 153 | + } |
| 154 | + |
| 155 | + ginkgo.By("get a list of Events with a label in the current namespace") |
| 156 | + eventList, err := client.List(context.TODO(), metav1.ListOptions{ |
| 157 | + LabelSelector: "testevent-set=true", |
| 158 | + }) |
| 159 | + framework.ExpectNoError(err, "failed to get a list of events") |
| 160 | + framework.ExpectEqual(len(eventList.Items), len(eventNames), fmt.Sprintf("unexpected event list: %#v", eventList)) |
| 161 | + |
| 162 | + ginkgo.By("delete a list of events") |
| 163 | + framework.Logf("requesting DeleteCollection of events") |
| 164 | + err = client.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{ |
| 165 | + LabelSelector: "testevent-set=true", |
| 166 | + }) |
| 167 | + framework.ExpectNoError(err, "failed to delete the test event") |
| 168 | + |
| 169 | + ginkgo.By("check that the list of events matches the requested quantity") |
| 170 | + eventList, err = client.List(context.TODO(), metav1.ListOptions{ |
| 171 | + LabelSelector: "testevent-set=true", |
| 172 | + }) |
| 173 | + framework.ExpectNoError(err, "failed to get a list of events") |
| 174 | + framework.ExpectEqual(len(eventList.Items), 0, fmt.Sprintf("unexpected event list: %#v", eventList)) |
| 175 | + }) |
| 176 | +}) |
0 commit comments