Debian Codegraph @

pkg/server/filters/watch_termination_test.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package filters
18
19 import (
20 "fmt"
21 "net/http"
22 "net/http/httptest"
23 "testing"
24
25 "k8s.io/apimachinery/pkg/runtime"
26 apirequest "k8s.io/apiserver/pkg/endpoints/request"
27 )
28
29 func TestWithWatchTerminationDuringShutdown(t *testing.T) {
30 tests := []struct {
31 name string
32 requestInfo *apirequest.RequestInfo
33 signal *fakeServerShutdownSignal
34 wg *fakeRequestWaitGroup
35 handlerInvoked int
36 statusCodeExpected int
37 retryAfterExpected bool
38 wgInvokedExpected int
39 signalAttachedToContext bool
40 }{
41 {
42 name: "no RequestInfo attached to request context",
43 handlerInvoked: 0,
44 statusCodeExpected: http.StatusInternalServerError,
45 },
46 {
47 name: "request is not a WATCH, not added into wait group",
48 requestInfo: &apirequest.RequestInfo{Verb: "get"},
49 handlerInvoked: 1,
50 statusCodeExpected: http.StatusOK,
51 },
52 {
53 name: "request is a WATCH, wait group is in waiting mode",
54 requestInfo: &apirequest.RequestInfo{Verb: "watch"},
55 wg: &fakeRequestWaitGroup{waiting: true},
56 handlerInvoked: 0,
57 signalAttachedToContext: false,
58 wgInvokedExpected: 1,
59 retryAfterExpected: true,
60 statusCodeExpected: http.StatusServiceUnavailable,
61 },
62 {
63 name: "request is a WATCH, wait group is accepting",
64 requestInfo: &apirequest.RequestInfo{Verb: "watch"},
65 wg: &fakeRequestWaitGroup{},
66 signal: &fakeServerShutdownSignal{},
67 wgInvokedExpected: 1,
68 signalAttachedToContext: true,
69 handlerInvoked: 1,
70 statusCodeExpected: http.StatusOK,
71 },
72 }
73
74 for _, test := range tests {
75 t.Run(test.name, func(t *testing.T) {
76 var (
77 handlerInvokedGot int
78 signalGot *fakeServerShutdownSignal
79 )
80 delegate := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
81 handlerInvokedGot++
82 if signal := apirequest.ServerShutdownSignalFrom(req.Context()); signal != nil {
83 signalGot, _ = signal.(*fakeServerShutdownSignal)
84 }
85 w.WriteHeader(http.StatusOK)
86 })
87
88 handler := WithWatchTerminationDuringShutdown(delegate, test.signal, test.wg)
89
90 req, err := http.NewRequest(http.MethodGet, "/apis/groups.k8s.io/v1/namespaces", nil)
91 if err != nil {
92 t.Fatalf("failed to create new http request - %v", err)
93 }
94 if test.requestInfo != nil {
95 req = req.WithContext(apirequest.WithRequestInfo(req.Context(), test.requestInfo))
96 }
97
98 w := httptest.NewRecorder()
99 w.Code = 0
100 handler.ServeHTTP(w, req)
101 responseGot := w.Result()
102
103 if test.handlerInvoked != handlerInvokedGot {
104 t.Errorf("expected the handler to be invoked: %d timed, but got: %d", test.handlerInvoked, handlerInvokedGot)
105 }
106 if test.statusCodeExpected != responseGot.StatusCode {
107 t.Errorf("expected status code: %d, but got: %d", test.statusCodeExpected, w.Result().StatusCode)
108 }
109 retryAfterGot := retryAfterSent(responseGot)
110 if test.retryAfterExpected != retryAfterGot {
111 t.Errorf("expected retry-after: %t, but got: %t, response: %v#", test.retryAfterExpected, retryAfterGot, responseGot)
112 }
113
114 switch {
115 case test.signalAttachedToContext:
116 if test.signal == nil || test.signal != signalGot {
117 t.Errorf("expected request context to have server shutdown signal: %p, but got: %p", test.signal, signalGot)
118 }
119 default:
120 if signalGot != nil {
121 t.Errorf("expected request context to not have server shutdown signal: %p, but got: %p", test.signal, signalGot)
122 }
123 }
124 if test.wg == nil {
125 return
126 }
127 if test.wg.inflight != 0 {
128 t.Errorf("expected wait group inflight to be zero, but got: %d", test.wg.inflight)
129 }
130 if test.wgInvokedExpected != test.wg.invoked {
131 t.Errorf("expected wait group Add to be invoked: %d times, but got: %d", test.wgInvokedExpected, test.wg.invoked)
132 }
133 })
134 }
135 }
136
137 type fakeServerShutdownSignal struct{}
138
139 func (fakeServerShutdownSignal) ShuttingDown() <-chan struct{} { return nil }
140
141 type fakeRequestWaitGroup struct {
142 waiting bool
143 invoked, inflight int
144 }
145
146 func (f *fakeRequestWaitGroup) Add(delta int) error {
147 f.invoked++
148 if f.waiting {
149 return fmt.Errorf("waitgroup is in waiting mode")
150 }
151 f.inflight += delta
152 return nil
153 }
154 func (f *fakeRequestWaitGroup) Done() { f.inflight-- }
155
156 func retryAfterSent(resp *http.Response) bool {
157 switch {
158 case resp.StatusCode == http.StatusServiceUnavailable &&
159 resp.Header.Get("Retry-After") == "1" &&
160 resp.Header.Get("Content-Type") == runtime.ContentTypeJSON &&
161 resp.Header.Get("X-Content-Type-Options") == "nosniff":
162 return true
163 default:
164 return false
165 }
166 }