1 /* 2 Copyright 2019 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 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/klog/v2" 24 ) 25 26 // FakeRecorder is used as a fake during tests. It is thread safe. It is usable 27 // when created manually and not by NewFakeRecorder, however all events may be 28 // thrown away in this case. 29 type FakeRecorder struct { 30 Events chan string 31 } 32 33 var _ EventRecorderLogger = &FakeRecorder{} 34 35 // Eventf emits an event 36 func (f *FakeRecorder) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) { 37 if f.Events != nil { 38 f.Events <- fmt.Sprintf(eventtype+" "+reason+" "+note, args...) 39 } 40 } 41 42 func (f *FakeRecorder) WithLogger(logger klog.Logger) EventRecorderLogger { 43 return f 44 } 45 46 // NewFakeRecorder creates new fake event recorder with event channel with 47 // buffer of given size. 48 func NewFakeRecorder(bufferSize int) *FakeRecorder { 49 return &FakeRecorder{ 50 Events: make(chan string, bufferSize), 51 } 52 } 53