Skip to content

Commit

Permalink
Merge pull request etcd-io#16601 from fuweid/fix-nakedret-lint
Browse files Browse the repository at this point in the history
*: fix nakedret lint
  • Loading branch information
ahrtr authored Sep 18, 2023
2 parents 0917288 + e72c2c4 commit 700411d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 45 deletions.
6 changes: 3 additions & 3 deletions client/pkg/transport/keepalive_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 20 additions & 15 deletions client/pkg/transport/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -254,7 +259,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali
zap.Error(err),
)
}
return
return info, err
}

tmpl := x509.Certificate{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions etcdctl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."):
Expand All @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions etcdutl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."):
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 20 additions & 13 deletions server/etcdserver/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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},
}
Expand All @@ -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"
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."):
Expand All @@ -37,7 +39,7 @@ func SplitTestArgs(args []string) (testArgs, appArgs []string) {
appArgs = append(appArgs, arg)
}
}
return
return testArgs, appArgs
}

func TestEmpty(t *testing.T) {}
Expand Down
2 changes: 1 addition & 1 deletion server/storage/wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions tests/robustness/validate/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 700411d

Please sign in to comment.