check that the recorded event is not nil on refreshExistingEventSeries

Signed-off-by: Yassine TIJANI <ytijani@vmware.com>

Kubernetes-commit: 000c2c557f862270ec83341c1af3fcd7c6f7d4a2
This commit is contained in:
Yassine TIJANI 2019-08-22 11:56:35 +01:00 committed by Kubernetes Publisher
parent 9fc8bb16d6
commit 72094bc6d5
2 changed files with 65 additions and 41 deletions

View File

@ -111,10 +111,12 @@ func (e *eventBroadcasterImpl) refreshExistingEventSeries() {
for isomorphicKey, event := range e.eventCache { for isomorphicKey, event := range e.eventCache {
if event.Series != nil { if event.Series != nil {
if recordedEvent, retry := recordEvent(e.sink, event); !retry { if recordedEvent, retry := recordEvent(e.sink, event); !retry {
if recordedEvent != nil {
e.eventCache[isomorphicKey] = recordedEvent e.eventCache[isomorphicKey] = recordedEvent
} }
} }
} }
}
} }
// finishSeries checks if a series has ended and either: // finishSeries checks if a series has ended and either:

View File

@ -30,6 +30,7 @@ import (
k8sruntime "k8s.io/apimachinery/pkg/runtime" k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
ref "k8s.io/client-go/tools/reference" ref "k8s.io/client-go/tools/reference"
) )
@ -299,10 +300,30 @@ func TestRefreshExistingEventSeries(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
LastObservedTime := metav1.MicroTime{Time: time.Now().Add(-9 * time.Minute)} LastObservedTime := metav1.MicroTime{Time: time.Now().Add(-9 * time.Minute)}
createEvent := make(chan *v1beta1.Event, 10) createEvent := make(chan *v1beta1.Event, 10)
updateEvent := make(chan *v1beta1.Event, 10) updateEvent := make(chan *v1beta1.Event, 10)
patchEvent := make(chan *v1beta1.Event, 10) patchEvent := make(chan *v1beta1.Event, 10)
table := []struct {
patchFunc func(event *v1beta1.Event, patch []byte) (*v1beta1.Event, error)
}{
{
patchFunc: func(event *v1beta1.Event, patch []byte) (*v1beta1.Event, error) {
// event we receive is already patched, usually the sink uses it
//only to retrieve the name and namespace, here we'll use it directly.
patchEvent <- event
return event, nil
},
},
{
patchFunc: func(event *v1beta1.Event, patch []byte) (*v1beta1.Event, error) {
// we simulate an apiserver error here
patchEvent <- nil
return nil, &restclient.RequestConstructionError{}
},
},
}
for _, item := range table {
testEvents := testEventSeriesSink{ testEvents := testEventSeriesSink{
OnCreate: func(event *v1beta1.Event) (*v1beta1.Event, error) { OnCreate: func(event *v1beta1.Event) (*v1beta1.Event, error) {
createEvent <- event createEvent <- event
@ -312,12 +333,7 @@ func TestRefreshExistingEventSeries(t *testing.T) {
updateEvent <- event updateEvent <- event
return event, nil return event, nil
}, },
OnPatch: func(event *v1beta1.Event, patch []byte) (*v1beta1.Event, error) { OnPatch: item.patchFunc,
// event we receive is already patched, usually the sink uses it
//only to retrieve the name and namespace, here we'll use it directly.
patchEvent <- event
return event, nil
},
} }
cache := map[eventKey]*v1beta1.Event{} cache := map[eventKey]*v1beta1.Event{}
eventBroadcaster := newBroadcaster(&testEvents, 0, cache).(*eventBroadcasterImpl) eventBroadcaster := newBroadcaster(&testEvents, 0, cache).(*eventBroadcasterImpl)
@ -343,7 +359,13 @@ func TestRefreshExistingEventSeries(t *testing.T) {
if len(patchEvent) != 0 || len(createEvent) != 0 || len(updateEvent) != 0 { if len(patchEvent) != 0 || len(createEvent) != 0 || len(updateEvent) != 0 {
t.Errorf("exactly one event should be emitted, but got %v", len(patchEvent)) t.Errorf("exactly one event should be emitted, but got %v", len(patchEvent))
} }
cacheEvent, exists := cache[cacheKey]
if cacheEvent == nil || !exists {
t.Errorf("expected event to exist and not being nil, but instead event: %v and exists: %v", cacheEvent, exists)
}
case <-time.After(wait.ForeverTestTimeout): case <-time.After(wait.ForeverTestTimeout):
t.Fatalf("timeout after %v", wait.ForeverTestTimeout) t.Fatalf("timeout after %v", wait.ForeverTestTimeout)
} }
}
} }