...

Source file src/k8s.io/kubernetes/test/utils/ktesting/examples/logging/example_test.go

Documentation: k8s.io/kubernetes/test/utils/ktesting/examples/logging

     1  //go:build example
     2  // +build example
     3  
     4  /*
     5  Copyright 2023 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package logging
    21  
    22  // The tests below will fail and therefore are excluded from
    23  // normal "make test" via the "example" build tag. To run
    24  // the tests and check the output, use "go test -tags example ."
    25  
    26  import (
    27  	"testing"
    28  
    29  	"k8s.io/kubernetes/test/utils/ktesting"
    30  )
    31  
    32  func TestError(t *testing.T) {
    33  	tCtx := ktesting.Init(t)
    34  	tCtx.Error("some", "thing")
    35  }
    36  
    37  func TestErrorf(t *testing.T) {
    38  	tCtx := ktesting.Init(t)
    39  	tCtx.Errorf("some %s", "thing")
    40  }
    41  
    42  func TestFatal(t *testing.T) {
    43  	tCtx := ktesting.Init(t)
    44  	tCtx.Fatal("some", "thing")
    45  	tCtx.Log("not reached")
    46  }
    47  
    48  func TestFatalf(t *testing.T) {
    49  	tCtx := ktesting.Init(t)
    50  	tCtx.Fatalf("some %s", "thing")
    51  	tCtx.Log("not reached")
    52  }
    53  
    54  func TestInfo(t *testing.T) {
    55  	tCtx := ktesting.Init(t)
    56  	tCtx.Log("hello via Log")
    57  	tCtx.Logger().Info("hello via Info")
    58  	tCtx.Error("some", "thing")
    59  }
    60  
    61  func TestWithStep(t *testing.T) {
    62  	tCtx := ktesting.Init(t)
    63  	bake(ktesting.WithStep(tCtx, "bake cake"))
    64  }
    65  
    66  func bake(tCtx ktesting.TContext) {
    67  	heatOven(ktesting.WithStep(tCtx, "set heat for baking"))
    68  }
    69  
    70  func heatOven(tCtx ktesting.TContext) {
    71  	tCtx.Log("Log()")
    72  	tCtx.Logger().Info("Logger().Info()")
    73  	tCtx.Fatal("oven not found")
    74  }
    75  

View as plain text