diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2023-08-22 16:22:13 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2023-08-31 09:28:30 +0300 |
commit | 5aca1564c1f6d0df26bb87852e364de50270129f (patch) | |
tree | 8a12aa10c13fc0abef6f7754e44306be03931a15 | |
parent | c5d9857e0080960010d5df7343b4faf009556f6c (diff) |
Print Gitaly and Praefect logs on test failure
Gitaly and Praefect logs are currently redirected to files. To debug
test failures, one has to open the file separately and download the
log artifacts from CI. Instead of redirecting the logs to a different
files, use the recording logger and output the logs automatically when
a test case fails.
-rw-r--r-- | internal/testhelper/logger.go | 44 | ||||
-rw-r--r-- | internal/testhelper/testserver/gitaly.go | 2 | ||||
-rw-r--r-- | internal/testhelper/testserver/praefect.go | 4 |
3 files changed, 3 insertions, 47 deletions
diff --git a/internal/testhelper/logger.go b/internal/testhelper/logger.go index 83a94b129..6e3d2e3cd 100644 --- a/internal/testhelper/logger.go +++ b/internal/testhelper/logger.go @@ -2,8 +2,6 @@ package testhelper import ( "bytes" - "fmt" - "io" "os" "path/filepath" "testing" @@ -55,48 +53,6 @@ func NewLogger(tb testing.TB, options ...LoggerOption) *logrus.Logger { return logger } -// newDiscardingLogger creates a logger that discards everything. -func newDiscardingLogger(tb testing.TB) *logrus.Logger { - logger := logrus.New() //nolint:forbidigo - logger.Out = io.Discard - return logger -} - -func newServerLogger(tb testing.TB, logName string) *logrus.Logger { - logDir := CreateTestLogDir(tb) - if len(logDir) == 0 { - return newDiscardingLogger(tb) - } - - path := filepath.Join(logDir, logName) - f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_RDWR, perm.SharedFile) - require.NoError(tb, err) - - tb.Cleanup(func() { require.NoError(tb, f.Close()) }) - - logger := logrus.New() //nolint:forbidigo - logger.SetOutput(f) - logger.SetLevel(logrus.InfoLevel) - logger.SetFormatter(&logrus.JSONFormatter{}) - logger.Infof(fmt.Sprintf("=== RUN %s", tb.Name())) - - return logger -} - -// NewGitalyServerLogger creates a new logger. If the `TEST_LOG_DIR` environment variable is set the logger will write -// to "gitaly_server.log" inside of a test-specific subdirectory in the directory identified by the environment -// variable. -func NewGitalyServerLogger(tb testing.TB) *logrus.Logger { - return newServerLogger(tb, "gitaly_server.log") -} - -// NewPraefectServerLogger creates a new logger. If the `TEST_LOG_DIR` environment variable is set the logger will write -// to "praefect_server.log" inside of a test-specific subdirectory in the directory identified by the environment -// variable. -func NewPraefectServerLogger(tb testing.TB) *logrus.Logger { - return newServerLogger(tb, "praefect_server.log") -} - // CreateTestLogDir creates a new log directory for testing purposes if the environment variable // `TEST_LOG_DIR` is set. The log directory will then be created as a subdirectory of the value that // `TEST_LOG_DIR` points to. The name of the subdirectory will match the executing test's name. diff --git a/internal/testhelper/testserver/gitaly.go b/internal/testhelper/testserver/gitaly.go index 5cb357878..0b473ea18 100644 --- a/internal/testhelper/testserver/gitaly.go +++ b/internal/testhelper/testserver/gitaly.go @@ -278,7 +278,7 @@ type gitalyServerDeps struct { func (gsd *gitalyServerDeps) createDependencies(tb testing.TB, cfg config.Cfg) *service.Dependencies { if gsd.logger == nil { - gsd.logger = testhelper.NewGitalyServerLogger(tb) + gsd.logger = testhelper.NewLogger(tb, testhelper.WithLoggerName("gitaly")) } if gsd.conns == nil { diff --git a/internal/testhelper/testserver/praefect.go b/internal/testhelper/testserver/praefect.go index 4a72b4b98..dc0a6b567 100644 --- a/internal/testhelper/testserver/praefect.go +++ b/internal/testhelper/testserver/praefect.go @@ -63,8 +63,8 @@ func StartPraefect(tb testing.TB, cfg config.Config) PraefectServer { }) // Redirect log output of the server to the Praefect server logger. This will cause us to write logs into a - // Praefect-specific file. - logWriter := testhelper.NewPraefectServerLogger(tb).Writer() + // Praefect-specific logger. + logWriter := testhelper.NewLogger(tb, testhelper.WithLoggerName("praefect")).Writer() tb.Cleanup(func() { testhelper.MustClose(tb, logWriter) }) cmd := exec.Command(binaryPath, "-config", configFilePath) |