/
options_test.go
201 lines (170 loc) · 5.24 KB
/
options_test.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package tflog_test
import (
"bytes"
"context"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-log/internal/loggertest"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
func testSubsystemTraceHelper(ctx context.Context, message string) {
tflog.SubsystemTrace(ctx, testSubsystem, message)
}
func TestWithAdditionalLocationOffset(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
additionalLocationOffset int
logImpl func(context.Context)
expectedOutput []map[string]interface{}
}{
"0-no-helper": {
additionalLocationOffset: 0,
logImpl: func(ctx context.Context) {
tflog.SubsystemTrace(ctx, testSubsystem, "test message")
},
expectedOutput: []map[string]interface{}{
{
// Caller line (number after colon) should match
// tflog.SubsystemTrace() line in test case implementation.
"@caller": "/tflog/options_test.go:30",
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystemModule,
},
},
},
"0-one-helper": {
additionalLocationOffset: 0,
logImpl: func(ctx context.Context) {
testSubsystemTraceHelper(ctx, "test message")
},
expectedOutput: []map[string]interface{}{
{
// Caller line (number after colon) should match
// tflog.SubsystemTrace() line in testSubsystemTraceHelper
// function implementation.
"@caller": "/tflog/options_test.go:16",
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystemModule,
},
},
},
"1-one-helper": {
additionalLocationOffset: 1,
logImpl: func(ctx context.Context) {
testSubsystemTraceHelper(ctx, "test message")
},
expectedOutput: []map[string]interface{}{
{
// Caller line (number after colon) should match
// testSubsystemTraceHelper() line in test case
// implementation.
"@caller": "/tflog/options_test.go:63",
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystemModule,
},
},
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
var outputBuffer bytes.Buffer
ctx := context.Background()
ctx = loggertest.ProviderRootWithLocation(ctx, &outputBuffer)
ctx = tflog.NewSubsystem(ctx, testSubsystem, tflog.WithAdditionalLocationOffset(testCase.additionalLocationOffset))
testCase.logImpl(ctx)
got, err := loggertest.MultilineJSONDecode(&outputBuffer)
if err != nil {
t.Fatalf("unable to read multiple line JSON: %s", err)
}
// Strip non-deterministic caller information up to this package, e.g.
// /Users/example/src/github.com/hashicorp/terraform-plugin-log/tflog/...
for _, gotEntry := range got {
caller, ok := gotEntry["@caller"].(string)
if !ok {
continue
}
packageIndex := strings.Index(caller, "/tflog/")
if packageIndex == -1 {
continue
}
gotEntry["@caller"] = caller[packageIndex:]
}
if diff := cmp.Diff(testCase.expectedOutput, got); diff != "" {
t.Errorf("unexpected output difference: %s", diff)
}
})
}
}
func TestWithRootFields(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
logMessage string
rootFields map[string]interface{}
subsystemFields map[string]interface{}
expectedOutput []map[string]interface{}
}{
"no-root-log-fields": {
subsystemFields: map[string]interface{}{
"test-subsystem-key": "test-subsystem-value",
},
logMessage: "test message",
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystemModule,
"test-subsystem-key": "test-subsystem-value",
},
},
},
"with-root-log-fields": {
subsystemFields: map[string]interface{}{
"test-subsystem-key": "test-subsystem-value",
},
logMessage: "test message",
rootFields: map[string]interface{}{
"test-root-key": "test-root-value",
},
expectedOutput: []map[string]interface{}{
{
"@level": hclog.Trace.String(),
"@message": "test message",
"@module": testSubsystemModule,
"test-root-key": "test-root-value",
"test-subsystem-key": "test-subsystem-value",
},
},
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
var outputBuffer bytes.Buffer
ctx := context.Background()
ctx = loggertest.ProviderRoot(ctx, &outputBuffer)
for key, value := range testCase.rootFields {
ctx = tflog.With(ctx, key, value)
}
ctx = tflog.NewSubsystem(ctx, testSubsystem, tflog.WithRootFields())
for key, value := range testCase.subsystemFields {
ctx = tflog.SubsystemWith(ctx, testSubsystem, key, value)
}
tflog.SubsystemTrace(ctx, testSubsystem, testCase.logMessage)
got, err := loggertest.MultilineJSONDecode(&outputBuffer)
if err != nil {
t.Fatalf("unable to read multiple line JSON: %s", err)
}
if diff := cmp.Diff(testCase.expectedOutput, got); diff != "" {
t.Errorf("unexpected output difference: %s", diff)
}
})
}
}