Skip to content

Commit

Permalink
Make blkdiscard a DiskWiper
Browse files Browse the repository at this point in the history
Should have been a DiskWiper from the beginning and its going to be
useful soon.
  • Loading branch information
mmlb committed Jun 10, 2024
1 parent a5c2608 commit 4b0de0e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions examples/blkdiscard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"time"

"github.com/metal-toolbox/ironlib/actions"
"github.com/metal-toolbox/ironlib/utils"
"github.com/sirupsen/logrus"
)
Expand All @@ -27,13 +28,13 @@ func main() {
logger.WithError(err).Fatal("failed to parse timeout duration")
}

blkdiscard := utils.NewBlkdiscardCmd()
var blkdiscard actions.DiskWiper = utils.NewBlkdiscardCmd()

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

logger.Info("running blkdiscard on ", *device)
err = blkdiscard.Discard(ctx, *device)
err = blkdiscard.WipeDisk(ctx, logger, *device)
if err != nil {
logger.WithError(err).Fatal("exiting")
}
Expand Down
8 changes: 8 additions & 0 deletions utils/blkdiscard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"cmp"
"context"
"os"

"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -37,6 +39,12 @@ func (b *Blkdiscard) Discard(ctx context.Context, device string) error {
return nil
}

// WipeDisk implements DiskWipe by calling Discard
func (b *Blkdiscard) WipeDisk(ctx context.Context, logger *logrus.Logger, device string) error {
logger.WithField("device", device).WithField("method", "blkdiscard").Info("wiping")
return b.Discard(ctx, device)
}

// NewFakeBlkdiscard returns a mock implementation of the Blkdiscard interface for use in tests.
func NewFakeBlkdiscard() *Blkdiscard {
return &Blkdiscard{
Expand Down
7 changes: 6 additions & 1 deletion utils/blkdiscard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"context"
"testing"

"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)

func Test_blkdiscard(t *testing.T) {
err := NewFakeBlkdiscard().Discard(context.TODO(), "/dev/sdZZZ")
// Test Fill function
logger, hook := test.NewNullLogger()
defer hook.Reset()

err := NewFakeBlkdiscard().WipeDisk(context.TODO(), logger, "/dev/sdZZZ")
assert.NoError(t, err)
}

0 comments on commit 4b0de0e

Please sign in to comment.