Skip to content

Commit 5bdc66a

Browse files
authored
add device utils unit tests
Add unit tests fpr device utils package
1 parent 9e3b007 commit 5bdc66a

File tree

14 files changed

+1222
-143
lines changed

14 files changed

+1222
-143
lines changed

mocks/mock_utils/mock_devices/mock_size_getter_client.go

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/devices/devices.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2024 NetApp, Inc. All Rights Reserved.
22

33
//go:generate mockgen -destination=../../mocks/mock_utils/mock_devices/mock_devices_client.go github.com/netapp/trident/utils/devices Devices
4+
//go:generate mockgen -destination=../../mocks/mock_utils/mock_devices/mock_size_getter_client.go github.com/netapp/trident/utils/devices SizeGetter
45

56
package devices
67

@@ -74,17 +75,28 @@ type Devices interface {
7475
) error
7576
}
7677

78+
type SizeGetter interface {
79+
GetDiskSize(ctx context.Context, devicePath string) (int64, error)
80+
}
81+
82+
type DiskSizeGetter struct{}
83+
84+
func NewDiskSizeGetter() *DiskSizeGetter {
85+
return &DiskSizeGetter{}
86+
}
87+
7788
type Client struct {
7889
chrootPathPrefix string
7990
command exec.Command
8091
osFs afero.Afero
92+
SizeGetter
8193
}
8294

8395
func New() *Client {
84-
return NewDetailed(exec.NewCommand(), afero.NewOsFs())
96+
return NewDetailed(exec.NewCommand(), afero.NewOsFs(), NewDiskSizeGetter())
8597
}
8698

