Skip to content

Commit

Permalink
Use stretchr/testify in fill_zero_test
Browse files Browse the repository at this point in the history
Helper funcs make for easier to read tests.
  • Loading branch information
mmlb committed Jun 10, 2024
1 parent 62240bf commit 317897a
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions utils/fill_zero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,25 @@ import (

"github.com/bmc-toolbox/common"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
)

func Test_NewFillZeroCmd(t *testing.T) {
// Test if NewFillZeroCmd returns a non-nil pointer
zw := NewFillZeroCmd(false)
if zw == nil {
t.Error("Expected non-nil pointer, got nil")
}
require.NotNil(t, NewFillZeroCmd(false))
}

func Test_WipeDrive(t *testing.T) {
for _, size := range []int{4095, 4096, 4097, 8192} {
t.Run(strconv.Itoa(size), func(t *testing.T) {
// Create a temporary file for testing
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.Remove(tmpfile.Name()) // clean up

// Write some content to the temporary file
expectedSize := int64(4096)
if _, err = tmpfile.Write(make([]byte, expectedSize)); err != nil {
t.Fatal(err)
}
_, err = tmpfile.Write(make([]byte, expectedSize))
require.NoError(t, err)

// Simulate a context
ctx := context.Background()
Expand All @@ -45,19 +39,12 @@ func Test_WipeDrive(t *testing.T) {
logger, hook := test.NewNullLogger()
defer hook.Reset()
err = zw.WipeDrive(ctx, logger, drive)
if err != nil {
t.Errorf("Fill returned an error: %v", err)
}
require.NoError(t, err)

// Check if the file size remains the same after overwrite
fileInfo, err := os.Stat(tmpfile.Name())
if err != nil {
t.Fatal(err)
}

if size := fileInfo.Size(); size != expectedSize {
t.Errorf("Expected file size to remain %d after overwrite, got %d", expectedSize, size)
}
require.NoError(t, err)
require.Equal(t, expectedSize, fileInfo.Size())
})
}
}

0 comments on commit 317897a

Please sign in to comment.