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:
parent
df546ad924
commit
b5d71ea779
|
@ -43,6 +43,7 @@ func testServerConfig(t *testing.T) (string, *Config) {
|
||||||
config.Bootstrap = true
|
config.Bootstrap = true
|
||||||
config.Datacenter = "dc1"
|
config.Datacenter = "dc1"
|
||||||
config.DataDir = dir
|
config.DataDir = dir
|
||||||
|
config.LogOutput = testutil.TestWriter(t)
|
||||||
|
|
||||||
// bind the rpc server to a random port. config.RPCAdvertise will be
|
// bind the rpc server to a random port. config.RPCAdvertise will be
|
||||||
// set to the listen address unless it was set in the configuration.
|
// set to the listen address unless it was set in the configuration.
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue