github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/util/logs/vzlog/vzlog_test.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package vzlog
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	vzlogInit "github.com/verrazzano/verrazzano-monitoring-operator/pkg/util/logs"
    13  	kzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
    14  
    15  	"go.uber.org/zap"
    16  )
    17  
    18  type fakeLogger struct {
    19  	expectedMsg string
    20  	actualMsg   string
    21  	count       int
    22  }
    23  
    24  // var _ SugaredLogger = fakeLogger{}
    25  
    26  // TestLog tests the ProgressLogger function periodic logging
    27  // GIVEN a ProgressLogger with a frequency of 3 seconds
    28  // WHEN log is called 5 times in 5 seconds to log the same message
    29  // THEN ensure that 2 messages are logged
    30  func TestLog(t *testing.T) {
    31  	msg := "test1"
    32  	logger := fakeLogger{expectedMsg: msg}
    33  	const rKey = "testns/test"
    34  	rl := EnsureContext(rKey)
    35  	l := rl.EnsureLogger("comp1", &logger, zap.S()).SetFrequency(3)
    36  
    37  	// 5 calls to log should result in only 2 log messages being written
    38  	// since the frequency is 3 secs
    39  	for i := 0; i < 5; i++ {
    40  		l.Progress(msg)
    41  		time.Sleep(time.Duration(1) * time.Second)
    42  	}
    43  	assert.Equal(t, 2, logger.count)
    44  	assert.Equal(t, logger.actualMsg, logger.expectedMsg)
    45  	DeleteLogContext(rKey)
    46  }
    47  
    48  // TestLogRepeat tests the ProgressLogger function ignore repeated logs
    49  // GIVEN a ProgressLogger with a frequency of 2 seconds
    50  // WHEN log is called 5 times with 1 message and no sleep
    51  // THEN ensure that 1 message is logged
    52  func TestLogRepeat(t *testing.T) {
    53  	msg := "test1"
    54  	logger := fakeLogger{expectedMsg: msg}
    55  	const rKey = "testns/test2"
    56  	rl := EnsureContext(rKey)
    57  	l := rl.EnsureLogger("comp1", &logger, zap.S()).SetFrequency(2)
    58  
    59  	// Calls to log should result in only 2 log messages being written
    60  	l.Progress(msg)
    61  	l.Progress(msg)
    62  	l.Progress(msg)
    63  	l.Progress(msg)
    64  	l.Progress(msg)
    65  	assert.Equal(t, 1, logger.count)
    66  	assert.Equal(t, logger.actualMsg, msg)
    67  	DeleteLogContext(rKey)
    68  }
    69  
    70  // TestHistory tests the ProgressLogger function ignore previous progrsss messages
    71  // GIVEN a ProgressLogger with a frequency of 2 seconds
    72  // WHEN log is called 5 times with 2 message using repeats, and no sleep
    73  // THEN ensure that 2 messages is logged
    74  func TestHistory(t *testing.T) {
    75  	msg := "test1"
    76  	msg2 := "test2"
    77  	logger := fakeLogger{expectedMsg: msg}
    78  	const rKey = "testns/test2"
    79  	rl := EnsureContext(rKey)
    80  	l := rl.EnsureLogger("comp1", &logger, zap.S()).SetFrequency(2)
    81  
    82  	// Calls to log should result in only 2 log messages being written
    83  	l.Progress(msg)
    84  	l.Progress(msg)
    85  	l.Progress(msg)
    86  	l.Progress(msg2)
    87  	l.Progress(msg2)
    88  	l.Progress(msg)
    89  	l.Progress(msg2)
    90  	l.Progress(msg)
    91  	assert.Equal(t, 2, logger.count)
    92  	assert.Equal(t, logger.actualMsg, msg2)
    93  	DeleteLogContext(rKey)
    94  }
    95  
    96  // TestHistoryOnce tests the ProgressLogger function ignore previous once messages
    97  // GIVEN a ProgressLogger with a frequency of 2 seconds
    98  // WHEN log is called 5 times with 2 message using repeats, and no sleep
    99  // THEN ensure that 2 messages is logged
   100  func TestHistoryOnce(t *testing.T) {
   101  	msg := "test1"
   102  	msg2 := "test2"
   103  	logger := fakeLogger{expectedMsg: msg}
   104  	const rKey = "testns/test2"
   105  	rl := EnsureContext(rKey)
   106  	l := rl.EnsureLogger("comp1", &logger, zap.S())
   107  
   108  	// Calls to log should result in only 2 log messages being written
   109  	l.Once(msg)
   110  	l.Once(msg)
   111  	l.Once(msg2)
   112  	l.Once(msg)
   113  	l.Once(msg2)
   114  	l.Once(msg)
   115  	assert.Equal(t, 2, logger.count)
   116  	assert.Equal(t, logger.actualMsg, msg2)
   117  	DeleteLogContext(rKey)
   118  }
   119  
   120  // TestLogNewMsg tests the ProgressLogger function periodic logging
   121  // GIVEN a ProgressLogger with a frequency of 2 seconds
   122  // WHEN log is called 5 times with 2 different message
   123  // THEN ensure that 2 messages are logged
   124  func TestLogNewMsg(t *testing.T) {
   125  	msg := "test1"
   126  	msg2 := "test2"
   127  	logger := fakeLogger{expectedMsg: msg}
   128  	const rKey = "testns/test2"
   129  	rl := EnsureContext(rKey)
   130  	l := rl.EnsureLogger("comp1", &logger, zap.S()).SetFrequency(2)
   131  
   132  	// Calls to log should result in only 2 log messages being written
   133  	l.Progress(msg)
   134  	l.Progress(msg)
   135  	l.Progress(msg)
   136  	l.Progress(msg2)
   137  	l.Progress(msg2)
   138  	assert.Equal(t, 2, logger.count)
   139  	assert.Equal(t, logger.actualMsg, msg2)
   140  	DeleteLogContext(rKey)
   141  }
   142  
   143  // TestLogFormat tests the ProgressLogger function message formatting
   144  // GIVEN a ProgressLogger
   145  // WHEN log.Infof is called with a string and a template
   146  // THEN ensure that the message is formatted correctly and logged
   147  func TestLogFormat(t *testing.T) {
   148  	template := "test %s"
   149  	inStr := "foo"
   150  	logger := fakeLogger{}
   151  	logger.expectedMsg = fmt.Sprintf(template, inStr)
   152  	const rKey = "testns/test3"
   153  	rl := EnsureContext(rKey)
   154  	l := rl.EnsureLogger("comp1", &logger, zap.S())
   155  	l.Progressf(template, inStr)
   156  	assert.Equal(t, 1, logger.count)
   157  	assert.Equal(t, logger.actualMsg, logger.expectedMsg)
   158  	DeleteLogContext(rKey)
   159  }
   160  
   161  // TestMultipleContexts tests the EnsureContext and DeleteLogContext
   162  // WHEN EnsureContext is called multiple times
   163  // THEN ensure that the context map has an entry for each context and that
   164  //
   165  //	the context map is empty when they the contexts are deleted
   166  func TestMultipleContexts(t *testing.T) {
   167  	const rKey1 = "k1"
   168  	const rKey2 = "k2"
   169  	c1 := EnsureContext(rKey1)
   170  	c2 := EnsureContext(rKey2)
   171  
   172  	assert.Equal(t, 2, len(LogContextMap))
   173  	c1Actual := LogContextMap[rKey1]
   174  	assert.Equal(t, c1, c1Actual)
   175  	c2Actual := LogContextMap[rKey2]
   176  	assert.Equal(t, c2, c2Actual)
   177  	DeleteLogContext(rKey1)
   178  	DeleteLogContext(rKey2)
   179  	assert.Equal(t, 0, len(LogContextMap))
   180  }
   181  
   182  // TestZap tests the zap SugaredLogger
   183  // GIVEN a zap SugaredLogger
   184  // WHEN EnsureContext is called with the SugaredLogger
   185  // THEN ensure that the ProgressMessage can be called
   186  func TestZap(t *testing.T) {
   187  	testOpts := kzap.Options{}
   188  	testOpts.Development = true
   189  	testOpts.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
   190  	vzlogInit.InitLogs(testOpts)
   191  	const rKey = "testns/test3"
   192  	l := EnsureContext(rKey).EnsureLogger("test", zap.S(), zap.S())
   193  	l.Progress("testmsg")
   194  	DeleteLogContext(rKey)
   195  }
   196  
   197  // SetZapLogger gets the zap logger
   198  func (l *fakeLogger) SetZapLogger(zap *zap.SugaredLogger) {
   199  }
   200  
   201  // GetZapLogger gets the zap logger
   202  func (l *fakeLogger) GetZapLogger() *zap.SugaredLogger {
   203  	return nil
   204  }
   205  
   206  func (l *fakeLogger) Info(args ...interface{}) {
   207  	s := fmt.Sprint(args...)
   208  	l.actualMsg = s
   209  	l.count = l.count + 1
   210  	fmt.Println(s)
   211  }
   212  
   213  // Infof formats a message and logs it
   214  func (l *fakeLogger) Infof(template string, args ...interface{}) {
   215  	s := fmt.Sprintf(template, args...)
   216  	l.Info(s)
   217  }
   218  
   219  // Debug is a wrapper for SugaredLogger Debug
   220  func (l *fakeLogger) Debug(args ...interface{}) {
   221  }
   222  
   223  // Debugf is a wrapper for SugaredLogger Debugf
   224  func (l *fakeLogger) Debugf(template string, args ...interface{}) {
   225  }
   226  
   227  // Error is a wrapper for SugaredLogger Error
   228  func (l *fakeLogger) Error(args ...interface{}) {
   229  }
   230  
   231  // Errorf is a wrapper for SugaredLogger Errorf
   232  func (l *fakeLogger) Errorf(template string, args ...interface{}) {
   233  }