Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions META.d/_summary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
schema: 1.1

partition: tf-ecosystem

summary:
owner: team-tf-core-plugins
description: |
Module for testing Terraform providers
visibility: public
16 changes: 16 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ func TestMain(m interface {
}
} else {
exitCode := m.Run()

tempDir := os.Getenv(plugintest.EnvTfAccTempDir)
if tempDir == "" {
tempDir = os.TempDir()
}
pathParts := []string{
strings.TrimRight(tempDir, string(os.PathSeparator)),
"plugintest-terraform",
strconv.Itoa(os.Getpid()),
}
tfDir := strings.Join(pathParts, string(os.PathSeparator))
if err := os.RemoveAll(tfDir); err != nil {
log.Printf("[WARN] Failed to remove temporary directory for Terraform CLI: %s", err)

}

os.Exit(exitCode)
}
}
Expand Down
30 changes: 13 additions & 17 deletions internal/logging/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@ package logging

import (
"context"
"os"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
helperlogging "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
testing "github.com/mitchellh/go-testing-interface"
)

// 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.NewSubsystem(ctx, SubsystemHelperSchema,
// All calls are through the HelperSchema* helper functions
tfsdklog.WithAdditionalLocationOffset(1),
tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperSchema),
// Propagate tf_req_id, tf_rpc, etc. fields
tfsdklog.WithRootFields(),
)

return ctx
}

// InitTestContext registers the terraform-plugin-log/tfsdklog test sink,
// configures the standard library log package, and creates SDK logger
// contexts. The incoming context is expected to be devoid of logging setup.
Expand All @@ -39,13 +25,23 @@ func InitTestContext(ctx context.Context, t testing.T) context.Context {

ctx = tfsdklog.RegisterTestSink(ctx, t)
ctx = tfsdklog.NewRootSDKLogger(ctx, tfsdklog.WithLevelFromEnv(EnvTfLogSdk))

ctx = tfsdklog.NewSubsystem(ctx, SubsystemHelperResource,
// All calls are through the HelperResource* helper functions
tfsdklog.WithAdditionalLocationOffset(1),
tfsdklog.WithLevelFromEnv(EnvTfLogSdkHelperResource),
)
ctx = TestNameContext(ctx, t.Name())

var logLevelOption = tfsdklog.WithLevel(hclog.Off)
if len(os.Getenv(EnvTfLogInstaller)) > 0 {
logLevelOption = tfsdklog.WithLevelFromEnv(EnvTfLogInstaller)
}

ctx = tfsdklog.NewSubsystem(ctx, SubsystemInstall,
tfsdklog.WithAdditionalLocationOffset(1),
logLevelOption,
)

return ctx
}

Expand Down
37 changes: 0 additions & 37 deletions internal/logging/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,6 @@ import (
"github.com/hashicorp/terraform-plugin-testing/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.SetField(ctx, "tf_rpc", "GetProviderSchema")
ctx = tfsdklog.SetField(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()

Expand Down
5 changes: 1 addition & 4 deletions internal/logging/environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,5 @@ const (
// unset.
EnvTfLogSdkHelperResource = "TF_LOG_SDK_HELPER_RESOURCE"

// EnvTfLogSdkHelperSchema is an environment variable that sets the logging
// level of SDK helper/schema loggers. Infers root SDK logging level, if
// unset.
EnvTfLogSdkHelperSchema = "TF_LOG_SDK_HELPER_SCHEMA"
EnvTfLogInstaller = "TF_LOG_INSTALLER"
)
1 change: 1 addition & 0 deletions internal/logging/helper_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const (
// SubsystemHelperResource is the tfsdklog subsystem name for helper/resource.
SubsystemHelperResource = "helper_resource"
SubsystemInstall = "install"
)

// HelperResourceTrace emits a helper/resource subsystem log at TRACE level.
Expand Down
35 changes: 0 additions & 35 deletions internal/logging/helper_schema.go

This file was deleted.

131 changes: 0 additions & 131 deletions internal/logging/helper_schema_test.go

This file was deleted.

Loading