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 c607234
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
8 changes: 4 additions & 4 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,7 +34,7 @@ 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
}
Expand Down
44 changes: 23 additions & 21 deletions internal/mount/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,38 @@ func TestParseBlkidCommand(t *testing.T) {
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, blk *Blk) []byte {
return []byte(fmt.Sprintf(formatString,
blk.DevName, blk.UUID, blk.BlockSize, blk.Type))
}(block.outFormatString, blk))

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

}
})
}
}

0 comments on commit c607234

Please sign in to comment.