Skip to content

Commit 34b7b8b

Browse files
authored
Additional iscsi unit tests
Add additional iSCSI unit tests
1 parent cb29fc3 commit 34b7b8b

File tree

11 files changed

+995
-42
lines changed

11 files changed

+995
-42
lines changed

mocks/mock_utils/mock_nvme_utils.go

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

mocks/mock_utils/mock_osutils/mock_osutils.go

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

mocks/mock_utils/mock_path/mock_path.go

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

utils/filesystem/filesystem.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,7 @@ func (f *FSClient) DeleteFile(ctx context.Context, filepath, fileDescription str
346346
}
347347

348348
func (f *FSClient) ScanFile(filename string) ([]byte, error) {
349-
fs := afero.NewOsFs()
350-
351-
file, err := fs.Open(filename)
349+
file, err := f.osFs.Open(filename)
352350
if err != nil {
353351
fmt.Println("Failed to open file:", err)
354352
return nil, err
@@ -364,9 +362,7 @@ func (f *FSClient) ScanFile(filename string) ([]byte, error) {
364362
}
365363

366364
func (f *FSClient) ScanDir(path string) ([]os.FileInfo, error) {
367-
fs := afero.NewOsFs()
368-
369-
dirEntries, err := afero.ReadDir(fs, path)
365+
dirEntries, err := afero.ReadDir(f.osFs, path)
370366
if err != nil {
371367
fmt.Println("Failed to read directory:", err)
372368
return nil, err

utils/iscsi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
var (
4040
iqnRegex = regexp.MustCompile(`^\s*InitiatorName\s*=\s*(?P<iqn>\S+)(|\s+.*)$`)
4141
mountClient, _ = mount.New()
42-
IscsiUtils = iscsi.NewReconcileUtils(osutils.ChrootPathPrefix, osutils.New())
42+
IscsiUtils = iscsi.NewReconcileUtils()
4343
devicesClient = devices.New()
4444
iscsiClient = iscsi.NewDetailed(osutils.ChrootPathPrefix, command, iscsi.DefaultSelfHealingExclusion, osutils.New(),
4545
devicesClient, filesystem.New(mountClient), mountClient, IscsiUtils, afero.Afero{Fs: afero.NewOsFs()}, nil)

utils/iscsi/iscsi.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
"os"
1313
"os/exec"
14-
"path/filepath"
1514
"regexp"
1615
"strconv"
1716
"strings"
@@ -154,7 +153,7 @@ func New() (*Client, error) {
154153
chrootPathPrefix := osutils.ChrootPathPrefix
155154

156155
osUtils := osutils.New()
157-
reconcileutils := NewReconcileUtils(chrootPathPrefix, osUtils)
156+
reconcileutils := NewReconcileUtils()
158157
osFs := afero.Afero{Fs: afero.NewOsFs()}
159158
mountClient, err := mount.New()
160159
if err != nil {
@@ -170,8 +169,7 @@ func New() (*Client, error) {
170169

171170
func NewDetailed(chrootPathPrefix string, command tridentexec.Command, selfHealingExclusion []string, osClient OS,
172171
devices devices.Devices, fileSystemClient filesystem.Filesystem, mountClient mount.Mount,
173-
iscsiUtils IscsiReconcileUtils,
174-
os afero.Afero, osUtils osutils.Utils,
172+
iscsiUtils IscsiReconcileUtils, os afero.Afero, osUtils osutils.Utils,
175173
) *Client {
176174
return &Client{
177175
chrootPathPrefix: chrootPathPrefix,
@@ -1896,7 +1894,7 @@ func (client *Client) SafeToLogOut(ctx context.Context, hostNumber, sessionNumbe
18961894
// See drivers/scsi/scsi_scan.c in Linux
18971895
// We assume the channel/bus and device/controller are always zero for iSCSI
18981896
targetPath := devicePath + fmt.Sprintf("/session%d/target%d:0:0", sessionNumber, hostNumber)
1899-
dirs, err := os.ReadDir(targetPath)
1897+
dirs, err := client.os.ReadDir(targetPath)
19001898
if err != nil {
19011899
if os.IsNotExist(err) {
19021900
return true
@@ -1970,7 +1968,7 @@ func (c *Client) GetISCSIDevices(ctx context.Context, getCredentials bool) ([]*m
19701968

19711969
// Start by reading the sessions from /sys/class/iscsi_session
19721970
sysPath := c.chrootPathPrefix + "/sys/class/iscsi_session/"
1973-
sessionDirs, err := os.ReadDir(sysPath)
1971+
sessionDirs, err := c.os.ReadDir(sysPath)
19741972
if err != nil {
19751973
Logc(ctx).WithField("error", err).Errorf("Could not read %s", sysPath)
19761974
return nil, err
@@ -2003,7 +2001,7 @@ func (c *Client) GetISCSIDevices(ctx context.Context, getCredentials bool) ([]*m
20032001
sessionValues := make(map[string]string, len(sessionFiles))
20042002
for file, value := range sessionFiles {
20052003
path := sessionPath + "/" + file
2006-
fileBytes, err := os.ReadFile(path)
2004+
fileBytes, err := c.os.ReadFile(path)
20072005
if err != nil {
20082006
Logc(ctx).WithFields(LogFields{
20092007
"path": path,
@@ -2044,7 +2042,7 @@ func (c *Client) GetISCSIDevices(ctx context.Context, getCredentials bool) ([]*m
20442042

20452043
// Find the one target at /sys/class/iscsi_session/sessionXXX/device/targetHH:BB:DD (host:bus:device)
20462044
sessionDevicePath := sessionPath + "/device/"
2047-
targetDirs, err := os.ReadDir(sessionDevicePath)
2045+
targetDirs, err := c.os.ReadDir(sessionDevicePath)
20482046
if err != nil {
20492047
Logc(ctx).WithField("error", err).Errorf("Could not read %s", sessionDevicePath)
20502048
return nil, err
@@ -2076,7 +2074,7 @@ func (c *Client) GetISCSIDevices(ctx context.Context, getCredentials bool) ([]*m
20762074
}).Debug("Found host/bus/device path.")
20772075

20782076
// Find the devices at /sys/class/iscsi_session/sessionXXX/device/targetHH:BB:DD/HH:BB:DD:LL (host:bus:device:lun)
2079-
hostBusDeviceLunDirs, err := os.ReadDir(sessionDeviceHBDPath)
2077+
hostBusDeviceLunDirs, err := c.os.ReadDir(sessionDeviceHBDPath)
20802078
if err != nil {
20812079
Logc(ctx).WithField("error", err).Errorf("Could not read %s", sessionDeviceHBDPath)
20822080
return nil, err
@@ -2110,7 +2108,7 @@ func (c *Client) GetISCSIDevices(ctx context.Context, getCredentials bool) ([]*m
21102108
blockPath := sessionDeviceHBDLPath + "/block/"
21112109

21122110
// Find the block device at /sys/class/iscsi_session/sessionXXX/device/targetHH:BB:DD/HH:BB:DD:LL/block
2113-
blockDeviceDirs, err := os.ReadDir(blockPath)
2111+
blockDeviceDirs, err := c.os.ReadDir(blockPath)
21142112
if err != nil {
21152113
Logc(ctx).WithField("error", err).Errorf("Could not read %s", blockPath)
21162114
return nil, err
@@ -2195,7 +2193,7 @@ func (client *Client) GetMountedISCSIDevices(ctx context.Context) ([]*models.Scs
21952193
var mountedDevice string
21962194
// Resolve any symlinks to get the real device
21972195
if hasDevMountSourcePrefix {
2198-
device, err := filepath.EvalSymlinks(procMount.MountSource)
2196+
device, err := client.osUtils.EvalSymlinks(procMount.MountSource)
21992197
if err != nil {
22002198
Logc(ctx).Error(err)
22012199
continue

utils/iscsi/iscsi_linux_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ func TestISCSIActiveOnHost(t *testing.T) {
355355
).Return([]byte(""), tt.expected.err)
356356

357357
osUtils := osutils.NewDetailed(mockExec, afero.NewMemMapFs())
358-
iscsiClient := NewDetailed("", mockExec, nil, nil, nil, nil, nil, nil, afero.Afero{Fs: afero.NewMemMapFs()}, osUtils)
358+
iscsiClient := NewDetailed("", mockExec, nil, nil, nil, nil, nil, nil,
359+
afero.Afero{Fs: afero.NewMemMapFs()}, osUtils)
359360
active, err := iscsiClient.ISCSIActiveOnHost(tt.args.ctx, tt.args.host)
360361
if tt.expected.err != nil {
361362
assert.Error(t, err)

0 commit comments

Comments
 (0)