Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/schema: Prevent missing SDK logging entries and confusing provider.stdio TRACE entries #936

Merged
merged 2 commits into from Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/936.txt
@@ -0,0 +1,3 @@
```release-note:bug
helper/schema: Prevented missing SDK logging entries and confusing provider.stdio TRACE logging entries
```
22 changes: 10 additions & 12 deletions internal/logging/context.go
Expand Up @@ -8,27 +8,29 @@ import (
testing "github.com/mitchellh/go-testing-interface"
)

// InitContext creates SDK logger contexts.
// InitContext creates SDK logger contexts when the provider is running in
// "production" (not under acceptance testing). The incoming context will
// already have the root SDK logger and root provider logger setup from
// terraform-plugin-go tf5server RPC handlers.
func InitContext(ctx context.Context) context.Context {
ctx = tfsdklog.NewRootSDKLogger(ctx)
ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperResource, tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperResource))
ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperSchema, tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperSchema))

return ctx
}
bflad marked this conversation as resolved.
Show resolved Hide resolved

// InitTestContext registers the terraform-plugin-log/tfsdklog test sink,
// configures the standard library log package, and creates SDK logger
// contexts.
// contexts. The incoming context is expected to be devoid of logging setup.
//
// It may be possible to eliminate the helper/logging handling if all
// log package calls are replaced with tfsdklog and any go-plugin or
// terraform-exec logger configurations are updated to the tfsdklog logger.
// The standard library log package handling is important as provider code
// under test may be using that package or another logging library outside of
// terraform-plugin-log.
func InitTestContext(ctx context.Context, t testing.T) context.Context {
helperlogging.SetOutput(t)

ctx = tfsdklog.RegisterTestSink(ctx, t)
ctx = InitContext(ctx)
ctx = tfsdklog.NewRootSDKLogger(ctx, tfsdklog.WithLevelFromEnv(EnvTfLogSdk))
ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperResource, tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperResource))
ctx = TestNameContext(ctx, t.Name())

return ctx
Expand All @@ -37,31 +39,27 @@ func InitTestContext(ctx context.Context, t testing.T) context.Context {
// TestNameContext adds the current test name to loggers.
func TestNameContext(ctx context.Context, testName string) context.Context {
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperResource, KeyTestName, testName)
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperSchema, KeyTestName, testName)

return ctx
}

// TestStepNumberContext adds the current test step number to loggers.
func TestStepNumberContext(ctx context.Context, stepNumber int) context.Context {
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperResource, KeyTestStepNumber, stepNumber)
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperSchema, KeyTestStepNumber, stepNumber)

return ctx
}

// TestTerraformPathContext adds the current test Terraform CLI path to loggers.
func TestTerraformPathContext(ctx context.Context, terraformPath string) context.Context {
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperResource, KeyTestTerraformPath, terraformPath)
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperSchema, KeyTestTerraformPath, terraformPath)

return ctx
}

// TestWorkingDirectoryContext adds the current test working directory to loggers.
func TestWorkingDirectoryContext(ctx context.Context, workingDirectory string) context.Context {
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperResource, KeyTestWorkingDirectory, workingDirectory)
ctx = tfsdklog.SubsystemWith(ctx, SubsystemHelperSchema, KeyTestWorkingDirectory, workingDirectory)

return ctx
}
10 changes: 10 additions & 0 deletions internal/logging/environment_variables.go
Expand Up @@ -2,6 +2,16 @@ package logging

// Environment variables.
const (
// EnvTfLogSdk is an environment variable that sets the logging level of
// the root SDK logger, while the provider is under test. In "production"
// usage, this environment variable is handled by terraform-plugin-go.
//
// Terraform CLI's logging must be explicitly turned on before this
// environment varable can be used to reduce the SDK logging levels. It
// cannot be used to show only SDK logging unless all other logging levels
// are turned off.
EnvTfLogSdk = "TF_LOG_SDK"

// EnvTfLogSdkHelperResource is an environment variable that sets the logging
// level of SDK helper/resource loggers. Infers root SDK logging level, if
// unset.
Expand Down