client-go/cache/testing: add ability to simulate watch disruption

This adds ResetWatch() to the FakeControllerSource, which lets the
controller simulate a re-list-and-watch.

Kubernetes-commit: 5aacacbdf000cee2d0ec548ee4afe564f35c60bf
This commit is contained in:
Casey Callendrello
2019-12-06 22:19:42 +01:00
committed by Kubernetes Publisher
parent dbf12d30ba
commit 802190f49e
2 changed files with 82 additions and 13 deletions

View File

@@ -20,7 +20,7 @@ import (
"sync"
"testing"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)
@@ -93,3 +93,44 @@ func TestRCNumber(t *testing.T) {
source.Shutdown()
wg.Wait()
}
// TestResetWatch validates that the FakeController correctly mocks a watch
// falling behind and ResourceVersions aging out.
func TestResetWatch(t *testing.T) {
pod := func(name string) *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
wg := &sync.WaitGroup{}
wg.Add(1)
source := NewFakeControllerSource()
source.Add(pod("foo")) // RV = 1
source.Modify(pod("foo")) // RV = 2
source.Modify(pod("foo")) // RV = 3
// Kill watch, delete change history
source.ResetWatch()
// This should fail, RV=1 was lost with ResetWatch
_, err := source.Watch(metav1.ListOptions{ResourceVersion: "1"})
if err == nil {
t.Fatalf("Unexpected non-error")
}
// This should succeed, RV=3 is current
w, err := source.Watch(metav1.ListOptions{ResourceVersion: "3"})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Modify again, ensure the watch is still working
source.Modify(pod("foo"))
go consume(t, w, []string{"4"}, wg)
source.Shutdown()
wg.Wait()
}