testutil: redirect some test agent logs to testing.T.Logf (#5304)

When tests fail, only the logs for the failing run are dumped to the
console which helps in diagnosis. This is easily added to other test
scenarios as they come up.
This commit is contained in:
R.B. Boyer 2019-02-01 09:21:54 -06:00 committed by GitHub
parent df546ad924
commit b5d71ea779
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -43,6 +43,7 @@ func testServerConfig(t *testing.T) (string, *Config) {
config.Bootstrap = true
config.Datacenter = "dc1"
config.DataDir = dir
config.LogOutput = testutil.TestWriter(t)
// bind the rpc server to a random port. config.RPCAdvertise will be
// set to the listen address unless it was set in the configuration.

30
testutil/testlog.go Normal file
View File

@ -0,0 +1,30 @@
package testutil
import (
"io"
"log"
"strings"
"testing"
)
func TestLogger(t testing.TB) *log.Logger {
return log.New(&testWriter{t}, "test: ", log.LstdFlags)
}
func TestLoggerWithName(t testing.TB, name string) *log.Logger {
return log.New(&testWriter{t}, "test["+name+"]: ", log.LstdFlags)
}
func TestWriter(t testing.TB) io.Writer {
return &testWriter{t}
}
type testWriter struct {
t testing.TB
}
func (tw *testWriter) Write(p []byte) (n int, err error) {
tw.t.Helper()
tw.t.Log(strings.TrimSpace(string(p)))
return len(p), nil
}