Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue: 1104] model name with empty spaces #1105

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pkg/base/linuxutils/lsblk/lsblk.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ func (l *LSBLK) GetBlockDevices(device string) ([]BlockDevice, error) {
return res, nil
}

// 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 := ""
// try to find it with lsblk
Expand All @@ -174,8 +171,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) &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add comments here - why comparison is done this way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these comments work?
// Compare drive's sn, vid, pid with lsblk output.
// 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.

or something shorter/longer?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

strings.HasPrefix(l.Model, pid) {
hasPrefixIgnoreSpaces(l.Model, pid) {
device = l.Name
break
}
Expand All @@ -188,3 +187,11 @@ 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)
}
21 changes: 21 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,27 @@ 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 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)
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