helper: Allow logs isolation per acceptance test

This commit is contained in:
Radek Simko 2017-10-14 16:35:36 +03:00
parent fe4cfd03b5
commit 4052a8f956
No known key found for this signature in database
GPG Key ID: 1F1C84FE689A88D7
3 changed files with 43 additions and 5 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
}