Skip to content

Commit

Permalink
*: unify the test logger (#7029)
Browse files Browse the repository at this point in the history
ref #5839

Signed-off-by: Ryan Leung <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
rleungx and ti-chi-bot[bot] authored Sep 4, 2023
1 parent 0e278e3 commit 91648e5
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pkg/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestLocalLogBackendUsingFile(t *testing.T) {
re := require.New(t)
backend := NewLocalLogBackend(true)
fname := initLog()
defer os.Remove(fname)
defer os.RemoveAll(fname)
req, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1:2379/test?test=test", strings.NewReader("testBody"))
re.False(backend.ProcessHTTPRequest(req))
info := requestutil.GetRequestInfo(req)
Expand Down Expand Up @@ -126,7 +126,7 @@ func BenchmarkLocalLogAuditUsingFile(b *testing.B) {
b.StopTimer()
backend := NewLocalLogBackend(true)
fname := initLog()
defer os.Remove(fname)
defer os.RemoveAll(fname)
req, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1:2379/test?test=test", strings.NewReader("testBody"))
b.StartTimer()
for i := 0; i < b.N; i++ {
Expand Down
4 changes: 2 additions & 2 deletions pkg/election/leadership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestExitWatch(t *testing.T) {
// Case7: loss the quorum when the watch loop is running
checkExitWatch(t, leaderKey, func(server *embed.Etcd, client *clientv3.Client) func() {
tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests")
defer os.Remove(tempStdoutFile.Name())
defer os.RemoveAll(tempStdoutFile.Name())
logCfg := &log.Config{}
logCfg.File.Filename = tempStdoutFile.Name()
logCfg.Level = "info"
Expand Down Expand Up @@ -284,7 +284,7 @@ func checkExitWatch(t *testing.T, leaderKey string, injectFunc func(server *embe
func TestRequestProgress(t *testing.T) {
checkWatcherRequestProgress := func(injectWatchChanBlock bool) {
tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests")
defer os.Remove(tempStdoutFile.Name())
defer os.RemoveAll(tempStdoutFile.Name())
logCfg := &log.Config{}
logCfg.File.Filename = tempStdoutFile.Name()
logCfg.Level = "debug"
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/etcdutil/etcdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {
func (suite *loopWatcherTestSuite) TestWatcherRequestProgress() {
checkWatcherRequestProgress := func(injectWatchChanBlock bool) {
tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests")
defer os.Remove(tempStdoutFile.Name())
defer os.RemoveAll(tempStdoutFile.Name())
cfg := &log.Config{}
cfg.File.Filename = tempStdoutFile.Name()
cfg.Level = "debug"
Expand Down
2 changes: 1 addition & 1 deletion server/api/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestGetVersion(t *testing.T) {
func() {
temp.Close()
os.Stdout = old
os.Remove(fname)
os.RemoveAll(fname)
svr.Close()
cancel()
testutil.CleanServer(cfg.DataDir)
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/tso/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (suite *tsoServerTestSuite) TestParticipantStartWithAdvertiseListenAddr() {
re.NoError(err)

// Setup the logger.
err = tests.InitTSOLogger(cfg)
err = tests.InitLogger(cfg.Log, cfg.Logger, cfg.LogProps, cfg.Security.RedactInfoLog)
re.NoError(err)

s, cleanup, err := tests.NewTSOTestServer(suite.ctx, cfg)
Expand Down
5 changes: 2 additions & 3 deletions tests/pdctl/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func TestPlacementRules(t *testing.T) {
f, _ := os.CreateTemp("/tmp", "pd_tests")
fname := f.Name()
f.Close()
defer os.RemoveAll(fname)

// test load
_, err = pdctl.ExecuteCommand(cmd, "-u", pdAddr, "config", "placement-rules", "load", "--out="+fname)
Expand Down Expand Up @@ -493,9 +494,7 @@ func TestPlacementRuleBundle(t *testing.T) {
re.NoError(err)
fname := f.Name()
f.Close()
defer func() {
os.RemoveAll(fname)
}()
defer os.RemoveAll(fname)

// test load
var bundles []placement.GroupBundle
Expand Down
2 changes: 1 addition & 1 deletion tests/scheduling_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (tc *TestSchedulingCluster) AddServer(addr string) error {
if err != nil {
return err
}
err = InitSchedulingLogger(generatedCfg)
err = InitLogger(generatedCfg.Log, generatedCfg.Logger, generatedCfg.LogProps, generatedCfg.Security.RedactInfoLog)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tests/server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (suite *middlewareTestSuite) TestAuditPrometheusBackend() {

func (suite *middlewareTestSuite) TestAuditLocalLogBackend() {
tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests")
defer os.Remove(tempStdoutFile.Name())
defer os.RemoveAll(tempStdoutFile.Name())
cfg := &log.Config{}
cfg.File.Filename = tempStdoutFile.Name()
cfg.Level = "info"
Expand Down Expand Up @@ -668,7 +668,7 @@ func (suite *redirectorTestSuite) TestXForwardedFor() {
leader := suite.cluster.GetServer(suite.cluster.GetLeader())
suite.NoError(leader.BootstrapCluster())
tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests")
defer os.Remove(tempStdoutFile.Name())
defer os.RemoveAll(tempStdoutFile.Name())
cfg := &log.Config{}
cfg.File.Filename = tempStdoutFile.Name()
cfg.Level = "info"
Expand Down
26 changes: 6 additions & 20 deletions tests/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,20 @@ import (
tso "github.com/tikv/pd/pkg/mcs/tso/server"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/zap"
)

var once sync.Once

// InitSchedulingLogger initializes the logger for test.
func InitSchedulingLogger(cfg *sc.Config) (err error) {
// InitLogger initializes the logger for test.
func InitLogger(logConfig log.Config, logger *zap.Logger, logProps *log.ZapProperties, isRedactInfoLogEnabled bool) (err error) {
once.Do(func() {
// Setup the logger.
err = logutil.SetupLogger(cfg.Log, &cfg.Logger, &cfg.LogProps, cfg.Security.RedactInfoLog)
err = logutil.SetupLogger(logConfig, &logger, &logProps, isRedactInfoLogEnabled)
if err != nil {
return
}
log.ReplaceGlobals(cfg.Logger, cfg.LogProps)
// Flushing any buffered log entries.
log.Sync()
})
return err
}

// InitTSOLogger initializes the logger for test.
func InitTSOLogger(cfg *tso.Config) (err error) {
once.Do(func() {
// Setup the logger.
err = logutil.SetupLogger(cfg.Log, &cfg.Logger, &cfg.LogProps, cfg.Security.RedactInfoLog)
if err != nil {
return
}
log.ReplaceGlobals(cfg.Logger, cfg.LogProps)
log.ReplaceGlobals(logger, logProps)
// Flushing any buffered log entries.
log.Sync()
})
Expand Down Expand Up @@ -88,7 +74,7 @@ func StartSingleTSOTestServerWithoutCheck(ctx context.Context, re *require.Asser
cfg, err := tso.GenerateConfig(cfg)
re.NoError(err)
// Setup the logger.
err = InitTSOLogger(cfg)
err = InitLogger(cfg.Log, cfg.Logger, cfg.LogProps, cfg.Security.RedactInfoLog)
re.NoError(err)
return NewTSOTestServer(ctx, cfg)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tso_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (tc *TestTSOCluster) AddServer(addr string) error {
if err != nil {
return err
}
err = InitTSOLogger(generatedCfg)
err = InitLogger(generatedCfg.Log, generatedCfg.Logger, generatedCfg.LogProps, generatedCfg.Security.RedactInfoLog)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-backup/pdbackup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *backupTestSuite) TestGetBackupInfo() {

tmpFile, err := os.CreateTemp(os.TempDir(), "pd_backup_info_test.json")
s.NoError(err)
defer os.Remove(tmpFile.Name())
defer os.RemoveAll(tmpFile.Name())

s.NoError(OutputToFile(actual, tmpFile))
_, err = tmpFile.Seek(0, 0)
Expand Down

0 comments on commit 91648e5

Please sign in to comment.