From def5710dcaa684ec4653a5dd3e69e0307b0d6001 Mon Sep 17 00:00:00 2001 From: Paddy Carver Date: Fri, 18 Dec 2020 13:09:17 -0800 Subject: [PATCH] Stop overriding log output in plugin.Serve in tests. 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. --- helper/resource/plugin.go | 2 ++ plugin/serve.go | 31 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/helper/resource/plugin.go b/helper/resource/plugin.go index b39e1324683..bef3a442f19 100644 --- a/helper/resource/plugin.go +++ b/helper/resource/plugin.go @@ -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 @@ -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 diff --git a/plugin/serve.go b/plugin/serve.go index 3294547db17..baaab2d1d1c 100644 --- a/plugin/serve.go +++ b/plugin/serve.go @@ -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.