test

package
v2.0.0-...-3f901c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package test contains a reusable unit test for logging output and behavior.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Benchmark

func Benchmark(b *testing.B, config OutputConfig)

Benchmark covers various special cases of emitting log output. It can be used for arbitrary logr.Logger implementations.

Loggers will be tested with direct calls to Info or as backend for plog.

func InitKlog

func InitKlog(tb testing.TB) *flag.FlagSet

InitKlog must be called in a test to configure klog for testing. The previous klog configuration will be restored automatically after the test.

The returned flag set has the klog flags registered. It can be used to make further changes to the klog configuration.

func Output

func Output(t *testing.T, config OutputConfig)

Output covers various special cases of emitting log output. It can be used for arbitrary logr.Logger implementations.

The expected output is what klog would print. When testing loggers that emit different output, a mapping from klog output to the corresponding logger output must be provided, otherwise the test will compare against the expected klog output.

Loggers will be tested with direct calls to Info or as backend for plog.

func ZaprOutputMappingDirect

func ZaprOutputMappingDirect() map[string]string

ZaprOutputMappingDirect provides a mapping from klog output to the corresponding zapr output when zapr is called directly.

func ZaprOutputMappingIndirect

func ZaprOutputMappingIndirect() map[string]string

ZaprOutputMappingIndirect provides a mapping from klog output to the corresponding zapr output when zapr is called indirectly through plog.

This is different from ZaprOutputMappingDirect because:

  • WithName gets added to the message by Output.
  • zap uses . as separator instead of / between WithName values, here we get slashes because Output concatenates these values.
  • WithValues are added to the normal key/value parameters by Output, which puts them after "v".
  • Output does that without emitting the warning that we get from zapr.
  • zap drops keys with missing values, here we get "(MISSING)".
  • zap does not de-duplicate key/value pairs, here klog does that for it.

Types

type OutputConfig

type OutputConfig struct {
	// NewLogger is called to create a new logger. If nil, output via klog
	// is tested. Support for -vmodule is optional.  ClearLogger is called
	// after each test, therefore it is okay to user SetLogger without
	// undoing that in the callback.
	NewLogger func(out io.Writer, v int, vmodule string) logr.Logger

	// AsBackend enables testing through klog and the logger set there with
	// SetLogger.
	AsBackend bool

	// ExpectedOutputMapping replaces the builtin expected output for test
	// cases with something else. If nil or a certain case is not present,
	// the original text is used.
	//
	// The expected output uses <LINE> as a placeholder for the line of the
	// log call. The source code is always the output.go file itself. When
	// testing a logger directly, <WITH-VALUES-LINE> is used for the first
	// WithValues call, <WITH-VALUES-LINE-2> for a second and
	// <WITH-VALUES-LINE-3> for a third.
	ExpectedOutputMapping map[string]string

	// SupportsVModule indicates that the logger supports the vmodule
	// parameter. Ignored when logging through plog.
	SupportsVModule bool
}

OutputConfig contains optional settings for Output.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL