github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/log/log_test.go (about)

     1  // Copyright 2023 Northern.tech AS
     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  package log
    15  
    16  import (
    17  	"context"
    18  	"io/ioutil"
    19  	"testing"
    20  
    21  	"github.com/sirupsen/logrus"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestNew(t *testing.T) {
    26  	l := New(Ctx{"foo": "bar"})
    27  	assert.NotNil(t, l)
    28  }
    29  
    30  func TestNewFromLogger(t *testing.T) {
    31  	baselog := logrus.New()
    32  	baselog.Level = logrus.PanicLevel
    33  	baselog.Out = ioutil.Discard
    34  
    35  	l := NewFromLogger(baselog, Ctx{})
    36  	assert.NotNil(t, l)
    37  	assert.Equal(t, l.Logger.Level, logrus.PanicLevel)
    38  	assert.Equal(t, l.Logger.Out, ioutil.Discard)
    39  }
    40  
    41  func TestSetup(t *testing.T) {
    42  	// setup with debug on
    43  	Setup(false)
    44  
    45  	l := New(Ctx{"foo": "bar"})
    46  
    47  	if l.Level() != logrus.InfoLevel {
    48  		t.Fatalf("expected info level")
    49  	}
    50  
    51  	Setup(true)
    52  
    53  	l = New(Ctx{"foo": "bar"})
    54  
    55  	if l.Level() != logrus.DebugLevel {
    56  		t.Fatalf("expected debug level")
    57  	}
    58  }
    59  
    60  func TestWithFields(t *testing.T) {
    61  
    62  	Setup(false)
    63  
    64  	l := New(Ctx{})
    65  
    66  	exp := map[string]interface{}{
    67  		"bar":    1,
    68  		"baz":    "cafe",
    69  		"module": "foo",
    70  	}
    71  	l = l.F(Ctx{
    72  		"bar": exp["bar"],
    73  		"baz": exp["baz"],
    74  	})
    75  
    76  	if len(l.Data) != len(exp)-1 {
    77  		t.Fatalf("log fields number mismatch: expected %v got %v",
    78  			len(exp), len(l.Data))
    79  	}
    80  
    81  	for k, v := range l.Data {
    82  		ev, ok := exp[k]
    83  		if ok != true {
    84  			t.Fatalf("unexpected key: %s", k)
    85  		}
    86  		if ev != v {
    87  			t.Fatalf("value mismatch: got %+v expected %+v",
    88  				v, ev)
    89  		}
    90  	}
    91  }
    92  
    93  func TestFromWithContext(t *testing.T) {
    94  	ctx := context.Background()
    95  
    96  	l := New(Ctx{"foo": "bar"})
    97  
    98  	// we should get back the same logger
    99  	ln := FromContext(WithContext(ctx, l))
   100  	assert.Equal(t, l, ln)
   101  
   102  	// since we're using a background context, a new, empty logger should be
   103  	// returned
   104  	ln2 := FromContext(context.Background())
   105  	assert.NotEqual(t, ln2, ln)
   106  	assert.Len(t, ln2.Data, 0)
   107  
   108  	ctx = WithContext(context.Background(), l)
   109  	assert.NotNil(t, ctx.Value(loggerContextKey))
   110  
   111  	assert.Nil(t, ctx.Value(0))
   112  }