summaryrefslogtreecommitdiff
path: root/workhorse/internal/log/logging_test.go
blob: 1cb6438532e60cde41ee73e1e8ceb2690f08aab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package log

import (
	"bytes"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/require"
)

func captureLogs(b *Builder, testFn func()) string {
	buf := &bytes.Buffer{}

	logger := b.entry.Logger
	oldOut := logger.Out
	logger.Out = buf
	defer func() {
		logger.Out = oldOut
	}()

	testFn()

	return buf.String()
}

func TestLogInfo(t *testing.T) {
	b := NewBuilder()
	logLine := captureLogs(b, func() {
		b.Info("an observation")
	})

	require.Regexp(t, `level=info msg="an observation"`, logLine)
}

func TestLogError(t *testing.T) {
	b := NewBuilder()
	logLine := captureLogs(b, func() {
		b.WithError(fmt.Errorf("the error")).Error()
	})

	require.Regexp(t, `level=error error="the error"`, logLine)
}

func TestLogErrorWithMessage(t *testing.T) {
	b := NewBuilder()
	logLine := captureLogs(b, func() {
		b.WithError(fmt.Errorf("the error")).Error("an error occurred")
	})

	require.Regexp(t, `level=error msg="an error occurred" error="the error"`, logLine)
}

func TestLogErrorWithRequest(t *testing.T) {
	tests := []struct {
		name        string
		method      string
		uri         string
		err         error
		logMatchers []string
	}{
		{
			name: "nil_request",
			err:  fmt.Errorf("cause"),
			logMatchers: []string{
				`level=error error=cause`,
			},
		},
		{
			name: "nil_request_nil_error",
			err:  nil,
			logMatchers: []string{
				`level=error error="<nil>"`,
			},
		},
		{
			name:   "basic_url",
			method: "GET",
			uri:    "http://localhost:3000/",
			err:    fmt.Errorf("cause"),
			logMatchers: []string{
				`level=error correlation_id= error=cause method=GET uri="http://localhost:3000/"`,
			},
		},
		{
			name:   "secret_url",
			method: "GET",
			uri:    "http://localhost:3000/path?certificate=123&sharedSecret=123&import_url=the_url&my_password_string=password",
			err:    fmt.Errorf("cause"),
			logMatchers: []string{
				`level=error correlation_id= error=cause method=GET uri="http://localhost:3000/path\?certificate=\[FILTERED\]&sharedSecret=\[FILTERED\]&import_url=\[FILTERED\]&my_password_string=\[FILTERED\]"`,
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			b := NewBuilder()

			var r *http.Request
			if tt.uri != "" {
				r = httptest.NewRequest(tt.method, tt.uri, nil)
			}

			logLine := captureLogs(b, func() {
				b.WithRequest(r).WithError(tt.err).Error()
			})

			for _, v := range tt.logMatchers {
				require.Regexp(t, v, logLine)
			}
		})
	}
}

func TestLogErrorWithFields(t *testing.T) {
	tests := []struct {
		name       string
		request    *http.Request
		err        error
		fields     map[string]interface{}
		logMatcher string
	}{
		{
			name:       "nil_request",
			err:        fmt.Errorf("cause"),
			fields:     map[string]interface{}{"extra_one": 123},
			logMatcher: `level=error error=cause extra_one=123`,
		},
		{
			name:       "nil_request_nil_error",
			err:        nil,
			fields:     map[string]interface{}{"extra_one": 123, "extra_two": "test"},
			logMatcher: `level=error error="<nil>" extra_one=123 extra_two=test`,
		},
		{
			name:       "basic_url",
			request:    httptest.NewRequest("GET", "http://localhost:3000/", nil),
			err:        fmt.Errorf("cause"),
			fields:     map[string]interface{}{"extra_one": 123, "extra_two": "test"},
			logMatcher: `level=error correlation_id= error=cause extra_one=123 extra_two=test method=GET uri="http://localhost:3000/`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			b := NewBuilder()

			logLine := captureLogs(b, func() {
				b.WithRequest(tt.request).WithFields(tt.fields).WithError(tt.err).Error()
			})

			require.Contains(t, logLine, tt.logMatcher)
		})
	}
}