本文整理匯總了Golang中k8s/io/kubernetes/pkg/storage.Watch函數的典型用法代碼示例。如果您正苦於以下問題:Golang Watch函數的具體用法?Golang Watch怎麽用?Golang Watch使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Watch函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestEtcdWatchNodesNotMatch
func TestEtcdWatchNodesNotMatch(t *testing.T) {
ctx := api.NewDefaultContext()
storage, fakeClient := newStorage(t)
node := validNewNode()
watching, err := storage.Watch(ctx,
labels.SelectorFromSet(labels.Set{"name": "bar"}),
fields.Everything(),
"1",
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fakeClient.WaitForWatchCompletion()
nodeBytes, _ := latest.Codec.Encode(node)
fakeClient.WatchResponse <- &etcd.Response{
Action: "create",
Node: &etcd.Node{
Value: string(nodeBytes),
},
}
select {
case <-watching.ResultChan():
t.Error("unexpected result from result channel")
case <-time.After(time.Millisecond * 100):
// expected case
}
}
示例2: TestEtcdWatchNode
func TestEtcdWatchNode(t *testing.T) {
ctx := api.NewDefaultContext()
storage, fakeClient := newStorage(t)
watching, err := storage.Watch(ctx,
labels.Everything(),
fields.Everything(),
"1",
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fakeClient.WaitForWatchCompletion()
select {
case _, ok := <-watching.ResultChan():
if !ok {
t.Errorf("watching channel should be open")
}
default:
}
fakeClient.WatchInjectError <- nil
if _, ok := <-watching.ResultChan(); ok {
t.Errorf("watching channel should be closed")
}
watching.Stop()
}
示例3: TestWatchErrorWithFieldSet
func TestWatchErrorWithFieldSet(t *testing.T) {
_, helper := newHelper(t)
storage := NewREST(helper)
_, err := storage.Watch(kapi.NewDefaultContext(), labels.Everything(), fields.SelectorFromSet(fields.Set{"foo": "bar"}), "1")
if err == nil {
t.Fatal("unexpected nil error")
}
if err.Error() != "field selectors are not supported on images" {
t.Fatalf("unexpected error: %s", err.Error())
}
}
示例4: TestEtcdWatchControllersMatch
// Tests that we can watch for the creation of daemon controllers with specified labels.
func TestEtcdWatchControllersMatch(t *testing.T) {
ctx := api.WithNamespace(api.NewDefaultContext(), validController.Namespace)
storage, fakeClient := newStorage(t)
fakeClient.ExpectNotFoundGet(etcdgeneric.NamespaceKeyRootFunc(ctx, "/registry/pods"))
watching, err := storage.Watch(ctx,
labels.SelectorFromSet(validController.Spec.Selector),
fields.Everything(),
"1",
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fakeClient.WaitForWatchCompletion()
// The watcher above is waiting for these Labels, on receiving them it should
// apply the ControllerStatus decorator, which lists pods, causing a query against
// the /registry/pods endpoint of the etcd client.
controller := &api.Daemon{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: validController.Spec.Selector,
Namespace: "default",
},
}
controllerBytes, _ := latest.Codec.Encode(controller)
fakeClient.WatchResponse <- &etcd.Response{
Action: "create",
Node: &etcd.Node{
Value: string(controllerBytes),
},
}
select {
case _, ok := <-watching.ResultChan():
if !ok {
t.Errorf("watching channel should be open")
}
case <-time.After(time.Millisecond * 100):
t.Error("unexpected timeout from result channel")
}
watching.Stop()
}
示例5: TestEtcdWatchControllersNotMatch
func TestEtcdWatchControllersNotMatch(t *testing.T) {
ctx := api.NewDefaultContext()
storage, fakeClient := newStorage(t)
fakeClient.ExpectNotFoundGet(etcdgeneric.NamespaceKeyRootFunc(ctx, "/registry/pods"))
watching, err := storage.Watch(ctx,
labels.SelectorFromSet(labels.Set{"name": "foo"}),
fields.Everything(),
"1",
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fakeClient.WaitForWatchCompletion()
controller := &api.Daemon{
ObjectMeta: api.ObjectMeta{
Name: "bar",
Labels: map[string]string{
"name": "bar",
},
},
}
controllerBytes, _ := latest.Codec.Encode(controller)
fakeClient.WatchResponse <- &etcd.Response{
Action: "create",
Node: &etcd.Node{
Value: string(controllerBytes),
},
}
select {
case <-watching.ResultChan():
t.Error("unexpected result from result channel")
case <-time.After(time.Millisecond * 100):
// expected case
}
}
示例6: TestWatchOK
func TestWatchOK(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
storage := NewREST(helper)
var tests = []struct {
label labels.Selector
images []*api.Image
expected []bool
}{
{
labels.Everything(),
[]*api.Image{
{ObjectMeta: kapi.ObjectMeta{Name: "a"}, DockerImageMetadata: api.DockerImage{}},
{ObjectMeta: kapi.ObjectMeta{Name: "b"}, DockerImageMetadata: api.DockerImage{}},
{ObjectMeta: kapi.ObjectMeta{Name: "c"}, DockerImageMetadata: api.DockerImage{}},
},
[]bool{
true,
true,
true,
},
},
{
labels.SelectorFromSet(labels.Set{"color": "blue"}),
[]*api.Image{
{ObjectMeta: kapi.ObjectMeta{Name: "a", Labels: map[string]string{"color": "blue"}}, DockerImageMetadata: api.DockerImage{}},
{ObjectMeta: kapi.ObjectMeta{Name: "b", Labels: map[string]string{"color": "green"}}, DockerImageMetadata: api.DockerImage{}},
{ObjectMeta: kapi.ObjectMeta{Name: "c", Labels: map[string]string{"color": "blue"}}, DockerImageMetadata: api.DockerImage{}},
},
[]bool{
true,
false,
true,
},
},
}
for _, tt := range tests {
watching, err := storage.Watch(kapi.NewDefaultContext(), tt.label, fields.Everything(), "1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
fakeEtcdClient.WaitForWatchCompletion()
for testIndex, image := range tt.images {
imageBytes, _ := latest.Codec.Encode(image)
fakeEtcdClient.WatchResponse <- &etcd.Response{
Action: "set",
Node: &etcd.Node{
Value: string(imageBytes),
},
}
select {
case event, ok := <-watching.ResultChan():
if !ok {
t.Errorf("watching channel should be open")
}
if !tt.expected[testIndex] {
t.Errorf("unexpected image returned from watch: %#v", event.Object)
}
if e, a := watch.Added, event.Type; e != a {
t.Errorf("Expected %v, got %v", e, a)
}
image.DockerImageMetadataVersion = "1.0"
if e, a := image, event.Object; !reflect.DeepEqual(e, a) {
t.Errorf("Objects did not match: %s", util.ObjectDiff(e, a))
}
case <-time.After(50 * time.Millisecond):
if tt.expected[testIndex] {
t.Errorf("Expected image %#v to be returned from watch", image)
}
}
}
select {
case _, ok := <-watching.ResultChan():
if !ok {
t.Errorf("watching channel should be open")
}
default:
}
fakeEtcdClient.WatchInjectError <- nil
if _, ok := <-watching.ResultChan(); ok {
t.Errorf("watching channel should be closed")
}
watching.Stop()
}
}
示例7: TestEtcdWatchControllersFields
// Tests that we can watch for daemon controllers with specified fields.
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: {
{"metadata.name": "foo"},
},
FAIL: {
{"metadata.name": "bar"},
{"name": "foo"},
},
}
testEtcdActions := []string{
etcdstorage.EtcdCreate,
etcdstorage.EtcdSet,
etcdstorage.EtcdCAS,
etcdstorage.EtcdDelete}
controller := &api.Daemon{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: validController.Spec.Selector,
Namespace: "default",
},
Status: api.DaemonStatus{
CurrentNumberScheduled: 2,
NumberMisscheduled: 1,
DesiredNumberScheduled: 4,
},
}
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",
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var prevNode *etcd.Node = nil
node := &etcd.Node{
Value: string(controllerBytes),
}
if action == etcdstorage.EtcdDelete {
prevNode = node
}
fakeClient.WaitForWatchCompletion()
fakeClient.WatchResponse <- &etcd.Response{
Action: action,
Node: node,
PrevNode: prevNode,
}
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()
}
}
}
}