Add the ability to watch fields of an rc

This commit is contained in:
Prashanth Balasubramanian
2015-03-24 19:57:04 -07:00
parent b77b529e16
commit b0068d05f8
6 changed files with 153 additions and 7 deletions

View File

@@ -34,6 +34,11 @@ import (
"github.com/coreos/go-etcd/etcd"
)
const (
PASS = iota
FAIL
)
// newStorage creates a REST storage backed by etcd helpers
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
fakeEtcdClient := tools.NewFakeEtcdClient(t)
@@ -531,6 +536,86 @@ func TestEtcdWatchControllersMatch(t *testing.T) {
watching.Stop()
}
func TestEtcdWatchControllersFields(t *testing.T) {
ctx := api.WithNamespace(api.NewDefaultContext(), validController.Namespace)
storage, fakeClient := newStorage(t)
fakeClient.ExpectNotFoundGet(etcdgeneric.NamespaceKeyRootFunc(ctx, "/registry/pods"))
testFieldMap := map[int][]fields.Set{
PASS: {
{"status.replicas": "0"},
{"name": "foo"},
{"status.replicas": "0", "name": "foo"},
},
FAIL: {
{"status.replicas": "10"},
{"name": "bar"},
{"status.replicas": "10", "name": "foo"},
{"status.replicas": "0", "name": "bar"},
},
}
testEtcdActions := []string{
tools.EtcdCreate,
tools.EtcdSet,
tools.EtcdCAS,
tools.EtcdDelete}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: validController.Spec.Selector,
Namespace: "default",
},
Status: api.ReplicationControllerStatus{
Replicas: 0,
},
}
controllerBytes, _ := latest.Codec.Encode(controller)
for expectedResult, fieldSet := range testFieldMap {
for _, field := range fieldSet {
for _, action := range testEtcdActions {
watching, err := storage.Watch(ctx,
labels.Everything(),
field.AsSelector(),
"1",
)
var prevNode *etcd.Node = nil
node := &etcd.Node{
Value: string(controllerBytes),
}
if action == tools.EtcdDelete {
prevNode = node
}
fakeClient.WaitForWatchCompletion()
fakeClient.WatchResponse <- &etcd.Response{
Action: action,
Node: node,
PrevNode: prevNode,
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
select {
case r, ok := <-watching.ResultChan():
if expectedResult == FAIL {
t.Errorf("Unexpected result from channel %#v", r)
}
if !ok {
t.Errorf("watching channel should be open")
}
case <-time.After(time.Millisecond * 100):
if expectedResult == PASS {
t.Error("unexpected timeout from result channel")
}
}
watching.Stop()
}
}
}
}
func TestEtcdWatchControllersNotMatch(t *testing.T) {
ctx := api.NewDefaultContext()
storage, fakeClient := newStorage(t)