helper: Allow logs isolation per acceptance test
This commit is contained in:
parent
fe4cfd03b5
commit
4052a8f956
|
@ -18,7 +18,7 @@ const (
|
||||||
EnvLogFile = "TF_LOG_PATH" // Set to a file
|
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.
|
// LogOutput determines where we should send logs (if anywhere) and the log level.
|
||||||
func LogOutput() (logOutput io.Writer, err error) {
|
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
|
// This was the default since the beginning
|
||||||
logOutput = &logutils.LevelFilter{
|
logOutput = &logutils.LevelFilter{
|
||||||
Levels: validLevels,
|
Levels: ValidLevels,
|
||||||
MinLevel: logutils.LogLevel(logLevel),
|
MinLevel: logutils.LogLevel(logLevel),
|
||||||
Writer: logOutput,
|
Writer: logOutput,
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ func LogLevel() string {
|
||||||
logLevel = strings.ToUpper(envLevel)
|
logLevel = strings.ToUpper(envLevel)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
|
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
|
||||||
envLevel, validLevels)
|
envLevel, ValidLevels)
|
||||||
}
|
}
|
||||||
|
|
||||||
return logLevel
|
return logLevel
|
||||||
|
@ -90,7 +90,7 @@ func IsDebugOrHigher() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isValidLogLevel(level string) bool {
|
func isValidLogLevel(level string) bool {
|
||||||
for _, l := range validLevels {
|
for _, l := range ValidLevels {
|
||||||
if strings.ToUpper(level) == string(l) {
|
if strings.ToUpper(level) == string(l) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,13 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/hashicorp/go-getter"
|
"github.com/hashicorp/go-getter"
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
"github.com/hashicorp/terraform/helper/logging"
|
"github.com/hashicorp/terraform/helper/logging"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
@ -359,6 +361,37 @@ type TestStep struct {
|
||||||
ImportStateVerifyIgnore []string
|
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.
|
// Test performs an acceptance test on a resource.
|
||||||
//
|
//
|
||||||
// Tests are not run unless an environmental variable "TF_ACC" is
|
// Tests are not run unless an environmental variable "TF_ACC" is
|
||||||
|
@ -380,7 +413,7 @@ func Test(t TestT, c TestCase) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logWriter, err := logging.LogOutput()
|
logWriter, err := LogOutput(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(fmt.Errorf("error setting up logging: %s", err))
|
t.Error(fmt.Errorf("error setting up logging: %s", err))
|
||||||
}
|
}
|
||||||
|
@ -961,6 +994,7 @@ type TestT interface {
|
||||||
Error(args ...interface{})
|
Error(args ...interface{})
|
||||||
Fatal(args ...interface{})
|
Fatal(args ...interface{})
|
||||||
Skip(args ...interface{})
|
Skip(args ...interface{})
|
||||||
|
Name() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is set to true by unit tests to alter some behavior
|
// This is set to true by unit tests to alter some behavior
|
||||||
|
|
|
@ -627,6 +627,10 @@ func (t *mockT) Skip(args ...interface{}) {
|
||||||
t.f = true
|
t.f = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *mockT) Name() string {
|
||||||
|
return "MockedName"
|
||||||
|
}
|
||||||
|
|
||||||
func (t *mockT) failed() bool {
|
func (t *mockT) failed() bool {
|
||||||
return t.f
|
return t.f
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue