Skip to content

Commit d609e41

Browse files
paddycarverPaddy
authored andcommitted
Fix test logging output.
In version 1.0.0-1.6.0, our in-process non-binary test driver didn't call go-plugin, and called log.SetOutput to control what got logged, to avoid flooding test output with debug logs. From version 1.6.0, the in-process non-binary test driver had the same behavior, but we added an out-of-process binary test driver, which used go-plugin, but had the provider in a separate process that sent its stdout and stderr through Terraform. Terraform then, not the test framework, was responsible for filtering the output. With the introduction of our in-process binary test driver, however, we're using go-plugin and Terraform isn't filtering log output for us. In theory, the log.SetOutput call that was working for the in-process non-binary test driver should've worked here, as well, and it sort of did. The issue was that go-plugin calls log.SetOutput and sets it to os.Stderr in plugin.Serve, which overrides our filter. To get this behavior back, after every call to plugin.Serve, we override the go-plugin log.SetOutput by calling our own again. This also removes the helper/resource.logOutput function, replacing its usage with helper/logging.SetOutput, which does the same thing. The only difference should be that filtering on test name is no longer supported. If we want to support that, we would need to plumb a testing.T through to runProviderCommand, which involves updating a lot of code for marginal gain; as far as I know, nobody actually uses the test name filtering for logging, preferring instead to log everything and only run the test they want logs for.
1 parent f9ebc90 commit d609e41

File tree

2 files changed

+5
-52
lines changed

2 files changed

+5
-52
lines changed

helper/resource/plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/hashicorp/go-hclog"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
grpcplugin "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/plugin"
1415
proto "github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfplugin5"
@@ -57,6 +58,9 @@ func runProviderCommand(f func() error, wd *tftest.WorkingDir, opts *plugin.Serv
5758
return err
5859
}
5960

61+
// plugin.DebugServe hijacks our log output location, so let's reset it
62+
logging.SetOutput()
63+
6064
reattachInfo := map[string]plugin.ReattachConfig{}
6165
var namespaces []string
6266
host := "registry.terraform.io"

helper/resource/testing.go

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import (
55
"errors"
66
"flag"
77
"fmt"
8-
"io"
9-
"io/ioutil"
108
"log"
119
"os"
1210
"regexp"
1311
"strconv"
1412
"strings"
15-
"syscall"
1613

1714
"github.com/hashicorp/go-multierror"
18-
"github.com/hashicorp/logutils"
1915
tftest "github.com/hashicorp/terraform-plugin-test"
2016
testing "github.com/mitchellh/go-testing-interface"
2117

@@ -451,49 +447,6 @@ type TestStep struct {
451447
providers map[string]*schema.Provider
452448
}
453449

454-
// Set to a file mask in sprintf format where %s is test name
455-
const envLogPathMask = "TF_LOG_PATH_MASK"
456-
457-
func logOutput(t testing.T) (logOutput io.Writer, err error) {
458-
logOutput = ioutil.Discard
459-
460-
logLevel := logging.LogLevel()
461-
if logLevel == "" {
462-
return
463-
}
464-
465-
logOutput = os.Stderr
466-
467-
if logPath := os.Getenv(logging.EnvLogFile); logPath != "" {
468-
var err error
469-
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
470-
if err != nil {
471-
return nil, err
472-
}
473-
}
474-
475-
if logPathMask := os.Getenv(envLogPathMask); logPathMask != "" {
476-
// Escape special characters which may appear if we have subtests
477-
testName := strings.Replace(t.Name(), "/", "__", -1)
478-
479-
logPath := fmt.Sprintf(logPathMask, testName)
480-
var err error
481-
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
482-
if err != nil {
483-
return nil, err
484-
}
485-
}
486-
487-
// This was the default since the beginning
488-
logOutput = &logutils.LevelFilter{
489-
Levels: logging.ValidLevels,
490-
MinLevel: logutils.LogLevel(logLevel),
491-
Writer: logOutput,
492-
}
493-
494-
return
495-
}
496-
497450
// ParallelTest performs an acceptance test on a resource, allowing concurrency
498451
// with other ParallelTest.
499452
//
@@ -526,11 +479,7 @@ func Test(t testing.T, c TestCase) {
526479
return
527480
}
528481

529-
logWriter, err := logOutput(t)
530-
if err != nil {
531-
t.Error(fmt.Errorf("error setting up logging: %s", err))
532-
}
533-
log.SetOutput(logWriter)
482+
logging.SetOutput()
534483

535484
// get instances of all providers, so we can use the individual
536485
// resources to shim the state during the tests.

0 commit comments

Comments
 (0)