istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/log/logr_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package log
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  
    21  	"github.com/go-logr/logr"
    22  )
    23  
    24  func runLogrTestWithScope(t *testing.T, s *Scope, f func(l logr.Logger)) []string {
    25  	lines, err := captureStdout(func() {
    26  		Configure(DefaultOptions())
    27  		l := NewLogrAdapter(s)
    28  		f(l)
    29  		_ = Sync()
    30  	})
    31  	if err != nil {
    32  		t.Fatalf("Got error '%v', expected success", err)
    33  	}
    34  	if lines[len(lines)-1] == "" {
    35  		return lines[:len(lines)-1]
    36  	}
    37  	return lines
    38  }
    39  
    40  func newScope() *Scope {
    41  	s := &Scope{name: "test"}
    42  	s.SetOutputLevel(InfoLevel)
    43  	s.SetStackTraceLevel(NoneLevel)
    44  	s.SetLogCallers(false)
    45  	return s
    46  }
    47  
    48  func runLogrTest(t *testing.T, f func(l logr.Logger)) []string {
    49  	s := newScope()
    50  	return runLogrTestWithScope(t, s, f)
    51  }
    52  
    53  func mustMatchLength(t *testing.T, l int, items []string) {
    54  	t.Helper()
    55  	if len(items) != l {
    56  		t.Fatalf("expected %v items, got %v: %v", l, len(items), items)
    57  	}
    58  }
    59  
    60  func TestLogr(t *testing.T) {
    61  	t.Run("newlines not duplicated", func(t *testing.T) {
    62  		lines := runLogrTest(t, func(l logr.Logger) {
    63  			l.Info("msg\n")
    64  		})
    65  		mustMatchLength(t, 1, lines)
    66  		mustRegexMatchString(t, lines[0], "msg")
    67  	})
    68  	t.Run("info level log is output", func(t *testing.T) {
    69  		lines := runLogrTest(t, func(l logr.Logger) {
    70  			l.Info("msg")
    71  		})
    72  		mustMatchLength(t, 1, lines)
    73  		mustRegexMatchString(t, lines[0], "msg")
    74  	})
    75  	t.Run("error level log is output", func(t *testing.T) {
    76  		lines := runLogrTest(t, func(l logr.Logger) {
    77  			l.Error(errors.New("some error"), "msg")
    78  		})
    79  		mustMatchLength(t, 1, lines)
    80  		mustRegexMatchString(t, lines[0], "some error.*msg")
    81  	})
    82  	t.Run("debug output still shows message", func(t *testing.T) {
    83  		s := newScope()
    84  		s.SetOutputLevel(DebugLevel)
    85  		lines := runLogrTestWithScope(t, s, func(l logr.Logger) {
    86  			l.Info("msg")
    87  			l.Error(errors.New("some error"), "msg")
    88  		})
    89  		mustMatchLength(t, 2, lines)
    90  		mustRegexMatchString(t, lines[0], "msg")
    91  		mustRegexMatchString(t, lines[1], "some error.*msg")
    92  	})
    93  	t.Run("warn output still shows errors", func(t *testing.T) {
    94  		s := newScope()
    95  		s.SetOutputLevel(WarnLevel)
    96  		lines := runLogrTestWithScope(t, s, func(l logr.Logger) {
    97  			l.Info("msg")
    98  			l.Error(errors.New("some error"), "msg")
    99  		})
   100  		mustMatchLength(t, 1, lines)
   101  		mustRegexMatchString(t, lines[0], "some error.*msg")
   102  	})
   103  
   104  	t.Run("info shows correct verbosity", func(t *testing.T) {
   105  		lines := runLogrTest(t, func(l logr.Logger) {
   106  			l.V(0).Info("0")
   107  			l.V(1).Info("1")
   108  			l.V(2).Info("2")
   109  			l.V(3).Info("3")
   110  			l.V(4).Info("4")
   111  
   112  			matchBool(t, true, l.V(0).Enabled())
   113  			matchBool(t, true, l.V(3).Enabled())
   114  			matchBool(t, false, l.V(4).Enabled())
   115  			matchBool(t, false, l.V(6).Enabled())
   116  		})
   117  		mustMatchLength(t, 4, lines)
   118  		mustRegexMatchString(t, lines[0], "0")
   119  		mustRegexMatchString(t, lines[1], "1")
   120  		mustRegexMatchString(t, lines[2], "2")
   121  		mustRegexMatchString(t, lines[3], "3")
   122  	})
   123  
   124  	t.Run("debug shows correct verbosity", func(t *testing.T) {
   125  		s := newScope()
   126  		s.SetOutputLevel(DebugLevel)
   127  		lines := runLogrTestWithScope(t, s, func(l logr.Logger) {
   128  			l.V(0).Info("0")
   129  			l.V(1).Info("1")
   130  			l.V(2).Info("2")
   131  			l.V(3).Info("3")
   132  			l.V(4).Info("4")
   133  
   134  			matchBool(t, true, l.V(0).Enabled())
   135  			matchBool(t, true, l.V(3).Enabled())
   136  			matchBool(t, true, l.V(4).Enabled())
   137  			matchBool(t, true, l.V(6).Enabled())
   138  		})
   139  		mustMatchLength(t, 5, lines)
   140  		mustRegexMatchString(t, lines[0], "0")
   141  		mustRegexMatchString(t, lines[1], "1")
   142  		mustRegexMatchString(t, lines[2], "2")
   143  		mustRegexMatchString(t, lines[3], "3")
   144  		mustRegexMatchString(t, lines[4], "4")
   145  	})
   146  }
   147  
   148  func matchBool(t *testing.T, want bool, got bool) {
   149  	t.Helper()
   150  	if want != got {
   151  		t.Fatalf("wanted %v got %v", want, got)
   152  	}
   153  }