Skip to content

Commit fb3c9e1

Browse files
committed
remove devices even if some are missing
1 parent 203e987 commit fb3c9e1

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

iscsi/iscsi.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (c *Connector) Connect() (string, error) {
279279
// GetISCSIDevices returns all devices if no paths are given
280280
if len(devicePaths) < 1 {
281281
c.Devices = []Device{}
282-
} else if c.Devices, err = GetISCSIDevices(devicePaths); err != nil {
282+
} else if c.Devices, err = GetISCSIDevices(devicePaths, true); err != nil {
283283
return "", err
284284
}
285285

@@ -457,10 +457,10 @@ func (c *Connector) IsMultipathEnabled() bool {
457457

458458
// GetSCSIDevices get SCSI devices from device paths
459459
// It will returns all SCSI devices if no paths are given
460-
func GetSCSIDevices(devicePaths []string) ([]Device, error) {
460+
func GetSCSIDevices(devicePaths []string, strict bool) ([]Device, error) {
461461
debug.Printf("Getting info about SCSI devices %s.\n", devicePaths)
462462

463-
deviceInfo, err := lsblk(devicePaths)
463+
deviceInfo, err := lsblk(devicePaths, strict)
464464
if err != nil {
465465
debug.Printf("An error occured while looking info about SCSI devices: %v", err)
466466
return nil, err
@@ -471,8 +471,8 @@ func GetSCSIDevices(devicePaths []string) ([]Device, error) {
471471

472472
// GetISCSIDevices get iSCSI devices from device paths
473473
// It will returns all iSCSI devices if no paths are given
474-
func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
475-
scsiDevices, err := GetSCSIDevices(devicePaths)
474+
func GetISCSIDevices(devicePaths []string, strict bool) (devices []Device, err error) {
475+
scsiDevices, err := GetSCSIDevices(devicePaths, strict)
476476
if err != nil {
477477
return
478478
}
@@ -488,21 +488,26 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
488488
}
489489

490490
// lsblk execute the lsblk commands
491-
func lsblk(devicePaths []string) (*deviceInfo, error) {
491+
func lsblk(devicePaths []string, strict bool) (*deviceInfo, error) {
492492
flags := []string{"-J", "-o", "NAME,HCTL,TYPE,TRAN,SIZE"}
493493
command := execCommand("lsblk", append(flags, devicePaths...)...)
494494
debug.Println(command.String())
495495
out, err := command.Output()
496496
if err != nil {
497497
if ee, ok := err.(*exec.ExitError); ok {
498-
return nil, fmt.Errorf("%s, (%v)", strings.Trim(string(ee.Stderr), "\n"), ee)
498+
err = fmt.Errorf("%s, (%w)", strings.Trim(string(ee.Stderr), "\n"), ee)
499+
if strict || ee.ExitCode() != 64 { // ignore the error if some devices have been found when not strict
500+
return nil, err
501+
}
502+
debug.Printf("lsblk could find only some devices: %v", err)
503+
} else {
504+
return nil, err
499505
}
500-
return nil, err
501506
}
502507

503508
var deviceInfo deviceInfo
504-
if err = json.Unmarshal(out, &deviceInfo); err != nil {
505-
return nil, err
509+
if jsonErr := json.Unmarshal(out, &deviceInfo); jsonErr != nil {
510+
return nil, jsonErr
506511
}
507512

508513
return &deviceInfo, nil
@@ -603,13 +608,13 @@ func GetConnectorFromFile(filePath string) (*Connector, error) {
603608
devicePaths = append(devicePaths, device.GetPath())
604609
}
605610

606-
if devices, err := GetSCSIDevices([]string{c.MountTargetDevice.GetPath()}); err != nil {
611+
if devices, err := GetSCSIDevices([]string{c.MountTargetDevice.GetPath()}, false); err != nil {
607612
return nil, err
608613
} else {
609614
c.MountTargetDevice = &devices[0]
610615
}
611616

612-
if c.Devices, err = GetSCSIDevices(devicePaths); err != nil {
617+
if c.Devices, err = GetSCSIDevices(devicePaths, false); err != nil {
613618
return nil, err
614619
}
615620

iscsi/iscsi_test.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,12 @@ func Test_getMultipathDevice(t *testing.T) {
581581
func Test_lsblk(t *testing.T) {
582582
sda := Device{Name: "sda", Children: []Device{{}}}
583583

584+
sdaOutput, err := json.Marshal(deviceInfo{BlockDevices: []Device{sda}})
585+
assert.New(t).Nil(err, "could not setup test")
586+
584587
tests := map[string]struct {
585588
devicePaths []string
589+
strict bool
586590
mockedStdout string
587591
mockedDevices []Device
588592
mockedExitStatus int
@@ -591,34 +595,43 @@ func Test_lsblk(t *testing.T) {
591595
"Basic": {
592596
devicePaths: []string{"/dev/sda"},
593597
mockedDevices: []Device{sda},
598+
mockedStdout: string(sdaOutput),
594599
},
595600
"NotABlockDevice": {
596601
devicePaths: []string{"/dev/sdzz"},
597602
mockedStdout: "lsblk: sdzz: not a block device",
598603
mockedExitStatus: 32,
604+
wantErr: true,
599605
},
600606
"InvalidJson": {
601607
mockedStdout: "{",
602608
mockedExitStatus: 0,
603609
wantErr: true,
604610
},
611+
"StrictWithMissingDevices": {
612+
devicePaths: []string{"/dev/sda", "/dev/sdb"},
613+
strict: true,
614+
mockedDevices: []Device{sda},
615+
mockedStdout: string(sdaOutput),
616+
mockedExitStatus: 64,
617+
wantErr: true,
618+
},
619+
"NotStrictWithMissingDevices": {
620+
devicePaths: []string{"/dev/sda", "/dev/sdb"},
621+
mockedDevices: []Device{sda},
622+
mockedStdout: string(sdaOutput),
623+
mockedExitStatus: 64,
624+
},
605625
}
606626

607627
for name, tt := range tests {
608628
t.Run(name, func(t *testing.T) {
609629
assert := assert.New(t)
610630

611-
mockedStdout := tt.mockedStdout
612-
if mockedStdout == "" {
613-
out, err := json.Marshal(deviceInfo{BlockDevices: tt.mockedDevices})
614-
assert.Nil(err, "could not setup test")
615-
mockedStdout = string(out)
616-
}
617-
618-
defer gostub.Stub(&execCommand, makeFakeExecCommand(tt.mockedExitStatus, mockedStdout)).Reset()
619-
deviceInfo, err := lsblk(tt.devicePaths)
631+
defer gostub.Stub(&execCommand, makeFakeExecCommand(tt.mockedExitStatus, tt.mockedStdout)).Reset()
632+
deviceInfo, err := lsblk(tt.devicePaths, tt.strict)
620633

621-
if tt.mockedExitStatus != 0 || tt.wantErr {
634+
if tt.wantErr {
622635
assert.Nil(deviceInfo)
623636
assert.NotNil(err)
624637
} else {

0 commit comments

Comments
 (0)