Skip to content

Commit

Permalink
fix: use require.NoError instead of t.Fatal(err) in client package
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Oct 8, 2024
1 parent 17fb752 commit d4a79ef
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 144 deletions.
78 changes: 27 additions & 51 deletions client/pkg/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

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

Expand Down Expand Up @@ -59,13 +60,11 @@ func TestCreateDirAll(t *testing.T) {
tmpdir := t.TempDir()

tmpdir2 := filepath.Join(tmpdir, "testdir")
if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil {
t.Fatal(err)
}
err := CreateDirAll(zaptest.NewLogger(t), tmpdir2)
require.NoError(t, err)

if err := os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode)
require.NoError(t, err)

if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") {
t.Fatalf("unexpected error %v", err)
Expand All @@ -84,9 +83,7 @@ func TestExist(t *testing.T) {
}

f, err := os.CreateTemp(os.TempDir(), "fileutil")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
f.Close()

if g := Exist(f.Name()); !g {
Expand All @@ -107,9 +104,7 @@ func TestDirEmpty(t *testing.T) {
}

file, err := os.CreateTemp(dir, "new_file")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
file.Close()

if DirEmpty(dir) {
Expand All @@ -122,42 +117,33 @@ func TestDirEmpty(t *testing.T) {

func TestZeroToEnd(t *testing.T) {
f, err := os.CreateTemp(os.TempDir(), "fileutil")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.Remove(f.Name())
defer f.Close()

// Ensure 0 size is a nop so zero-to-end on an empty file won't give EINVAL.
if err = ZeroToEnd(f); err != nil {
t.Fatal(err)
}
err = ZeroToEnd(f)
require.NoError(t, err)

b := make([]byte, 1024)
for i := range b {
b[i] = 12
}
if _, err = f.Write(b); err != nil {
t.Fatal(err)
}
if _, err = f.Seek(512, io.SeekStart); err != nil {
t.Fatal(err)
}
if err = ZeroToEnd(f); err != nil {
t.Fatal(err)
}
_, err = f.Write(b)
require.NoError(t, err)
_, err = f.Seek(512, io.SeekStart)
require.NoError(t, err)
err = ZeroToEnd(f)
require.NoError(t, err)
off, serr := f.Seek(0, io.SeekCurrent)
if serr != nil {
t.Fatal(serr)
}
require.NoError(t, serr)
if off != 512 {
t.Fatalf("expected offset 512, got %d", off)
}

b = make([]byte, 512)
if _, err = f.Read(b); err != nil {
t.Fatal(err)
}
_, err = f.Read(b)
require.NoError(t, err)
for i := range b {
if b[i] != 0 {
t.Errorf("expected b[%d] = 0, got %d", i, b[i])
Expand All @@ -170,9 +156,8 @@ func TestDirPermission(t *testing.T) {

tmpdir2 := filepath.Join(tmpdir, "testpermission")
// create a new dir with 0700
if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil {
t.Fatal(err)
}
err := CreateDirAll(zaptest.NewLogger(t), tmpdir2)
require.NoError(t, err)
// check dir permission with mode different than created dir
if err := CheckDirPermission(tmpdir2, 0600); err == nil {
t.Errorf("expected error, got nil")
Expand All @@ -182,14 +167,10 @@ func TestDirPermission(t *testing.T) {
func TestRemoveMatchFile(t *testing.T) {
tmpdir := t.TempDir()
f, err := os.CreateTemp(tmpdir, "tmp")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
f.Close()
f, err = os.CreateTemp(tmpdir, "foo.tmp")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
f.Close()

err = RemoveMatchFile(zaptest.NewLogger(t), tmpdir, func(fileName string) bool {
Expand All @@ -199,17 +180,13 @@ func TestRemoveMatchFile(t *testing.T) {
t.Errorf("expected nil, got error")
}
fnames, err := ReadDir(tmpdir)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if len(fnames) != 1 {
t.Errorf("expected exist 1 files, got %d", len(fnames))
}

f, err = os.CreateTemp(tmpdir, "tmp")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
f.Close()
err = RemoveMatchFile(zaptest.NewLogger(t), tmpdir, func(fileName string) bool {
os.Remove(filepath.Join(tmpdir, fileName))
Expand All @@ -226,7 +203,6 @@ func TestTouchDirAll(t *testing.T) {
TouchDirAll(nil, tmpdir)
}, "expected panic with nil log")

if err := TouchDirAll(zaptest.NewLogger(t), tmpdir); err != nil {
t.Fatal(err)
}
err := TouchDirAll(zaptest.NewLogger(t), tmpdir)
assert.NoError(t, err)
}
29 changes: 11 additions & 18 deletions client/pkg/fileutil/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,30 @@ import (
"os"
"testing"
"time"

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

func TestLockAndUnlock(t *testing.T) {
f, err := os.CreateTemp("", "lock")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
f.Close()
defer func() {
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
}()

// lock the file
l, err := LockFile(f.Name(), os.O_WRONLY, PrivateFileMode)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// try lock a locked file
if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); err != ErrLocked {
t.Fatal(err)
}
_, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode)
require.ErrorIs(t, err, ErrLocked)

// unlock the file
if err = l.Close(); err != nil {
t.Fatal(err)
}
err = l.Close()
require.NoError(t, err)

// try lock the unlocked file
dupl, err := TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode)
Expand Down Expand Up @@ -75,9 +69,8 @@ func TestLockAndUnlock(t *testing.T) {
}

// unlock
if err = dupl.Close(); err != nil {
t.Fatal(err)
}
err = dupl.Close()
require.NoError(t, err)

// the previously blocked routine should be unblocked
select {
Expand Down
24 changes: 9 additions & 15 deletions client/pkg/fileutil/preallocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package fileutil
import (
"os"
"testing"

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

func TestPreallocateExtend(t *testing.T) {
Expand All @@ -32,14 +34,11 @@ func TestPreallocateExtendTrunc(t *testing.T) {

func testPreallocateExtend(t *testing.T, f *os.File, pf func(*os.File, int64) error) {
size := int64(64 * 1000)
if err := pf(f, size); err != nil {
t.Fatal(err)
}
err := pf(f, size)
require.NoError(t, err)

stat, err := f.Stat()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if stat.Size() != size {
t.Errorf("size = %d, want %d", stat.Size(), size)
}
Expand All @@ -48,14 +47,11 @@ func testPreallocateExtend(t *testing.T, f *os.File, pf func(*os.File, int64) er
func TestPreallocateFixed(t *testing.T) { runPreallocTest(t, testPreallocateFixed) }
func testPreallocateFixed(t *testing.T, f *os.File) {
size := int64(64 * 1000)
if err := Preallocate(f, size, false); err != nil {
t.Fatal(err)
}
err := Preallocate(f, size, false)
require.NoError(t, err)

stat, err := f.Stat()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if stat.Size() != 0 {
t.Errorf("size = %d, want %d", stat.Size(), 0)
}
Expand All @@ -65,8 +61,6 @@ func runPreallocTest(t *testing.T, test func(*testing.T, *os.File)) {
p := t.TempDir()

f, err := os.CreateTemp(p, "")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
test(t, f)
}
30 changes: 9 additions & 21 deletions client/pkg/fileutil/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"
"time"

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

Expand All @@ -31,9 +32,7 @@ func TestPurgeFile(t *testing.T) {
// minimal file set
for i := 0; i < 3; i++ {
f, ferr := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i)))
if ferr != nil {
t.Fatal(ferr)
}
require.NoError(t, ferr)
f.Close()
}

Expand Down Expand Up @@ -68,9 +67,7 @@ func TestPurgeFile(t *testing.T) {
}

fnames, rerr := ReadDir(dir)
if rerr != nil {
t.Fatal(rerr)
}
require.NoError(t, rerr)
wnames := []string{"7.test", "8.test", "9.test"}
if !reflect.DeepEqual(fnames, wnames) {
t.Errorf("filenames = %v, want %v", fnames, wnames)
Expand All @@ -93,18 +90,14 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
for i := 0; i < 10; i++ {
var f *os.File
f, err := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i)))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
f.Close()
}

// create a purge barrier at 5
p := filepath.Join(dir, fmt.Sprintf("%d.test", 5))
l, err := LockFile(p, os.O_WRONLY, PrivateFileMode)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

stop, purgec := make(chan struct{}), make(chan string, 10)
errch := purgeFile(zaptest.NewLogger(t), dir, "test", 3, time.Millisecond, stop, purgec, nil, true)
Expand All @@ -118,9 +111,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
}

fnames, rerr := ReadDir(dir)
if rerr != nil {
t.Fatal(rerr)
}
require.NoError(t, rerr)

wnames := []string{"5.test", "6.test", "7.test", "8.test", "9.test"}
if !reflect.DeepEqual(fnames, wnames) {
Expand All @@ -136,9 +127,8 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
}

// remove the purge barrier
if err = l.Close(); err != nil {
t.Fatal(err)
}
err = l.Close()
require.NoError(t, err)

// wait for rest of purges (5, 6)
for i := 0; i < 2; i++ {
Expand All @@ -150,9 +140,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
}

fnames, rerr = ReadDir(dir)
if rerr != nil {
t.Fatal(rerr)
}
require.NoError(t, rerr)
wnames = []string{"7.test", "8.test", "9.test"}
if !reflect.DeepEqual(fnames, wnames) {
t.Errorf("filenames = %v, want %v", fnames, wnames)
Expand Down
6 changes: 3 additions & 3 deletions client/pkg/tlsutil/cipher_suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package tlsutil
import (
"crypto/tls"
"testing"

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

func TestGetCipherSuite_not_existing(t *testing.T) {
_, ok := GetCipherSuite("not_existing")
if ok {
t.Fatal("Expected not ok")
}
require.Falsef(t, ok, "Expected not ok")
}

func CipherSuiteExpectedToExist(tb testing.TB, cipher string, expectedID uint16) {
Expand Down
5 changes: 2 additions & 3 deletions client/pkg/transport/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

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

Expand Down Expand Up @@ -536,9 +537,7 @@ func TestNewListenerTLSInfoSelfCert(t *testing.T) {
tmpdir := t.TempDir()

tlsinfo, err := SelfCert(zaptest.NewLogger(t), tmpdir, []string{"127.0.0.1"}, 1)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if tlsinfo.Empty() {
t.Fatalf("tlsinfo should have certs (%+v)", tlsinfo)
}
Expand Down
Loading

0 comments on commit d4a79ef

Please sign in to comment.