Skip to content

Commit

Permalink
add tests and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Nov 2, 2021
1 parent 736e81c commit c1dd946
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 88 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all build clean install
.PHONY: all build clean install test coverage

all: clean build install

Expand All @@ -16,3 +16,6 @@ install:
test:
go test ./iscsi/

coverage:
go test ./iscsi -coverprofile=coverage.out
go tool cover -html=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/kubernetes-csi/csi-lib-iscsi

go 1.15

require github.com/prashantv/gostub v1.0.0
require (
github.com/prashantv/gostub v1.0.0
github.com/stretchr/testify v1.7.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.0.0 h1:wTzvgO04xSS3gHuz6Vhuo0/kvWelyJxwNS0IRBPAwGY=
github.com/prashantv/gostub v1.0.0/go.mod h1:dP1v6T1QzyGJJKFocwAU0lSZKpfjstjH8TlhkEU0on0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
49 changes: 29 additions & 20 deletions iscsi/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
osStat = os.Stat
filepathGlob = filepath.Glob
osOpenFile = os.OpenFile
sleep = time.Sleep
)

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

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

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

if err := pathExists(devicePath, deviceTransport); err == nil {
Expand All @@ -195,7 +196,11 @@ func pathExists(devicePath *string, deviceTransport string) error {
return err
}
} else {
fpath, _ := filepathGlob(*devicePath)
fpath, err := filepathGlob(*devicePath)

if err != nil {
return err
}
if fpath == nil {
return os.ErrNotExist
}
Expand All @@ -210,22 +215,17 @@ func pathExists(devicePath *string, deviceTransport string) error {

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

for _, device := range devices {
devicePaths = append(devicePaths, device.GetPath())
}
out, err := lsblk("-J", devicePaths)
deviceInfo, err := lsblk("", devicePaths)
if err != nil {
return nil, err
}

if err = json.Unmarshal(out, &deviceInfo); err != nil {
return nil, err
}

for _, device := range deviceInfo.BlockDevices {
if len(device.Children) != 1 {
return nil, fmt.Errorf("device is not mapped to exactly one multipath device: %v", device.Children)
Expand Down Expand Up @@ -454,18 +454,12 @@ func (c *Connector) IsMultipathEnabled() bool {
func GetSCSIDevices(devicePaths []string) ([]Device, error) {
debug.Printf("Getting info about SCSI devices %s.\n", devicePaths)

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

var deviceInfo deviceInfo
err = json.Unmarshal(out, &deviceInfo)
if err != nil {
return nil, err
}

return deviceInfo.BlockDevices, nil
}

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

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

func lsblk(flags string, devicePaths []string) (*deviceInfo, error) {
var deviceInfo deviceInfo

out, err := lsblkRaw("-J "+flags, devicePaths)
if err != nil {
return nil, err
}

if err = json.Unmarshal(out, &deviceInfo); err != nil {
return nil, err
}

return &deviceInfo, nil
}

// writeInSCSIDeviceFile write into special devices files to change devices state
func writeInSCSIDeviceFile(hctl string, file string, content string) error {
filename := filepath.Join("/sys/class/scsi_device", hctl, "device", file)
Expand Down Expand Up @@ -614,15 +623,15 @@ func (d *Device) WriteDeviceFile(name string, content string) error {

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

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

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

0 comments on commit c1dd946

Please sign in to comment.