*: fix nakedret lint

Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
Wei Fu
2023-09-17 02:09:13 +00:00
parent fb8a315be6
commit e72c2c40d4
9 changed files with 60 additions and 45 deletions

View File

@ -101,10 +101,10 @@ type tlsKeepaliveListener struct {
// Accept waits for and returns the next incoming TLS connection. // Accept waits for and returns the next incoming TLS connection.
// The returned connection c is a *tls.Conn. // The returned connection c is a *tls.Conn.
func (l *tlsKeepaliveListener) Accept() (c net.Conn, err error) { func (l *tlsKeepaliveListener) Accept() (net.Conn, error) {
c, err = l.Listener.Accept() c, err := l.Listener.Accept()
if err != nil { if err != nil {
return return nil, err
} }
c = tls.Server(c, l.config) c = tls.Server(c, l.config)

View File

@ -103,14 +103,17 @@ func newListener(addr, scheme string, opts ...ListenerOption) (net.Listener, err
return wrapTLS(scheme, lnOpts.tlsInfo, lnOpts.Listener) 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 { if cfg != nil {
ln, err = cfg.Listen(context.TODO(), "tcp", addr) ln, err = cfg.Listen(context.TODO(), "tcp", addr)
} else { } else {
ln, err = net.Listen("tcp", addr) ln, err = net.Listen("tcp", addr)
} }
if err != nil { if err != nil {
return return nil, err
} }
return NewKeepAliveListener(ln, "tcp", nil) return NewKeepAliveListener(ln, "tcp", nil)
@ -204,16 +207,18 @@ func (info TLSInfo) Empty() bool {
return info.CertFile == "" && info.KeyFile == "" 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") verify.Assert(lg != nil, "nil log isn't allowed")
info.Logger = lg
var err error
info := TLSInfo{Logger: lg}
if selfSignedCertValidity == 0 { if selfSignedCertValidity == 0 {
err = errors.New("selfSignedCertValidity is invalid,it should be greater than 0") err = errors.New("selfSignedCertValidity is invalid,it should be greater than 0")
info.Logger.Warn( info.Logger.Warn(
"cannot generate cert", "cannot generate cert",
zap.Error(err), zap.Error(err),
) )
return return info, err
} }
err = fileutil.TouchDirAll(lg, dirpath) err = fileutil.TouchDirAll(lg, dirpath)
if err != nil { if err != nil {
@ -223,16 +228,16 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
zap.Error(err), zap.Error(err),
) )
} }
return return info, err
} }
certPath, err := filepath.Abs(filepath.Join(dirpath, "cert.pem")) certPath, err := filepath.Abs(filepath.Join(dirpath, "cert.pem"))
if err != nil { if err != nil {
return return info, err
} }
keyPath, err := filepath.Abs(filepath.Join(dirpath, "key.pem")) keyPath, err := filepath.Abs(filepath.Join(dirpath, "key.pem"))
if err != nil { if err != nil {
return return info, err
} }
_, errcert := os.Stat(certPath) _, errcert := os.Stat(certPath)
_, errkey := os.Stat(keyPath) _, errkey := os.Stat(keyPath)
@ -242,7 +247,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
info.ClientCertFile = certPath info.ClientCertFile = certPath
info.ClientKeyFile = keyPath info.ClientKeyFile = keyPath
info.selfCert = true info.selfCert = true
return return info, err
} }
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) 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), zap.Error(err),
) )
} }
return return info, err
} }
tmpl := x509.Certificate{ tmpl := x509.Certificate{
@ -292,7 +297,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
zap.Error(err), zap.Error(err),
) )
} }
return return info, err
} }
derBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &priv.PublicKey, priv) 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), zap.Error(err),
) )
} }
return return info, err
} }
certOut, err := os.Create(certPath) certOut, err := os.Create(certPath)
@ -313,7 +318,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
zap.String("path", certPath), zap.String("path", certPath),
zap.Error(err), zap.Error(err),
) )
return return info, err
} }
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close() certOut.Close()
@ -323,7 +328,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
b, err := x509.MarshalECPrivateKey(priv) b, err := x509.MarshalECPrivateKey(priv)
if err != nil { if err != nil {
return return info, err
} }
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil { if err != nil {
@ -334,7 +339,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
zap.Error(err), zap.Error(err),
) )
} }
return return info, err
} }
pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}) pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b})
keyOut.Close() keyOut.Close()

