Skip to content

Commit

Permalink
refactor: Helper function for disk formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmetzner committed Oct 18, 2024
1 parent 8147b51 commit aff32c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 50 deletions.
13 changes: 13 additions & 0 deletions test/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"regexp"
"strconv"
"testing"

"k8s.io/mount-utils"
)

const testImageName = "hcloud-csi-driver-integrationtests"
Expand Down Expand Up @@ -105,3 +107,14 @@ func (w TestingWriter) Write(p []byte) (n int, err error) {
}
return len(p), nil
}

func formatDisk(mounter *mount.SafeFormatAndMount, device string, fstype string) error {
tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(device, tmppath, fstype, nil)
}
57 changes: 7 additions & 50 deletions test/integration/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,15 @@ func TestVolumePublishUnpublish(t *testing.T) {
name: "plain-correct-formatted",
mountOpts: volumes.MountOpts{},
prepare: func(mounter *mount.SafeFormatAndMount, cs *volumes.CryptSetup, device string) error {
tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(device, tmppath, "ext4", nil)
return formatDisk(mounter, device, "ext4")
},
expectedError: nil,
},
{
name: "plain-wrong-formatted",
mountOpts: volumes.MountOpts{},
prepare: func(mounter *mount.SafeFormatAndMount, cs *volumes.CryptSetup, device string) error {
tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(device, tmppath, "xfs", nil)
return formatDisk(mounter, device, "xfs")
},
expectedError: mount.NewMountError(mount.FilesystemMismatch, ""),
},
Expand Down Expand Up @@ -101,29 +87,15 @@ func TestVolumePublishUnpublish(t *testing.T) {

luksDevicePath := volumes.GenerateLUKSDevicePath(luksDeviceName)

tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(luksDevicePath, tmppath, "ext4", nil)
return formatDisk(mounter, luksDevicePath, "ext4")
},
expectedError: nil,
},
{
name: "encrypted-wrong-formatted-1",
mountOpts: volumes.MountOpts{EncryptionPassphrase: "passphrase"},
prepare: func(mounter *mount.SafeFormatAndMount, cs *volumes.CryptSetup, device string) error {
tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(device, tmppath, "ext4", nil)
return formatDisk(mounter, device, "ext4")
},
expectedError: fmt.Errorf("requested encrypted volume, but disk /dev-fake-encrypted-wrong-formatted-1 already is formatted with ext4"),
},
Expand Down Expand Up @@ -157,6 +129,7 @@ func TestVolumePublishUnpublish(t *testing.T) {
// Required as FS volumes require target dir, but block volumes require
// target file
targetPath = path.Join(targetPath, "target-path")
publishErr := mountService.Publish(targetPath, device, test.mountOpts)
defer func() {
err := mountService.Unpublish(targetPath)
if err != nil {
Expand All @@ -165,7 +138,6 @@ func TestVolumePublishUnpublish(t *testing.T) {
t.Logf("Unpublished targetPath %s", targetPath)
}
}()
publishErr := mountService.Publish(targetPath, device, test.mountOpts)

if test.expectedError != nil {
if publishErr == nil {
Expand All @@ -183,7 +155,6 @@ func TestVolumePublishUnpublish(t *testing.T) {
}
} else if test.expectedError.Error() != publishErr.Error() {
t.Fatal(fmt.Errorf("expected error %q but got %q", test.expectedError.Error(), publishErr.Error()))
return
}
}

Expand Down Expand Up @@ -339,28 +310,14 @@ func TestDetectDiskFormat(t *testing.T) {
{
name: "ext4",
prepare: func(mounter *mount.SafeFormatAndMount, device string) error {
tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(device, tmppath, "ext4", nil)
return formatDisk(mounter, device, "ext4")
},
expectedFormat: "ext4",
},
{
name: "xfs",
prepare: func(mounter *mount.SafeFormatAndMount, device string) error {
tmppath, err := os.MkdirTemp(os.TempDir(), "csi-driver-prepare")
if err != nil {
return err
}

defer os.RemoveAll(tmppath)
defer mounter.Unmount(tmppath)
return mounter.FormatAndMount(device, tmppath, "xfs", nil)
return formatDisk(mounter, device, "xfs")
},
expectedFormat: "xfs",
},
Expand Down

0 comments on commit aff32c5

Please sign in to comment.