v2k8s.io/klog/v2/test Index | Files

package test

import "k8s.io/klog/v2/test"

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

Experimental

Notice: This package is EXPERIMENTAL and may be changed or removed in a later release.

Index

Functions

func InitKlog

func InitKlog()

InitKlog must be called once in an init function of a test package to configure klog for testing with Output.

Experimental

Notice: This function is EXPERIMENTAL and may be changed or removed in a later release.

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 klog.

Experimental

Notice: This function is EXPERIMENTAL and may be changed or removed in a later release. The test cases and thus the expected output also may change.

func ZaprOutputMappingDirect

func ZaprOutputMappingDirect() map[string]string

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

Experimental

Notice: This package is EXPERIMENTAL and may be changed or removed in a later release.

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 klog.

This is different from ZaprOutputMappingDirect because:

Experimental

Notice: This package is EXPERIMENTAL and may be changed or removed in a later release.

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 klog.
	SupportsVModule bool
}

OutputConfig contains optional settings for Output.

Experimental

Notice: This type is EXPERIMENTAL and may be changed or removed in a later release.

Source Files

output.go output_helper.go zapr.go

Version
v2.60.1
Published
Mar 20, 2022
Platform
linux/amd64
Imports
14 packages
Last checked
6 hours ago

Tools for package owners.