From faed88bb7200cc4693b7a3f9cecff1a99ea1fc95 Mon Sep 17 00:00:00 2001 From: wojtekt Date: Wed, 14 Jul 2021 16:36:38 +0200 Subject: [PATCH] Add additional APF test for handling other panic types --- .../filters/priority-and-fairness_test.go | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness_test.go b/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness_test.go index be3f326f3a2..7ebf1c5167b 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness_test.go @@ -373,6 +373,9 @@ type fakeWatchApfFilter struct { inflight int capacity int + postExecutePanic bool + preExecutePanic bool + utilflowcontrol.WatchTracker } @@ -402,7 +405,13 @@ func (f *fakeWatchApfFilter) Handle(ctx context.Context, return } + if f.preExecutePanic { + panic("pre-exec-panic") + } execFn() + if f.postExecutePanic { + panic("post-exec-panic") + } f.lock.Lock() defer f.lock.Unlock() @@ -546,6 +555,53 @@ func TestApfWatchPanic(t *testing.T) { } } +func TestApfWatchHandlePanic(t *testing.T) { + preExecutePanicingFilter := newFakeWatchApfFilter(1) + preExecutePanicingFilter.preExecutePanic = true + + postExecutePanicingFilter := newFakeWatchApfFilter(1) + postExecutePanicingFilter.postExecutePanic = true + + testCases := []struct { + name string + filter *fakeWatchApfFilter + }{ + { + name: "pre-execute panic", + filter: preExecutePanicingFilter, + }, + { + name: "post-execute panic", + filter: postExecutePanicingFilter, + }, + } + + onExecuteFunc := func() { + time.Sleep(5 * time.Second) + } + postExecuteFunc := func() {} + + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + apfHandler := newApfHandlerWithFilter(t, test.filter, onExecuteFunc, postExecuteFunc) + handler := func(w http.ResponseWriter, r *http.Request) { + defer func() { + if err := recover(); err == nil { + t.Errorf("expected panic, got %v", err) + } + }() + apfHandler.ServeHTTP(w, r) + } + server := httptest.NewServer(http.HandlerFunc(handler)) + defer server.Close() + + if err := expectHTTPGet(fmt.Sprintf("%s/api/v1/namespaces/default/pods?watch=true", server.URL), http.StatusOK); err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + } +} + // TestContextClosesOnRequestProcessed ensures that the request context is cancelled // automatically even if the server doesn't cancel is explicitly. // This is required to ensure we won't be leaking goroutines that wait for context