From 4bd4e18defcf73f22c206ce11bdde604c64d05fd Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Mon, 7 Dec 2015 18:10:30 -0600 Subject: [PATCH] core: use same logging setup for acctests We weren't doing any log setup for acceptance tests, which made it difficult to wrangle log output in CI. This moves the log setup functions we use in `main` over into a helper package so we can use them for acceptance tests as well. This means that acceptance tests will by default be a _lot_ quieter, only printing out actual test output. Setting `TF_LOG=trace` will restore the full prior noise level. Only minor behavior change is to make `ioutil.Discard` the default return value rather than a `nil` that needs to be checked for. --- log.go => helper/logging/logging.go | 9 +++++---- helper/resource/testing.go | 7 +++++++ main.go | 10 ++-------- 3 files changed, 14 insertions(+), 12 deletions(-) rename log.go => helper/logging/logging.go (88%) diff --git a/log.go b/helper/logging/logging.go similarity index 88% rename from log.go rename to helper/logging/logging.go index 1077c3e55..b8de7a37f 100644 --- a/log.go +++ b/helper/logging/logging.go @@ -1,7 +1,8 @@ -package main +package logging import ( "io" + "io/ioutil" "log" "os" "strings" @@ -18,9 +19,9 @@ const ( var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"} -// logOutput determines where we should send logs (if anywhere) and the log level. -func logOutput() (logOutput io.Writer, err error) { - logOutput = nil +// LogOutput determines where we should send logs (if anywhere) and the log level. +func LogOutput() (logOutput io.Writer, err error) { + logOutput = ioutil.Discard envLevel := os.Getenv(EnvLog) if envLevel == "" { return diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 0b53c3c61..18a40553b 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/go-getter" "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/terraform" ) @@ -103,6 +104,12 @@ func Test(t TestT, c TestCase) { return } + logWriter, err := logging.LogOutput() + if err != nil { + t.Error(fmt.Errorf("error setting up logging: %s", err)) + } + log.SetOutput(logWriter) + // We require verbose mode so that the user knows what is going on. if !testTesting && !testing.Verbose() { t.Fatal("Acceptance tests must be run with the -v flag on tests") diff --git a/main.go b/main.go index 168583107..da71456d5 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "os" "sync" + "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/plugin" "github.com/mitchellh/cli" "github.com/mitchellh/panicwrap" @@ -23,14 +24,11 @@ func realMain() int { if !panicwrap.Wrapped(&wrapConfig) { // Determine where logs should go in general (requested by the user) - logWriter, err := logOutput() + logWriter, err := logging.LogOutput() if err != nil { fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err) return 1 } - if logWriter == nil { - logWriter = ioutil.Discard - } // We always send logs to a temporary file that we use in case // there is a panic. Otherwise, we delete it. @@ -42,10 +40,6 @@ func realMain() int { defer os.Remove(logTempFile.Name()) defer logTempFile.Close() - // Tell the logger to log to this file - os.Setenv(EnvLog, "") - os.Setenv(EnvLogFile, "") - // Setup the prefixed readers that send data properly to // stdout/stderr. doneCh := make(chan struct{})