github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/log/mdc_test.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestContextWithMdc(t *testing.T) {
    12  	t.Run("add MDC to context without MDC", func(t2 *testing.T) {
    13  		ctx := context.Background()
    14  		if mdc := MdcFromContext(ctx); nil != mdc {
    15  			t.Fatalf("[pre-condition] There should not have been any MDC in the provided context!")
    16  		}
    17  
    18  		ctx = ContextWithMdc(ctx)
    19  		if mdc := MdcFromContext(ctx); nil == mdc {
    20  			t.Fatalf("An MDC instance should have been attached to the context")
    21  		}
    22  	})
    23  
    24  	t.Run("do not replace the MDC instance in a context", func(t2 *testing.T) {
    25  		ctx := ContextWithMdc(context.Background())
    26  
    27  		var inner *map[string]interface{}
    28  		if mdc := MdcFromContext(ctx); nil == mdc {
    29  			t.Fatalf("[pre-condition] There should have been MDC in the provided context!")
    30  		} else {
    31  			inner = &mdc.mdc
    32  		}
    33  
    34  		ctx = ContextWithMdc(ctx)
    35  		if mdc := MdcFromContext(ctx); nil == mdc {
    36  			t.Fatalf("An MDC instance should have been attached to the context")
    37  		} else {
    38  			require.Equal(t, inner, &mdc.mdc)
    39  		}
    40  	})
    41  }
    42  
    43  func TestAppendMdcToLogEntry(t *testing.T) {
    44  	t.Run("do not modify log entry on empty MDC", func(t *testing.T) {
    45  		mdc := NewMappedDiagnosticContext()
    46  		entry := logrus.WithFields(make(map[string]interface{}))
    47  		newEntry := mdc.appendFields(entry)
    48  
    49  		require.Equal(t, entry, newEntry)
    50  	})
    51  
    52  	t.Run("append MDC content to a log entry", func(t *testing.T) {
    53  		const testValue = "test-value"
    54  		const testKey = "test"
    55  
    56  		mdc := NewMappedDiagnosticContext()
    57  		mdc.Set(testKey, testValue)
    58  
    59  		entry := logrus.WithFields(make(map[string]interface{}))
    60  		newEntry := mdc.appendFields(entry)
    61  
    62  		require.NotEqual(t, entry, newEntry)
    63  
    64  		value, ok := newEntry.Data[testKey]
    65  		require.Truef(t, ok, "Missing key: %v", testKey)
    66  		require.Equal(t, testValue, value)
    67  	})
    68  }
    69  
    70  func TestMdcSetValues(t *testing.T) {
    71  	t.Run("add value to mdc", func(t *testing.T) {
    72  		const testKey = "test"
    73  		const testValue = "test-val"
    74  
    75  		mdc := NewMappedDiagnosticContext()
    76  		mdc.SetIfNotEmpty(testKey, testValue)
    77  
    78  		_, ok := mdc.mdc[testKey]
    79  		require.True(t, ok, "Missing key: %v:%v", testKey, testValue)
    80  	})
    81  
    82  	t.Run("do not add empty value to mdc", func(t *testing.T) {
    83  		const testKey = "test"
    84  
    85  		mdc := NewMappedDiagnosticContext()
    86  		mdc.SetIfNotEmpty(testKey, "")
    87  
    88  		val, ok := mdc.mdc[testKey]
    89  		require.False(t, ok, "Unexpected value: %v", val)
    90  	})
    91  }