2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-02-01 15:21:54 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2020-05-06 20:40:16 +00:00
|
|
|
"bytes"
|
2019-02-01 15:21:54 +00:00
|
|
|
"io"
|
2019-02-01 18:25:04 +00:00
|
|
|
"os"
|
2020-05-06 20:40:16 +00:00
|
|
|
"sync"
|
2019-02-01 15:21:54 +00:00
|
|
|
"testing"
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-02-01 15:21:54 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 18:40:19 +00:00
|
|
|
// TestLogLevel is set from the TEST_LOG_LEVEL environment variable. It can
|
|
|
|
// be used by tests to set the log level of a hclog.Logger. Defaults to
|
|
|
|
// hclog.Warn if the environment variable is unset, or if the value of the
|
|
|
|
// environment variable can not be matched to a log level.
|
|
|
|
var TestLogLevel = testLogLevel()
|
|
|
|
|
|
|
|
func testLogLevel() hclog.Level {
|
|
|
|
level := hclog.LevelFromString(os.Getenv("TEST_LOG_LEVEL"))
|
|
|
|
if level != hclog.NoLevel {
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
return hclog.Warn
|
|
|
|
}
|
|
|
|
|
2020-09-10 13:04:56 +00:00
|
|
|
func Logger(t TestingTB) hclog.InterceptLogger {
|
2020-05-06 20:40:16 +00:00
|
|
|
return LoggerWithOutput(t, NewLogBuffer(t))
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 13:04:56 +00:00
|
|
|
func LoggerWithOutput(t TestingTB, output io.Writer) hclog.InterceptLogger {
|
2020-01-28 23:50:41 +00:00
|
|
|
return hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Name: t.Name(),
|
2020-02-03 14:26:47 +00:00
|
|
|
Level: hclog.Trace,
|
2020-01-28 23:50:41 +00:00
|
|
|
Output: output,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
var sendTestLogsToStdout = os.Getenv("NOLOGBUFFER") == "1"
|
2021-05-17 20:01:32 +00:00
|
|
|
var testLogOnlyFailed = os.Getenv("TEST_LOGGING_ONLY_FAILED") == "1"
|
2019-02-01 15:21:54 +00:00
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
// NewLogBuffer returns an io.Writer which buffers all writes. When the test
|
2020-09-04 16:24:11 +00:00
|
|
|
// ends, t.Failed is checked. If the test has failed or has been run in verbose
|
|
|
|
// mode all log output is printed to stdout.
|
2020-05-06 20:40:16 +00:00
|
|
|
//
|
|
|
|
// Set the env var NOLOGBUFFER=1 to disable buffering, resulting in all log
|
|
|
|
// output being written immediately to stdout.
|
2021-05-17 20:01:32 +00:00
|
|
|
//
|
|
|
|
// Typically log output is written either for failed tests or when go test
|
|
|
|
// is running with the verbose flag (-v) set. Setting TEST_LOGGING_ONLY_FAILED=1
|
|
|
|
// will prevent logs being output when the verbose flag is set if the test
|
|
|
|
// case is successful.
|
2020-09-10 13:04:56 +00:00
|
|
|
func NewLogBuffer(t TestingTB) io.Writer {
|
2020-05-06 20:40:16 +00:00
|
|
|
if sendTestLogsToStdout {
|
|
|
|
return os.Stdout
|
|
|
|
}
|
|
|
|
buf := &logBuffer{buf: new(bytes.Buffer)}
|
|
|
|
t.Cleanup(func() {
|
2021-05-17 20:01:32 +00:00
|
|
|
if t.Failed() || (!testLogOnlyFailed && testing.Verbose()) {
|
2020-05-06 20:40:16 +00:00
|
|
|
buf.Lock()
|
|
|
|
defer buf.Unlock()
|
|
|
|
buf.buf.WriteTo(os.Stdout)
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
})
|
2020-05-06 20:40:16 +00:00
|
|
|
return buf
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
type logBuffer struct {
|
|
|
|
buf *bytes.Buffer
|
|
|
|
sync.Mutex
|
2019-02-01 15:21:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:40:16 +00:00
|
|
|
func (lb *logBuffer) Write(p []byte) (n int, err error) {
|
|
|
|
lb.Lock()
|
|
|
|
defer lb.Unlock()
|
|
|
|
return lb.buf.Write(p)
|
2019-02-01 15:21:54 +00:00
|
|
|
}
|