client-go/rest/warnings_test.go
Patrick Ohly 2b2015d460 client-go/test: warning handler with contextual logging
The default handler now uses contextual logging. Instead of

     warnings.go:106] warning 1

it now logs the caller of client-go and uses structured, contextual
logging

     main.go:100] "Warning" message="warning 1"

Users of client-go have the choice whether the handler that they provide uses
the traditional API (no API break!) or contextual logging.

Kubernetes-commit: 48fb886325fce4b16e4067caadb7bcd3044d460f
2024-09-02 17:51:01 +02:00

58 lines
1.7 KiB
Go

/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rest
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDefaultWarningHandler(t *testing.T) {
t.Run("default", func(t *testing.T) {
assert.IsType(t, WarningHandlerWithContext(WarningLogger{}), getDefaultWarningHandler())
})
deferRestore := func(t *testing.T) {
handler := getDefaultWarningHandler()
t.Cleanup(func() {
SetDefaultWarningHandlerWithContext(handler)
})
}
t.Run("no-context", func(t *testing.T) {
deferRestore(t)
handler := &fakeWarningHandlerWithLogging{}
//nolint:logcheck
SetDefaultWarningHandler(handler)
getDefaultWarningHandler().HandleWarningHeaderWithContext(context.Background(), 0, "", "message")
assert.Equal(t, []string{"message"}, handler.messages)
SetDefaultWarningHandler(nil)
assert.Nil(t, getDefaultWarningHandler())
})
t.Run("with-context", func(t *testing.T) {
deferRestore(t)
handler := &fakeWarningHandlerWithContext{}
SetDefaultWarningHandlerWithContext(handler)
assert.Equal(t, handler, getDefaultWarningHandler())
SetDefaultWarningHandlerWithContext(nil)
assert.Nil(t, getDefaultWarningHandler())
})
}