Skip to content

Commit

Permalink
Merge pull request #9678 from gyuho/integration-logger
Browse files Browse the repository at this point in the history
integration: use "zap" in tests
  • Loading branch information
gyuho authored May 1, 2018
2 parents f85c3bf + 6cf3dae commit c94cd5e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ See [security doc](https://github.com/coreos/etcd/blob/master/Documentation/op-g
- Rename [**`embed.Config.LogOutput`** to **`embed.Config.LogOutputs`**](https://github.com/coreos/etcd/pull/9624) to support multiple log outputs.
- Change [**`embed.Config.LogOutputs`** type from `string` to `[]string`](https://github.com/coreos/etcd/pull/9579) to support multiple log outputs.

### Package `integration`

- Add [`CLUSTER_DEBUG` to enable test cluster logging](https://github.com/coreos/etcd/pull/9678).
- Deprecated `capnslog` in integration tests.

### API

- Add [`snapshot`](https://github.com/coreos/etcd/pull/9118) package for snapshot restore/save operations (see [`godoc.org/github.com/etcd/snapshot`](https://godoc.org/github.com/coreos/etcd/snapshot) for more).
Expand Down
2 changes: 0 additions & 2 deletions clientv3/integration/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ import (

"github.com/coreos/etcd/clientv3"

"github.com/coreos/pkg/capnslog"
"google.golang.org/grpc/grpclog"
)

func init() {
capnslog.SetGlobalLogLevel(capnslog.CRITICAL)
clientv3.SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
}
2 changes: 0 additions & 2 deletions clientv3/ordering/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ import (

"github.com/coreos/etcd/clientv3"

"github.com/coreos/pkg/capnslog"
"google.golang.org/grpc/grpclog"
)

func init() {
capnslog.SetGlobalLogLevel(capnslog.CRITICAL)
clientv3.SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
}
2 changes: 1 addition & 1 deletion etcdserver/api/v3rpc/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (ws *watchServer) Watch(stream pb.Watch_WatchServer) (err error) {
if rerr := sws.recvLoop(); rerr != nil {
if isClientCtxErr(stream.Context().Err(), rerr) {
if sws.lg != nil {
sws.lg.Debug("failed to receive watch request from gRPC stream", zap.Error(err))
sws.lg.Debug("failed to receive watch request from gRPC stream", zap.Error(rerr))
} else {
plog.Debugf("failed to receive watch request from gRPC stream (%q)", rerr.Error())
}
Expand Down
2 changes: 0 additions & 2 deletions etcdserver/v2store/store_v2v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ import (
"github.com/coreos/etcd/etcdserver/v2store"
"github.com/coreos/etcd/integration"

"github.com/coreos/pkg/capnslog"
"google.golang.org/grpc/grpclog"
)

func init() {
capnslog.SetGlobalLogLevel(capnslog.CRITICAL)
clientv3.SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
}

Expand Down
98 changes: 87 additions & 11 deletions integration/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ import (
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/rafthttp"

"github.com/coreos/pkg/capnslog"
"github.com/soheilhy/cmux"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -105,9 +105,15 @@ var (
ClientCertAuth: true,
}

plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "integration")
lg = zap.NewNop()
)

func init() {
if os.Getenv("CLUSTER_DEBUG") != "" {
lg, _ = zap.NewProduction()
}
}

type ClusterConfig struct {
Size int
PeerTLS *transport.TLSInfo
Expand Down Expand Up @@ -626,14 +632,35 @@ func mustNewMember(t *testing.T, mcfg memberConfig) *member {

m.InitialCorruptCheck = true

m.LoggerConfig = &zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),

OutputPaths: []string{"/dev/null"},
ErrorOutputPaths: []string{"/dev/null"},
}
if os.Getenv("CLUSTER_DEBUG") != "" {
m.LoggerConfig.OutputPaths = []string{"stderr"}
m.LoggerConfig.ErrorOutputPaths = []string{"stderr"}
}
m.Logger, err = m.LoggerConfig.Build()
if err != nil {
t.Fatal(err)
}
return m
}

// listenGRPC starts a grpc server over a unix domain socket on the member
func (m *member) listenGRPC() error {
// prefix with localhost so cert has right domain
m.grpcAddr = "localhost:" + m.Name
if m.useIP { // for IP-only sTLS certs
if m.useIP { // for IP-only TLS certs
m.grpcAddr = "127.0.0.1:" + m.Name
}
l, err := transport.NewUnixListener(m.grpcAddr)
Expand Down Expand Up @@ -720,7 +747,13 @@ func (m *member) Clone(t *testing.T) *member {
// Launch starts a member based on ServerConfig, PeerListeners
// and ClientListeners.
func (m *member) Launch() error {
plog.Printf("launching %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"launching a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
var err error
if m.s, err = etcdserver.NewServer(m.ServerConfig); err != nil {
return fmt.Errorf("failed to initialize the etcd server: %v", err)
Expand Down Expand Up @@ -860,7 +893,13 @@ func (m *member) Launch() error {
m.serverClosers = append(m.serverClosers, closer)
}

plog.Printf("launched %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"launched a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
return nil
}

Expand Down Expand Up @@ -920,10 +959,22 @@ func (m *member) Close() {

// Stop stops the member, but the data dir of the member is preserved.
func (m *member) Stop(t *testing.T) {
plog.Printf("stopping %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"stopping a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
m.Close()
m.serverClosers = nil
plog.Printf("stopped %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"stopped a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
}

// checkLeaderTransition waits for leader transition, returning the new leader ID.
Expand All @@ -942,7 +993,13 @@ func (m *member) StopNotify() <-chan struct{} {

// Restart starts the member using the preserved data dir.
func (m *member) Restart(t *testing.T) error {
plog.Printf("restarting %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"restarting a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
newPeerListeners := make([]net.Listener, 0)
for _, ln := range m.PeerListeners {
newPeerListeners = append(newPeerListeners, NewListenerWithAddr(t, ln.Addr().String()))
Expand All @@ -961,20 +1018,39 @@ func (m *member) Restart(t *testing.T) error {
}

err := m.Launch()
plog.Printf("restarted %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"restarted a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
zap.Error(err),
)
return err
}

// Terminate stops the member and removes the data dir.
func (m *member) Terminate(t *testing.T) {
plog.Printf("terminating %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"terminating a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
m.Close()
if !m.keepDataDirTerminate {
if err := os.RemoveAll(m.ServerConfig.DataDir); err != nil {
t.Fatal(err)
}
}
plog.Printf("terminated %s (%s)", m.Name, m.grpcAddr)
lg.Info(
"terminated a member",
zap.String("name", m.Name),
zap.Strings("advertise-peer-urls", m.PeerURLs.StringSlice()),
zap.Strings("listen-client-urls", m.ClientURLs.StringSlice()),
zap.String("grpc-address", m.grpcAddr),
)
}

// Metric gets the metric value for a member
Expand Down
4 changes: 0 additions & 4 deletions integration/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ import (

"github.com/coreos/etcd/clientv3"

"github.com/coreos/pkg/capnslog"
"google.golang.org/grpc/grpclog"
)

const defaultLogLevel = capnslog.CRITICAL

func init() {
capnslog.SetGlobalLogLevel(defaultLogLevel)
clientv3.SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
}

0 comments on commit c94cd5e

Please sign in to comment.