...

Source file src/k8s.io/component-base/logs/example/test/logger_test.go

Documentation: k8s.io/component-base/logs/example/test

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package logger_test demonstrates how to write unit tests with per-test
    18  // output. Per-test output only works for code which supports contextual
    19  // logging.
    20  package logger_test
    21  
    22  import (
    23  	"flag"
    24  	"testing"
    25  
    26  	"k8s.io/component-base/logs/example"
    27  	"k8s.io/klog/v2/ktesting"
    28  	// This import could be used to add command line flags for per-test
    29  	// output with a default verbosity of 5. This example instead
    30  	// uses a TestMain where the default verbosity gets lowered.
    31  	// "k8s.io/klog/v2/ktesting/init"
    32  )
    33  
    34  func TestLogger(t *testing.T) {
    35  	// Produce some output in two different tests which run in parallel.
    36  	for _, testcase := range []string{"abc", "xyz"} {
    37  		t.Run(testcase, func(t *testing.T) {
    38  			t.Parallel()
    39  			_ /* logger */, ctx := ktesting.NewTestContext(t)
    40  			example.Run(ctx)
    41  		})
    42  	}
    43  }
    44  
    45  func TestMain(m *testing.M) {
    46  	// Run with verbosity 2, this is the default log level in production.
    47  	// The name of the flags also could be customized here. The defaults
    48  	// are -testing.v and -testing.vmodule.
    49  	ktesting.DefaultConfig = ktesting.NewConfig(ktesting.Verbosity(2))
    50  	ktesting.DefaultConfig.AddFlags(flag.CommandLine)
    51  	flag.Parse()
    52  	m.Run()
    53  }
    54  

View as plain text