Skip to content

Commit 68ff107

Browse files
authored
internal/logging: Add unit testing (#957)
1 parent 790d730 commit 68ff107

File tree

3 files changed

+464
-0
lines changed

3 files changed

+464
-0
lines changed

internal/logging/context_test.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package logging_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
10+
"github.com/hashicorp/terraform-plugin-log/tfsdklogtest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
12+
)
13+
14+
func TestInitContext(t *testing.T) {
15+
t.Parallel()
16+
17+
var output bytes.Buffer
18+
19+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
20+
21+
// Simulate root logger fields that would have been associated by
22+
// terraform-plugin-go prior to the InitContext() call.
23+
ctx = tfsdklog.With(ctx, "tf_rpc", "GetProviderSchema")
24+
ctx = tfsdklog.With(ctx, "tf_req_id", "123-testing-123")
25+
26+
ctx = logging.InitContext(ctx)
27+
28+
logging.HelperSchemaTrace(ctx, "test message")
29+
30+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
31+
32+
if err != nil {
33+
t.Fatalf("unable to read multiple line JSON: %s", err)
34+
}
35+
36+
expectedEntries := []map[string]interface{}{
37+
{
38+
"@level": "trace",
39+
"@message": "test message",
40+
"@module": "sdk.helper_schema",
41+
"tf_rpc": "GetProviderSchema",
42+
"tf_req_id": "123-testing-123",
43+
},
44+
}
45+
46+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
47+
t.Errorf("unexpected difference: %s", diff)
48+
}
49+
}
50+
51+
func TestTestNameContext(t *testing.T) {
52+
t.Parallel()
53+
54+
var output bytes.Buffer
55+
56+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
57+
58+
// InitTestContext messes with the standard library log package, which
59+
// we want to avoid in this unit testing. Instead, just create the
60+
// helper_resource subsystem and avoid the other InitTestContext logic.
61+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
62+
63+
ctx = logging.TestNameContext(ctx, "TestTestTest")
64+
65+
logging.HelperResourceTrace(ctx, "test message")
66+
67+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
68+
69+
if err != nil {
70+
t.Fatalf("unable to read multiple line JSON: %s", err)
71+
}
72+
73+
expectedEntries := []map[string]interface{}{
74+
{
75+
"@level": "trace",
76+
"@message": "test message",
77+
"@module": "sdk.helper_resource",
78+
"test_name": "TestTestTest",
79+
},
80+
}
81+
82+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
83+
t.Errorf("unexpected difference: %s", diff)
84+
}
85+
}
86+
87+
func TestTestStepNumberContext(t *testing.T) {
88+
t.Parallel()
89+
90+
var output bytes.Buffer
91+
92+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
93+
94+
// InitTestContext messes with the standard library log package, which
95+
// we want to avoid in this unit testing. Instead, just create the
96+
// helper_resource subsystem and avoid the other InitTestContext logic.
97+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
98+
99+
ctx = logging.TestStepNumberContext(ctx, 123)
100+
101+
logging.HelperResourceTrace(ctx, "test message")
102+
103+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
104+
105+
if err != nil {
106+
t.Fatalf("unable to read multiple line JSON: %s", err)
107+
}
108+
109+
expectedEntries := []map[string]interface{}{
110+
{
111+
"@level": "trace",
112+
"@message": "test message",
113+
"@module": "sdk.helper_resource",
114+
"test_step_number": float64(123), // float64 due to default JSON unmarshalling
115+
},
116+
}
117+
118+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
119+
t.Errorf("unexpected difference: %s", diff)
120+
}
121+
}
122+
123+
func TestTestTerraformPathContext(t *testing.T) {
124+
t.Parallel()
125+
126+
var output bytes.Buffer
127+
128+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
129+
130+
// InitTestContext messes with the standard library log package, which
131+
// we want to avoid in this unit testing. Instead, just create the
132+
// helper_resource subsystem and avoid the other InitTestContext logic.
133+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
134+
135+
ctx = logging.TestTerraformPathContext(ctx, "/usr/local/bin/terraform")
136+
137+
logging.HelperResourceTrace(ctx, "test message")
138+
139+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
140+
141+
if err != nil {
142+
t.Fatalf("unable to read multiple line JSON: %s", err)
143+
}
144+
145+
expectedEntries := []map[string]interface{}{
146+
{
147+
"@level": "trace",
148+
"@message": "test message",
149+
"@module": "sdk.helper_resource",
150+
"test_terraform_path": "/usr/local/bin/terraform",
151+
},
152+
}
153+
154+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
155+
t.Errorf("unexpected difference: %s", diff)
156+
}
157+
}
158+
159+
func TestTestWorkingDirectoryContext(t *testing.T) {
160+
t.Parallel()
161+
162+
var output bytes.Buffer
163+
164+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
165+
166+
// InitTestContext messes with the standard library log package, which
167+
// we want to avoid in this unit testing. Instead, just create the
168+
// helper_resource subsystem and avoid the other InitTestContext logic.
169+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
170+
171+
ctx = logging.TestWorkingDirectoryContext(ctx, "/tmp/test")
172+
173+
logging.HelperResourceTrace(ctx, "test message")
174+
175+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
176+
177+
if err != nil {
178+
t.Fatalf("unable to read multiple line JSON: %s", err)
179+
}
180+
181+
expectedEntries := []map[string]interface{}{
182+
{
183+
"@level": "trace",
184+
"@message": "test message",
185+
"@module": "sdk.helper_resource",
186+
"test_working_directory": "/tmp/test",
187+
},
188+
}
189+
190+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
191+
t.Errorf("unexpected difference: %s", diff)
192+
}
193+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package logging_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
10+
"github.com/hashicorp/terraform-plugin-log/tfsdklogtest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
12+
)
13+
14+
func TestHelperResourceDebug(t *testing.T) {
15+
t.Parallel()
16+
17+
var output bytes.Buffer
18+
19+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
20+
21+
// InitTestContext messes with the standard library log package, which
22+
// we want to avoid in this unit testing. Instead, just create the
23+
// helper_resource subsystem and avoid the other InitTestContext logic.
24+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
25+
26+
logging.HelperResourceDebug(ctx, "test message")
27+
28+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
29+
30+
if err != nil {
31+
t.Fatalf("unable to read multiple line JSON: %s", err)
32+
}
33+
34+
expectedEntries := []map[string]interface{}{
35+
{
36+
"@level": "debug",
37+
"@message": "test message",
38+
"@module": "sdk.helper_resource",
39+
},
40+
}
41+
42+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
43+
t.Errorf("unexpected difference: %s", diff)
44+
}
45+
}
46+
47+
func TestHelperResourceError(t *testing.T) {
48+
t.Parallel()
49+
50+
var output bytes.Buffer
51+
52+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
53+
54+
// InitTestContext messes with the standard library log package, which
55+
// we want to avoid in this unit testing. Instead, just create the
56+
// helper_resource subsystem and avoid the other InitTestContext logic.
57+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
58+
59+
logging.HelperResourceError(ctx, "test message")
60+
61+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
62+
63+
if err != nil {
64+
t.Fatalf("unable to read multiple line JSON: %s", err)
65+
}
66+
67+
expectedEntries := []map[string]interface{}{
68+
{
69+
"@level": "error",
70+
"@message": "test message",
71+
"@module": "sdk.helper_resource",
72+
},
73+
}
74+
75+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
76+
t.Errorf("unexpected difference: %s", diff)
77+
}
78+
}
79+
80+
func TestHelperResourceTrace(t *testing.T) {
81+
t.Parallel()
82+
83+
var output bytes.Buffer
84+
85+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
86+
87+
// InitTestContext messes with the standard library log package, which
88+
// we want to avoid in this unit testing. Instead, just create the
89+
// helper_resource subsystem and avoid the other InitTestContext logic.
90+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
91+
92+
logging.HelperResourceTrace(ctx, "test message")
93+
94+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
95+
96+
if err != nil {
97+
t.Fatalf("unable to read multiple line JSON: %s", err)
98+
}
99+
100+
expectedEntries := []map[string]interface{}{
101+
{
102+
"@level": "trace",
103+
"@message": "test message",
104+
"@module": "sdk.helper_resource",
105+
},
106+
}
107+
108+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
109+
t.Errorf("unexpected difference: %s", diff)
110+
}
111+
}
112+
113+
func TestHelperResourceWarn(t *testing.T) {
114+
t.Parallel()
115+
116+
var output bytes.Buffer
117+
118+
ctx := tfsdklogtest.RootLogger(context.Background(), &output)
119+
120+
// InitTestContext messes with the standard library log package, which
121+
// we want to avoid in this unit testing. Instead, just create the
122+
// helper_resource subsystem and avoid the other InitTestContext logic.
123+
ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource)
124+
125+
logging.HelperResourceWarn(ctx, "test message")
126+
127+
entries, err := tfsdklogtest.MultilineJSONDecode(&output)
128+
129+
if err != nil {
130+
t.Fatalf("unable to read multiple line JSON: %s", err)
131+
}
132+
133+
expectedEntries := []map[string]interface{}{
134+
{
135+
"@level": "warn",
136+
"@message": "test message",
137+
"@module": "sdk.helper_resource",
138+
},
139+
}
140+
141+
if diff := cmp.Diff(entries, expectedEntries); diff != "" {
142+
t.Errorf("unexpected difference: %s", diff)
143+
}
144+
}

0 commit comments

Comments
 (0)