mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
finish testing client/cache
This commit is contained in:
parent
03fe91cc4a
commit
041d56f3d0
@ -123,20 +123,16 @@ func fieldPtr(v reflect.Value, fieldName string, dest interface{}) error {
|
|||||||
// Returns an error if this isn't the case.
|
// Returns an error if this isn't the case.
|
||||||
func newGenericJSONBase(v reflect.Value) (genericJSONBase, error) {
|
func newGenericJSONBase(v reflect.Value) (genericJSONBase, error) {
|
||||||
g := genericJSONBase{}
|
g := genericJSONBase{}
|
||||||
err := fieldPtr(v, "ID", &g.id)
|
if err := fieldPtr(v, "ID", &g.id); err != nil {
|
||||||
if err != nil {
|
|
||||||
return g, err
|
return g, err
|
||||||
}
|
}
|
||||||
err = fieldPtr(v, "APIVersion", &g.apiVersion)
|
if err := fieldPtr(v, "APIVersion", &g.apiVersion); err != nil {
|
||||||
if err != nil {
|
|
||||||
return g, err
|
return g, err
|
||||||
}
|
}
|
||||||
err = fieldPtr(v, "Kind", &g.kind)
|
if err := fieldPtr(v, "Kind", &g.kind); err != nil {
|
||||||
if err != nil {
|
|
||||||
return g, err
|
return g, err
|
||||||
}
|
}
|
||||||
err = fieldPtr(v, "ResourceVersion", &g.resourceVersion)
|
if err := fieldPtr(v, "ResourceVersion", &g.resourceVersion); err != nil {
|
||||||
if err != nil {
|
|
||||||
return g, err
|
return g, err
|
||||||
}
|
}
|
||||||
return g, nil
|
return g, nil
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
func TestGenericJSONBase(t *testing.T) {
|
func TestGenericJSONBase(t *testing.T) {
|
||||||
j := JSONBase{
|
j := JSONBase{
|
||||||
|
ID: "foo",
|
||||||
APIVersion: "a",
|
APIVersion: "a",
|
||||||
Kind: "b",
|
Kind: "b",
|
||||||
ResourceVersion: 1,
|
ResourceVersion: 1,
|
||||||
@ -31,8 +32,11 @@ func TestGenericJSONBase(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("new err: %v", err)
|
t.Fatalf("new err: %v", err)
|
||||||
}
|
}
|
||||||
// Proove g supports JSONBaseInterface.
|
// Prove g supports JSONBaseInterface.
|
||||||
jbi := JSONBaseInterface(g)
|
jbi := JSONBaseInterface(g)
|
||||||
|
if e, a := "foo", jbi.ID(); e != a {
|
||||||
|
t.Errorf("expected %v, got %v", e, a)
|
||||||
|
}
|
||||||
if e, a := "a", jbi.APIVersion(); e != a {
|
if e, a := "a", jbi.APIVersion(); e != a {
|
||||||
t.Errorf("expected %v, got %v", e, a)
|
t.Errorf("expected %v, got %v", e, a)
|
||||||
}
|
}
|
||||||
@ -43,10 +47,15 @@ func TestGenericJSONBase(t *testing.T) {
|
|||||||
t.Errorf("expected %v, got %v", e, a)
|
t.Errorf("expected %v, got %v", e, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jbi.SetID("bar")
|
||||||
jbi.SetAPIVersion("c")
|
jbi.SetAPIVersion("c")
|
||||||
jbi.SetKind("d")
|
jbi.SetKind("d")
|
||||||
jbi.SetResourceVersion(2)
|
jbi.SetResourceVersion(2)
|
||||||
|
|
||||||
|
// Prove that jbi changes the original object.
|
||||||
|
if e, a := "bar", j.ID; e != a {
|
||||||
|
t.Errorf("expected %v, got %v", e, a)
|
||||||
|
}
|
||||||
if e, a := "c", j.APIVersion; e != a {
|
if e, a := "c", j.APIVersion; e != a {
|
||||||
t.Errorf("expected %v, got %v", e, a)
|
t.Errorf("expected %v, got %v", e, a)
|
||||||
}
|
}
|
||||||
|
2
pkg/client/cache/doc.go
vendored
2
pkg/client/cache/doc.go
vendored
@ -16,7 +16,7 @@ limitations under the License.
|
|||||||
|
|
||||||
// Package cache is a client-side caching mechanism. It is useful for
|
// Package cache is a client-side caching mechanism. It is useful for
|
||||||
// reducing the number of server calls you'd otherwise need to make.
|
// reducing the number of server calls you'd otherwise need to make.
|
||||||
// Getter watches a server and updates a Store. Two stores are provided;
|
// Reflector watches a server and updates a Store. Two stores are provided;
|
||||||
// one that simply caches objects (for example, to allow a scheduler to
|
// one that simply caches objects (for example, to allow a scheduler to
|
||||||
// list currently available minions), and one that additionally acts as
|
// list currently available minions), and one that additionally acts as
|
||||||
// a FIFO queue (for example, to allow a scheduler to process incoming
|
// a FIFO queue (for example, to allow a scheduler to process incoming
|
||||||
|
10
pkg/client/cache/fifo.go
vendored
10
pkg/client/cache/fifo.go
vendored
@ -20,6 +20,11 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FIFO recieves adds and updates from a Reflector, and puts them in a queue for
|
||||||
|
// FIFO order processing. If multiple adds/updates of a single item happen while
|
||||||
|
// an item is in the queue before it has been processed, it will only be
|
||||||
|
// processed once, and when it is processed, the most recent version will be
|
||||||
|
// processed. This can't be done with a channel.
|
||||||
type FIFO struct {
|
type FIFO struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
cond sync.Cond
|
cond sync.Cond
|
||||||
@ -91,13 +96,14 @@ func (f *FIFO) Pop() interface{} {
|
|||||||
// Item may have been deleted subsequently.
|
// Item may have been deleted subsequently.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
delete(f.items, id)
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFIFOStore returns a Store which can be used to queue up items to
|
// NewFIFO returns a Store which can be used to queue up items to
|
||||||
// process.
|
// process.
|
||||||
func NewFIFOStore() *FIFO {
|
func NewFIFO() *FIFO {
|
||||||
f := &FIFO{
|
f := &FIFO{
|
||||||
items: map[string]interface{}{},
|
items: map[string]interface{}{},
|
||||||
queue: []string{},
|
queue: []string{},
|
||||||
|
83
pkg/client/cache/fifo_test.go
vendored
Normal file
83
pkg/client/cache/fifo_test.go
vendored
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFIFO_basic(t *testing.T) {
|
||||||
|
f := NewFIFO()
|
||||||
|
const amount = 500
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < amount; i++ {
|
||||||
|
f.Add(string([]rune{'a', rune(i)}), i+1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
for u := uint(0); u < amount; u++ {
|
||||||
|
f.Add(string([]rune{'b', rune(u)}), u+1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
lastInt := int(0)
|
||||||
|
lastUint := uint(0)
|
||||||
|
for i := 0; i < amount*2; i++ {
|
||||||
|
switch obj := f.Pop().(type) {
|
||||||
|
case int:
|
||||||
|
if obj <= lastInt {
|
||||||
|
t.Errorf("got %v (int) out of order, last was %v", obj, lastInt)
|
||||||
|
}
|
||||||
|
lastInt = obj
|
||||||
|
case uint:
|
||||||
|
if obj <= lastUint {
|
||||||
|
t.Errorf("got %v (uint) out of order, last was %v", obj, lastUint)
|
||||||
|
} else {
|
||||||
|
lastUint = obj
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected type %#v", obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFIFO_addUpdate(t *testing.T) {
|
||||||
|
f := NewFIFO()
|
||||||
|
f.Add("foo", 10)
|
||||||
|
f.Update("foo", 15)
|
||||||
|
got := make(chan int, 2)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
got <- f.Pop().(int)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
first := <-got
|
||||||
|
if e, a := 15, first; e != a {
|
||||||
|
t.Errorf("Didn't get updated value (%v), got %v", e, a)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case unexpected := <-got:
|
||||||
|
t.Errorf("Got second value %v", unexpected)
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
_, exists := f.Get("foo")
|
||||||
|
if exists {
|
||||||
|
t.Errorf("item did not get removed")
|
||||||
|
}
|
||||||
|
}
|
@ -27,9 +27,9 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Store is a generic object storage interface. Getter knows how to watch a server
|
// Store is a generic object storage interface. Reflector knows how to watch a server
|
||||||
// and update a store. A generic store is provided, which allows Getter to be used
|
// and update a store. A generic store is provided, which allows Reflector to be used
|
||||||
// as a local caching system, and an LRU store, which allows Getter to work like a
|
// as a local caching system, and an LRU store, which allows Reflector to work like a
|
||||||
// queue of items yet to be processed.
|
// queue of items yet to be processed.
|
||||||
type Store interface {
|
type Store interface {
|
||||||
Add(ID string, obj interface{})
|
Add(ID string, obj interface{})
|
||||||
@ -39,20 +39,20 @@ type Store interface {
|
|||||||
Get(ID string) (item interface{}, exists bool)
|
Get(ID string) (item interface{}, exists bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter watches a specified resource and causes all changes to be reflected in the given store.
|
// Reflector watches a specified resource and causes all changes to be reflected in the given store.
|
||||||
type Getter struct {
|
type Reflector struct {
|
||||||
kubeClient *client.Client
|
kubeClient *client.Client
|
||||||
resource string
|
resource string
|
||||||
expectedType reflect.Type
|
expectedType reflect.Type
|
||||||
store Store
|
store Store
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGetter makes a new Getter object which will keep the given store up to
|
// NewReflector makes a new Reflector object which will keep the given store up to
|
||||||
// date with the server's contents for the given resource. Getter promises to
|
// date with the server's contents for the given resource. Reflector promises to
|
||||||
// only put things in the store that have the type of expectedType.
|
// only put things in the store that have the type of expectedType.
|
||||||
// TODO: define a query so you only locally cache a subset of items.
|
// TODO: define a query so you only locally cache a subset of items.
|
||||||
func NewGetter(resource string, kubeClient *client.Client, expectedType interface{}, store Store) *Getter {
|
func NewReflector(resource string, kubeClient *client.Client, expectedType interface{}, store Store) *Reflector {
|
||||||
gc := &Getter{
|
gc := &Reflector{
|
||||||
resource: resource,
|
resource: resource,
|
||||||
kubeClient: kubeClient,
|
kubeClient: kubeClient,
|
||||||
store: store,
|
store: store,
|
||||||
@ -61,20 +61,22 @@ func NewGetter(resource string, kubeClient *client.Client, expectedType interfac
|
|||||||
return gc
|
return gc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *Getter) Run() {
|
func (gc *Reflector) Run() {
|
||||||
go util.Forever(gc.watch, 5*time.Second)
|
go util.Forever(func() {
|
||||||
|
w, err := gc.startWatch()
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("failed to watch %v: %v", gc.resource, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gc.watchHandler(w)
|
||||||
|
}, 5*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *Getter) watch() {
|
func (gc *Reflector) startWatch() (watch.Interface, error) {
|
||||||
w, err := gc.kubeClient.Get().Path(gc.resource).Watch()
|
return gc.kubeClient.Get().Path(gc.resource).Path("watch").Watch()
|
||||||
if err != nil {
|
|
||||||
glog.Errorf("failed to watch %v: %v", gc.resource, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
gc.watchHandler(w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *Getter) watchHandler(w watch.Interface) {
|
func (gc *Reflector) watchHandler(w watch.Interface) {
|
||||||
for {
|
for {
|
||||||
event, ok := <-w.ResultChan()
|
event, ok := <-w.ResultChan()
|
||||||
if !ok {
|
if !ok {
|
94
pkg/client/cache/reflector_test.go
vendored
Normal file
94
pkg/client/cache/reflector_test.go
vendored
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReflector_watchHandler(t *testing.T) {
|
||||||
|
s := NewStore()
|
||||||
|
g := NewReflector("foo", nil, &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"}})
|
||||||
|
go func() {
|
||||||
|
fw.Modify(&api.Pod{JSONBase: api.JSONBase{ID: "bar", ResourceVersion: 55}})
|
||||||
|
fw.Add(&api.Pod{JSONBase: api.JSONBase{ID: "baz"}})
|
||||||
|
fw.Add(&api.Service{JSONBase: api.JSONBase{ID: "rejected"}})
|
||||||
|
fw.Delete(&api.Pod{JSONBase: api.JSONBase{ID: "foo"}})
|
||||||
|
fw.Stop()
|
||||||
|
}()
|
||||||
|
g.watchHandler(fw)
|
||||||
|
|
||||||
|
table := []struct {
|
||||||
|
ID string
|
||||||
|
RV uint64
|
||||||
|
exists bool
|
||||||
|
}{
|
||||||
|
{"foo", 0, false},
|
||||||
|
{"rejected", 0, false},
|
||||||
|
{"bar", 55, true},
|
||||||
|
{"baz", 0, true},
|
||||||
|
}
|
||||||
|
for _, item := range table {
|
||||||
|
obj, exists := s.Get(item.ID)
|
||||||
|
if e, a := item.exists, exists; e != a {
|
||||||
|
t.Errorf("%v: expected %v, got %v", item.ID, e, a)
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if e, a := item.RV, obj.(*api.Pod).ResourceVersion; e != a {
|
||||||
|
t.Errorf("%v: expected %v, got %v", item.ID, e, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReflector_startWatch(t *testing.T) {
|
||||||
|
table := []struct{ resource, path string }{
|
||||||
|
{"pods", "/api/v1beta1/pods/watch"},
|
||||||
|
{"services", "/api/v1beta1/services/watch"},
|
||||||
|
}
|
||||||
|
for _, testItem := range table {
|
||||||
|
got := make(chan struct{})
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
if req.URL.Path == testItem.path {
|
||||||
|
close(got)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Errorf("unexpected path %v", req.URL.Path)
|
||||||
|
}))
|
||||||
|
s := NewStore()
|
||||||
|
c := client.New(srv.URL, nil)
|
||||||
|
g := NewReflector(testItem.resource, c, &api.Pod{}, s)
|
||||||
|
_, err := g.startWatch()
|
||||||
|
// We're just checking that it watches the right path.
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("unexpected non-error")
|
||||||
|
}
|
||||||
|
<-got
|
||||||
|
}
|
||||||
|
}
|
@ -47,6 +47,7 @@ func (c *cache) Delete(ID string, obj interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List returns a list of all the items.
|
// List returns a list of all the items.
|
||||||
|
// List is completely threadsafe as long as you treat all items as immutable.
|
||||||
func (c *cache) List() []interface{} {
|
func (c *cache) List() []interface{} {
|
||||||
c.lock.RLock()
|
c.lock.RLock()
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.RUnlock()
|
||||||
@ -58,6 +59,7 @@ func (c *cache) List() []interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the requested item, or sets exists=false.
|
// Get returns the requested item, or sets exists=false.
|
||||||
|
// Get is completely threadsafe as long as you treat all items as immutable.
|
||||||
func (c *cache) Get(ID string) (item interface{}, exists bool) {
|
func (c *cache) Get(ID string) (item interface{}, exists bool) {
|
||||||
c.lock.RLock()
|
c.lock.RLock()
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.RUnlock()
|
4
pkg/client/cache/store_test.go
vendored
4
pkg/client/cache/store_test.go
vendored
@ -62,3 +62,7 @@ func doTestStore(t *testing.T, store Store) {
|
|||||||
func TestCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
doTestStore(t, NewStore())
|
doTestStore(t, NewStore())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFIFOCache(t *testing.T) {
|
||||||
|
doTestStore(t, NewFIFO())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user