View File

@ -21,7 +21,9 @@ import (
"testing" "testing"
) )
func SplitTestArgs(args []string) (testArgs, appArgs []string) { func SplitTestArgs(args []string) ([]string, []string) {
var testArgs, appArgs []string
for i, arg := range args { for i, arg := range args {
switch { switch {
case strings.HasPrefix(arg, "-test."): case strings.HasPrefix(arg, "-test."):
@ -33,7 +35,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) {
appArgs = append(appArgs, arg) appArgs = append(appArgs, arg)
} }
} }
return return testArgs, appArgs
} }
// TestEmpty is an empty test to avoid no-tests warning. // TestEmpty is an empty test to avoid no-tests warning.

View File

@ -21,7 +21,9 @@ import (
"testing" "testing"
) )
func SplitTestArgs(args []string) (testArgs, appArgs []string) { func SplitTestArgs(args []string) ([]string, []string) {
var testArgs, appArgs []string
for i, arg := range args { for i, arg := range args {
switch { switch {
case strings.HasPrefix(arg, "-test."): case strings.HasPrefix(arg, "-test."):
@ -33,7 +35,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) {
appArgs = append(appArgs, arg) appArgs = append(appArgs, arg)
} }
} }
return return testArgs, appArgs
} }
// TestEmpty is empty test to avoid no-tests warning. // TestEmpty is empty test to avoid no-tests warning.

View File

@ -424,8 +424,7 @@ function ineffassign_pass {
} }
function nakedret_pass { function nakedret_pass {
# TODO: nakedret should work with -set_exit_status run_for_modules generic_checker run_go_tool "github.com/alexkohler/nakedret/cmd/nakedret"
run_for_modules generic_checker run_go_tool "github.com/alexkohler/nakedret"
} }
function license_header_per_module { function license_header_per_module {

View File

@ -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 // create the temporary data dir
dataDir = t.TempDir() dataDir := t.TempDir()
// create ${dataDir}/member/snap // create ${dataDir}/member/snap
if err = os.MkdirAll(datadir.ToSnapDir(dataDir), 0700); err != nil { if err = os.MkdirAll(datadir.ToSnapDir(dataDir), 0700); err != nil {
return return "", err
} }
// create ${dataDir}/member/wal // create ${dataDir}/member/wal
err = os.MkdirAll(datadir.ToWalDir(dataDir), 0700) err = os.MkdirAll(datadir.ToWalDir(dataDir), 0700)
if err != nil {
return "", err
}
return return dataDir, nil
} }
// prepare data for the test case // 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 var snapshotTerm, snapshotIndex uint64 = 2, 5
if err = createWALFileWithSnapshotRecord(cfg, snapshotTerm, snapshotIndex); err != nil { if err := createWALFileWithSnapshotRecord(cfg, snapshotTerm, snapshotIndex); err != nil {
return return err
} }
return createSnapshotAndBackendDB(cfg, snapshotTerm, snapshotIndex) 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) { func createWALFileWithSnapshotRecord(cfg config.ServerConfig, snapshotTerm, snapshotIndex uint64) (err error) {
var w *wal.WAL var w *wal.WAL
if w, err = wal.Create(cfg.Logger, cfg.WALDir(), []byte("somedata")); err != nil { if w, err = wal.Create(cfg.Logger, cfg.WALDir(), []byte("somedata")); err != nil {
return return err
} }
defer func() { defer func() {
@ -262,13 +267,15 @@ func createWALFileWithSnapshotRecord(cfg config.ServerConfig, snapshotTerm, snap
} }
if err = w.SaveSnapshot(walSnap); err != nil { if err = w.SaveSnapshot(walSnap); err != nil {
return return err
} }
return w.Save(raftpb.HardState{Term: snapshotTerm, Vote: 3, Commit: snapshotIndex}, nil) 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{ confState := raftpb.ConfState{
Voters: []uint64{1, 2, 3}, Voters: []uint64{1, 2, 3},
} }
@ -283,7 +290,7 @@ func createSnapshotAndBackendDB(cfg config.ServerConfig, snapshotTerm, snapshotI
Term: snapshotTerm, Term: snapshotTerm,
}, },
}); err != nil { }); err != nil {
return return err
} }
// create snapshot db file: "%016x.snap.db" // 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.UnsafeUpdateConsistentIndex(be.BatchTx(), snapshotIndex, snapshotTerm)
schema.MustUnsafeSaveConfStateToBackend(cfg.Logger, be.BatchTx(), &confState) schema.MustUnsafeSaveConfStateToBackend(cfg.Logger, be.BatchTx(), &confState)
if err = be.Close(); err != nil { if err = be.Close(); err != nil {
return return err
} }
sdb := filepath.Join(cfg.SnapDir(), fmt.Sprintf("%016x.snap.db", snapshotIndex)) sdb := filepath.Join(cfg.SnapDir(), fmt.Sprintf("%016x.snap.db", snapshotIndex))
if err = os.Rename(cfg.BackendPath(), sdb); err != nil { if err = os.Rename(cfg.BackendPath(), sdb); err != nil {
return return err
} }
// create backend db file // create backend db file

View File

@ -25,7 +25,9 @@ import (
"go.etcd.io/etcd/server/v3/etcdmain" "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 { for i, arg := range args {
switch { switch {
case strings.HasPrefix(arg, "-test."): case strings.HasPrefix(arg, "-test."):
@ -37,7 +39,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) {
appArgs = append(appArgs, arg) appArgs = append(appArgs, arg)
} }
} }
return return testArgs, appArgs
} }
func TestEmpty(t *testing.T) {} func TestEmpty(t *testing.T) {}

View File

@ -553,7 +553,7 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
// create encoder (chain crc with the decoder), enable appending // create encoder (chain crc with the decoder), enable appending
w.encoder, err = newFileEncoder(w.tail().File, w.decoder.LastCRC()) w.encoder, err = newFileEncoder(w.tail().File, w.decoder.LastCRC())
if err != nil { if err != nil {
return return nil, state, nil, err
} }
} }
w.decoder = nil w.decoder = nil

View File

@ -28,24 +28,22 @@ import (
"go.etcd.io/etcd/tests/v3/robustness/model" "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 const timeout = 5 * time.Minute
lg.Info("Validating linearizable operations", zap.Duration("timeout", timeout)) lg.Info("Validating linearizable operations", zap.Duration("timeout", timeout))
result, visualize := validateLinearizableOperationAndVisualize(lg, operations, timeout) result, visualize := validateLinearizableOperationAndVisualize(lg, operations, timeout)
switch result { switch result {
case porcupine.Illegal: case porcupine.Illegal:
t.Error("Linearization failed") t.Error("Linearization failed")
return
case porcupine.Unknown: case porcupine.Unknown:
t.Error("Linearization has timed out") t.Error("Linearization has timed out")
return
case porcupine.Ok: case porcupine.Ok:
t.Log("Linearization success") t.Log("Linearization success")
lg.Info("Validating serializable operations")
validateSerializableOperations(t, operations, eventHistory)
default: default:
t.Fatalf("Unknown Linearization") t.Fatalf("Unknown Linearization")
} }
lg.Info("Validating serializable operations")
validateSerializableOperations(t, operations, eventHistory)
return visualize return visualize
} }