1package log
2
3import (
4	"bytes"
5	"context"
6	"testing"
7
8	"github.com/stretchr/testify/require"
9	"gitlab.com/gitlab-org/labkit/correlation"
10)
11
12func TestContextLogger(t *testing.T) {
13	tests := []struct {
14		name          string
15		correlationID string
16		matchRegExp   string
17	}{
18		{
19			name:          "none",
20			correlationID: "",
21			matchRegExp:   `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*level=info msg=Hello correlation_id=\n$`,
22		},
23		{
24			name:          "set",
25			correlationID: "123456",
26			matchRegExp:   `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*level=info msg=Hello correlation_id=123456\n$`,
27		},
28	}
29
30	for _, tt := range tests {
31		t.Run(tt.name, func(t *testing.T) {
32			buf := &bytes.Buffer{}
33			closer, err := Initialize(WithWriter(buf))
34			require.NoError(t, err)
35			defer closer.Close()
36
37			ctx := context.Background()
38			if tt.correlationID != "" {
39				ctx = correlation.ContextWithCorrelation(ctx, tt.correlationID)
40			}
41
42			ContextLogger(ctx).Info("Hello")
43			require.Regexp(t, tt.matchRegExp, buf.String())
44		})
45	}
46}
47
48func TestWithContextFields(t *testing.T) {
49	tests := []struct {
50		name          string
51		correlationID string
52		fields        Fields
53		matchRegExp   string
54	}{
55		{
56			name:          "none",
57			correlationID: "",
58			fields:        nil,
59			matchRegExp:   `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*level=info msg=Hello correlation_id=\n$`,
60		},
61		{
62			name:          "single",
63			correlationID: "123456",
64			fields:        Fields{"field": "value"},
65			matchRegExp:   `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*level=info msg=Hello correlation_id=123456 field=value\n$`,
66		},
67		{
68			name:          "multiple",
69			correlationID: "123456",
70			fields:        Fields{"field": "value", "field2": "value2"},
71			matchRegExp:   `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.*level=info msg=Hello correlation_id=123456 field=value field2=value2\n$`,
72		},
73	}
74
75	for _, tt := range tests {
76		t.Run(tt.name, func(t *testing.T) {
77			buf := &bytes.Buffer{}
78			closer, err := Initialize(WithWriter(buf))
79			require.NoError(t, err)
80			defer closer.Close()
81
82			ctx := context.Background()
83			if tt.correlationID != "" {
84				ctx = correlation.ContextWithCorrelation(ctx, tt.correlationID)
85			}
86
87			WithContextFields(ctx, tt.fields).Info("Hello")
88			require.Regexp(t, tt.matchRegExp, buf.String())
89		})
90	}
91}
92
93func TestContextFields(t *testing.T) {
94	tests := []struct {
95		name          string
96		correlationID string
97		fields        Fields
98	}{
99		{
100			name:          "none",
101			correlationID: "",
102			fields:        Fields{correlation.FieldName: ""},
103		},
104		{
105			name:          "single",
106			correlationID: "123456",
107			fields:        Fields{correlation.FieldName: "123456"},
108		},
109	}
110
111	for _, tt := range tests {
112		t.Run(tt.name, func(t *testing.T) {
113			ctx := context.Background()
114			if tt.correlationID != "" {
115				ctx = correlation.ContextWithCorrelation(ctx, tt.correlationID)
116			}
117
118			got := ContextFields(ctx)
119			require.Equal(t, tt.fields, got)
120		})
121	}
122}
123