Skip to content

Commit 45133e6

Browse files
authored
helper/resource: Added terraform-plugin-log logger and extensive log entries (#891)
Reference: #838 This new subsystem logger is meant for instrumenting the acceptance testing framework. Most of the changes are to support passing the context to many of the functions and adding `DEBUG`/`TRACE` log entries for provider developer and SDK maintainer troubleshooting. Verified `go test` logging behaviors via terraform-provider-corner: - No logs by default - `TF_LOG_SDK_HELPER_RESOURCE` environment variable control of new logger - `TF_LOG` enabling new logger - `TF_ACC_LOG_PATH` enabling new logger
1 parent 3ab51c3 commit 45133e6

File tree

15 files changed

+444
-130
lines changed

15 files changed

+444
-130
lines changed

.changelog/891.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:note
2+
helper/resource: The new terraform-plugin-log `sdk.helper_resource` logger inherits the `TF_LOG`, `TF_LOG_PATH_MASK`, and `TF_ACC_LOG_PATH` environment variable settings, similar to the prior logging. The `TF_LOG_SDK_HELPER_RESOURCE` environment variable can be used to separately control the new logger level.
3+
```
4+
5+
```release-note:enhancement
6+
helper/resource: Added terraform-plugin-log `sdk.helper_resource` logger and extensive `TRACE` log entries
7+
```

helper/logging/logging.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package logging
22

33
import (
4-
"context"
54
"fmt"
65
"io"
76
"io/ioutil"
@@ -11,7 +10,6 @@ import (
1110
"syscall"
1211

1312
"github.com/hashicorp/logutils"
14-
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
1513
testing "github.com/mitchellh/go-testing-interface"
1614
)
1715

@@ -138,12 +136,3 @@ func isValidLogLevel(level string) bool {
138136

139137
return false
140138
}
141-
142-
// GetTestLogContext creates a context that is registered to the SDK log sink.
143-
// This function is for internal usage only and is not supported by the project's
144-
// compatibility promises.
145-
func GetTestLogContext(t testing.T) context.Context {
146-
ctx := context.Background()
147-
ctx = tfsdklog.RegisterTestSink(ctx, t)
148-
return ctx
149-
}

helper/resource/plugin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/hashicorp/terraform-exec/tfexec"
1414
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1515
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
16-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1716
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1817
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
1918
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
@@ -26,7 +25,7 @@ type providerFactories struct {
2625
protov6 map[string]func() (tfprotov6.ProviderServer, error)
2726
}
2827

29-
func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir, factories providerFactories) error {
28+
func runProviderCommand(ctx context.Context, t testing.T, f func() error, wd *plugintest.WorkingDir, factories providerFactories) error {
3029
// don't point to this as a test failure location
3130
// point to whatever called it
3231
t.Helper()
@@ -35,7 +34,7 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
3534
// reattach behavior in Terraform. This ensures we get test coverage
3635
// and enables the use of delve as a debugger.
3736

38-
ctx, cancel := context.WithCancel(logging.GetTestLogContext(t))
37+
ctx, cancel := context.WithCancel(ctx)
3938
defer cancel()
4039

4140
// this is needed so Terraform doesn't default to expecting protocol 4;

helper/resource/testing.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package resource
22

33
import (
4+
"context"
45
"errors"
56
"flag"
67
"fmt"
@@ -16,9 +17,9 @@ import (
1617

1718
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1819
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
19-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
2020
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2121
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
2223
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
2324
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2425
)
@@ -583,6 +584,9 @@ func ParallelTest(t testing.T, c TestCase) {
583584
func Test(t testing.T, c TestCase) {
584585
t.Helper()
585586

587+
ctx := context.Background()
588+
ctx = logging.InitTestContext(ctx, t)
589+
586590
// We only run acceptance tests if an env var is set because they're
587591
// slow and generally require some outside configuration. You can opt out
588592
// of this with OverrideEnvVar on individual TestCases.
@@ -593,8 +597,6 @@ func Test(t testing.T, c TestCase) {
593597
return
594598
}
595599

596-
logging.SetOutput(t)
597-
598600
// Copy any explicitly passed providers to factories, this is for backwards compatibility.
599601
if len(c.Providers) > 0 {
600602
c.ProviderFactories = map[string]func() (*schema.Provider, error){}
@@ -610,26 +612,34 @@ func Test(t testing.T, c TestCase) {
610612
}
611613
}
612614

615+
logging.HelperResourceDebug(ctx, "Starting TestCase")
616+
613617
// Run the PreCheck if we have it.
614618
// This is done after the auto-configure to allow providers
615619
// to override the default auto-configure parameters.
616620
if c.PreCheck != nil {
621+
logging.HelperResourceDebug(ctx, "Calling TestCase PreCheck")
622+
617623
c.PreCheck()
624+
625+
logging.HelperResourceDebug(ctx, "Called TestCase PreCheck")
618626
}
619627

620628
sourceDir, err := os.Getwd()
621629
if err != nil {
622630
t.Fatalf("Error getting working dir: %s", err)
623631
}
624-
helper := plugintest.AutoInitProviderHelper(sourceDir)
632+
helper := plugintest.AutoInitProviderHelper(ctx, sourceDir)
625633
defer func(helper *plugintest.Helper) {
626634
err := helper.Close()
627635
if err != nil {
628-
log.Printf("Error cleaning up temporary test files: %s", err)
636+
logging.HelperResourceError(ctx, "Unable to clean up temporary test files", logging.KeyError, err)
629637
}
630638
}(helper)
631639

632-
runNewTest(t, c, helper)
640+
runNewTest(ctx, t, c, helper)
641+
642+
logging.HelperResourceDebug(ctx, "Finished TestCase")
633643
}
634644

635645
// testProviderConfig takes the list of Providers in a TestCase and returns a

helper/resource/testing_config.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package resource
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
6-
"log"
77

8+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
910
)
1011

11-
func testStepTaint(state *terraform.State, step TestStep) error {
12+
func testStepTaint(ctx context.Context, state *terraform.State, step TestStep) error {
13+
if len(step.Taint) == 0 {
14+
return nil
15+
}
16+
17+
logging.HelperResourceTrace(ctx, fmt.Sprintf("Using TestStep Taint: %v", step.Taint))
18+
1219
for _, p := range step.Taint {
1320
m := state.RootModule()
1421
if m == nil {
@@ -18,7 +25,7 @@ func testStepTaint(state *terraform.State, step TestStep) error {
1825
if !ok {
1926
return fmt.Errorf("resource %q not found in state", p)
2027
}
21-
log.Printf("[WARN] Test: Explicitly tainting resource %q", p)
28+
logging.HelperResourceWarn(ctx, fmt.Sprintf("Explicitly tainting resource %q", p))
2229
rs.Taint()
2330
}
2431
return nil

0 commit comments

Comments
 (0)