From 4052a8f956277376129310c1f400b967a14cbae7 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 14 Oct 2017 16:35:36 +0300 Subject: [PATCH] helper: Allow logs isolation per acceptance test --- helper/logging/logging.go | 8 ++++---- helper/resource/testing.go | 36 ++++++++++++++++++++++++++++++++- helper/resource/testing_test.go | 4 ++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/helper/logging/logging.go b/helper/logging/logging.go index 433cd77d3..6bd92f777 100644 --- a/helper/logging/logging.go +++ b/helper/logging/logging.go @@ -18,7 +18,7 @@ const ( EnvLogFile = "TF_LOG_PATH" // Set to a file ) -var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"} +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) { @@ -40,7 +40,7 @@ func LogOutput() (logOutput io.Writer, err error) { // This was the default since the beginning logOutput = &logutils.LevelFilter{ - Levels: validLevels, + Levels: ValidLevels, MinLevel: logutils.LogLevel(logLevel), Writer: logOutput, } @@ -77,7 +77,7 @@ func LogLevel() string { logLevel = strings.ToUpper(envLevel) } else { log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v", - envLevel, validLevels) + envLevel, ValidLevels) } return logLevel @@ -90,7 +90,7 @@ func IsDebugOrHigher() bool { } func isValidLogLevel(level string) bool { - for _, l := range validLevels { + for _, l := range ValidLevels { if strings.ToUpper(level) == string(l) { return true } diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 8db38a77c..aa0aa51fb 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -11,11 +11,13 @@ import ( "reflect" "regexp" "strings" + "syscall" "testing" "github.com/davecgh/go-spew/spew" "github.com/hashicorp/go-getter" "github.com/hashicorp/go-multierror" + "github.com/hashicorp/logutils" "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/terraform" @@ -359,6 +361,37 @@ type TestStep struct { ImportStateVerifyIgnore []string } +// Set to a file mask in sprintf format where %s is test name +const EnvLogPathMask = "TF_LOG_PATH_MASK" + +func LogOutput(t TestT) (logOutput io.Writer, err error) { + logOutput = ioutil.Discard + + logLevel := logging.LogLevel() + if logLevel == "" { + return + } + + logOutput = os.Stderr + if logPathMask := os.Getenv(EnvLogPathMask); logPathMask != "" { + logPath := fmt.Sprintf(logPathMask, t.Name()) + var err error + logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666) + if err != nil { + return nil, err + } + } + + // This was the default since the beginning + logOutput = &logutils.LevelFilter{ + Levels: logging.ValidLevels, + MinLevel: logutils.LogLevel(logLevel), + Writer: logOutput, + } + + return +} + // Test performs an acceptance test on a resource. // // Tests are not run unless an environmental variable "TF_ACC" is @@ -380,7 +413,7 @@ func Test(t TestT, c TestCase) { return } - logWriter, err := logging.LogOutput() + logWriter, err := LogOutput(t) if err != nil { t.Error(fmt.Errorf("error setting up logging: %s", err)) } @@ -961,6 +994,7 @@ type TestT interface { Error(args ...interface{}) Fatal(args ...interface{}) Skip(args ...interface{}) + Name() string } // This is set to true by unit tests to alter some behavior diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 8712c8794..fcbde3ed9 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -627,6 +627,10 @@ func (t *mockT) Skip(args ...interface{}) { t.f = true } +func (t *mockT) Name() string { + return "MockedName" +} + func (t *mockT) failed() bool { return t.f }