blob: ee95578ebc8adbf54f9ad5da98e599d487213d68 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package testhelper
import (
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v15/internal/helper/perm"
)
// NewDiscardingLogger creates a logger that discards everything.
func NewDiscardingLogger(tb testing.TB) *logrus.Logger {
logger := logrus.New()
logger.Out = io.Discard
return logger
}
// NewDiscardingLogEntry creates a logrus entry that discards everything.
func NewDiscardingLogEntry(tb testing.TB) *logrus.Entry {
return logrus.NewEntry(NewDiscardingLogger(tb))
}
// NewGitalyServerLogger creates a logger for Gitaly servers started by current test. The logger
// writes logs to gitaly_server.log inside log dir from TEST_LOG_DIR env.
func NewGitalyServerLogger(tb testing.TB) *logrus.Logger {
logDir := CreateTestLogDir(tb)
if len(logDir) == 0 {
return NewDiscardingLogger(tb)
}
path := filepath.Join(logDir, "gitaly_server.log")
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()
logger.SetOutput(f)
logger.SetLevel(logrus.InfoLevel)
logger.SetFormatter(&logrus.JSONFormatter{})
logger.Infof(fmt.Sprintf("=== RUN %s", tb.Name()))
return logger
}
// 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.
//
// Returns the name of the created log directory. If the environment variable is not set then this
// functions returns an empty string.
func CreateTestLogDir(tb testing.TB) string {
testLogDir := os.Getenv("TEST_LOG_DIR")
if len(testLogDir) == 0 {
return ""
}
logDir := filepath.Join(testLogDir, tb.Name())
require.NoError(tb, os.MkdirAll(logDir, perm.SharedDir))
return logDir
}
|