From 725a8c5e027e4e23fa2fedbba15a41c6654ce74e Mon Sep 17 00:00:00 2001 From: Piotr Tabor Date: Tue, 16 Mar 2021 22:21:13 +0100 Subject: [PATCH] Enable configuring delegated zap-logging for embed server. --- server/embed/config_logging.go | 7 ++++- .../clientv3/snapshot/v3_snapshot_test.go | 7 ++--- tests/integration/snapshot/member_test.go | 9 +++---- .../integration/snapshot/v3_snapshot_test.go | 27 ++++++------------- tests/integration/testing.go | 10 +++++++ 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/server/embed/config_logging.go b/server/embed/config_logging.go index 7ab81191e..9092b37a7 100644 --- a/server/embed/config_logging.go +++ b/server/embed/config_logging.go @@ -21,6 +21,7 @@ import ( "os" "sync" + grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap" "go.etcd.io/etcd/pkg/v3/logutil" "go.uber.org/zap" @@ -232,7 +233,11 @@ func NewZapCoreLoggerBuilder(lg *zap.Logger, cr zapcore.Core, syncer zapcore.Wri cfg.loggerWriteSyncer = syncer grpcLogOnce.Do(func() { - grpclog.SetLoggerV2(logutil.NewGRPCLoggerV2FromZapCore(cr, syncer)) + if cr != nil && syncer != nil { + grpclog.SetLoggerV2(logutil.NewGRPCLoggerV2FromZapCore(cr, syncer)) + } else { + grpc_zap.ReplaceGrpcLoggerV2(cfg.logger.Named("grpc")) + } }) return nil } diff --git a/tests/integration/clientv3/snapshot/v3_snapshot_test.go b/tests/integration/clientv3/snapshot/v3_snapshot_test.go index 87da75683..3f8f200eb 100644 --- a/tests/integration/clientv3/snapshot/v3_snapshot_test.go +++ b/tests/integration/clientv3/snapshot/v3_snapshot_test.go @@ -29,6 +29,7 @@ import ( "go.etcd.io/etcd/pkg/v3/fileutil" "go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/server/v3/embed" + "go.etcd.io/etcd/tests/v3/integration" "go.uber.org/zap" ) @@ -64,15 +65,11 @@ func createSnapshotFile(t *testing.T, kvs []kv) string { urls := newEmbedURLs(clusterN * 2) cURLs, pURLs := urls[:clusterN], urls[clusterN:] - cfg := embed.NewConfig() - cfg.Logger = "zap" - cfg.LogOutputs = []string{"/dev/null"} - cfg.Name = "default" + cfg := integration.NewEmbedConfig(t, "default") cfg.ClusterState = "new" cfg.LCUrls, cfg.ACUrls = cURLs, cURLs cfg.LPUrls, cfg.APUrls = pURLs, pURLs cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) - cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond())) srv, err := embed.StartEtcd(cfg) if err != nil { t.Fatal(err) diff --git a/tests/integration/snapshot/member_test.go b/tests/integration/snapshot/member_test.go index 22997272d..932aa1fd2 100644 --- a/tests/integration/snapshot/member_test.go +++ b/tests/integration/snapshot/member_test.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "os" - "path/filepath" "testing" "time" @@ -26,6 +25,7 @@ import ( "go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/server/v3/embed" "go.etcd.io/etcd/server/v3/etcdserver" + "go.etcd.io/etcd/tests/v3/integration" ) // TestSnapshotV3RestoreMultiMemberAdd ensures that multiple members @@ -37,6 +37,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) { clusterN := 3 cURLs, pURLs, srvs := restoreCluster(t, clusterN, dbPath) + defer func() { for i := 0; i < clusterN; i++ { os.RemoveAll(srvs[i].Config().Dir) @@ -62,10 +63,7 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) { // wait for membership reconfiguration apply time.Sleep(testutil.ApplyTimeout) - cfg := embed.NewConfig() - cfg.Logger = "zap" - cfg.LogOutputs = []string{"/dev/null"} - cfg.Name = "3" + cfg := integration.NewEmbedConfig(t, "3") cfg.InitialClusterToken = testClusterTkn cfg.ClusterState = "existing" cfg.LCUrls, cfg.ACUrls = newCURLs, newCURLs @@ -76,7 +74,6 @@ func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) { } cfg.InitialCluster = cfg.InitialCluster[1:] cfg.InitialCluster += fmt.Sprintf(",%s=%s", cfg.Name, newPURLs[0].String()) - cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond())) srv, err := embed.StartEtcd(cfg) if err != nil { diff --git a/tests/integration/snapshot/v3_snapshot_test.go b/tests/integration/snapshot/v3_snapshot_test.go index 0b7fcc52c..8c3f4ae46 100644 --- a/tests/integration/snapshot/v3_snapshot_test.go +++ b/tests/integration/snapshot/v3_snapshot_test.go @@ -29,6 +29,7 @@ import ( "go.etcd.io/etcd/etcdctl/v3/snapshot" "go.etcd.io/etcd/pkg/v3/testutil" "go.etcd.io/etcd/server/v3/embed" + "go.etcd.io/etcd/tests/v3/integration" "go.uber.org/zap/zaptest" ) @@ -43,16 +44,12 @@ func TestSnapshotV3RestoreSingle(t *testing.T) { urls := newEmbedURLs(clusterN * 2) cURLs, pURLs := urls[:clusterN], urls[clusterN:] - cfg := embed.NewConfig() - cfg.Logger = "zap" - cfg.LogOutputs = []string{"/dev/null"} - cfg.Name = "s1" + cfg := integration.NewEmbedConfig(t, "s1") cfg.InitialClusterToken = testClusterTkn cfg.ClusterState = "existing" cfg.LCUrls, cfg.ACUrls = cURLs, cURLs cfg.LPUrls, cfg.APUrls = pURLs, pURLs cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) - cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond())) sp := snapshot.NewV3(zaptest.NewLogger(t)) pss := make([]string, 0, len(pURLs)) @@ -108,13 +105,11 @@ func TestSnapshotV3RestoreSingle(t *testing.T) { func TestSnapshotV3RestoreMulti(t *testing.T) { kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}} dbPath := createSnapshotFile(t, kvs) - defer os.RemoveAll(dbPath) clusterN := 3 cURLs, _, srvs := restoreCluster(t, clusterN, dbPath) defer func() { for i := 0; i < clusterN; i++ { - os.RemoveAll(srvs[i].Config().Dir) srvs[i].Close() } }() @@ -143,7 +138,8 @@ func TestSnapshotV3RestoreMulti(t *testing.T) { // TestCorruptedBackupFileCheck tests if we can correctly identify a corrupted backup file. func TestCorruptedBackupFileCheck(t *testing.T) { - dbPath := "testdata/corrupted_backup.db" + dbPath := integration.MustAbsPath("testdata/corrupted_backup.db") + integration.BeforeTest(t) if _, err := os.Stat(dbPath); err != nil { t.Fatalf("test file [%s] does not exist: %v", dbPath, err) } @@ -178,21 +174,16 @@ func createSnapshotFile(t *testing.T, kvs []kv) string { urls := newEmbedURLs(clusterN * 2) cURLs, pURLs := urls[:clusterN], urls[clusterN:] - cfg := embed.NewConfig() - cfg.Logger = "zap" - cfg.LogOutputs = []string{"/dev/null"} - cfg.Name = "default" + cfg := integration.NewEmbedConfig(t, "default") cfg.ClusterState = "new" cfg.LCUrls, cfg.ACUrls = cURLs, cURLs cfg.LPUrls, cfg.APUrls = pURLs, pURLs cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String()) - cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond())) srv, err := embed.StartEtcd(cfg) if err != nil { t.Fatal(err) } defer func() { - os.RemoveAll(cfg.Dir) srv.Close() }() select { @@ -244,18 +235,15 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) ( cfgs := make([]*embed.Config, clusterN) for i := 0; i < clusterN; i++ { - cfg := embed.NewConfig() - cfg.Logger = "zap" - cfg.LogOutputs = []string{"/dev/null"} - cfg.Name = fmt.Sprintf("%d", i) + cfg := integration.NewEmbedConfig(t, fmt.Sprintf("%d", i)) cfg.InitialClusterToken = testClusterTkn cfg.ClusterState = "existing" cfg.LCUrls, cfg.ACUrls = []url.URL{cURLs[i]}, []url.URL{cURLs[i]} cfg.LPUrls, cfg.APUrls = []url.URL{pURLs[i]}, []url.URL{pURLs[i]} cfg.InitialCluster = ics - cfg.Dir = filepath.Join(t.TempDir(), fmt.Sprint(time.Now().Nanosecond()+i)) sp := snapshot.NewV3(zaptest.NewLogger(t)) + if err := sp.Restore(snapshot.RestoreConfig{ SnapshotPath: dbPath, Name: cfg.Name, @@ -266,6 +254,7 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) ( }); err != nil { t.Fatal(err) } + cfgs[i] = cfg } diff --git a/tests/integration/testing.go b/tests/integration/testing.go index 8f558aa47..4e1e9e981 100644 --- a/tests/integration/testing.go +++ b/tests/integration/testing.go @@ -17,10 +17,12 @@ package integration import ( "os" "path/filepath" + "testing" grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable" grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap" "go.etcd.io/etcd/pkg/v3/testutil" + "go.etcd.io/etcd/server/v3/embed" "go.uber.org/zap/zaptest" ) @@ -54,3 +56,11 @@ func MustAbsPath(path string) string { } return abs } + +func NewEmbedConfig(t testing.TB, name string) *embed.Config { + cfg := embed.NewConfig() + cfg.Name = name + cfg.ZapLoggerBuilder = embed.NewZapCoreLoggerBuilder(zaptest.NewLogger(t).Named(cfg.Name), nil, nil) + cfg.Dir = t.TempDir() + return cfg +}