Add context object in test cases flows

This commit is contained in:
derekwaynecarr 2014-09-25 14:35:10 -04:00
parent 3e685674e7
commit be85ad7a3d
9 changed files with 108 additions and 65 deletions

View File

@ -30,6 +30,7 @@ import (
"testing" "testing"
"time" "time"
"code.google.com/p/go.net/context"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
apierrs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" apierrs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
@ -88,18 +89,18 @@ type SimpleRESTStorage struct {
injectedFunction func(obj runtime.Object) (returnObj runtime.Object, err error) injectedFunction func(obj runtime.Object) (returnObj runtime.Object, err error)
} }
func (storage *SimpleRESTStorage) List(label, field labels.Selector) (runtime.Object, error) { func (storage *SimpleRESTStorage) List(ctx context.Context, label, field labels.Selector) (runtime.Object, error) {
result := &SimpleList{ result := &SimpleList{
Items: storage.list, Items: storage.list,
} }
return result, storage.errors["list"] return result, storage.errors["list"]
} }
func (storage *SimpleRESTStorage) Get(id string) (runtime.Object, error) { func (storage *SimpleRESTStorage) Get(ctx context.Context, id string) (runtime.Object, error) {
return api.Scheme.CopyOrDie(&storage.item), storage.errors["get"] return api.Scheme.CopyOrDie(&storage.item), storage.errors["get"]
} }
func (storage *SimpleRESTStorage) Delete(id string) (<-chan runtime.Object, error) { func (storage *SimpleRESTStorage) Delete(ctx context.Context, id string) (<-chan runtime.Object, error) {
storage.deleted = id storage.deleted = id
if err := storage.errors["delete"]; err != nil { if err := storage.errors["delete"]; err != nil {
return nil, err return nil, err
@ -116,7 +117,7 @@ func (storage *SimpleRESTStorage) New() runtime.Object {
return &Simple{} return &Simple{}
} }
func (storage *SimpleRESTStorage) Create(obj runtime.Object) (<-chan runtime.Object, error) { func (storage *SimpleRESTStorage) Create(ctx context.Context, obj runtime.Object) (<-chan runtime.Object, error) {
storage.created = obj.(*Simple) storage.created = obj.(*Simple)
if err := storage.errors["create"]; err != nil { if err := storage.errors["create"]; err != nil {
return nil, err return nil, err
@ -129,7 +130,7 @@ func (storage *SimpleRESTStorage) Create(obj runtime.Object) (<-chan runtime.Obj
}), nil }), nil
} }
func (storage *SimpleRESTStorage) Update(obj runtime.Object) (<-chan runtime.Object, error) { func (storage *SimpleRESTStorage) Update(ctx context.Context, obj runtime.Object) (<-chan runtime.Object, error) {
storage.updated = obj.(*Simple) storage.updated = obj.(*Simple)
if err := storage.errors["update"]; err != nil { if err := storage.errors["update"]; err != nil {
return nil, err return nil, err
@ -143,7 +144,7 @@ func (storage *SimpleRESTStorage) Update(obj runtime.Object) (<-chan runtime.Obj
} }
// Implement ResourceWatcher. // Implement ResourceWatcher.
func (storage *SimpleRESTStorage) Watch(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { func (storage *SimpleRESTStorage) Watch(ctx context.Context, label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) {
storage.requestedLabelSelector = label storage.requestedLabelSelector = label
storage.requestedFieldSelector = field storage.requestedFieldSelector = field
storage.requestedResourceVersion = resourceVersion storage.requestedResourceVersion = resourceVersion
@ -155,7 +156,7 @@ func (storage *SimpleRESTStorage) Watch(label, field labels.Selector, resourceVe
} }
// Implement Redirector. // Implement Redirector.
func (storage *SimpleRESTStorage) ResourceLocation(id string) (string, error) { func (storage *SimpleRESTStorage) ResourceLocation(ctx context.Context, id string) (string, error) {
storage.requestedResourceLocationID = id storage.requestedResourceLocationID = id
if err := storage.errors["resourceLocation"]; err != nil { if err := storage.errors["resourceLocation"]; err != nil {
return "", err return "", err

View File

@ -22,6 +22,8 @@ import (
"reflect" "reflect"
"testing" "testing"
"code.google.com/p/go.net/context"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
@ -52,24 +54,25 @@ func TestNewREST(t *testing.T) {
} }
func TestRESTUnsupported(t *testing.T) { func TestRESTUnsupported(t *testing.T) {
var ctx context.Context
mockRegistry := MockRegistry{ mockRegistry := MockRegistry{
OnApplyBinding: func(b *api.Binding) error { return nil }, OnApplyBinding: func(b *api.Binding) error { return nil },
} }
b := NewREST(mockRegistry) b := NewREST(mockRegistry)
if _, err := b.Delete("binding id"); err == nil { if _, err := b.Delete(ctx, "binding id"); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
if _, err := b.Update(&api.Binding{PodID: "foo", Host: "new machine"}); err == nil { if _, err := b.Update(ctx, &api.Binding{PodID: "foo", Host: "new machine"}); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
if _, err := b.Get("binding id"); err == nil { if _, err := b.Get(ctx, "binding id"); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
if _, err := b.List(labels.Set{"name": "foo"}.AsSelector(), labels.Everything()); err == nil { if _, err := b.List(ctx, labels.Set{"name": "foo"}.AsSelector(), labels.Everything()); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
// Try sending wrong object just to get 100% coverage // Try sending wrong object just to get 100% coverage
if _, err := b.Create(&api.Pod{}); err == nil { if _, err := b.Create(ctx, &api.Pod{}); err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
} }
@ -93,8 +96,9 @@ func TestRESTPost(t *testing.T) {
return item.err return item.err
}, },
} }
ctx := api.NewContext()
b := NewREST(mockRegistry) b := NewREST(mockRegistry)
resultChan, err := b.Create(item.b) resultChan, err := b.Create(ctx, item.b)
if err != nil { if err != nil {
t.Errorf("Unexpected error %v", err) t.Errorf("Unexpected error %v", err)
continue continue

View File

@ -24,6 +24,8 @@ import (
"testing" "testing"
"time" "time"
"code.google.com/p/go.net/context"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
@ -39,7 +41,8 @@ func TestListControllersError(t *testing.T) {
storage := REST{ storage := REST{
registry: &mockRegistry, registry: &mockRegistry,
} }
controllers, err := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
controllers, err := storage.List(ctx, labels.Everything(), labels.Everything())
if err != mockRegistry.Err { if err != mockRegistry.Err {
t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err) t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err)
} }
@ -53,7 +56,8 @@ func TestListEmptyControllerList(t *testing.T) {
storage := REST{ storage := REST{
registry: &mockRegistry, registry: &mockRegistry,
} }
controllers, err := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
controllers, err := storage.List(ctx, labels.Everything(), labels.Everything())
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -86,7 +90,8 @@ func TestListControllerList(t *testing.T) {
storage := REST{ storage := REST{
registry: &mockRegistry, registry: &mockRegistry,
} }
controllersObj, err := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
controllersObj, err := storage.List(ctx, labels.Everything(), labels.Everything())
controllers := controllersObj.(*api.ReplicationControllerList) controllers := controllersObj.(*api.ReplicationControllerList)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
@ -240,7 +245,8 @@ func TestCreateController(t *testing.T) {
PodTemplate: validPodTemplate, PodTemplate: validPodTemplate,
}, },
} }
channel, err := storage.Create(controller) ctx := api.NewContext()
channel, err := storage.Create(ctx, controller)
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }
@ -263,7 +269,6 @@ func TestControllerStorageValidatesCreate(t *testing.T) {
podLister: nil, podLister: nil,
pollPeriod: time.Millisecond * 1, pollPeriod: time.Millisecond * 1,
} }
failureCases := map[string]api.ReplicationController{ failureCases := map[string]api.ReplicationController{
"empty ID": { "empty ID": {
JSONBase: api.JSONBase{ID: ""}, JSONBase: api.JSONBase{ID: ""},
@ -277,7 +282,8 @@ func TestControllerStorageValidatesCreate(t *testing.T) {
}, },
} }
for _, failureCase := range failureCases { for _, failureCase := range failureCases {
c, err := storage.Create(&failureCase) ctx := api.NewContext()
c, err := storage.Create(ctx, &failureCase)
if c != nil { if c != nil {
t.Errorf("Expected nil channel") t.Errorf("Expected nil channel")
} }
@ -307,7 +313,8 @@ func TestControllerStorageValidatesUpdate(t *testing.T) {
}, },
} }
for _, failureCase := range failureCases { for _, failureCase := range failureCases {
c, err := storage.Update(&failureCase) ctx := api.NewContext()
c, err := storage.Update(ctx, &failureCase)
if c != nil { if c != nil {
t.Errorf("Expected nil channel") t.Errorf("Expected nil channel")
} }
@ -323,7 +330,7 @@ type fakePodLister struct {
s labels.Selector s labels.Selector
} }
func (f *fakePodLister) ListPods(s labels.Selector) (*api.PodList, error) { func (f *fakePodLister) ListPods(ctx context.Context, s labels.Selector) (*api.PodList, error) {
f.s = s f.s = s
return &f.l, f.e return &f.l, f.e
} }
@ -349,7 +356,8 @@ func TestFillCurrentState(t *testing.T) {
}, },
}, },
} }
storage.fillCurrentState(&controller) ctx := api.NewContext()
storage.fillCurrentState(ctx, &controller)
if controller.CurrentState.Replicas != 2 { if controller.CurrentState.Replicas != 2 {
t.Errorf("expected 2, got: %d", controller.CurrentState.Replicas) t.Errorf("expected 2, got: %d", controller.CurrentState.Replicas)
} }

View File

@ -34,7 +34,8 @@ func TestGetEndpoints(t *testing.T) {
}, },
} }
storage := NewREST(registry) storage := NewREST(registry)
obj, err := storage.Get("foo") ctx := api.NewContext()
obj, err := storage.Get(ctx, "foo")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %#v", err) t.Fatalf("unexpected error: %#v", err)
} }
@ -48,9 +49,9 @@ func TestGetEndpointsMissingService(t *testing.T) {
Err: errors.NewNotFound("service", "foo"), Err: errors.NewNotFound("service", "foo"),
} }
storage := NewREST(registry) storage := NewREST(registry)
ctx := api.NewContext()
// returns service not found // returns service not found
_, err := storage.Get("foo") _, err := storage.Get(ctx, "foo")
if !errors.IsNotFound(err) || !reflect.DeepEqual(err, errors.NewNotFound("service", "foo")) { if !errors.IsNotFound(err) || !reflect.DeepEqual(err, errors.NewNotFound("service", "foo")) {
t.Errorf("expected NotFound error, got %#v", err) t.Errorf("expected NotFound error, got %#v", err)
} }
@ -60,7 +61,7 @@ func TestGetEndpointsMissingService(t *testing.T) {
registry.Service = &api.Service{ registry.Service = &api.Service{
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
} }
obj, err := storage.Get("foo") obj, err := storage.Get(ctx, "foo")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -79,7 +80,8 @@ func TestEndpointsRegistryList(t *testing.T) {
{JSONBase: api.JSONBase{ID: "bar"}}, {JSONBase: api.JSONBase{ID: "bar"}},
}, },
} }
s, _ := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
s, _ := storage.List(ctx, labels.Everything(), labels.Everything())
sl := s.(*api.EndpointsList) sl := s.(*api.EndpointsList)
if len(sl.Items) != 2 { if len(sl.Items) != 2 {
t.Fatalf("Expected 2 endpoints, but got %v", len(sl.Items)) t.Fatalf("Expected 2 endpoints, but got %v", len(sl.Items))

View File

@ -410,7 +410,8 @@ func TestEtcdEmptyListPods(t *testing.T) {
E: nil, E: nil,
} }
registry := NewTestEtcdRegistry(fakeClient) registry := NewTestEtcdRegistry(fakeClient)
pods, err := registry.ListPods(labels.Everything()) ctx := api.NewContext()
pods, err := registry.ListPods(ctx, labels.Everything())
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -428,7 +429,8 @@ func TestEtcdListPodsNotFound(t *testing.T) {
E: tools.EtcdErrorNotFound, E: tools.EtcdErrorNotFound,
} }
registry := NewTestEtcdRegistry(fakeClient) registry := NewTestEtcdRegistry(fakeClient)
pods, err := registry.ListPods(labels.Everything()) ctx := api.NewContext()
pods, err := registry.ListPods(ctx, labels.Everything())
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -463,7 +465,8 @@ func TestEtcdListPods(t *testing.T) {
E: nil, E: nil,
} }
registry := NewTestEtcdRegistry(fakeClient) registry := NewTestEtcdRegistry(fakeClient)
pods, err := registry.ListPods(labels.Everything()) ctx := api.NewContext()
pods, err := registry.ListPods(ctx, labels.Everything())
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }

