mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 07:27:21 +00:00
Fix cache to use the "List then Watch" pattern.
This commit is contained in:
147
pkg/client/cache/reflector_test.go
vendored
147
pkg/client/cache/reflector_test.go
vendored
@@ -17,15 +17,27 @@ limitations under the License.
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
type testLW struct {
|
||||
ListFunc func() (runtime.Object, error)
|
||||
WatchFunc func(resourceVersion uint64) (watch.Interface, error)
|
||||
}
|
||||
|
||||
func (t *testLW) List() (runtime.Object, error) { return t.ListFunc() }
|
||||
func (t *testLW) Watch(resourceVersion uint64) (watch.Interface, error) {
|
||||
return t.WatchFunc(resourceVersion)
|
||||
}
|
||||
|
||||
func TestReflector_watchHandler(t *testing.T) {
|
||||
s := NewStore()
|
||||
g := NewReflector(nil, &api.Pod{}, s)
|
||||
g := NewReflector(&testLW{}, &api.Pod{}, s)
|
||||
fw := watch.NewFake()
|
||||
s.Add("foo", &api.Pod{JSONBase: api.JSONBase{ID: "foo"}})
|
||||
s.Add("bar", &api.Pod{JSONBase: api.JSONBase{ID: "bar"}})
|
||||
@@ -68,27 +80,32 @@ func TestReflector_watchHandler(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflector_Run(t *testing.T) {
|
||||
func TestReflector_listAndWatch(t *testing.T) {
|
||||
createdFakes := make(chan *watch.FakeWatcher)
|
||||
|
||||
// Expect our starter to get called at the beginning of the watch with 0, and again with 3 when we
|
||||
// inject an error at 2.
|
||||
expectedRVs := []uint64{0, 3}
|
||||
watchStarter := func(rv uint64) (watch.Interface, error) {
|
||||
fw := watch.NewFake()
|
||||
if e, a := expectedRVs[0], rv; e != a {
|
||||
t.Errorf("Expected rv %v, but got %v", e, a)
|
||||
}
|
||||
expectedRVs = expectedRVs[1:]
|
||||
// channel is not buffered because the for loop below needs to block. But
|
||||
// we don't want to block here, so report the new fake via a go routine.
|
||||
go func() { createdFakes <- fw }()
|
||||
return fw, nil
|
||||
// The ListFunc says that it's at revision 1. Therefore, we expect our WatchFunc
|
||||
// to get called at the beginning of the watch with 1, and again with 4 when we
|
||||
// inject an error at 3.
|
||||
expectedRVs := []uint64{1, 4}
|
||||
lw := &testLW{
|
||||
WatchFunc: func(rv uint64) (watch.Interface, error) {
|
||||
fw := watch.NewFake()
|
||||
if e, a := expectedRVs[0], rv; e != a {
|
||||
t.Errorf("Expected rv %v, but got %v", e, a)
|
||||
}
|
||||
expectedRVs = expectedRVs[1:]
|
||||
// channel is not buffered because the for loop below needs to block. But
|
||||
// we don't want to block here, so report the new fake via a go routine.
|
||||
go func() { createdFakes <- fw }()
|
||||
return fw, nil
|
||||
},
|
||||
ListFunc: func() (runtime.Object, error) {
|
||||
return &api.PodList{JSONBase: api.JSONBase{ResourceVersion: 1}}, nil
|
||||
},
|
||||
}
|
||||
s := NewFIFO()
|
||||
r := NewReflector(watchStarter, &api.Pod{}, s)
|
||||
r.period = 0
|
||||
r.Run()
|
||||
r := NewReflector(lw, &api.Pod{}, s)
|
||||
go r.listAndWatch()
|
||||
|
||||
ids := []string{"foo", "bar", "baz", "qux", "zoo"}
|
||||
var fw *watch.FakeWatcher
|
||||
@@ -96,9 +113,9 @@ func TestReflector_Run(t *testing.T) {
|
||||
if fw == nil {
|
||||
fw = <-createdFakes
|
||||
}
|
||||
sendingRV := uint64(i + 1)
|
||||
sendingRV := uint64(i + 2)
|
||||
fw.Add(&api.Pod{JSONBase: api.JSONBase{ID: id, ResourceVersion: sendingRV}})
|
||||
if sendingRV == 2 {
|
||||
if sendingRV == 3 {
|
||||
// Inject a failure.
|
||||
fw.Stop()
|
||||
fw = nil
|
||||
@@ -111,7 +128,7 @@ func TestReflector_Run(t *testing.T) {
|
||||
if e, a := id, pod.ID; e != a {
|
||||
t.Errorf("%v: Expected %v, got %v", i, e, a)
|
||||
}
|
||||
if e, a := uint64(i+1), pod.ResourceVersion; e != a {
|
||||
if e, a := uint64(i+2), pod.ResourceVersion; e != a {
|
||||
t.Errorf("%v: Expected %v, got %v", i, e, a)
|
||||
}
|
||||
}
|
||||
@@ -120,3 +137,91 @@ func TestReflector_Run(t *testing.T) {
|
||||
t.Error("called watchStarter an unexpected number of times")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflector_listAndWatchWithErrors(t *testing.T) {
|
||||
mkPod := func(id string, rv uint64) *api.Pod {
|
||||
return &api.Pod{JSONBase: api.JSONBase{ID: id, ResourceVersion: rv}}
|
||||
}
|
||||
mkList := func(rv uint64, pods ...*api.Pod) *api.PodList {
|
||||
list := &api.PodList{JSONBase: api.JSONBase{ResourceVersion: rv}}
|
||||
for _, pod := range pods {
|
||||
list.Items = append(list.Items, *pod)
|
||||
}
|
||||
return list
|
||||
}
|
||||
table := []struct {
|
||||
list *api.PodList
|
||||
listErr error
|
||||
events []watch.Event
|
||||
watchErr error
|
||||
}{
|
||||
{
|
||||
list: mkList(1),
|
||||
events: []watch.Event{
|
||||
{watch.Added, mkPod("foo", 2)},
|
||||
{watch.Added, mkPod("bar", 3)},
|
||||
},
|
||||
}, {
|
||||
list: mkList(3, mkPod("foo", 2), mkPod("bar", 3)),
|
||||
events: []watch.Event{
|
||||
{watch.Deleted, mkPod("foo", 4)},
|
||||
{watch.Added, mkPod("qux", 5)},
|
||||
},
|
||||
}, {
|
||||
listErr: fmt.Errorf("a list error"),
|
||||
}, {
|
||||
list: mkList(5, mkPod("bar", 3), mkPod("qux", 5)),
|
||||
watchErr: fmt.Errorf("a watch error"),
|
||||
}, {
|
||||
list: mkList(5, mkPod("bar", 3), mkPod("qux", 5)),
|
||||
events: []watch.Event{
|
||||
{watch.Added, mkPod("baz", 6)},
|
||||
},
|
||||
}, {
|
||||
list: mkList(6, mkPod("bar", 3), mkPod("qux", 5), mkPod("baz", 6)),
|
||||
},
|
||||
}
|
||||
|
||||
s := NewFIFO()
|
||||
for line, item := range table {
|
||||
if item.list != nil {
|
||||
// Test that the list is what currently exists in the store.
|
||||
current := s.List()
|
||||
checkMap := map[string]uint64{}
|
||||
for _, item := range current {
|
||||
pod := item.(*api.Pod)
|
||||
checkMap[pod.ID] = pod.ResourceVersion
|
||||
}
|
||||
for _, pod := range item.list.Items {
|
||||
if e, a := pod.ResourceVersion, checkMap[pod.ID]; e != a {
|
||||
t.Errorf("%v: expected %v, got %v for pod %v", line, e, a, pod.ID)
|
||||
}
|
||||
}
|
||||
if e, a := len(item.list.Items), len(checkMap); e != a {
|
||||
t.Errorf("%v: expected %v, got %v", line, e, a)
|
||||
}
|
||||
}
|
||||
watchRet, watchErr := item.events, item.watchErr
|
||||
lw := &testLW{
|
||||
WatchFunc: func(rv uint64) (watch.Interface, error) {
|
||||
if watchErr != nil {
|
||||
return nil, watchErr
|
||||
}
|
||||
watchErr = fmt.Errorf("second watch")
|
||||
fw := watch.NewFake()
|
||||
go func() {
|
||||
for _, e := range watchRet {
|
||||
fw.Action(e.Type, e.Object)
|
||||
}
|
||||
fw.Stop()
|
||||
}()
|
||||
return fw, nil
|
||||
},
|
||||
ListFunc: func() (runtime.Object, error) {
|
||||
return item.list, item.listErr
|
||||
},
|
||||
}
|
||||
r := NewReflector(lw, &api.Pod{}, s)
|
||||
r.listAndWatch()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user