diff --git a/internal/logging/context_test.go b/internal/logging/context_test.go new file mode 100644 index 00000000000..b193b9c019c --- /dev/null +++ b/internal/logging/context_test.go @@ -0,0 +1,193 @@ +package logging_test + +import ( + "bytes" + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-log/tfsdklog" + "github.com/hashicorp/terraform-plugin-log/tfsdklogtest" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging" +) + +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.HelperSchemaTrace(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.helper_schema", + "tf_rpc": "GetProviderSchema", + "tf_req_id": "123-testing-123", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestTestNameContext(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + ctx = logging.TestNameContext(ctx, "TestTestTest") + + logging.HelperResourceTrace(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.helper_resource", + "test_name": "TestTestTest", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestTestStepNumberContext(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + ctx = logging.TestStepNumberContext(ctx, 123) + + logging.HelperResourceTrace(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.helper_resource", + "test_step_number": float64(123), // float64 due to default JSON unmarshalling + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestTestTerraformPathContext(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + ctx = logging.TestTerraformPathContext(ctx, "/usr/local/bin/terraform") + + logging.HelperResourceTrace(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.helper_resource", + "test_terraform_path": "/usr/local/bin/terraform", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestTestWorkingDirectoryContext(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + ctx = logging.TestWorkingDirectoryContext(ctx, "/tmp/test") + + logging.HelperResourceTrace(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.helper_resource", + "test_working_directory": "/tmp/test", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} diff --git a/internal/logging/helper_resource_test.go b/internal/logging/helper_resource_test.go new file mode 100644 index 00000000000..bcb07e897ad --- /dev/null +++ b/internal/logging/helper_resource_test.go @@ -0,0 +1,144 @@ +package logging_test + +import ( + "bytes" + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-log/tfsdklog" + "github.com/hashicorp/terraform-plugin-log/tfsdklogtest" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging" +) + +func TestHelperResourceDebug(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + logging.HelperResourceDebug(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": "debug", + "@message": "test message", + "@module": "sdk.helper_resource", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestHelperResourceError(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + logging.HelperResourceError(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": "error", + "@message": "test message", + "@module": "sdk.helper_resource", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestHelperResourceTrace(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + logging.HelperResourceTrace(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.helper_resource", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestHelperResourceWarn(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + + // InitTestContext messes with the standard library log package, which + // we want to avoid in this unit testing. Instead, just create the + // helper_resource subsystem and avoid the other InitTestContext logic. + ctx = tfsdklog.NewSubsystem(ctx, logging.SubsystemHelperResource) + + logging.HelperResourceWarn(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": "warn", + "@message": "test message", + "@module": "sdk.helper_resource", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} diff --git a/internal/logging/helper_schema_test.go b/internal/logging/helper_schema_test.go new file mode 100644 index 00000000000..984e07d0247 --- /dev/null +++ b/internal/logging/helper_schema_test.go @@ -0,0 +1,127 @@ +package logging_test + +import ( + "bytes" + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/terraform-plugin-log/tfsdklogtest" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging" +) + +func TestHelperSchemaDebug(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + ctx = logging.InitContext(ctx) + + logging.HelperSchemaDebug(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": "debug", + "@message": "test message", + "@module": "sdk.helper_schema", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestHelperSchemaError(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + ctx = logging.InitContext(ctx) + + logging.HelperSchemaError(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": "error", + "@message": "test message", + "@module": "sdk.helper_schema", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestHelperSchemaTrace(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + ctx = logging.InitContext(ctx) + + logging.HelperSchemaTrace(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.helper_schema", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +} + +func TestHelperSchemaWarn(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + ctx := tfsdklogtest.RootLogger(context.Background(), &output) + ctx = logging.InitContext(ctx) + + logging.HelperSchemaWarn(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": "warn", + "@message": "test message", + "@module": "sdk.helper_schema", + }, + } + + if diff := cmp.Diff(entries, expectedEntries); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } +}