From e72c2c40d4f4aef952fe9c0afd403b1a10abf8a6 Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Sun, 17 Sep 2023 02:09:13 +0000 Subject: [PATCH] *: fix nakedret lint Signed-off-by: Wei Fu --- client/pkg/transport/keepalive_listener.go | 6 ++-- client/pkg/transport/listener.go | 35 ++++++++++++---------- etcdctl/main_test.go | 6 ++-- etcdutl/main_test.go | 6 ++-- scripts/test.sh | 3 +- server/etcdserver/bootstrap_test.go | 33 ++++++++++++-------- server/main_test.go | 6 ++-- server/storage/wal/wal.go | 2 +- tests/robustness/validate/operations.go | 8 ++--- 9 files changed, 60 insertions(+), 45 deletions(-) diff --git a/client/pkg/transport/keepalive_listener.go b/client/pkg/transport/keepalive_listener.go index 07b2dc13678..b3f3e011617 100644 --- a/client/pkg/transport/keepalive_listener.go +++ b/client/pkg/transport/keepalive_listener.go @@ -101,10 +101,10 @@ type tlsKeepaliveListener struct { // Accept waits for and returns the next incoming TLS connection. // The returned connection c is a *tls.Conn. -func (l *tlsKeepaliveListener) Accept() (c net.Conn, err error) { - c, err = l.Listener.Accept() +func (l *tlsKeepaliveListener) Accept() (net.Conn, error) { + c, err := l.Listener.Accept() if err != nil { - return + return nil, err } c = tls.Server(c, l.config) diff --git a/client/pkg/transport/listener.go b/client/pkg/transport/listener.go index 5e0e13e25a7..314a736044b 100644 --- a/client/pkg/transport/listener.go +++ b/client/pkg/transport/listener.go @@ -103,14 +103,17 @@ func newListener(addr, scheme string, opts ...ListenerOption) (net.Listener, err return wrapTLS(scheme, lnOpts.tlsInfo, lnOpts.Listener) } -func newKeepAliveListener(cfg *net.ListenConfig, addr string) (ln net.Listener, err error) { +func newKeepAliveListener(cfg *net.ListenConfig, addr string) (net.Listener, error) { + var ln net.Listener + var err error + if cfg != nil { ln, err = cfg.Listen(context.TODO(), "tcp", addr) } else { ln, err = net.Listen("tcp", addr) } if err != nil { - return + return nil, err } return NewKeepAliveListener(ln, "tcp", nil) @@ -204,16 +207,18 @@ func (info TLSInfo) Empty() bool { return info.CertFile == "" && info.KeyFile == "" } -func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertValidity uint, additionalUsages ...x509.ExtKeyUsage) (info TLSInfo, err error) { +func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertValidity uint, additionalUsages ...x509.ExtKeyUsage) (TLSInfo, error) { verify.Assert(lg != nil, "nil log isn't allowed") - info.Logger = lg + + var err error + info := TLSInfo{Logger: lg} if selfSignedCertValidity == 0 { err = errors.New("selfSignedCertValidity is invalid,it should be greater than 0") info.Logger.Warn( "cannot generate cert", zap.Error(err), ) - return + return info, err } err = fileutil.TouchDirAll(lg, dirpath) if err != nil { @@ -223,16 +228,16 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali zap.Error(err), ) } - return + return info, err } certPath, err := filepath.Abs(filepath.Join(dirpath, "cert.pem")) if err != nil { - return + return info, err } keyPath, err := filepath.Abs(filepath.Join(dirpath, "key.pem")) if err != nil { - return + return info, err } _, errcert := os.Stat(certPath) _, errkey := os.Stat(keyPath) @@ -242,7 +247,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali info.ClientCertFile = certPath info.ClientKeyFile = keyPath info.selfCert = true - return + return info, err } serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) @@ -254,7 +259,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali zap.Error(err), ) } - return + return info, err } tmpl := x509.Certificate{ @@ -292,7 +297,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali zap.Error(err), ) } - return + return info, err } derBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &priv.PublicKey, priv) @@ -303,7 +308,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali zap.Error(err), ) } - return + return info, err } certOut, err := os.Create(certPath) @@ -313,7 +318,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali zap.String("path", certPath), zap.Error(err), ) - return + return info, err } pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) certOut.Close() @@ -323,7 +328,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali b, err := x509.MarshalECPrivateKey(priv) if err != nil { - return + return info, err } keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { @@ -334,7 +339,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali zap.Error(err), ) } - return + return info, err } pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}) keyOut.Close() diff --git a/etcdctl/main_test.go b/etcdctl/main_test.go index 8edbb0ae9b8..501f9e7fd21 100644 --- a/etcdctl/main_test.go +++ b/etcdctl/main_test.go @@ -21,7 +21,9 @@ import ( "testing" ) -func SplitTestArgs(args []string) (testArgs, appArgs []string) { +func SplitTestArgs(args []string) ([]string, []string) { + var testArgs, appArgs []string + for i, arg := range args { switch { case strings.HasPrefix(arg, "-test."): @@ -33,7 +35,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) { appArgs = append(appArgs, arg) } } - return + return testArgs, appArgs } // TestEmpty is an empty test to avoid no-tests warning. diff --git a/etcdutl/main_test.go b/etcdutl/main_test.go index 1fe58afb837..118cfa655a5 100644 --- a/etcdutl/main_test.go +++ b/etcdutl/main_test.go @@ -21,7 +21,9 @@ import ( "testing" ) -func SplitTestArgs(args []string) (testArgs, appArgs []string) { +func SplitTestArgs(args []string) ([]string, []string) { + var testArgs, appArgs []string + for i, arg := range args { switch { case strings.HasPrefix(arg, "-test."): @@ -33,7 +35,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) { appArgs = append(appArgs, arg) } } - return + return testArgs, appArgs } // TestEmpty is empty test to avoid no-tests warning. diff --git a/scripts/test.sh b/scripts/test.sh index c8c6550ffa6..0bd2b6a02ce 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -424,8 +424,7 @@ function ineffassign_pass { } function nakedret_pass { - # TODO: nakedret should work with -set_exit_status - run_for_modules generic_checker run_go_tool "github.com/alexkohler/nakedret" + run_for_modules generic_checker run_go_tool "github.com/alexkohler/nakedret/cmd/nakedret" } function license_header_per_module { diff --git a/server/etcdserver/bootstrap_test.go b/server/etcdserver/bootstrap_test.go index 55a20684fe8..b2584c67faa 100644 --- a/server/etcdserver/bootstrap_test.go +++ b/server/etcdserver/bootstrap_test.go @@ -216,27 +216,32 @@ func TestBootstrapBackend(t *testing.T) { } } -func createDataDir(t *testing.T) (dataDir string, err error) { +func createDataDir(t *testing.T) (string, error) { + var err error + // create the temporary data dir - dataDir = t.TempDir() + dataDir := t.TempDir() // create ${dataDir}/member/snap if err = os.MkdirAll(datadir.ToSnapDir(dataDir), 0700); err != nil { - return + return "", err } // create ${dataDir}/member/wal err = os.MkdirAll(datadir.ToWalDir(dataDir), 0700) + if err != nil { + return "", err + } - return + return dataDir, nil } // prepare data for the test case -func prepareData(cfg config.ServerConfig) (err error) { +func prepareData(cfg config.ServerConfig) error { var snapshotTerm, snapshotIndex uint64 = 2, 5 - if err = createWALFileWithSnapshotRecord(cfg, snapshotTerm, snapshotIndex); err != nil { - return + if err := createWALFileWithSnapshotRecord(cfg, snapshotTerm, snapshotIndex); err != nil { + return err } return createSnapshotAndBackendDB(cfg, snapshotTerm, snapshotIndex) @@ -245,7 +250,7 @@ func prepareData(cfg config.ServerConfig) (err error) { func createWALFileWithSnapshotRecord(cfg config.ServerConfig, snapshotTerm, snapshotIndex uint64) (err error) { var w *wal.WAL if w, err = wal.Create(cfg.Logger, cfg.WALDir(), []byte("somedata")); err != nil { - return + return err } defer func() { @@ -262,13 +267,15 @@ func createWALFileWithSnapshotRecord(cfg config.ServerConfig, snapshotTerm, snap } if err = w.SaveSnapshot(walSnap); err != nil { - return + return err } return w.Save(raftpb.HardState{Term: snapshotTerm, Vote: 3, Commit: snapshotIndex}, nil) } -func createSnapshotAndBackendDB(cfg config.ServerConfig, snapshotTerm, snapshotIndex uint64) (err error) { +func createSnapshotAndBackendDB(cfg config.ServerConfig, snapshotTerm, snapshotIndex uint64) error { + var err error + confState := raftpb.ConfState{ Voters: []uint64{1, 2, 3}, } @@ -283,7 +290,7 @@ func createSnapshotAndBackendDB(cfg config.ServerConfig, snapshotTerm, snapshotI Term: snapshotTerm, }, }); err != nil { - return + return err } // create snapshot db file: "%016x.snap.db" @@ -292,11 +299,11 @@ func createSnapshotAndBackendDB(cfg config.ServerConfig, snapshotTerm, snapshotI schema.UnsafeUpdateConsistentIndex(be.BatchTx(), snapshotIndex, snapshotTerm) schema.MustUnsafeSaveConfStateToBackend(cfg.Logger, be.BatchTx(), &confState) if err = be.Close(); err != nil { - return + return err } sdb := filepath.Join(cfg.SnapDir(), fmt.Sprintf("%016x.snap.db", snapshotIndex)) if err = os.Rename(cfg.BackendPath(), sdb); err != nil { - return + return err } // create backend db file diff --git a/server/main_test.go b/server/main_test.go index bc96bb33d74..e537ba54818 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -25,7 +25,9 @@ import ( "go.etcd.io/etcd/server/v3/etcdmain" ) -func SplitTestArgs(args []string) (testArgs, appArgs []string) { +func SplitTestArgs(args []string) ([]string, []string) { + var testArgs, appArgs []string + for i, arg := range args { switch { case strings.HasPrefix(arg, "-test."): @@ -37,7 +39,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) { appArgs = append(appArgs, arg) } } - return + return testArgs, appArgs } func TestEmpty(t *testing.T) {} diff --git a/server/storage/wal/wal.go b/server/storage/wal/wal.go index 7f8b25f5ddd..4eeafbd9098 100644 --- a/server/storage/wal/wal.go +++ b/server/storage/wal/wal.go @@ -553,7 +553,7 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb. // create encoder (chain crc with the decoder), enable appending w.encoder, err = newFileEncoder(w.tail().File, w.decoder.LastCRC()) if err != nil { - return + return nil, state, nil, err } } w.decoder = nil diff --git a/tests/robustness/validate/operations.go b/tests/robustness/validate/operations.go index 9705531ecf2..a0df260e98b 100644 --- a/tests/robustness/validate/operations.go +++ b/tests/robustness/validate/operations.go @@ -28,24 +28,22 @@ import ( "go.etcd.io/etcd/tests/v3/robustness/model" ) -func validateOperationsAndVisualize(t *testing.T, lg *zap.Logger, operations []porcupine.Operation, eventHistory []model.WatchEvent) (visualize func(basepath string) error) { +func validateOperationsAndVisualize(t *testing.T, lg *zap.Logger, operations []porcupine.Operation, eventHistory []model.WatchEvent) func(basepath string) error { const timeout = 5 * time.Minute lg.Info("Validating linearizable operations", zap.Duration("timeout", timeout)) result, visualize := validateLinearizableOperationAndVisualize(lg, operations, timeout) switch result { case porcupine.Illegal: t.Error("Linearization failed") - return case porcupine.Unknown: t.Error("Linearization has timed out") - return case porcupine.Ok: t.Log("Linearization success") + lg.Info("Validating serializable operations") + validateSerializableOperations(t, operations, eventHistory) default: t.Fatalf("Unknown Linearization") } - lg.Info("Validating serializable operations") - validateSerializableOperations(t, operations, eventHistory) return visualize }