From 3f9c5ea3b858472f0e27a039e1c96070385de90b Mon Sep 17 00:00:00 2001 From: Katarzyna Kulpa Date: Thu, 7 Mar 2024 15:35:56 +0100 Subject: [PATCH] [issue-1114] Reverting SerachDrivePath changes --- pkg/base/linuxutils/lsblk/lsblk.go | 26 +++++------- pkg/base/linuxutils/lsblk/lsblk_test.go | 53 +++++-------------------- pkg/mocks/commands.go | 18 +-------- 3 files changed, 22 insertions(+), 75 deletions(-) diff --git a/pkg/base/linuxutils/lsblk/lsblk.go b/pkg/base/linuxutils/lsblk/lsblk.go index cbd4925c4..f703ca8d9 100644 --- a/pkg/base/linuxutils/lsblk/lsblk.go +++ b/pkg/base/linuxutils/lsblk/lsblk.go @@ -157,9 +157,16 @@ func (l *LSBLK) GetBlockDevices(device string) ([]BlockDevice, error) { return res, nil } -// SearchDrivePath searches for the drive path by comparing drive's sn, vid, pid with lsblk output +// SearchDrivePath if not defined returns drive path based on drive S/N, VID and PID. +// Receives an instance of drivecrd.Drive struct +// Returns drive's path based on provided drivecrd.Drive or error if something went wrong func (l *LSBLK) SearchDrivePath(drive *api.Drive) (string, error) { - device := "" + // device path might be already set by hwmgr + device := drive.Path + if device != "" { + return device, nil + } + // try to find it with lsblk lsblkOut, err := l.GetBlockDevices("") if err != nil { @@ -171,11 +178,8 @@ func (l *LSBLK) SearchDrivePath(drive *api.Drive) (string, error) { vid := drive.VID pid := drive.PID for _, l := range lsblkOut { - lvid := strings.TrimSpace(l.Vendor) - // The hasPrefixIgnoreSpaces function is used to compare pid and lsblk model, accounting for drives that may have - // multiple spaces in their pid, and situations where pid is truncated or limited to 16 digits compared to lsblk model. - if strings.EqualFold(l.Serial, sn) && (lvid == "" || strings.EqualFold(lvid, vid)) && - hasPrefixIgnoreSpaces(l.Model, pid) { + if strings.EqualFold(l.Serial, sn) && strings.EqualFold(l.Vendor, vid) && + strings.EqualFold(l.Model, pid) { device = l.Name break } @@ -188,11 +192,3 @@ func (l *LSBLK) SearchDrivePath(drive *api.Drive) (string, error) { return device, nil } -func hasPrefixIgnoreSpaces(s, prefix string) bool { - empty := "" - emptySpace := " " - s = strings.ReplaceAll(s, emptySpace, empty) - prefix = strings.ReplaceAll(prefix, emptySpace, empty) - - return strings.HasPrefix(s, prefix) -} diff --git a/pkg/base/linuxutils/lsblk/lsblk_test.go b/pkg/base/linuxutils/lsblk/lsblk_test.go index 3fb4c4ee6..f3ac59b94 100644 --- a/pkg/base/linuxutils/lsblk/lsblk_test.go +++ b/pkg/base/linuxutils/lsblk/lsblk_test.go @@ -85,6 +85,15 @@ func TestLSBLK_SearchDrivePath_Success(t *testing.T) { l := NewLSBLK(testLogger) l.e = e + // path is in drive spec + dCR := testDriveCR + path := "/dev/sda" + dCR.Spec.Path = path + + res, err := l.SearchDrivePath(&dCR.Spec) + assert.Nil(t, err) + assert.Equal(t, path, res) + // got from lsblk output e.On("RunCmd", allDevicesCmd).Return(mocks.LsblkTwoDevicesStr, "", nil) sn = "hdd1" // from mocks.LsblkTwoDevicesStr @@ -92,28 +101,7 @@ func TestLSBLK_SearchDrivePath_Success(t *testing.T) { d2CR := testDriveCR d2CR.Spec.SerialNumber = sn - res, err := l.SearchDrivePath(&d2CR.Spec) - assert.Nil(t, err) - assert.Equal(t, expectedDevice, res) -} - -func TestLSBLK_SearchDrivePath_ModelWithEmptySpaces_Success(t *testing.T) { - l := NewLSBLK(testLogger) - e := &mocks.GoMockExecutor{} - e.On("RunCmd", allDevicesCmd).Return(mocks.LsblkDevV2, "", nil) - l.e = e - - sn := "5000cca0bbce17ff" - //pid := "HGS THUS728T8TAL" - pid := "HGS THUS728T8TA" - vendor := "ATA" - expectedDevice := "/dev/sdc" - d2CR := testDriveCR - d2CR.Spec.SerialNumber = sn - d2CR.Spec.VID = vendor - d2CR.Spec.PID = pid - - res, err := l.SearchDrivePath(&d2CR.Spec) + res, err = l.SearchDrivePath(&d2CR.Spec) assert.Nil(t, err) assert.Equal(t, expectedDevice, res) } @@ -150,27 +138,6 @@ func TestLSBLK_SearchDrivePath(t *testing.T) { assert.NotNil(t, err) } -func TestLSBLK_SearchDrivePath_EmptyVendor_Success(t *testing.T) { - l := NewLSBLK(testLogger) - e := &mocks.GoMockExecutor{} - - e.On("RunCmd", allDevicesCmd).Return(mocks.LsblkDevNullVendor, "", nil) - l.e = e - - sn := "PHLJ043300VY4P0DGN" - pid := "Dell Express Flash NVMe P4510 4TB SFF" - vendor := "0x8086" - expectedDevice := "/dev/sdc" - d2CR := testDriveCR - d2CR.Spec.SerialNumber = sn - d2CR.Spec.VID = vendor - d2CR.Spec.PID = pid - - res, err := l.SearchDrivePath(&d2CR.Spec) - assert.Nil(t, err) - assert.Equal(t, expectedDevice, res) -} - func TestLSBLK_GetBlockDevicesV2_Success(t *testing.T) { l := NewLSBLK(testLogger) e := &mocks.GoMockExecutor{} diff --git a/pkg/mocks/commands.go b/pkg/mocks/commands.go index 5ef54ef48..15ed1b9ea 100644 --- a/pkg/mocks/commands.go +++ b/pkg/mocks/commands.go @@ -163,23 +163,7 @@ var LsblkDevV2 = `{ "serial":"5000cca0bbce17ff", "wwn":"0x5000cca0bbce17ff", "vendor":"ATA ", - "model":"HGS THUS728T8TAL", - "rev":"RT04", - "mountpoint":null, - "fstype":null, - "partuuid":null - }]}` - -// LsblkDevNullVendor provides output for lsblk with null vendor -var LsblkDevNullVendor = `{ - "blockdevices": [{ - "name":"/dev/sdc", - "type":"disk", - "size":8001563222016, - "rota":true, - "serial":"PHLJ043300VY4P0DGN", - "vendor":null, - "model":"Dell Express Flash NVMe P4510 4TB SFF", + "model":"HGST_HUS728T8TAL", "rev":"RT04", "mountpoint":null, "fstype":null,