mirror of
1
Fork 0

[chore] Log less output on failed test (#2804)

* [chore] Log less output on failed test

This changes the testrig log level to be error by default instead of
info. This makes test failures a lot easier to read, as we don't have
the parade of info logs for each failure to scroll through.

It speeds up the test suite by a couple of seconds since we need
to buffer and flush a lot less messages. On a clean run, so no test
failures, it's about a 3s difference on my machine. Depending on the
amount of test failures, total time saved can vary.

This also introduces a GTS_TESTRIG_LOG_LEVEL environment variable that
we explicitly check for, making it easy to override the log level should
we have a need for it. This would be primarily for running locally, and
not so much as part of go test.

Lastly, it updates the syslog tests to use log.Error because if the log
level is set to error but we call log.Info no message is emitted and we
hang indefinitely on the channel read.

* [chore] Rename the testrig log level env var
This commit is contained in:
Daenney 2024-04-04 17:42:42 +02:00 committed by GitHub
parent 8ed1b8142c
commit 236604bd6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 11 deletions

View File

@ -62,10 +62,10 @@ func (suite *SyslogTestSuite) TearDownTest() {
} }
func (suite *SyslogTestSuite) TestSyslog() { func (suite *SyslogTestSuite) TestSyslog() {
log.Info(nil, "this is a test of the emergency broadcast system!") log.Error(nil, "this is a test of the emergency broadcast system!")
entry := <-suite.syslogChannel entry := <-suite.syslogChannel
suite.Regexp(regexp.MustCompile(`timestamp=.* func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"]) suite.Regexp(regexp.MustCompile(`timestamp=.* func=.* level=ERROR msg="this is a test of the emergency broadcast system!"`), entry["content"])
} }
func (suite *SyslogTestSuite) TestSyslogDisableTimestamp() { func (suite *SyslogTestSuite) TestSyslogDisableTimestamp() {
@ -78,20 +78,20 @@ func (suite *SyslogTestSuite) TestSyslogDisableTimestamp() {
// Set old timestamp on return. // Set old timestamp on return.
defer log.SetTimeFormat(timefmt) defer log.SetTimeFormat(timefmt)
log.Info(nil, "this is a test of the emergency broadcast system!") log.Error(nil, "this is a test of the emergency broadcast system!")
entry := <-suite.syslogChannel entry := <-suite.syslogChannel
suite.Regexp(regexp.MustCompile(`func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"]) suite.Regexp(regexp.MustCompile(`func=.* level=ERROR msg="this is a test of the emergency broadcast system!"`), entry["content"])
} }
func (suite *SyslogTestSuite) TestSyslogLongMessage() { func (suite *SyslogTestSuite) TestSyslogLongMessage() {
log.Warn(nil, longMessage) log.Error(nil, longMessage)
funcName := log.Caller(2) funcName := log.Caller(2)
prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=WARN msg="`, funcName) prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=ERROR msg="`, funcName)
entry := <-suite.syslogChannel entry := <-suite.syslogChannel
regex := fmt.Sprintf(`timestamp=.* func=.* level=WARN msg="%s`, longMessage[:2048-len(prefix)]) regex := fmt.Sprintf(`timestamp=.* func=.* level=ERROR msg="%s`, longMessage[:2048-len(prefix)])
suite.Regexp(regexp.MustCompile(regex), entry["content"]) suite.Regexp(regexp.MustCompile(regex), entry["content"])
} }

View File

@ -53,13 +53,13 @@ func (suite *SyslogTestSuite) TestSyslogLongMessageUnixgram() {
testrig.InitTestLog() testrig.InitTestLog()
log.Warn(nil, longMessage) log.Error(nil, longMessage)
funcName := log.Caller(2) funcName := log.Caller(2)
prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=WARN msg="`, funcName) prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=ERROR msg="`, funcName)
entry := <-syslogChannel entry := <-syslogChannel
regex := fmt.Sprintf(`timestamp=.* func=.* level=WARN msg="%s`, longMessage[:2048-len(prefix)]) regex := fmt.Sprintf(`timestamp=.* func=.* level=ERROR msg="%s`, longMessage[:2048-len(prefix)])
suite.Regexp(regexp.MustCompile(regex), entry["content"]) suite.Regexp(regexp.MustCompile(regex), entry["content"])

View File

@ -18,6 +18,7 @@
package testrig package testrig
import ( import (
"os"
"time" "time"
"codeberg.org/gruf/go-bytesize" "codeberg.org/gruf/go-bytesize"
@ -33,8 +34,16 @@ func InitTestConfig() {
}) })
} }
func logLevel() string {
level := "error"
if lv := os.Getenv("GTS_LOG_LEVEL"); lv != "" {
level = lv
}
return level
}
var testDefaults = config.Configuration{ var testDefaults = config.Configuration{
LogLevel: "info", LogLevel: logLevel(),
LogTimestampFormat: "02/01/2006 15:04:05.000", LogTimestampFormat: "02/01/2006 15:04:05.000",
LogDbQueries: true, LogDbQueries: true,
ApplicationName: "gotosocial", ApplicationName: "gotosocial",