|
| 1 | +/* |
| 2 | +Copyright 2025 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 events |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + |
| 27 | + v1 "k8s.io/api/core/v1" |
| 28 | + networkingv1 "k8s.io/api/networking/v1" |
| 29 | + |
| 30 | + eventsv1 "k8s.io/api/events/v1" |
| 31 | + apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/runtime" |
| 34 | + utilvalidation "k8s.io/apimachinery/pkg/util/validation" |
| 35 | + "k8s.io/apimachinery/pkg/watch" |
| 36 | + testclocks "k8s.io/utils/clock/testing" |
| 37 | +) |
| 38 | + |
| 39 | +func TestEventf(t *testing.T) { |
| 40 | + // use a fixed time for generated names that depend on the unix timestamp |
| 41 | + fakeClock := testclocks.NewFakeClock(time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)) |
| 42 | + |
| 43 | + testCases := []struct { |
| 44 | + desc string |
| 45 | + regarding runtime.Object |
| 46 | + related runtime.Object |
| 47 | + eventtype string |
| 48 | + reason string |
| 49 | + action string |
| 50 | + note string |
| 51 | + args []interface{} |
| 52 | + expectedEvent *eventsv1.Event |
| 53 | + }{ |
| 54 | + { |
| 55 | + desc: "normal event", |
| 56 | + regarding: &v1.Pod{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: "pod1", |
| 59 | + Namespace: "ns1", |
| 60 | + UID: "12345", |
| 61 | + }, |
| 62 | + }, |
| 63 | + eventtype: "Normal", |
| 64 | + reason: "Started", |
| 65 | + action: "starting", |
| 66 | + note: "Pod started", |
| 67 | + expectedEvent: &eventsv1.Event{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{ |
| 69 | + Name: fmt.Sprintf("pod1.%x", fakeClock.Now().UnixNano()), |
| 70 | + Namespace: "ns1", |
| 71 | + }, |
| 72 | + Regarding: v1.ObjectReference{ |
| 73 | + Kind: "Pod", |
| 74 | + Name: "pod1", |
| 75 | + Namespace: "ns1", |
| 76 | + UID: "12345", |
| 77 | + APIVersion: "v1", |
| 78 | + }, |
| 79 | + Type: "Normal", |
| 80 | + Reason: "Started", |
| 81 | + Action: "starting", |
| 82 | + Note: "Pod started", |
| 83 | + ReportingController: "c1", |
| 84 | + ReportingInstance: "i1", |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + desc: "event with related object and format args", |
| 89 | + regarding: &v1.Pod{ |
| 90 | + ObjectMeta: metav1.ObjectMeta{ |
| 91 | + Name: "pod1", |
| 92 | + Namespace: "ns1", |
| 93 | + UID: "12345", |
| 94 | + }, |
| 95 | + }, |
| 96 | + related: &v1.Node{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: "node1", |
| 99 | + UID: "67890", |
| 100 | + }, |
| 101 | + }, |
| 102 | + eventtype: "Warning", |
| 103 | + reason: "FailedScheduling", |
| 104 | + action: "scheduling", |
| 105 | + |
| 106 | + note: "Pod failed to schedule on %s: %s", |
| 107 | + args: []interface{}{"node1", "not enough resources"}, |
| 108 | + expectedEvent: &eventsv1.Event{ |
| 109 | + ObjectMeta: metav1.ObjectMeta{ |
| 110 | + Name: fmt.Sprintf("pod1.%x", fakeClock.Now().UnixNano()), |
| 111 | + Namespace: "ns1", |
| 112 | + }, |
| 113 | + Regarding: v1.ObjectReference{ |
| 114 | + Kind: "Pod", |
| 115 | + Name: "pod1", |
| 116 | + Namespace: "ns1", |
| 117 | + UID: "12345", |
| 118 | + APIVersion: "v1", |
| 119 | + }, |
| 120 | + Related: &v1.ObjectReference{ |
| 121 | + Kind: "Node", |
| 122 | + Name: "node1", |
| 123 | + UID: "67890", |
| 124 | + APIVersion: "v1", |
| 125 | + }, |
| 126 | + Type: "Warning", |
| 127 | + Reason: "FailedScheduling", |
| 128 | + Action: "scheduling", |
| 129 | + Note: "Pod failed to schedule on node1: not enough resources", |
| 130 | + ReportingController: "c1", |
| 131 | + ReportingInstance: "i1", |
| 132 | + }, |
| 133 | + }, { |
| 134 | + desc: "event with invalid Event name", |
| 135 | + regarding: &networkingv1.IPAddress{ |
| 136 | + ObjectMeta: metav1.ObjectMeta{ |
| 137 | + Name: "2001:db8::123", |
| 138 | + UID: "12345", |
| 139 | + }, |
| 140 | + }, |
| 141 | + eventtype: "Warning", |
| 142 | + reason: "IPAddressNotAllocated", |
| 143 | + action: "IPAddressAllocation", |
| 144 | + note: "Service default/test appears to have leaked", |
| 145 | + |
| 146 | + expectedEvent: &eventsv1.Event{ |
| 147 | + ObjectMeta: metav1.ObjectMeta{ |
| 148 | + Namespace: "default", |
| 149 | + }, |
| 150 | + Regarding: v1.ObjectReference{ |
| 151 | + Kind: "IPAddress", |
| 152 | + Name: "2001:db8::123", |
| 153 | + UID: "12345", |
| 154 | + APIVersion: "networking.k8s.io/v1", |
| 155 | + }, |
| 156 | + Type: "Warning", |
| 157 | + Reason: "IPAddressNotAllocated", |
| 158 | + Action: "IPAddressAllocation", |
| 159 | + Note: "Service default/test appears to have leaked", |
| 160 | + ReportingController: "c1", |
| 161 | + ReportingInstance: "i1", |
| 162 | + }, |
| 163 | + }, { |
| 164 | + desc: "large event name", |
| 165 | + regarding: &v1.Pod{ |
| 166 | + ObjectMeta: metav1.ObjectMeta{ |
| 167 | + Name: strings.Repeat("x", utilvalidation.DNS1123SubdomainMaxLength*4), |
| 168 | + Namespace: "ns1", |
| 169 | + UID: "12345", |
| 170 | + }, |
| 171 | + }, |
| 172 | + eventtype: "Normal", |
| 173 | + reason: "Started", |
| 174 | + action: "starting", |
| 175 | + note: "Pod started", |
| 176 | + expectedEvent: &eventsv1.Event{ |
| 177 | + ObjectMeta: metav1.ObjectMeta{ |
| 178 | + Namespace: "ns1", |
| 179 | + }, |
| 180 | + Regarding: v1.ObjectReference{ |
| 181 | + Kind: "Pod", |
| 182 | + Name: strings.Repeat("x", utilvalidation.DNS1123SubdomainMaxLength*4), |
| 183 | + Namespace: "ns1", |
| 184 | + UID: "12345", |
| 185 | + APIVersion: "v1", |
| 186 | + }, |
| 187 | + Type: "Normal", |
| 188 | + Reason: "Started", |
| 189 | + Action: "starting", |
| 190 | + Note: "Pod started", |
| 191 | + ReportingController: "c1", |
| 192 | + ReportingInstance: "i1", |
| 193 | + }, |
| 194 | + }, |
| 195 | + } |
| 196 | + |
| 197 | + for _, tc := range testCases { |
| 198 | + t.Run(tc.desc, func(t *testing.T) { |
| 199 | + broadcaster := watch.NewBroadcaster(1, watch.WaitIfChannelFull) |
| 200 | + recorder := &recorderImpl{ |
| 201 | + scheme: runtime.NewScheme(), |
| 202 | + reportingController: "c1", |
| 203 | + reportingInstance: "i1", |
| 204 | + Broadcaster: broadcaster, |
| 205 | + clock: fakeClock, |
| 206 | + } |
| 207 | + |
| 208 | + if err := v1.AddToScheme(recorder.scheme); err != nil { |
| 209 | + t.Fatal(err) |
| 210 | + } |
| 211 | + if err := networkingv1.AddToScheme(recorder.scheme); err != nil { |
| 212 | + t.Fatal(err) |
| 213 | + } |
| 214 | + ch, err := broadcaster.Watch() |
| 215 | + if err != nil { |
| 216 | + t.Fatal(err) |
| 217 | + } |
| 218 | + recorder.Eventf(tc.regarding, tc.related, tc.eventtype, tc.reason, tc.action, tc.note, tc.args...) |
| 219 | + |
| 220 | + select { |
| 221 | + case event := <-ch.ResultChan(): |
| 222 | + actualEvent := event.Object.(*eventsv1.Event) |
| 223 | + if errs := apimachineryvalidation.NameIsDNSSubdomain(actualEvent.Name, false); len(errs) > 0 { |
| 224 | + t.Errorf("Event Name = %s; not a valid name: %v", actualEvent.Name, errs) |
| 225 | + } // Overwrite fields that are not relevant for comparison |
| 226 | + tc.expectedEvent.EventTime = actualEvent.EventTime |
| 227 | + // invalid event names generate random names |
| 228 | + if tc.expectedEvent.Name == "" { |
| 229 | + actualEvent.Name = "" |
| 230 | + } |
| 231 | + if diff := cmp.Diff(tc.expectedEvent, actualEvent); diff != "" { |
| 232 | + t.Errorf("Unexpected event diff (-want, +got):\n%s", diff) |
| 233 | + } |
| 234 | + case <-time.After(time.Second): |
| 235 | + t.Errorf("Timeout waiting for event") |
| 236 | + } |
| 237 | + |
| 238 | + }) |
| 239 | + } |
| 240 | +} |
0 commit comments