From 5337bc220aa1b0844ebe929f8871b9b1e496235c Mon Sep 17 00:00:00 2001 From: Hongchao Deng Date: Tue, 10 May 2016 22:40:31 -0700 Subject: [PATCH] etcd_watcher: test for ensuring delete event have latest index --- pkg/storage/etcd/etcd_watcher_test.go | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/pkg/storage/etcd/etcd_watcher_test.go b/pkg/storage/etcd/etcd_watcher_test.go index 386f59b56a1..c1aa0f11cba 100644 --- a/pkg/storage/etcd/etcd_watcher_test.go +++ b/pkg/storage/etcd/etcd_watcher_test.go @@ -220,6 +220,74 @@ func TestWatchInterpretation_ResponseBadData(t *testing.T) { } } +func TestSendResultDeleteEventHaveLatestIndex(t *testing.T) { + codec := testapi.Default.Codec() + filter := func(obj runtime.Object) bool { + return obj.(*api.Pod).Name != "bar" + } + w := newEtcdWatcher(false, false, nil, filter, codec, versioner, nil, &fakeEtcdCache{}) + + eventChan := make(chan watch.Event, 1) + w.emit = func(e watch.Event) { + eventChan <- e + } + + fooPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} + barPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "bar"}} + fooBytes, err := runtime.Encode(codec, fooPod) + if err != nil { + t.Fatalf("Encode failed: %v", err) + } + barBytes, err := runtime.Encode(codec, barPod) + if err != nil { + t.Fatalf("Encode failed: %v", err) + } + + tests := []struct { + response *etcd.Response + expRV string + }{{ // Delete event + response: &etcd.Response{ + Action: EtcdDelete, + Node: &etcd.Node{ + ModifiedIndex: 2, + }, + PrevNode: &etcd.Node{ + Value: string(fooBytes), + ModifiedIndex: 1, + }, + }, + expRV: "2", + }, { // Modify event with uninterested data + response: &etcd.Response{ + Action: EtcdSet, + Node: &etcd.Node{ + Value: string(barBytes), + ModifiedIndex: 2, + }, + PrevNode: &etcd.Node{ + Value: string(fooBytes), + ModifiedIndex: 1, + }, + }, + expRV: "2", + }} + + for i, tt := range tests { + w.sendResult(tt.response) + ev := <-eventChan + if ev.Type != watch.Deleted { + t.Errorf("#%d: event type want=Deleted, get=%s", i, ev.Type) + return + } + rv := ev.Object.(*api.Pod).ResourceVersion + if rv != tt.expRV { + t.Errorf("#%d: resource version want=%s, get=%s", i, tt.expRV, rv) + } + } + w.Stop() +} + func TestWatch(t *testing.T) { codec := testapi.Default.Codec() server := etcdtesting.NewEtcdTestClientServer(t)