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 92f00e7
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 152 deletions.
72 changes: 21 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,9 @@ 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)
}
require.NoError(t, CreateDirAll(zaptest.NewLogger(t), tmpdir2))

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

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 +81,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 +102,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 +115,31 @@ 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)
}
require.NoError(t, ZeroToEnd(f))

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)
require.NoError(t, ZeroToEnd(f))
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 +152,7 @@ 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)
}
require.NoError(t, CreateDirAll(zaptest.NewLogger(t), tmpdir2))
// 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 +162,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 +175,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 +198,5 @@ 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)
}
assert.NoError(t, TouchDirAll(zaptest.NewLogger(t), tmpdir))
}
28 changes: 9 additions & 19 deletions client/pkg/fileutil/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,28 @@ 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, os.Remove(f.Name()))
}()

// 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)
}
require.NoError(t, l.Close())

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

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

// the previously blocked routine should be unblocked
select {
Expand Down
22 changes: 7 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,10 @@ 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)
}
require.NoError(t, pf(f, size))

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 +46,10 @@ 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)
}
require.NoError(t, Preallocate(f, size, false))

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 +59,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)
}
29 changes: 8 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,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
}

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

// wait for rest of purges (5, 6)
for i := 0; i < 2; i++ {
Expand All @@ -150,9 +139,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
14 changes: 5 additions & 9 deletions client/pkg/tlsutil/cipher_suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@ package tlsutil
import (
"crypto/tls"
"testing"

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

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

func CipherSuiteExpectedToExist(tb testing.TB, cipher string, expectedID uint16) {
vid, ok := GetCipherSuite(cipher)
if !ok {
tb.Errorf("Expected %v cipher to exist", cipher)
}
if vid != expectedID {
tb.Errorf("For %v expected=%v found=%v", cipher, expectedID, vid)
}
assert.Truef(tb, ok, "Expected %v cipher to exist", cipher)
assert.Equalf(tb, expectedID, vid, "For %v expected=%v found=%v", cipher, expectedID, vid)
}

func TestGetCipherSuite_success(t *testing.T) {
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 92f00e7

Please sign in to comment.