Skip to content

Commit c1dd946

Browse files
committed
add tests and remove dead code
1 parent 736e81c commit c1dd946

File tree

9 files changed

+551
-88
lines changed

9 files changed

+551
-88
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: all build clean install
1+
.PHONY: all build clean install test coverage
22

33
all: clean build install
44

@@ -16,3 +16,6 @@ install:
1616
test:
1717
go test ./iscsi/
1818

19+
coverage:
20+
go test ./iscsi -coverprofile=coverage.out
21+
go tool cover -html=coverage.out

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/kubernetes-csi/csi-lib-iscsi
22

33
go 1.15
44

5-
require github.com/prashantv/gostub v1.0.0
5+
require (
6+
github.com/prashantv/gostub v1.0.0
7+
github.com/stretchr/testify v1.7.0
8+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15
github.com/prashantv/gostub v1.0.0 h1:wTzvgO04xSS3gHuz6Vhuo0/kvWelyJxwNS0IRBPAwGY=
26
github.com/prashantv/gostub v1.0.0/go.mod h1:dP1v6T1QzyGJJKFocwAU0lSZKpfjstjH8TlhkEU0on0=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

iscsi/iscsi.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
osStat = os.Stat
2828
filepathGlob = filepath.Glob
2929
osOpenFile = os.OpenFile
30+
sleep = time.Sleep
3031
)
3132

3233
// iscsiSession contains information avout an iSCSI session
@@ -162,14 +163,14 @@ func getCurrentSessions() ([]iscsiSession, error) {
162163

163164
// waitForPathToExist wait for a file at a path to exists on disk
164165
func waitForPathToExist(devicePath *string, maxRetries, intervalSeconds uint, deviceTransport string) error {
165-
if devicePath == nil {
166+
if devicePath == nil || *devicePath == "" {
166167
return fmt.Errorf("unable to check unspecified devicePath")
167168
}
168169

169170
for i := uint(0); i <= maxRetries; i++ {
170171
if i != 0 {
171172
debug.Printf("Device path %q doesn't exists yet, retrying in %d seconds (%d/%d)", *devicePath, intervalSeconds, i, maxRetries)
172-
time.Sleep(time.Second * time.Duration(intervalSeconds))
173+
sleep(time.Second * time.Duration(intervalSeconds))
173174
}
174175

175176
if err := pathExists(devicePath, deviceTransport); err == nil {
@@ -195,7 +196,11 @@ func pathExists(devicePath *string, deviceTransport string) error {
195196
return err
196197
}
197198
} else {
198-
fpath, _ := filepathGlob(*devicePath)
199+
fpath, err := filepathGlob(*devicePath)
200+
201+
if err != nil {
202+
return err
203+
}
199204
if fpath == nil {
200205
return os.ErrNotExist
201206
}
@@ -210,22 +215,17 @@ func pathExists(devicePath *string, deviceTransport string) error {
210215

211216
// getMultipathDevice returns a multipath device for the configured targets if it exists
212217
func getMultipathDevice(devices []Device) (*Device, error) {
213-
var deviceInfo deviceInfo
214218
var multipathDevice *Device
215219
var devicePaths []string
216220

217221
for _, device := range devices {
218222
devicePaths = append(devicePaths, device.GetPath())
219223
}
220-
out, err := lsblk("-J", devicePaths)
224+
deviceInfo, err := lsblk("", devicePaths)
221225
if err != nil {
222226
return nil, err
223227
}
224228

225-
if err = json.Unmarshal(out, &deviceInfo); err != nil {
226-
return nil, err
227-
}
228-
229229
for _, device := range deviceInfo.BlockDevices {
230230
if len(device.Children) != 1 {
231231
return nil, fmt.Errorf("device is not mapped to exactly one multipath device: %v", device.Children)
@@ -454,18 +454,12 @@ func (c *Connector) IsMultipathEnabled() bool {
454454
func GetSCSIDevices(devicePaths []string) ([]Device, error) {
455455
debug.Printf("Getting info about SCSI devices %s.\n", devicePaths)
456456

457-
out, err := lsblk("-JS", devicePaths)
457+
deviceInfo, err := lsblk("-S", devicePaths)
458458
if err != nil {
459459
debug.Printf("An error occured while looking info about SCSI devices: %v", err)
460460
return nil, err
461461
}
462462

463-
var deviceInfo deviceInfo
464-
err = json.Unmarshal(out, &deviceInfo)
465-
if err != nil {
466-
return nil, err
467-
}
468-
469463
return deviceInfo.BlockDevices, nil
470464
}
471465

@@ -488,7 +482,7 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
488482
}
489483

490484
// lsblk execute the lsblk commands
491-
func lsblk(flags string, devicePaths []string) ([]byte, error) {
485+
func lsblkRaw(flags string, devicePaths []string) ([]byte, error) {
492486
out, err := execCommand("lsblk", append([]string{flags}, devicePaths...)...).CombinedOutput()
493487
debug.Printf("lsblk %s %s", flags, strings.Join(devicePaths, " "))
494488
if err != nil {
@@ -498,6 +492,21 @@ func lsblk(flags string, devicePaths []string) ([]byte, error) {
498492
return out, nil
499493
}
500494

495+
func lsblk(flags string, devicePaths []string) (*deviceInfo, error) {
496+
var deviceInfo deviceInfo
497+
498+
out, err := lsblkRaw("-J "+flags, devicePaths)
499+
if err != nil {
500+
return nil, err
501+
}
502+
503+
if err = json.Unmarshal(out, &deviceInfo); err != nil {
504+
return nil, err
505+
}
506+
507+
return &deviceInfo, nil
508+
}
509+
501510
// writeInSCSIDeviceFile write into special devices files to change devices state
502511
func writeInSCSIDeviceFile(hctl string, file string, content string) error {
503512
filename := filepath.Join("/sys/class/scsi_device", hctl, "device", file)
@@ -614,15 +623,15 @@ func (d *Device) WriteDeviceFile(name string, content string) error {
614623

615624
// Shutdown turn off an SCSI device by writing offline\n in /sys/class/scsi_device/h:c:t:l/device/state
616625
func (d *Device) Shutdown() error {
617-
return writeInSCSIDeviceFile(d.Hctl, "state", "offline\n")
626+
return d.WriteDeviceFile("state", "offline\n")
618627
}
619628

620629
// Delete detach an SCSI device by writing 1 in /sys/class/scsi_device/h:c:t:l/device/delete
621630
func (d *Device) Delete() error {
622-
return writeInSCSIDeviceFile(d.Hctl, "delete", "1")
631+
return d.WriteDeviceFile("delete", "1")
623632
}
624633

625634
// Rescan rescan an SCSI device by writing 1 in /sys/class/scsi_device/h:c:t:l/device/rescan
626635
func (d *Device) Rescan() error {
627-
return writeInSCSIDeviceFile(d.Hctl, "rescan", "1")
636+
return d.WriteDeviceFile("rescan", "1")
628637
}

0 commit comments

Comments
 (0)