Skip to content

Commit 67ad9b5

Browse files
stlazk8s-publishing-bot
authored andcommitted
authz tests: delay response in context cancelled scenario
The net/http transport checks for immediate response when its handling context cancellation. The unit test was racing with this check by responding too fast. Signed-off-by: Stanislav Láznička <slznika@microsoft.com> Kubernetes-commit: d74bd66e9ea17ff5ad86d61d25d7f6206eaa66c2
1 parent 3cd3d02 commit 67ad9b5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

plugin/pkg/authorizer/webhook/metrics_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,16 @@ func TestAuthorizerMetrics(t *testing.T) {
107107
if service.statusCode == 0 {
108108
service.statusCode = 200
109109
}
110-
service.reviewHook = func(*authorizationv1.SubjectAccessReview) {
111-
if scenario.canceledRequest {
110+
111+
testFinishedCtx, testFinishedCancel := context.WithCancel(context.Background())
112+
defer testFinishedCancel()
113+
if scenario.canceledRequest {
114+
service.reviewHook = func(*authorizationv1.SubjectAccessReview) {
112115
cancel()
116+
// net/http transport still attempts to use a response if it's
117+
// available right when it's handling context cancellation,
118+
// we need to delay the response.
119+
<-testFinishedCtx.Done()
113120
}
114121
}
115122
service.allow = !scenario.authFakeServiceDeny
@@ -120,6 +127,9 @@ func TestAuthorizerMetrics(t *testing.T) {
120127
return
121128
}
122129
defer server.Close()
130+
// testFinishedCtx must be cancelled before we close the server, otherwise
131+
// closing the server hangs on an active connection from the listener.
132+
defer testFinishedCancel()
123133

124134
fakeAuthzMetrics := &fakeAuthorizerMetrics{}
125135
wh, err := newV1Authorizer(server.URL, scenario.clientCert, scenario.clientKey, scenario.clientCA, 0, fakeAuthzMetrics, cel.NewDefaultCompiler(), []apiserver.WebhookMatchCondition{}, "")

0 commit comments

Comments
 (0)