Skip to content

Commit

Permalink
[Issue-1104] model mismatch (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
katarzyna-kulpa authored Mar 5, 2024
1 parent a1af520 commit 0c413ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pkg/base/linuxutils/lsblk/lsblk.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ func (l *LSBLK) SearchDrivePath(drive *api.Drive) (string, error) {
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) && strings.EqualFold(lvid, vid) &&
strings.HasPrefix(l.Model, pid) {
hasPrefixIgnoreSpaces(l.Model, pid) {
device = l.Name
break
}
Expand All @@ -188,3 +190,12 @@ 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)
}
20 changes: 20 additions & 0 deletions pkg/base/linuxutils/lsblk/lsblk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ func TestLSBLK_SearchDrivePath_Success(t *testing.T) {
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 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)
assert.Nil(t, err)
assert.Equal(t, expectedDevice, res)
}

func TestLSBLK_SearchDrivePath(t *testing.T) {
e := &mocks.GoMockExecutor{}
l := NewLSBLK(testLogger)
Expand Down
2 changes: 1 addition & 1 deletion pkg/mocks/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ var LsblkDevV2 = `{
"serial":"5000cca0bbce17ff",
"wwn":"0x5000cca0bbce17ff",
"vendor":"ATA ",
"model":"HGST_HUS728T8TAL",
"model":"HGS THUS728T8TAL",
"rev":"RT04",
"mountpoint":null,
"fstype":null,
Expand Down

0 comments on commit 0c413ba

Please sign in to comment.