fix flake on TestStreamTranslator_WebSocketServerErrors

The metrics assertion race with the metric update, and since this
happens at the serverside, we use an active look to check the metrics
instead of expecting to be updated immidiatly.

Change-Id: I9a64b66301d5f4ac3df0c0a01de10602a20f89ea

Kubernetes-commit: f07dcd443d7335d09dc0de7a47485e2e6c87d725
This commit is contained in:
Antonio Ojea 2025-07-24 21:56:42 +00:00 committed by Kubernetes Publisher
parent 1c694e9979
commit 8f1beab7b2
1 changed files with 9 additions and 3 deletions

View File

@ -755,15 +755,21 @@ func TestStreamTranslator_WebSocketServerErrors(t *testing.T) {
t.Errorf("expected websocket bad handshake error, got (%s)", err)
}
}
// Validate the streamtranslator metrics; should have one 500 failure.
// Validate the streamtranslator metrics; should have one 400 failure.
// Use polling to wait for the metric to be updated asynchronously.
metricNames := []string{"apiserver_stream_translator_requests_total"}
expected := `
# HELP apiserver_stream_translator_requests_total [ALPHA] Total number of requests that were handled by the StreamTranslatorProxy, which processes streaming RemoteCommand/V5
# TYPE apiserver_stream_translator_requests_total counter
apiserver_stream_translator_requests_total{code="400"} 1
`
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), metricNames...); err != nil {
t.Fatal(err)
if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 2*time.Second, true, func(ctx context.Context) (bool, error) {
if testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), metricNames...) == nil {
return true, nil
}
return false, nil
}); err != nil {
t.Fatalf("Failed to observe metric after waiting 2 seconds: %v", err)
}
}