| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package watch |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"reflect" |
| 22 |
goruntime "runtime" |
| 23 |
"sort" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/google/go-cmp/cmp" |
| 28 |
|
| 29 |
corev1 "k8s.io/api/core/v1" |
| 30 |
apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 31 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 |
"k8s.io/apimachinery/pkg/runtime" |
| 33 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 34 |
"k8s.io/apimachinery/pkg/util/dump" |
| 35 |
"k8s.io/apimachinery/pkg/util/wait" |
| 36 |
"k8s.io/apimachinery/pkg/watch" |
| 37 |
clientfeatures "k8s.io/client-go/features" |
| 38 |
clientfeaturestesting "k8s.io/client-go/features/testing" |
| 39 |
fakeclientset "k8s.io/client-go/kubernetes/fake" |
| 40 |
testcore "k8s.io/client-go/testing" |
| 41 |
"k8s.io/client-go/tools/cache" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func TestEventProcessorExit(t *testing.T) { |
| 47 |
event := watch.Event{} |
| 48 |
|
| 49 |
tests := []struct { |
| 50 |
name string |
| 51 |
write func(e *eventProcessor) |
| 52 |
}{ |
| 53 |
{ |
| 54 |
name: "exit on blocked read", |
| 55 |
write: func(e *eventProcessor) { |
| 56 |
e.push(event) |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "exit on blocked write", |
| 61 |
write: func(e *eventProcessor) { |
| 62 |
e.push(event) |
| 63 |
e.push(event) |
| 64 |
}, |
| 65 |
}, |
| 66 |
} |
| 67 |
for _, test := range tests { |
| 68 |
t.Run(test.name, func(t *testing.T) { |
| 69 |
out := make(chan watch.Event) |
| 70 |
e := newEventProcessor(out) |
| 71 |
|
| 72 |
test.write(e) |
| 73 |
|
| 74 |
exited := make(chan struct{}) |
| 75 |
go func() { |
| 76 |
e.run() |
| 77 |
close(exited) |
| 78 |
}() |
| 79 |
|
| 80 |
<-out |
| 81 |
e.stop() |
| 82 |
goruntime.Gosched() |
| 83 |
<-exited |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
type apiInt int |
| 89 |
|
| 90 |
func (apiInt) GetObjectKind() schema.ObjectKind { return nil } |
| 91 |
func (apiInt) DeepCopyObject() runtime.Object { return nil } |
| 92 |
|
| 93 |
func TestEventProcessorOrdersEvents(t *testing.T) { |
| 94 |
out := make(chan watch.Event) |
| 95 |
e := newEventProcessor(out) |
| 96 |
go e.run() |
| 97 |
|
| 98 |
numProcessed := 0 |
| 99 |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 100 |
go func() { |
| 101 |
for i := 0; i < 1000; i++ { |
| 102 |
e := <-out |
| 103 |
if got, want := int(e.Object.(apiInt)), i; got != want { |
| 104 |
t.Errorf("unexpected event: got=%d, want=%d", got, want) |
| 105 |
} |
| 106 |
numProcessed++ |
| 107 |
} |
| 108 |
cancel() |
| 109 |
}() |
| 110 |
|
| 111 |
for i := 0; i < 1000; i++ { |
| 112 |
e.push(watch.Event{Object: apiInt(i)}) |
| 113 |
} |
| 114 |
|
| 115 |
<-ctx.Done() |
| 116 |
e.stop() |
| 117 |
|
| 118 |
if numProcessed != 1000 { |
| 119 |
t.Errorf("unexpected number of events processed: %d", numProcessed) |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
type byEventTypeAndName []watch.Event |
| 125 |
|
| 126 |
func (a byEventTypeAndName) Len() int { return len(a) } |
| 127 |
func (a byEventTypeAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 128 |
func (a byEventTypeAndName) Less(i, j int) bool { |
| 129 |
if a[i].Type < a[j].Type { |
| 130 |
return true |
| 131 |
} |
| 132 |
|
| 133 |
if a[i].Type > a[j].Type { |
| 134 |
return false |
| 135 |
} |
| 136 |
|
| 137 |
return a[i].Object.(*corev1.Secret).Name < a[j].Object.(*corev1.Secret).Name |
| 138 |
} |
| 139 |
|
| 140 |
func newTestSecret(name, key, value string) *corev1.Secret { |
| 141 |
return &corev1.Secret{ |
| 142 |
ObjectMeta: metav1.ObjectMeta{ |
| 143 |
Name: name, |
| 144 |
}, |
| 145 |
StringData: map[string]string{ |
| 146 |
key: value, |
| 147 |
}, |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestNewInformerWatcher(t *testing.T) { |
| 152 |
|
| 153 |
tt := []struct { |
| 154 |
name string |
| 155 |
watchListFeatureEnabled bool |
| 156 |
objects []runtime.Object |
| 157 |
inputEvents []watch.Event |
| 158 |
outputEvents []watch.Event |
| 159 |
}{ |
| 160 |
{ |
| 161 |
name: "WatchListClient feature disabled", |
| 162 |
watchListFeatureEnabled: false, |
| 163 |
objects: []runtime.Object{ |
| 164 |
newTestSecret("pod-1", "foo-1", "initial"), |
| 165 |
newTestSecret("pod-2", "foo-2", "initial"), |
| 166 |
newTestSecret("pod-3", "foo-3", "initial"), |
| 167 |
}, |
| 168 |
inputEvents: []watch.Event{ |
| 169 |
{ |
| 170 |
Type: watch.Added, |
| 171 |
Object: newTestSecret("pod-4", "foo-4", "initial"), |
| 172 |
}, |
| 173 |
{ |
| 174 |
Type: watch.Modified, |
| 175 |
Object: newTestSecret("pod-2", "foo-2", "new"), |
| 176 |
}, |
| 177 |
{ |
| 178 |
Type: watch.Deleted, |
| 179 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 180 |
}, |
| 181 |
}, |
| 182 |
outputEvents: []watch.Event{ |
| 183 |
|
| 184 |
|
| 185 |
{ |
| 186 |
Type: watch.Added, |
| 187 |
Object: newTestSecret("pod-1", "foo-1", "initial"), |
| 188 |
}, |
| 189 |
{ |
| 190 |
Type: watch.Added, |
| 191 |
Object: newTestSecret("pod-2", "foo-2", "initial"), |
| 192 |
}, |
| 193 |
{ |
| 194 |
Type: watch.Added, |
| 195 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 196 |
}, |
| 197 |
|
| 198 |
{ |
| 199 |
Type: watch.Added, |
| 200 |
Object: newTestSecret("pod-4", "foo-4", "initial"), |
| 201 |
}, |
| 202 |
{ |
| 203 |
Type: watch.Modified, |
| 204 |
Object: newTestSecret("pod-2", "foo-2", "new"), |
| 205 |
}, |
| 206 |
{ |
| 207 |
Type: watch.Deleted, |
| 208 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 209 |
}, |
| 210 |
}, |
| 211 |
}, |
| 212 |
{ |
| 213 |
name: "WatchListClient feature enabled", |
| 214 |
watchListFeatureEnabled: true, |
| 215 |
objects: []runtime.Object{ |
| 216 |
newTestSecret("pod-1", "foo-1", "initial"), |
| 217 |
newTestSecret("pod-2", "foo-2", "initial"), |
| 218 |
newTestSecret("pod-3", "foo-3", "initial"), |
| 219 |
}, |
| 220 |
inputEvents: []watch.Event{ |
| 221 |
{ |
| 222 |
Type: watch.Added, |
| 223 |
Object: newTestSecret("pod-1", "foo-1", "initial"), |
| 224 |
}, |
| 225 |
{ |
| 226 |
Type: watch.Added, |
| 227 |
Object: newTestSecret("pod-2", "foo-2", "initial"), |
| 228 |
}, |
| 229 |
{ |
| 230 |
Type: watch.Added, |
| 231 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 232 |
}, |
| 233 |
|
| 234 |
{ |
| 235 |
Type: watch.Bookmark, |
| 236 |
Object: &corev1.Secret{ |
| 237 |
ObjectMeta: metav1.ObjectMeta{ |
| 238 |
Annotations: map[string]string{ |
| 239 |
metav1.InitialEventsAnnotationKey: "true", |
| 240 |
}, |
| 241 |
}, |
| 242 |
}, |
| 243 |
}, |
| 244 |
{ |
| 245 |
Type: watch.Added, |
| 246 |
Object: newTestSecret("pod-4", "foo-4", "initial"), |
| 247 |
}, |
| 248 |
{ |
| 249 |
Type: watch.Modified, |
| 250 |
Object: newTestSecret("pod-2", "foo-2", "new"), |
| 251 |
}, |
| 252 |
{ |
| 253 |
Type: watch.Deleted, |
| 254 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 255 |
}, |
| 256 |
}, |
| 257 |
outputEvents: []watch.Event{ |
| 258 |
|
| 259 |
|
| 260 |
{ |
| 261 |
Type: watch.Added, |
| 262 |
Object: newTestSecret("pod-1", "foo-1", "initial"), |
| 263 |
}, |
| 264 |
{ |
| 265 |
Type: watch.Added, |
| 266 |
Object: newTestSecret("pod-2", "foo-2", "initial"), |
| 267 |
}, |
| 268 |
{ |
| 269 |
Type: watch.Added, |
| 270 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 271 |
}, |
| 272 |
|
| 273 |
|
| 274 |
{ |
| 275 |
Type: watch.Added, |
| 276 |
Object: newTestSecret("pod-4", "foo-4", "initial"), |
| 277 |
}, |
| 278 |
{ |
| 279 |
Type: watch.Modified, |
| 280 |
Object: newTestSecret("pod-2", "foo-2", "new"), |
| 281 |
}, |
| 282 |
{ |
| 283 |
Type: watch.Deleted, |
| 284 |
Object: newTestSecret("pod-3", "foo-3", "initial"), |
| 285 |
}, |
| 286 |
}, |
| 287 |
}, |
| 288 |
} |
| 289 |
|
| 290 |
for _, tc := range tt { |
| 291 |
t.Run(tc.name, func(t *testing.T) { |
| 292 |
clientfeaturestesting.SetFeatureDuringTest(t, clientfeatures.WatchListClient, tc.watchListFeatureEnabled) |
| 293 |
|
| 294 |
fake := fakeclientset.NewSimpleClientset(tc.objects...) |
| 295 |
inputCh := make(chan watch.Event) |
| 296 |
inputWatcher := watch.NewProxyWatcher(inputCh) |
| 297 |
|
| 298 |
|
| 299 |
defer inputWatcher.Stop() |
| 300 |
inputStopCh := inputWatcher.StopChan() |
| 301 |
fake.PrependWatchReactor("secrets", testcore.DefaultWatchReactor(inputWatcher, nil)) |
| 302 |
|
| 303 |
inputDoneCh := make(chan struct{}) |
| 304 |
go func() { |
| 305 |
defer close(inputDoneCh) |
| 306 |
for _, e := range tc.inputEvents { |
| 307 |
select { |
| 308 |
case inputCh <- e: |
| 309 |
case <-inputStopCh: |
| 310 |
return |
| 311 |
} |
| 312 |
} |
| 313 |
}() |
| 314 |
|
| 315 |
lw := &cache.ListWatch{ |
| 316 |
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 317 |
return fake.CoreV1().Secrets("").List(context.TODO(), options) |
| 318 |
}, |
| 319 |
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 320 |
return fake.CoreV1().Secrets("").Watch(context.TODO(), options) |
| 321 |
}, |
| 322 |
} |
| 323 |
|
| 324 |
_, _, outputWatcher, informerDoneCh := NewIndexerInformerWatcher(lw, &corev1.Secret{}) |
| 325 |
outputCh := outputWatcher.ResultChan() |
| 326 |
timeoutCh := time.After(wait.ForeverTestTimeout) |
| 327 |
var result []watch.Event |
| 328 |
loop: |
| 329 |
for { |
| 330 |
select { |
| 331 |
case event, ok := <-outputCh: |
| 332 |
if !ok { |
| 333 |
t.Errorf("Output result channel closed prematurely") |
| 334 |
break loop |
| 335 |
} |
| 336 |
result = append(result, *event.DeepCopy()) |
| 337 |
if len(result) >= len(tc.outputEvents) { |
| 338 |
break loop |
| 339 |
} |
| 340 |
case <-timeoutCh: |
| 341 |
t.Error("Timed out waiting for events") |
| 342 |
break loop |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
sort.Sort(byEventTypeAndName(tc.outputEvents)) |
| 348 |
sort.Sort(byEventTypeAndName(result)) |
| 349 |
|
| 350 |
if !reflect.DeepEqual(tc.outputEvents, result) { |
| 351 |
t.Errorf("\nexpected: %s,\ngot: %s,\ndiff: %s", dump.Pretty(tc.outputEvents), dump.Pretty(result), cmp.Diff(tc.outputEvents, result)) |
| 352 |
return |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
go func() { |
| 358 |
defer close(inputCh) |
| 359 |
for _, e := range tc.inputEvents { |
| 360 |
select { |
| 361 |
case inputCh <- e: |
| 362 |
case <-inputStopCh: |
| 363 |
return |
| 364 |
} |
| 365 |
} |
| 366 |
}() |
| 367 |
|
| 368 |
|
| 369 |
outputWatcher.Stop() |
| 370 |
|
| 371 |
select { |
| 372 |
case <-informerDoneCh: |
| 373 |
case <-time.After(wait.ForeverTestTimeout): |
| 374 |
t.Error("Timed out waiting for informer to cleanup") |
| 375 |
} |
| 376 |
}) |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
func TestInformerWatcherDeletedFinalStateUnknown(t *testing.T) { |
| 388 |
listCalls := 0 |
| 389 |
watchCalls := 0 |
| 390 |
lw := &cache.ListWatch{ |
| 391 |
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 392 |
retval := &corev1.SecretList{} |
| 393 |
if listCalls == 0 { |
| 394 |
|
| 395 |
retval.ResourceVersion = "1" |
| 396 |
retval.Items = []corev1.Secret{{ObjectMeta: metav1.ObjectMeta{Name: "secret1", Namespace: "ns1", ResourceVersion: "123"}}} |
| 397 |
} else { |
| 398 |
|
| 399 |
retval.ResourceVersion = "2" |
| 400 |
} |
| 401 |
listCalls++ |
| 402 |
return retval, nil |
| 403 |
}, |
| 404 |
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 405 |
w := watch.NewRaceFreeFake() |
| 406 |
if options.ResourceVersion == "1" { |
| 407 |
go func() { |
| 408 |
|
| 409 |
w.Error(&apierrors.NewGone("gone").ErrStatus) |
| 410 |
w.Stop() |
| 411 |
}() |
| 412 |
} |
| 413 |
watchCalls++ |
| 414 |
return w, nil |
| 415 |
}, |
| 416 |
} |
| 417 |
|
| 418 |
_, _, w, done := NewIndexerInformerWatcher(lw, &corev1.Secret{}) |
| 419 |
defer w.Stop() |
| 420 |
|
| 421 |
|
| 422 |
select { |
| 423 |
case event, ok := <-w.ResultChan(): |
| 424 |
if !ok { |
| 425 |
t.Fatal("unexpected close") |
| 426 |
} |
| 427 |
if event.Type != watch.Added { |
| 428 |
t.Fatalf("expected Added event, got %#v", event) |
| 429 |
} |
| 430 |
if event.Object.(*corev1.Secret).ResourceVersion != "123" { |
| 431 |
t.Fatalf("expected added Secret with rv=123, got %#v", event.Object) |
| 432 |
} |
| 433 |
case <-time.After(time.Second * 10): |
| 434 |
t.Fatal("timeout") |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
select { |
| 439 |
case event, ok := <-w.ResultChan(): |
| 440 |
if !ok { |
| 441 |
t.Fatal("unexpected close") |
| 442 |
} |
| 443 |
if event.Type != watch.Deleted { |
| 444 |
t.Fatalf("expected Deleted event, got %#v", event) |
| 445 |
} |
| 446 |
if event.Object.(*corev1.Secret).ResourceVersion != "123" { |
| 447 |
t.Fatalf("expected deleted Secret with rv=123, got %#v", event.Object) |
| 448 |
} |
| 449 |
case <-time.After(time.Second * 10): |
| 450 |
t.Fatal("timeout") |
| 451 |
} |
| 452 |
|
| 453 |
w.Stop() |
| 454 |
select { |
| 455 |
case <-done: |
| 456 |
case <-time.After(time.Second * 10): |
| 457 |
t.Fatal("timeout") |
| 458 |
} |
| 459 |
|
| 460 |
if listCalls < 2 { |
| 461 |
t.Fatalf("expected at least 2 list calls, got %d", listCalls) |
| 462 |
} |
| 463 |
if watchCalls < 1 { |
| 464 |
t.Fatalf("expected at least 1 watch call, got %d", watchCalls) |
| 465 |
} |
| 466 |
} |