Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper: Allow logs isolation per acceptance test #16356

Merged
merged 1 commit into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions helper/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
36 changes: 35 additions & 1 deletion helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down