@@ -20,14 +20,15 @@ import (
20
20
const defaultPort = "3260"
21
21
22
22
var (
23
- debug * log.Logger
24
- execCommand = exec .Command
25
- execWithTimeout = ExecWithTimeout
23
+ debug * log.Logger
24
+ execCommand = exec .Command
25
+ execCommandContext = exec .CommandContext
26
+ execWithTimeout = ExecWithTimeout
27
+ osStat = os .Stat
28
+ filepathGlob = filepath .Glob
29
+ osOpenFile = os .OpenFile
26
30
)
27
31
28
- type statFunc func (string ) (os.FileInfo , error )
29
- type globFunc func (string ) ([]string , error )
30
-
31
32
// iscsiSession contains information avout an iSCSI session
32
33
type iscsiSession struct {
33
34
Protocol string
@@ -161,10 +162,6 @@ func getCurrentSessions() ([]iscsiSession, error) {
161
162
162
163
// waitForPathToExist wait for a file at a path to exists on disk
163
164
func waitForPathToExist (devicePath * string , maxRetries , intervalSeconds uint , deviceTransport string ) error {
164
- return waitForPathToExistImpl (devicePath , maxRetries , intervalSeconds , deviceTransport , os .Stat , filepath .Glob )
165
- }
166
-
167
- func waitForPathToExistImpl (devicePath * string , maxRetries , intervalSeconds uint , deviceTransport string , osStat statFunc , filepathGlob globFunc ) error {
168
165
if devicePath == nil {
169
166
return fmt .Errorf ("unable to check unspecified devicePath" )
170
167
}
@@ -175,7 +172,7 @@ func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds uint
175
172
time .Sleep (time .Second * time .Duration (intervalSeconds ))
176
173
}
177
174
178
- if err := pathExistsImpl (devicePath , deviceTransport , osStat , filepathGlob ); err == nil {
175
+ if err := pathExists (devicePath , deviceTransport ); err == nil {
179
176
return nil
180
177
} else if ! os .IsNotExist (err ) {
181
178
return err
@@ -187,10 +184,6 @@ func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds uint
187
184
188
185
// pathExists checks if a file at a path exists on disk
189
186
func pathExists (devicePath * string , deviceTransport string ) error {
190
- return pathExistsImpl (devicePath , deviceTransport , os .Stat , filepath .Glob )
191
- }
192
-
193
- func pathExistsImpl (devicePath * string , deviceTransport string , osStat statFunc , filepathGlob globFunc ) error {
194
187
if deviceTransport == "tcp" {
195
188
_ , err := osStat (* devicePath )
196
189
if err != nil {
@@ -423,11 +416,7 @@ func (c *Connector) DisconnectVolume() error {
423
416
} else {
424
417
devicePath := c .MountTargetDevice .GetPath ()
425
418
debug .Printf ("Removing normal device in path %s.\n " , devicePath )
426
- device , err := GetISCSIDevice (devicePath )
427
- if err != nil {
428
- return err
429
- }
430
- if err = RemoveSCSIDevices (* device ); err != nil {
419
+ if err := RemoveSCSIDevices (* c .MountTargetDevice ); err != nil {
431
420
return err
432
421
}
433
422
}
@@ -460,18 +449,6 @@ func (c *Connector) IsMultipathEnabled() bool {
460
449
return len (c .Devices ) > 1
461
450
}
462
451
463
- // GetISCSIDevice get an SCSI device from a device name
464
- func GetISCSIDevice (deviceName string ) (* Device , error ) {
465
- iscsiDevices , err := GetISCSIDevices ([]string {deviceName })
466
- if err != nil {
467
- return nil , err
468
- }
469
- if len (iscsiDevices ) == 0 {
470
- return nil , fmt .Errorf ("device %q not found" , deviceName )
471
- }
472
- return & iscsiDevices [0 ], nil
473
- }
474
-
475
452
// GetSCSIDevices get SCSI devices from device paths
476
453
// It will returns all SCSI devices if no paths are given
477
454
func GetSCSIDevices (devicePaths []string ) ([]Device , error ) {
@@ -512,7 +489,7 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
512
489
513
490
// lsblk execute the lsblk commands
514
491
func lsblk (flags string , devicePaths []string ) ([]byte , error ) {
515
- out , err := exec . Command ("lsblk" , append ([]string {flags }, devicePaths ... )... ).CombinedOutput ()
492
+ out , err := execCommand ("lsblk" , append ([]string {flags }, devicePaths ... )... ).CombinedOutput ()
516
493
debug .Printf ("lsblk %s %s" , flags , strings .Join (devicePaths , " " ))
517
494
if err != nil {
518
495
return nil , fmt .Errorf ("lsblk: %s" , string (out ))
@@ -526,7 +503,7 @@ func writeInSCSIDeviceFile(hctl string, file string, content string) error {
526
503
filename := filepath .Join ("/sys/class/scsi_device" , hctl , "device" , file )
527
504
debug .Printf ("Write %q in %q.\n " , content , filename )
528
505
529
- f , err := os . OpenFile (filename , os .O_TRUNC | os .O_WRONLY , 0200 )
506
+ f , err := osOpenFile (filename , os .O_TRUNC | os .O_WRONLY , 0200 )
530
507
if err != nil {
531
508
debug .Printf ("Error while opening file %v: %v\n " , filename , err )
532
509
return err
@@ -549,7 +526,7 @@ func RemoveSCSIDevices(devices ...Device) error {
549
526
for _ , device := range devices {
550
527
debug .Printf ("Flush SCSI device %v.\n " , device .Name )
551
528
if err := device .Exists (); err == nil {
552
- out , err := exec . Command ("blockdev" , "--flushbufs" , device .GetPath ()).CombinedOutput ()
529
+ out , err := execCommand ("blockdev" , "--flushbufs" , device .GetPath ()).CombinedOutput ()
553
530
if err != nil {
554
531
debug .Printf ("Command 'blockdev --flushbufs %s' did not succeed to flush the device: %v\n " , device .GetPath (), err )
555
532
return errors .New (string (out ))
@@ -617,7 +594,7 @@ func GetConnectorFromFile(filePath string) (*Connector, error) {
617
594
618
595
// Exists check if the device exists at its path and returns an error otherwise
619
596
func (d * Device ) Exists () error {
620
- _ , err := os . Stat (d .GetPath ())
597
+ _ , err := osStat (d .GetPath ())
621
598
return err
622
599
}
623
600
0 commit comments