Skip to content

Commit

Permalink
Stop overriding log output in plugin.Serve in tests.
Browse files Browse the repository at this point in the history
When testing, we don't want to override where log output gets sent--the
test framework takes care of that itself. go-plugin has disabled its use
of log.SetOutput for us, but #639 introduced a log.SetOutput in
plugin.Serve. This adds a plugin.ServeOpt property to disable that
log.SetOutput call so we can finally have tests where log lines don't
randomly show up in test output.
  • Loading branch information
paddycarver committed Dec 18, 2020
1 parent 6f728bc commit def5710
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions helper/resource/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
Level: hclog.Trace,
Output: ioutil.Discard,
}),
NoLogOutputOverride: true,
}

// let's actually start the provider server
Expand Down Expand Up @@ -160,6 +161,7 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
Level: hclog.Trace,
Output: ioutil.Discard,
}),
NoLogOutputOverride: true,
}

// let's actually start the provider server
Expand Down
31 changes: 19 additions & 12 deletions plugin/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,30 @@ type ServeOpts struct {
// plugin's lifecycle and communicate connection information. See the
// go-plugin GoDoc for more information.
TestConfig *plugin.ServeTestConfig

// Set NoLogOutputOverride to not override the log output with an hclog
// adapter. This should only be used when running the plugin in
// acceptance tests.
NoLogOutputOverride bool
}

// Serve serves a plugin. This function never returns and should be the final
// function called in the main function of the plugin.
func Serve(opts *ServeOpts) {
// In order to allow go-plugin to correctly pass log-levels through to
// terraform, we need to use an hclog.Logger with JSON output. We can
// inject this into the std `log` package here, so existing providers will
// make use of it automatically.
logger := hclog.New(&hclog.LoggerOptions{
// We send all output to terraform. Go-plugin will take the output and
// pass it through another hclog.Logger on the client side where it can
// be filtered.
Level: hclog.Trace,
JSONFormat: true,
})
log.SetOutput(logger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true}))
if !opts.NoLogOutputOverride {
// In order to allow go-plugin to correctly pass log-levels through to
// terraform, we need to use an hclog.Logger with JSON output. We can
// inject this into the std `log` package here, so existing providers will
// make use of it automatically.
logger := hclog.New(&hclog.LoggerOptions{
// We send all output to terraform. Go-plugin will take the output and
// pass it through another hclog.Logger on the client side where it can
// be filtered.
Level: hclog.Trace,
JSONFormat: true,
})
log.SetOutput(logger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true}))
}

// since the plugins may not yet be aware of the new protocol, we
// automatically wrap the plugins in the grpc shims.
Expand Down

0 comments on commit def5710

Please sign in to comment.