Files
nvke/controller/internal/server/logging_test.go
Mark Stillwell add23211d1 fix(controller): log API rejection reasons (#1825)
* fix(controller): log API rejection reasons

Add stable reason codes to trace-aware request completion logs for every 4xx response, with more specific codes for OpenAPI parsing, validation, and managed-by policy rejections. Avoid copying client-controlled response text into persistent logs.

NKX-18496

Created with AI assistance

Signed-off-by: Mark Stillwell <mstillwell@nvidia.com>

* fix(controller): classify cluster API rejections

Add safe, stable request-log reasons for duplicate cluster names and invalid cluster network input without persisting client-provided error text.

NKX-18496

Created with AI assistance

Signed-off-by: Mark Stillwell <mstillwell@nvidia.com>

* fix(controller): preserve traced request route

* docs(controller): clarify middleware ordering

Signed-off-by: Mark Stillwell <mstillwell@nvidia.com>

* DCO Remediation Commit for Mark Stillwell <mstillwell@nvidia.com>

I, Mark Stillwell <mstillwell@nvidia.com>, hereby add my Signed-off-by to this commit: be1e227e82

Signed-off-by: Mark Stillwell <mstillwell@nvidia.com>

* DCO Remediation Commit for Mark Stillwell <mstillwell@nvidia.com>

I, Mark Stillwell <mstillwell@nvidia.com>, hereby add my Signed-off-by to this commit: 281cc9a254

Signed-off-by: Mark Stillwell <mstillwell@nvidia.com>

---------

Signed-off-by: Mark Stillwell <mstillwell@nvidia.com>
2026-10-21 10:18:38 +00:00

107 lines
3.3 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestHTTPOutcome(t *testing.T) {
tests := []struct {
status int
want string
}{
{http.StatusOK, "succeeded"},
{http.StatusCreated, "succeeded"},
{http.StatusNotModified, "succeeded"},
{http.StatusBadRequest, "rejected"},
{http.StatusNotFound, "rejected"},
{http.StatusConflict, "rejected"},
{http.StatusInternalServerError, "failed"},
{http.StatusBadGateway, "failed"},
}
for _, tt := range tests {
t.Run(http.StatusText(tt.status), func(t *testing.T) {
require.Equal(t, tt.want, httpOutcome(tt.status))
})
}
}
func TestHTTPRejectionReason(t *testing.T) {
tests := []struct {
status int
want string
}{
{http.StatusOK, ""},
{http.StatusBadRequest, "bad_request"},
{http.StatusUnauthorized, "unauthorized"},
{http.StatusForbidden, "forbidden"},
{http.StatusNotFound, "not_found"},
{http.StatusConflict, "conflict"},
{http.StatusUnprocessableEntity, "unprocessable_entity"},
{http.StatusTooManyRequests, "too_many_requests"},
{http.StatusTeapot, "client_error"},
{http.StatusInternalServerError, ""},
}
for _, tt := range tests {
t.Run(http.StatusText(tt.status), func(t *testing.T) {
require.Equal(t, tt.want, httpRejectionReason(tt.status))
})
}
}
func TestResponseWriterRetainsFirstFinalStatus(t *testing.T) {
recorder := httptest.NewRecorder()
state := &requestLogState{}
writer := responseWriter{ResponseWriter: recorder, status: http.StatusOK, requestLogState: state}
writer.WriteHeader(http.StatusConflict)
writer.WriteHeader(http.StatusEarlyHints)
writer.WriteHeader(http.StatusInternalServerError)
require.Equal(t, http.StatusConflict, recorder.Code)
require.Equal(t, http.StatusConflict, writer.status)
require.Equal(t, "conflict", state.rejectionReason)
}
func TestResponseWriterTreatsSwitchingProtocolsAsFinal(t *testing.T) {
recorder := httptest.NewRecorder()
writer := responseWriter{ResponseWriter: recorder, status: http.StatusOK, requestLogState: &requestLogState{}}
writer.WriteHeader(http.StatusSwitchingProtocols)
writer.WriteHeader(http.StatusOK)
require.Equal(t, http.StatusSwitchingProtocols, recorder.Code)
require.Equal(t, http.StatusSwitchingProtocols, writer.status)
}
func TestResponseWriterRetainsImplicitSuccessStatus(t *testing.T) {
recorder := httptest.NewRecorder()
state := &requestLogState{}
writer := responseWriter{ResponseWriter: recorder, status: http.StatusOK, requestLogState: state}
_, err := writer.Write([]byte("ok"))
require.NoError(t, err)
writer.WriteHeader(http.StatusConflict)
require.Equal(t, http.StatusOK, recorder.Code)
require.Equal(t, http.StatusOK, writer.status)
require.Empty(t, state.rejectionReason)
}
func TestResponseWriterRetainsSpecificRejectionReason(t *testing.T) {
recorder := httptest.NewRecorder()
state := &requestLogState{}
writer := responseWriter{ResponseWriter: recorder, status: http.StatusOK, requestLogState: state}
writer.recordRejectionReason(rejectionReasonInvalidClusterNetwork)
writer.WriteHeader(http.StatusBadRequest)
require.Equal(t, rejectionReasonInvalidClusterNetwork, state.rejectionReason)
}