Skip to content

Commit

Permalink
Merge pull request #18752 from mmorel-35/tools/testifier/require-error
Browse files Browse the repository at this point in the history
fix: use require.NoError instead of t.Fatal(err) in contrib and tools packages
  • Loading branch information
ahrtr authored Oct 18, 2024
2 parents c09b07f + 63dad79 commit 3869f0e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 42 deletions.
11 changes: 5 additions & 6 deletions contrib/raftexample/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package main
import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func Test_kvstore_snapshot(t *testing.T) {
Expand All @@ -29,14 +31,11 @@ func Test_kvstore_snapshot(t *testing.T) {
}

data, err := s.getSnapshot()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
s.kvStore = nil

if err := s.recoverFromSnapshot(data); err != nil {
t.Fatal(err)
}
err = s.recoverFromSnapshot(data)
require.NoError(t, err)
v, _ = s.Lookup("foo")
if v != "bar" {
t.Fatalf("foo has unexpected value, got %s", v)
Expand Down
23 changes: 8 additions & 15 deletions contrib/raftexample/raftexample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"go.etcd.io/raft/v3/raftpb"
)

Expand Down Expand Up @@ -96,9 +98,8 @@ func (clus *cluster) Close() (err error) {

func (clus *cluster) closeNoErrors(t *testing.T) {
t.Log("closing cluster...")
if err := clus.Close(); err != nil {
t.Fatal(err)
}
err := clus.Close()
require.NoError(t, err)
t.Log("closing cluster [done]")
}

Expand Down Expand Up @@ -201,27 +202,19 @@ func TestPutAndGetKeyValue(t *testing.T) {
cli := srv.Client()

req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
req.Header.Set("Content-Type", "text/html; charset=utf-8")
_, err = cli.Do(req)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// wait for a moment for processing message, otherwise get would be failed.
<-time.After(time.Second)

resp, err := cli.Get(url)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer resp.Body.Close()

if gotValue := string(data); wantValue != gotValue {
Expand Down
29 changes: 8 additions & 21 deletions tools/etcd-dump-logs/etcd-dump-log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

"go.etcd.io/etcd/api/v3/authpb"
Expand All @@ -36,9 +37,7 @@ import (
func TestEtcdDumpLogEntryType(t *testing.T) {
// directory where the command is
binDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// TODO(ptabor): The test does not run by default from ./scripts/test.sh.
dumpLogsBinary := path.Join(binDir + "/etcd-dump-logs")
Expand Down Expand Up @@ -79,13 +78,9 @@ func TestEtcdDumpLogEntryType(t *testing.T) {
t.Run(argtest.name, func(t *testing.T) {
cmd := exec.Command(dumpLogsBinary, argtest.args...)
actual, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expected, err := os.ReadFile(path.Join(binDir, argtest.fileExpected))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

assert.EqualValues(t, string(expected), string(actual))
// The output files contains a lot of trailing whitespaces... difficult to diagnose without printing them explicitly.
Expand All @@ -98,21 +93,15 @@ func TestEtcdDumpLogEntryType(t *testing.T) {
func mustCreateWALLog(t *testing.T, path string) {
memberdir := filepath.Join(path, "member")
err := os.Mkdir(memberdir, 0744)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
waldir := walDir(path)
snapdir := snapDir(path)

w, err := wal.Create(zaptest.NewLogger(t), waldir, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = os.Mkdir(snapdir, 0744)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

ents := make([]raftpb.Entry, 0)

Expand All @@ -124,9 +113,7 @@ func mustCreateWALLog(t *testing.T, path string) {

// force commit newly appended entries
err = w.Save(raftpb.HardState{}, ents)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
w.Close()
}

Expand Down

0 comments on commit 3869f0e

Please sign in to comment.