Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: Introduce logging unit testing (#316)
Creates internal/testing/emptyprovider so it can be shared for multiple packages, then uses it to verify the logging setup with `(internal/proto6server.Server).GetProviderSchema()`. This could be expanded to other RPC handlers, if desired and the testing setup is trivial enough. Also added unit testing within internal/logging to ensure it is fully covered.
- Loading branch information
Showing
7 changed files
with
312 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
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-go/tftypes" | ||
"github.com/hashicorp/terraform-plugin-log/tfsdklogtest" | ||
) | ||
|
||
func TestFrameworkDebug(t *testing.T) { | ||
t.Parallel() | ||
|
||
var output bytes.Buffer | ||
|
||
ctx := tfsdklogtest.RootLogger(context.Background(), &output) | ||
ctx = logging.InitContext(ctx) | ||
|
||
logging.FrameworkDebug(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.framework", | ||
}, | ||
} | ||
|
||
if diff := cmp.Diff(entries, expectedEntries); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
} | ||
|
||
func TestFrameworkError(t *testing.T) { | ||
t.Parallel() | ||
|
||
var output bytes.Buffer | ||
|
||
ctx := tfsdklogtest.RootLogger(context.Background(), &output) | ||
ctx = logging.InitContext(ctx) | ||
|
||
logging.FrameworkError(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.framework", | ||
}, | ||
} | ||
|
||
if diff := cmp.Diff(entries, expectedEntries); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
} | ||
|
||
func TestFrameworkTrace(t *testing.T) { | ||
t.Parallel() | ||
|
||
var output bytes.Buffer | ||
|
||
ctx := tfsdklogtest.RootLogger(context.Background(), &output) | ||
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", | ||
}, | ||
} | ||
|
||
if diff := cmp.Diff(entries, expectedEntries); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
} | ||
|
||
func TestFrameworkWarn(t *testing.T) { | ||
t.Parallel() | ||
|
||
var output bytes.Buffer | ||
|
||
ctx := tfsdklogtest.RootLogger(context.Background(), &output) | ||
ctx = logging.InitContext(ctx) | ||
|
||
logging.FrameworkWarn(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.framework", | ||
}, | ||
} | ||
|
||
if diff := cmp.Diff(entries, expectedEntries); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
} | ||
|
||
func TestFrameworkWithAttributePath(t *testing.T) { | ||
t.Parallel() | ||
|
||
var output bytes.Buffer | ||
|
||
ctx := tfsdklogtest.RootLogger(context.Background(), &output) | ||
ctx = logging.InitContext(ctx) | ||
|
||
ctx = logging.FrameworkWithAttributePath(ctx, tftypes.NewAttributePath().WithAttributeName("test_attr").String()) | ||
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_attribute_path": "AttributeName(\"test_attr\")", | ||
}, | ||
} | ||
|
||
if diff := cmp.Diff(entries, expectedEntries); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package emptyprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
) | ||
|
||
var _ tfsdk.Provider = &Provider{} | ||
|
||
// tfsdk.Provider that is completely empty, e.g. | ||
// | ||
// - No Schema | ||
// - No DataSources | ||
// - No Resources | ||
// | ||
type Provider struct{} | ||
|
||
func (t *Provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { | ||
return tfsdk.Schema{}, nil | ||
} | ||
|
||
func (t *Provider) Configure(_ context.Context, _ tfsdk.ConfigureProviderRequest, _ *tfsdk.ConfigureProviderResponse) { | ||
// intentionally empty | ||
} | ||
|
||
func (t *Provider) GetDataSources(_ context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) { | ||
return map[string]tfsdk.DataSourceType{}, nil | ||
} | ||
|
||
func (t *Provider) GetResources(_ context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) { | ||
return map[string]tfsdk.ResourceType{}, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters