From 1af7ee87a27e8ce19e92a5cee14aa7279e434d25 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 1 Aug 2016 17:16:22 -0400 Subject: [PATCH] Silence log output when not verbose Set the default log package output to iotuil.Discard during tests if the `-v` flag isn't set. If we are verbose, then apply the filter according to the TF_LOG env variable. --- command/command_test.go | 16 ++++++++++++++++ config/config_test.go | 19 +++++++++++++++++++ dag/dag_test.go | 19 +++++++++++++++++++ helper/logging/logging.go | 16 ++++++++++++++++ terraform/terraform_test.go | 16 ++++++++++++++++ 5 files changed, 86 insertions(+) diff --git a/command/command_test.go b/command/command_test.go index 14e19ee3b..174d439ac 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -1,7 +1,9 @@ package command import ( + "flag" "io/ioutil" + "log" "os" "path/filepath" "strings" @@ -9,6 +11,7 @@ import ( "github.com/hashicorp/go-getter" "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/terraform" ) @@ -27,6 +30,19 @@ func init() { } } +func TestMain(m *testing.M) { + flag.Parse() + if testing.Verbose() { + // if we're verbose, use the logging requested by TF_LOG + logging.SetOutput() + } else { + // otherwise silence all logs + log.SetOutput(ioutil.Discard) + } + + os.Exit(m.Run()) +} + func tempDir(t *testing.T) string { dir, err := ioutil.TempDir("", "tf") if err != nil { diff --git a/config/config_test.go b/config/config_test.go index 6d6de1422..66f712f67 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,15 +1,34 @@ package config import ( + "flag" + "io/ioutil" + "log" + "os" "path/filepath" "reflect" "strings" "testing" + + "github.com/hashicorp/terraform/helper/logging" ) // This is the directory where our test fixtures are. const fixtureDir = "./test-fixtures" +func TestMain(m *testing.M) { + flag.Parse() + if testing.Verbose() { + // if we're verbose, use the logging requested by TF_LOG + logging.SetOutput() + } else { + // otherwise silence all logs + log.SetOutput(ioutil.Discard) + } + + os.Exit(m.Run()) +} + func TestConfigCopy(t *testing.T) { c := testConfig(t, "copy-basic") rOrig := c.Resources[0] diff --git a/dag/dag_test.go b/dag/dag_test.go index 2d17a8c37..4f8aa3d0d 100644 --- a/dag/dag_test.go +++ b/dag/dag_test.go @@ -1,13 +1,32 @@ package dag import ( + "flag" "fmt" + "io/ioutil" + "log" + "os" "reflect" "strings" "sync" "testing" + + "github.com/hashicorp/terraform/helper/logging" ) +func TestMain(m *testing.M) { + flag.Parse() + if testing.Verbose() { + // if we're verbose, use the logging requested by TF_LOG + logging.SetOutput() + } else { + // otherwise silence all logs + log.SetOutput(ioutil.Discard) + } + + os.Exit(m.Run()) +} + func TestAcyclicGraphRoot(t *testing.T) { var g AcyclicGraph g.Add(1) diff --git a/helper/logging/logging.go b/helper/logging/logging.go index bff2c7254..969a6f151 100644 --- a/helper/logging/logging.go +++ b/helper/logging/logging.go @@ -47,6 +47,22 @@ func LogOutput() (logOutput io.Writer, err error) { return } +// SetOutput checks for a log destination with LogOutput, and calls +// log.SetOutput with the result. If LogOutput returns nil, SetOutput uses +// ioutil.Discard. Any error from LogOutout is fatal. +func SetOutput() { + out, err := LogOutput() + if err != nil { + log.Fatal(err) + } + + if out == nil { + out = ioutil.Discard + } + + log.SetOutput(out) +} + // LogLevel returns the current log level string based the environment vars func LogLevel() string { envLevel := os.Getenv(EnvLog) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index fbcf6c61e..7f81bf05a 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -1,9 +1,11 @@ package terraform import ( + "flag" "fmt" "io" "io/ioutil" + "log" "os" "path/filepath" "strings" @@ -13,11 +15,25 @@ import ( "github.com/hashicorp/go-getter" "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/helper/logging" ) // This is the directory where our test fixtures are. const fixtureDir = "./test-fixtures" +func TestMain(m *testing.M) { + flag.Parse() + if testing.Verbose() { + // if we're verbose, use the logging requested by TF_LOG + logging.SetOutput() + } else { + // otherwise silence all logs + log.SetOutput(ioutil.Discard) + } + + os.Exit(m.Run()) +} + func tempDir(t *testing.T) string { dir, err := ioutil.TempDir("", "tf") if err != nil {