github.com/slackhq/nebula@v1.9.0/util/error_test.go (about)

     1  package util
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  type m map[string]interface{}
    13  
    14  type TestLogWriter struct {
    15  	Logs []string
    16  }
    17  
    18  func NewTestLogWriter() *TestLogWriter {
    19  	return &TestLogWriter{Logs: make([]string, 0)}
    20  }
    21  
    22  func (tl *TestLogWriter) Write(p []byte) (n int, err error) {
    23  	tl.Logs = append(tl.Logs, string(p))
    24  	return len(p), nil
    25  }
    26  
    27  func (tl *TestLogWriter) Reset() {
    28  	tl.Logs = tl.Logs[:0]
    29  }
    30  
    31  func TestContextualError_Log(t *testing.T) {
    32  	l := logrus.New()
    33  	l.Formatter = &logrus.TextFormatter{
    34  		DisableTimestamp: true,
    35  		DisableColors:    true,
    36  	}
    37  
    38  	tl := NewTestLogWriter()
    39  	l.Out = tl
    40  
    41  	// Test a full context line
    42  	tl.Reset()
    43  	e := NewContextualError("test message", m{"field": "1"}, errors.New("error"))
    44  	e.Log(l)
    45  	assert.Equal(t, []string{"level=error msg=\"test message\" error=error field=1\n"}, tl.Logs)
    46  
    47  	// Test a line with an error and msg but no fields
    48  	tl.Reset()
    49  	e = NewContextualError("test message", nil, errors.New("error"))
    50  	e.Log(l)
    51  	assert.Equal(t, []string{"level=error msg=\"test message\" error=error\n"}, tl.Logs)
    52  
    53  	// Test just a context and fields
    54  	tl.Reset()
    55  	e = NewContextualError("test message", m{"field": "1"}, nil)
    56  	e.Log(l)
    57  	assert.Equal(t, []string{"level=error msg=\"test message\" field=1\n"}, tl.Logs)
    58  
    59  	// Test just a context
    60  	tl.Reset()
    61  	e = NewContextualError("test message", nil, nil)
    62  	e.Log(l)
    63  	assert.Equal(t, []string{"level=error msg=\"test message\"\n"}, tl.Logs)
    64  
    65  	// Test just an error
    66  	tl.Reset()
    67  	e = NewContextualError("", nil, errors.New("error"))
    68  	e.Log(l)
    69  	assert.Equal(t, []string{"level=error error=error\n"}, tl.Logs)
    70  }
    71  
    72  func TestLogWithContextIfNeeded(t *testing.T) {
    73  	l := logrus.New()
    74  	l.Formatter = &logrus.TextFormatter{
    75  		DisableTimestamp: true,
    76  		DisableColors:    true,
    77  	}
    78  
    79  	tl := NewTestLogWriter()
    80  	l.Out = tl
    81  
    82  	// Test ignoring fallback context
    83  	tl.Reset()
    84  	e := NewContextualError("test message", m{"field": "1"}, errors.New("error"))
    85  	LogWithContextIfNeeded("This should get thrown away", e, l)
    86  	assert.Equal(t, []string{"level=error msg=\"test message\" error=error field=1\n"}, tl.Logs)
    87  
    88  	// Test using fallback context
    89  	tl.Reset()
    90  	err := fmt.Errorf("this is a normal error")
    91  	LogWithContextIfNeeded("Fallback context woo", err, l)
    92  	assert.Equal(t, []string{"level=error msg=\"Fallback context woo\" error=\"this is a normal error\"\n"}, tl.Logs)
    93  }
    94  
    95  func TestContextualizeIfNeeded(t *testing.T) {
    96  	// Test ignoring fallback context
    97  	e := NewContextualError("test message", m{"field": "1"}, errors.New("error"))
    98  	assert.Same(t, e, ContextualizeIfNeeded("should be ignored", e))
    99  
   100  	// Test using fallback context
   101  	err := fmt.Errorf("this is a normal error")
   102  	cErr := ContextualizeIfNeeded("Fallback context woo", err)
   103  
   104  	switch v := cErr.(type) {
   105  	case *ContextualError:
   106  		assert.Equal(t, err, v.RealError)
   107  	default:
   108  		t.Error("Error was not wrapped")
   109  		t.Fail()
   110  	}
   111  }