Skip to content

Commit

Permalink
fixup! internal/mount: (pre-)merge mounting code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmxnzo committed Jan 22, 2025
1 parent c728ff7 commit 1fd8541
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
20 changes: 10 additions & 10 deletions internal/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import (
"strings"
)

// SetupMount formats the csi device to ext4 in case it is not ext4 formatted and mounts it to the provided mount point.
// SetupMount formats the device to ext4 in case it is not ext4 formatted and mounts it to the provided mount point.
func SetupMount(ctx context.Context, logger *slog.Logger, devPath, mountPoint string) error {
blk, err := blkid(ctx, devPath)
if errors.Is(err, errNotIdentified) {
logger.Info("csi device not identified, assuming first start, formatting", "device", devPath)
logger.Info("device not identified, formatting", "device", devPath)
if err := mkfsExt4(ctx, devPath); err != nil {
return err
}
} else if err != nil {
return err
} else if blk.Type != "ext4" {
logger.Info("csi device is not ext4, assuming first start, formatting", "device", devPath)
logger.Info("device is not ext4, formatting", "device", devPath)
if err := mkfsExt4(ctx, devPath); err != nil {
return err
}
Expand All @@ -34,13 +34,13 @@ func SetupMount(ctx context.Context, logger *slog.Logger, devPath, mountPoint st
if err := mount(ctx, devPath, mountPoint); err != nil {
return err
}
logger.Info("csi device mounted to state disk mount point", "dev", devPath, "mountPoint", mountPoint)
logger.Info("device mounted successfully", "dev", devPath, "mountPoint", mountPoint)

return nil
}

// Blk holds the main attributes of a block device used by blkid system executable command.
type Blk struct {
// blk holds the main attributes of a block device used by blkid system executable command.
type blk struct {
DevName string
UUID string
BlockSize int
Expand All @@ -49,8 +49,8 @@ type Blk struct {

var errNotIdentified = errors.New("blkid did not identify the device")

// blkid creates a Blk struct of the device located at the provided devPath.
func blkid(ctx context.Context, devPath string) (*Blk, error) {
// blkid creates a blk struct of the device located at the provided devPath.
func blkid(ctx context.Context, devPath string) (*blk, error) {
cmd := exec.CommandContext(ctx, "blkid", "-o", "export", devPath)
out, err := cmd.CombinedOutput()
var exitErr *exec.ExitError
Expand All @@ -64,9 +64,9 @@ func blkid(ctx context.Context, devPath string) (*Blk, error) {
}

// parseBlkidCommand parses the output of the blkid system command to rerpresentative Blkid struct.
func parseBlkidCommand(out []byte) (*Blk, error) {
func parseBlkidCommand(out []byte) (*blk, error) {
lines := strings.Split(string(out), "\n")
b := &Blk{}
b := &blk{}

for _, line := range lines {
if line == "" {
Expand Down
46 changes: 24 additions & 22 deletions internal/mount/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,45 @@ import (
)

func TestParseBlkidCommand(t *testing.T) {
blk := &Blk{
blk := &blk{
DevName: "/dev/nvme1n1p1",
UUID: "f3e74cbb-4b6c-442b-8cd1-d12e3fc7adcd",
BlockSize: 1,
Type: "ntfs",
}

blocks := []struct {
blocks := map[string]struct {
outFormatString string
Error bool
expError bool
}{
{
"valid output": {
outFormatString: "DEVNAME=%s\nUUID=%s\nBLOCK_SIZE=%d\nTYPE=%s\n",
Error: false,
expError: false,
},
{
"no equal sign": {
outFormatString: "No-Equal-Sign\n",
Error: true,
expError: true,
},
{
"block size is string": {
outFormatString: "BLOCK_SIZE=isString\n",
Error: true,
expError: true,
},
}

for _, block := range blocks {
parsedBlock, err := parseBlkidCommand(func(formatString string, blk *Blk) []byte {
return []byte(fmt.Sprintf(formatString,
blk.DevName, blk.UUID, blk.BlockSize, blk.Type))
}(block.outFormatString, blk))

if block.Error {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, blk, parsedBlock)

}
for name, block := range blocks {
t.Run(name, func(t *testing.T) {
parsedBlock, err := parseBlkidCommand(func(formatString string) []byte {
return []byte(fmt.Sprintf(formatString,
blk.DevName, blk.UUID, blk.BlockSize, blk.Type))
}(block.outFormatString))

if block.expError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, blk, parsedBlock)

}
})
}
}

0 comments on commit 1fd8541

Please sign in to comment.