87-
func NewDetailed(command exec.Command, osFs afero.Fs) *Client {
99+
func NewDetailed(command exec.Command, osFs afero.Fs, diskSizeGetter SizeGetter) *Client {
88100
chrootPathPrefix := ""
89101
if os.Getenv("DOCKER_PLUGIN_MODE") != "" {
90102
chrootPathPrefix = "/host"
@@ -93,6 +105,7 @@ func NewDetailed(command exec.Command, osFs afero.Fs) *Client {
93105
chrootPathPrefix: chrootPathPrefix,
94106
command: command,
95107
osFs: afero.Afero{Fs: osFs},
108+
SizeGetter: diskSizeGetter,
96109
}
97110
}
98111

@@ -297,7 +310,7 @@ func (c *Client) FindDevicesForMultipathDevice(ctx context.Context, device strin
297310
devices := make([]string, 0)
298311

299312
slavesDir := c.chrootPathPrefix + "/sys/block/" + device + "/slaves"
300-
if dirs, err := os.ReadDir(slavesDir); err == nil {
313+
if dirs, err := c.osFs.ReadDir(slavesDir); err == nil {
301314
for _, f := range dirs {
302315
name := f.Name()
303316
if strings.HasPrefix(name, "sd") {
@@ -585,7 +598,7 @@ func (c *Client) GetMultipathDeviceDisks(
585598
multipathDevice := strings.TrimPrefix(multipathDevicePath, "/dev/")
586599

587600
diskPath := c.chrootPathPrefix + fmt.Sprintf("/sys/block/%s/slaves/", multipathDevice)
588-
diskDirs, err := os.ReadDir(diskPath)
601+
diskDirs, err := c.osFs.ReadDir(diskPath)
589602
if err != nil {
590603
Logc(ctx).WithError(err).Errorf("Could not read %s", diskPath)
591604
return nil, fmt.Errorf("failed to identify multipath device disks; unable to read '%s'", diskPath)
@@ -607,7 +620,7 @@ func (c *Client) GetMultipathDeviceDisks(
607620
func (c *Client) GetMultipathDeviceBySerial(ctx context.Context, hexSerial string) (string, error) {
608621
sysPath := c.chrootPathPrefix + "/sys/block/"
609622

610-
blockDirs, err := os.ReadDir(sysPath)
623+
blockDirs, err := c.osFs.ReadDir(sysPath)
611624
if err != nil {
612625
Logc(ctx).WithError(err).Errorf("Could not read %s", sysPath)
613626
return "", fmt.Errorf("failed to find multipath device by serial; unable to read '%s'", sysPath)
@@ -647,12 +660,12 @@ func (c *Client) GetMultipathDeviceUUID(multipathDevicePath string) (string, err
647660

648661
deviceUUIDPath := c.chrootPathPrefix + fmt.Sprintf("/sys/block/%s/dm/uuid", multipathDevice)
649662

650-
exists, err := PathExists(deviceUUIDPath)
663+
exists, err := PathExists(c.osFs, deviceUUIDPath)
651664
if !exists || err != nil {
652665
return "", errors.NotFoundError("multipath device '%s' UUID not found", multipathDevice)
653666
}
654667

655-
UUID, err := os.ReadFile(deviceUUIDPath)
668+
UUID, err := c.osFs.ReadFile(deviceUUIDPath)
656669
if err != nil {
657670
return "", err
658671
}
@@ -666,15 +679,15 @@ func (c *Client) RemoveDevice(ctx context.Context, devices []string, ignoreError
666679
defer Logc(ctx).Debug("<<<< devices.removeDevice")
667680

668681
var (
669-
f *os.File
682+
f afero.File
670683
err error
671684
)
672685

673686
c.ListAllDevices(ctx)
674687
for _, deviceName := range devices {
675688

676689
filename := fmt.Sprintf(c.chrootPathPrefix+"/sys/block/%s/device/delete", deviceName)
677-
if f, err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o200); err != nil {
690+
if f, err = c.osFs.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o200); err != nil {
678691
Logc(ctx).WithField("file", filename).Warning("Could not open file for writing.")
679692
if ignoreErrors {
680693
continue
@@ -746,7 +759,7 @@ func (c *Client) GetLUKSDeviceForMultipathDevice(multipathDevice string) (string
746759
dmDevice := strings.TrimSuffix(strings.TrimPrefix(multipathDevice, "/dev/"), "/")
747760

748761
// Get holder of mpath device
749-
dirents, err := os.ReadDir(fmt.Sprintf("/sys/block/%s/holders/", dmDevice))
762+
dirents, err := c.osFs.ReadDir(fmt.Sprintf("/sys/block/%s/holders/", dmDevice))
750763
if err != nil {
751764
return "", err
752765
}
@@ -759,7 +772,7 @@ func (c *Client) GetLUKSDeviceForMultipathDevice(multipathDevice string) (string
759772
holder := dirents[0].Name()
760773

761774
// Verify holder is LUKS device
762-
b, err := os.ReadFile(fmt.Sprintf("/sys/block/%s/dm/uuid", holder))
775+
b, err := c.osFs.ReadFile(fmt.Sprintf("/sys/block/%s/dm/uuid", holder))
763776
if err != nil {
764777
return "", err
765778
}

utils/devices/devices_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Client) VerifyMultipathDeviceSize(ctx context.Context, multipathDevice,
3737
}
3838

3939
// GetDiskSize unused stub function
40-
func (c *Client) GetDiskSize(ctx context.Context, _ string) (int64, error) {
40+
func (c *DiskSizeGetter) GetDiskSize(ctx context.Context, _ string) (int64, error) {
4141
Logc(ctx).Debug(">>>> devices_darwin.GetDiskSize")
4242
defer Logc(ctx).Debug("<<<< devices_darwin.GetDiskSize")
4343
return 0, errors.UnsupportedError("GetDiskSize is not supported for darwin")

utils/devices/devices_darwin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func TestFlushOneDevice(t *testing.T) {
1717
ctx := context.Background()
1818

19-
devices := NewDetailed(exec.NewCommand(), afero.NewMemMapFs())
19+
devices := NewDetailed(exec.NewCommand(), afero.NewMemMapFs(), nil)
2020
result := devices.FlushOneDevice(ctx, "/test/path")
2121
assert.Error(t, result, "no error")
2222
assert.True(t, errors.IsUnsupportedError(result), "not UnsupportedError")
@@ -25,7 +25,7 @@ func TestFlushOneDevice(t *testing.T) {
2525
func TestGetISCSIDiskSize(t *testing.T) {
2626
ctx := context.Background()
2727

28-
devices := NewDetailed(exec.NewCommand(), afero.NewMemMapFs())
28+
devices := NewDetailed(exec.NewCommand(), afero.NewMemMapFs(), NewDiskSizeGetter())
2929
result, err := devices.GetDiskSize(ctx, "/test/path")
3030
assert.Equal(t, result, int64(0), "received disk size")
3131
assert.Error(t, err, "no error")

utils/devices/devices_linux.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package devices
77
import (
88
"fmt"
99
"os"
10-
"os/exec"
1110
"strings"
1211
"syscall"
1312
"time"
@@ -19,22 +18,12 @@ import (
1918
"github.com/netapp/trident/internal/fiji"
2019
. "github.com/netapp/trident/logging"
2120
"github.com/netapp/trident/utils/errors"
21+
execCmd "github.com/netapp/trident/utils/exec"
2222
"github.com/netapp/trident/utils/filesystem"
2323
)
2424

2525
const (
26-
luksCloseTimeout = 30 * time.Second
27-
luksCypherMode = "aes-xts-plain64"
28-
luksType = "luks2"
29-
30-
// Return codes for `cryptsetup status`
31-
cryptsetupStatusDeviceDoesExistStatusCode = 0
32-
cryptsetupStatusDeviceDoesNotExistStatusCode = 4
33-
34-
// Return codes for `cryptsetup isLuks`
35-
cryptsetupIsLuksDeviceIsLuksStatusCode = 0
36-
cryptsetupIsLuksDeviceIsNotLuksStatusCode = 1
37-
26+
luksCloseTimeout = 30 * time.Second
3827
luksCloseMaxWaitDuration = 2 * time.Minute
3928
)
4029

@@ -71,7 +60,7 @@ func (c *Client) FlushOneDevice(ctx context.Context, devicePath string) error {
7160
}
7261

7362
// GetDiskSize queries the current block size in bytes
74-
func (c *Client) GetDiskSize(ctx context.Context, devicePath string) (int64, error) {
63+
func (c *DiskSizeGetter) GetDiskSize(ctx context.Context, devicePath string) (int64, error) {
7564
fields := LogFields{"rawDevicePath": devicePath}
7665
Logc(ctx).WithFields(fields).Debug(">>>> devices_linux.GetDiskSize")
7766
defer Logc(ctx).WithFields(fields).Debug("<<<< devices_linux.GetDiskSize")
@@ -203,7 +192,7 @@ func (c *Client) GetDeviceFSType(ctx context.Context, device string) (string, er
203192
if errors.IsTimeoutError(err) {
204193
c.ListAllDevices(ctx)
205194
return "", err
206-
} else if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 2 {
195+
} else if exitErr, ok := err.(execCmd.ExitError); ok && exitErr.ExitCode() == 2 {
207196
// EITHER: Disk device is unformatted.
208197
// OR: For 'blkid', if the specified token (TYPE/PTTYPE, etc) was
209198
// not found, or no (specified) devices could be identified, an
@@ -293,7 +282,7 @@ func (c *Client) WaitForDevice(ctx context.Context, device string) error {
293282
Logc(ctx).WithFields(fields).Debug(">>>> devices.waitForDevice")
294283
defer Logc(ctx).WithFields(fields).Debug("<<<< devices.waitForDevice")
295284

296-
exists, err := PathExists(device)
285+
exists, err := PathExists(c.osFs, device)
297286
if !exists || err != nil {
298287
return errors.New("device not yet present")
299288
} else {

0 commit comments

Comments
 (0)