Merge pull request #12265 from hashicorp/dnephin/logging-in-tests

sdk: add TestLogLevel for setting log level in tests
This commit is contained in:
Daniel Nephin 2022-02-07 16:11:23 -05:00 committed by GitHub
commit 7ec658b7ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 18 deletions

View File

@ -67,7 +67,7 @@ func NewTestACLAgent(t *testing.T, name string, hcl string, resolveAuthz authzRe
bd.Logger = hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: name,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: logBuffer,
TimeFormat: "04:05.000",
})

View File

@ -8,10 +8,11 @@ import (
"testing"
"time"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestAE_scaleFactor(t *testing.T) {
@ -401,7 +402,6 @@ func (m *mock) SyncChanges() error {
func testSyncer(t *testing.T) *StateSyncer {
logger := hclog.New(&hclog.LoggerOptions{
Level: 0,
Output: testutil.NewLogBuffer(t),
})

View File

@ -507,7 +507,7 @@ func newDefaultDeps(t *testing.T, c *Config) Deps {
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: c.NodeName,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: testutil.NewLogBuffer(t),
})

View File

@ -1445,7 +1445,7 @@ func TestLeader_ConfigEntryBootstrap_Fail(t *testing.T) {
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: config.NodeName,
Level: hclog.Debug,
Level: testutil.TestLogLevel,
Output: io.MultiWriter(pw, testutil.NewLogBuffer(t)),
})

View File

@ -3017,9 +3017,7 @@ func TestPreparedQuery_queryFailover(t *testing.T) {
if queries := mock.JoinQueryLog(); queries != "dc1:PreparedQuery.ExecuteRemote|dc2:PreparedQuery.ExecuteRemote|dc4:PreparedQuery.ExecuteRemote" {
t.Fatalf("bad: %s", queries)
}
if !strings.Contains(mock.LogBuffer.String(), "Skipping unknown datacenter") {
t.Fatalf("bad: %s", mock.LogBuffer.String())
}
require.Contains(t, mock.LogBuffer.String(), "Skipping unknown datacenter")
}
// Same setup as before but dc1 is going to return an error and should

View File

@ -21,6 +21,7 @@ import (
"github.com/NYTimes/gziphandler"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
@ -651,7 +652,10 @@ func TestHTTP_wrap_obfuscateLog(t *testing.T) {
t.Parallel()
buf := &syncBuffer{b: new(bytes.Buffer)}
a := StartTestAgent(t, TestAgent{LogOutput: buf})
a := StartTestAgent(t, TestAgent{
LogOutput: buf,
LogLevel: hclog.Debug,
})
defer a.Shutdown()
handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
@ -694,9 +698,7 @@ func TestHTTP_wrap_obfuscateLog(t *testing.T) {
req, _ := http.NewRequest("GET", url, nil)
a.srv.wrap(handler, []string{"GET"})(resp, req)
bufout := buf.String()
if !strings.Contains(bufout, want) {
t.Fatalf("got %s want %s", bufout, want)
}
require.Contains(t, bufout, want)
})
}
}

View File

@ -25,10 +25,7 @@ func init() {
func GetBufferedLogger() hclog.Logger {
localLogBuffer = new(bytes.Buffer)
return hclog.New(&hclog.LoggerOptions{
Level: 0,
Output: localLogBuffer,
})
return hclog.New(&hclog.LoggerOptions{Output: localLogBuffer})
}
type fauxConnPool struct {

View File

@ -56,6 +56,7 @@ type TestAgent struct {
// The io.Writer must allow concurrent reads and writes. Note that
// bytes.Buffer is not safe for concurrent reads and writes.
LogOutput io.Writer
LogLevel hclog.Level
// DataDir may be set to a directory which exists. If is it not set,
// TestAgent.Start will create one and set DataDir to the directory path.
@ -158,8 +159,12 @@ func (a *TestAgent) Start(t *testing.T) error {
logOutput = testutil.NewLogBuffer(t)
}
if a.LogLevel == 0 {
a.LogLevel = testutil.TestLogLevel
}
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Level: hclog.Debug,
Level: a.LogLevel,
Output: logOutput,
TimeFormat: "04:05.000",
Name: name,

View File

@ -10,6 +10,20 @@ import (
"github.com/hashicorp/go-hclog"
)
// 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
}
func Logger(t TestingTB) hclog.InterceptLogger {
return LoggerWithOutput(t, NewLogBuffer(t))
}