-
Notifications
You must be signed in to change notification settings - Fork 90
/
context_test.go
49 lines (37 loc) · 1.18 KB
/
context_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
package logging_test
import (
"bytes"
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
"github.com/hashicorp/terraform-plugin-log/tfsdklogtest"
)
func TestInitContext(t *testing.T) {
t.Parallel()
var output bytes.Buffer
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
// Simulate root logger fields that would have been associated by
// terraform-plugin-go prior to the InitContext() call.
ctx = tfsdklog.With(ctx, "tf_rpc", "GetProviderSchema")
ctx = tfsdklog.With(ctx, "tf_req_id", "123-testing-123")
ctx = logging.InitContext(ctx)
logging.FrameworkTrace(ctx, "test message")
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
if err != nil {
t.Fatalf("unable to read multiple line JSON: %s", err)
}
expectedEntries := []map[string]interface{}{
{
"@level": "trace",
"@message": "test message",
"@module": "sdk.framework",
"tf_rpc": "GetProviderSchema",
"tf_req_id": "123-testing-123",
},
}
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
}