View File

@ -27,18 +27,18 @@ import (
func TestMinionREST(t *testing.T) { func TestMinionREST(t *testing.T) {
m := NewRegistry([]string{"foo", "bar"}) m := NewRegistry([]string{"foo", "bar"})
ms := NewREST(m) ms := NewREST(m)
ctx := api.NewContext()
if obj, err := ms.Get("foo"); err != nil || obj.(*api.Minion).ID != "foo" { if obj, err := ms.Get(ctx, "foo"); err != nil || obj.(*api.Minion).ID != "foo" {
t.Errorf("missing expected object") t.Errorf("missing expected object")
} }
if obj, err := ms.Get("bar"); err != nil || obj.(*api.Minion).ID != "bar" { if obj, err := ms.Get(ctx, "bar"); err != nil || obj.(*api.Minion).ID != "bar" {
t.Errorf("missing expected object") t.Errorf("missing expected object")
} }
if _, err := ms.Get("baz"); err != ErrDoesNotExist { if _, err := ms.Get(ctx, "baz"); err != ErrDoesNotExist {
t.Errorf("has unexpected object") t.Errorf("has unexpected object")
} }
c, err := ms.Create(&api.Minion{JSONBase: api.JSONBase{ID: "baz"}}) c, err := ms.Create(ctx, &api.Minion{JSONBase: api.JSONBase{ID: "baz"}})
if err != nil { if err != nil {
t.Errorf("insert failed") t.Errorf("insert failed")
} }
@ -46,11 +46,11 @@ func TestMinionREST(t *testing.T) {
if m, ok := obj.(*api.Minion); !ok || m.ID != "baz" { if m, ok := obj.(*api.Minion); !ok || m.ID != "baz" {
t.Errorf("insert return value was weird: %#v", obj) t.Errorf("insert return value was weird: %#v", obj)
} }
if obj, err := ms.Get("baz"); err != nil || obj.(*api.Minion).ID != "baz" { if obj, err := ms.Get(ctx, "baz"); err != nil || obj.(*api.Minion).ID != "baz" {
t.Errorf("insert didn't actually insert") t.Errorf("insert didn't actually insert")
} }
c, err = ms.Delete("bar") c, err = ms.Delete(ctx, "bar")
if err != nil { if err != nil {
t.Errorf("delete failed") t.Errorf("delete failed")
} }
@ -58,16 +58,16 @@ func TestMinionREST(t *testing.T) {
if s, ok := obj.(*api.Status); !ok || s.Status != api.StatusSuccess { if s, ok := obj.(*api.Status); !ok || s.Status != api.StatusSuccess {
t.Errorf("delete return value was weird: %#v", obj) t.Errorf("delete return value was weird: %#v", obj)
} }
if _, err := ms.Get("bar"); err != ErrDoesNotExist { if _, err := ms.Get(ctx, "bar"); err != ErrDoesNotExist {
t.Errorf("delete didn't actually delete") t.Errorf("delete didn't actually delete")
} }
_, err = ms.Delete("bar") _, err = ms.Delete(ctx, "bar")
if err != ErrDoesNotExist { if err != ErrDoesNotExist {
t.Errorf("delete returned wrong error") t.Errorf("delete returned wrong error")
} }
list, err := ms.List(labels.Everything(), labels.Everything()) list, err := ms.List(ctx, labels.Everything(), labels.Everything())
if err != nil { if err != nil {
t.Errorf("got error calling List") t.Errorf("got error calling List")
} }

View File

@ -69,7 +69,8 @@ func TestCreatePodRegistryError(t *testing.T) {
}, },
} }
pod := &api.Pod{DesiredState: desiredState} pod := &api.Pod{DesiredState: desiredState}
ch, err := storage.Create(pod) ctx := api.NewContext()
ch, err := storage.Create(ctx, pod)
if err != nil { if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err) t.Errorf("Expected %#v, Got %#v", nil, err)
} }
@ -88,7 +89,8 @@ func TestCreatePodSetsIds(t *testing.T) {
}, },
} }
pod := &api.Pod{DesiredState: desiredState} pod := &api.Pod{DesiredState: desiredState}
ch, err := storage.Create(pod) ctx := api.NewContext()
ch, err := storage.Create(ctx, pod)
if err != nil { if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err) t.Errorf("Expected %#v, Got %#v", nil, err)
} }
@ -114,7 +116,8 @@ func TestCreatePodSetsUUIDs(t *testing.T) {
}, },
} }
pod := &api.Pod{DesiredState: desiredState} pod := &api.Pod{DesiredState: desiredState}
ch, err := storage.Create(pod) ctx := api.NewContext()
ch, err := storage.Create(ctx, pod)
if err != nil { if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err) t.Errorf("Expected %#v, Got %#v", nil, err)
} }
@ -131,7 +134,8 @@ func TestListPodsError(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
pods, err := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
pods, err := storage.List(ctx, labels.Everything(), labels.Everything())
if err != podRegistry.Err { if err != podRegistry.Err {
t.Errorf("Expected %#v, Got %#v", podRegistry.Err, err) t.Errorf("Expected %#v, Got %#v", podRegistry.Err, err)
} }
@ -145,7 +149,8 @@ func TestListEmptyPodList(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
pods, err := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
pods, err := storage.List(ctx, labels.Everything(), labels.Everything())
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -177,7 +182,8 @@ func TestListPodList(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
podsObj, err := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
podsObj, err := storage.List(ctx, labels.Everything(), labels.Everything())
pods := podsObj.(*api.PodList) pods := podsObj.(*api.PodList)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
@ -217,6 +223,7 @@ func TestListPodListSelection(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
ctx := api.NewContext()
table := []struct { table := []struct {
label, field string label, field string
@ -256,7 +263,7 @@ func TestListPodListSelection(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
continue continue
} }
podsObj, err := storage.List(label, field) podsObj, err := storage.List(ctx, label, field)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -305,7 +312,8 @@ func TestGetPod(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
obj, err := storage.Get("foo") ctx := api.NewContext()
obj, err := storage.Get(ctx, "foo")
pod := obj.(*api.Pod) pod := obj.(*api.Pod)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
@ -324,7 +332,8 @@ func TestGetPodCloud(t *testing.T) {
registry: podRegistry, registry: podRegistry,
cloudProvider: fakeCloud, cloudProvider: fakeCloud,
} }
obj, err := storage.Get("foo") ctx := api.NewContext()
obj, err := storage.Get(ctx, "foo")
pod := obj.(*api.Pod) pod := obj.(*api.Pod)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
@ -487,8 +496,9 @@ func TestPodStorageValidatesCreate(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
ctx := api.NewContext()
pod := &api.Pod{} pod := &api.Pod{}
c, err := storage.Create(pod) c, err := storage.Create(ctx, pod)
if c != nil { if c != nil {
t.Errorf("Expected nil channel") t.Errorf("Expected nil channel")
} }
@ -503,8 +513,9 @@ func TestPodStorageValidatesUpdate(t *testing.T) {
storage := REST{ storage := REST{
registry: podRegistry, registry: podRegistry,
} }
ctx := api.NewContext()
pod := &api.Pod{} pod := &api.Pod{}
c, err := storage.Update(pod) c, err := storage.Update(ctx, pod)
if c != nil { if c != nil {
t.Errorf("Expected nil channel") t.Errorf("Expected nil channel")
} }
@ -534,7 +545,8 @@ func TestCreatePod(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
DesiredState: desiredState, DesiredState: desiredState,
} }
channel, err := storage.Create(pod) ctx := api.NewContext()
channel, err := storage.Create(ctx, pod)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }

View File

@ -22,6 +22,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch" "github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"code.google.com/p/go.net/context"
) )
type PodRegistry struct { type PodRegistry struct {
@ -57,7 +59,7 @@ func (r *PodRegistry) ListPodsPredicate(filter func(*api.Pod) bool) (*api.PodLis
return &pods, nil return &pods, nil
} }
func (r *PodRegistry) ListPods(selector labels.Selector) (*api.PodList, error) { func (r *PodRegistry) ListPods(ctx context.Context, selector labels.Selector) (*api.PodList, error) {
return r.ListPodsPredicate(func(pod *api.Pod) bool { return r.ListPodsPredicate(func(pod *api.Pod) bool {
return selector.Matches(labels.Set(pod.Labels)) return selector.Matches(labels.Set(pod.Labels))
}) })

View File

@ -40,7 +40,8 @@ func TestServiceRegistryCreate(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
} }
c, _ := storage.Create(svc) ctx := api.NewContext()
c, _ := storage.Create(ctx, svc)
created_svc := <-c created_svc := <-c
created_service := created_svc.(*api.Service) created_service := created_svc.(*api.Service)
if created_service.ID != "foo" { if created_service.ID != "foo" {
@ -75,8 +76,9 @@ func TestServiceStorageValidatesCreate(t *testing.T) {
Selector: map[string]string{}, Selector: map[string]string{},
}, },
} }
ctx := api.NewContext()
for _, failureCase := range failureCases { for _, failureCase := range failureCases {
c, err := storage.Create(&failureCase) c, err := storage.Create(ctx, &failureCase)
if c != nil { if c != nil {
t.Errorf("Expected nil channel") t.Errorf("Expected nil channel")
} }
@ -95,7 +97,8 @@ func TestServiceRegistryUpdate(t *testing.T) {
Selector: map[string]string{"bar": "baz1"}, Selector: map[string]string{"bar": "baz1"},
}) })
storage := NewREST(registry, nil, nil) storage := NewREST(registry, nil, nil)
c, err := storage.Update(&api.Service{ ctx := api.NewContext()
c, err := storage.Update(ctx, &api.Service{
Port: 6502, Port: 6502,
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz2"}, Selector: map[string]string{"bar": "baz2"},
@ -136,8 +139,9 @@ func TestServiceStorageValidatesUpdate(t *testing.T) {
Selector: map[string]string{}, Selector: map[string]string{},
}, },
} }
ctx := api.NewContext()
for _, failureCase := range failureCases { for _, failureCase := range failureCases {
c, err := storage.Update(&failureCase) c, err := storage.Update(ctx, &failureCase)
if c != nil { if c != nil {
t.Errorf("Expected nil channel") t.Errorf("Expected nil channel")
} }
@ -158,7 +162,8 @@ func TestServiceRegistryExternalService(t *testing.T) {
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
CreateExternalLoadBalancer: true, CreateExternalLoadBalancer: true,
} }
c, _ := storage.Create(svc) ctx := api.NewContext()
c, _ := storage.Create(ctx, svc)
<-c <-c
if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[0] != "get-zone" || fakeCloud.Calls[1] != "create" { if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[0] != "get-zone" || fakeCloud.Calls[1] != "create" {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
@ -185,7 +190,8 @@ func TestServiceRegistryExternalServiceError(t *testing.T) {
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
CreateExternalLoadBalancer: true, CreateExternalLoadBalancer: true,
} }
c, _ := storage.Create(svc) ctx := api.NewContext()
c, _ := storage.Create(ctx, svc)
<-c <-c
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "get-zone" { if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "get-zone" {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
@ -204,8 +210,9 @@ func TestServiceRegistryDelete(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
} }
ctx := api.NewContext()
registry.CreateService(svc) registry.CreateService(svc)
c, _ := storage.Delete(svc.ID) c, _ := storage.Delete(ctx, svc.ID)
<-c <-c
if len(fakeCloud.Calls) != 0 { if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
@ -225,8 +232,9 @@ func TestServiceRegistryDeleteExternal(t *testing.T) {
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
CreateExternalLoadBalancer: true, CreateExternalLoadBalancer: true,
} }
ctx := api.NewContext()
registry.CreateService(svc) registry.CreateService(svc)
c, _ := storage.Delete(svc.ID) c, _ := storage.Delete(ctx, svc.ID)
<-c <-c
if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[0] != "get-zone" || fakeCloud.Calls[1] != "delete" { if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[0] != "get-zone" || fakeCloud.Calls[1] != "delete" {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
@ -309,7 +317,8 @@ func TestServiceRegistryGet(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
}) })
storage.Get("foo") ctx := api.NewContext()
storage.Get(ctx, "foo")
if len(fakeCloud.Calls) != 0 { if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
} }
@ -324,12 +333,13 @@ func TestServiceRegistryResourceLocation(t *testing.T) {
fakeCloud := &cloud.FakeCloud{} fakeCloud := &cloud.FakeCloud{}
machines := []string{"foo", "bar", "baz"} machines := []string{"foo", "bar", "baz"}
storage := NewREST(registry, fakeCloud, minion.NewRegistry(machines)) storage := NewREST(registry, fakeCloud, minion.NewRegistry(machines))
ctx := api.NewContext()
registry.CreateService(&api.Service{ registry.CreateService(&api.Service{
JSONBase: api.JSONBase{ID: "foo"}, JSONBase: api.JSONBase{ID: "foo"},
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
}) })
redirector := apiserver.Redirector(storage) redirector := apiserver.Redirector(storage)
location, err := redirector.ResourceLocation("foo") location, err := redirector.ResourceLocation(ctx, "foo")
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
@ -342,7 +352,7 @@ func TestServiceRegistryResourceLocation(t *testing.T) {
// Test error path // Test error path
registry.Err = fmt.Errorf("fake error") registry.Err = fmt.Errorf("fake error")
if _, err = redirector.ResourceLocation("foo"); err == nil { if _, err = redirector.ResourceLocation(ctx, "foo"); err == nil {
t.Errorf("unexpected nil error") t.Errorf("unexpected nil error")
} }
} }
@ -361,7 +371,8 @@ func TestServiceRegistryList(t *testing.T) {
Selector: map[string]string{"bar2": "baz2"}, Selector: map[string]string{"bar2": "baz2"},
}) })
registry.List.ResourceVersion = 1 registry.List.ResourceVersion = 1
s, _ := storage.List(labels.Everything(), labels.Everything()) ctx := api.NewContext()
s, _ := storage.List(ctx, labels.Everything(), labels.Everything())
sl := s.(*api.ServiceList) sl := s.(*api.ServiceList)
if len(fakeCloud.Calls) != 0 { if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)