(t *testing.T)
| 139 | } |
| 140 | |
| 141 | func TestReflectorWatchHandler(t *testing.T) { |
| 142 | s := NewStore(MetaNamespaceKeyFunc) |
| 143 | g := NewReflector(&testLW{}, &v1.Pod{}, s, 0) |
| 144 | fw := watch.NewFake() |
| 145 | s.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) |
| 146 | s.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}) |
| 147 | go func() { |
| 148 | fw.Add(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "rejected"}}) |
| 149 | fw.Delete(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) |
| 150 | fw.Modify(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "55"}}) |
| 151 | fw.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "baz", ResourceVersion: "32"}}) |
| 152 | fw.Stop() |
| 153 | }() |
| 154 | var resumeRV string |
| 155 | err := g.watchHandler(fw, &resumeRV, nevererrc, wait.NeverStop) |
| 156 | if err != nil { |
| 157 | t.Errorf("unexpected error %v", err) |
| 158 | } |
| 159 | |
| 160 | mkPod := func(id string, rv string) *v1.Pod { |
| 161 | return &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: id, ResourceVersion: rv}} |
| 162 | } |
| 163 | |
| 164 | table := []struct { |
| 165 | Pod *v1.Pod |
| 166 | exists bool |
| 167 | }{ |
| 168 | {mkPod("foo", ""), false}, |
| 169 | {mkPod("rejected", ""), false}, |
| 170 | {mkPod("bar", "55"), true}, |
| 171 | {mkPod("baz", "32"), true}, |
| 172 | } |
| 173 | for _, item := range table { |
| 174 | obj, exists, _ := s.Get(item.Pod) |
| 175 | if e, a := item.exists, exists; e != a { |
| 176 | t.Errorf("%v: expected %v, got %v", item.Pod, e, a) |
| 177 | } |
| 178 | if !exists { |
| 179 | continue |
| 180 | } |
| 181 | if e, a := item.Pod.ResourceVersion, obj.(*v1.Pod).ResourceVersion; e != a { |
| 182 | t.Errorf("%v: expected %v, got %v", item.Pod, e, a) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // RV should send the last version we see. |
| 187 | if e, a := "32", resumeRV; e != a { |
| 188 | t.Errorf("expected %v, got %v", e, a) |
| 189 | } |
| 190 | |
| 191 | // last sync resource version should be the last version synced with store |
| 192 | if e, a := "32", g.LastSyncResourceVersion(); e != a { |
| 193 | t.Errorf("expected %v, got %v", e, a) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestReflectorStopWatch(t *testing.T) { |
| 198 | s := NewStore(MetaNamespaceKeyFunc) |
nothing calls this directly
no test coverage detected