Skip to content

Commit

Permalink
fix tests and example
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Nov 2, 2021
1 parent 898cec5 commit df4b0a3
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 146 deletions.
31 changes: 18 additions & 13 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

var (
portals = flag.String("portals", "192.168.1.112:3260", "Comma delimited. Eg: 1.1.1.1,2.2.2.2")
iqn = flag.String("iqn", "iqn.2010-10.org.openstack:volume-95739000-1557-44f8-9f40-e9d29fe6ec47", "")
username = flag.String("username", "3aX7EEf3CEgvESQG75qh", "")
password = flag.String("password", "eJBDC7Bt7WE3XFDq", "")
lun = flag.Int("lun", 1, "")
debug = flag.Bool("debug", false, "enable logging")
portals = flag.String("portals", "192.168.1.112:3260", "Comma delimited. Eg: 1.1.1.1,2.2.2.2")
iqn = flag.String("iqn", "iqn.2010-10.org.openstack:volume-95739000-1557-44f8-9f40-e9d29fe6ec47", "")
username = flag.String("username", "3aX7EEf3CEgvESQG75qh", "")
password = flag.String("password", "eJBDC7Bt7WE3XFDq", "")
lun = flag.Int("lun", 1, "")
debug = flag.Bool("debug", false, "enable logging")
)

func main() {
Expand All @@ -32,9 +32,9 @@ func main() {
parts := strings.Split(tgtp, ":")
targets = append(targets, iscsi.TargetInfo{
// Specify the target iqn we're dealing with
Iqn: *iqn,
Iqn: *iqn,
Portal: parts[0],
Port: parts[1],
Port: parts[1],
})
}

Expand Down Expand Up @@ -62,16 +62,21 @@ func main() {

// Now we can just issue a connection request using our Connector
// A succesful connection will include the device path to access our iscsi volume
path, err := iscsi.Connect(c)
path, err := c.Connect()
if err != nil {
log.Printf("Error returned from iscsi.Connect: %s", err.Error())
log.Printf("Error returned from c.Connect: %s", err.Error())
os.Exit(1)
}

log.Printf("Connected device at path: %s\n", path)
time.Sleep(3 * time.Second)

// Disconnect is easy as well, we don't need the full Connector any more, just the Target IQN and the Portals
/// this should disconnect the volume as well as clear out the iscsi DB entries associated with it
iscsi.Disconnect(*iqn, tgtps)
// This will disconnect the volume
if err := c.DisconnectVolume(); err != nil {
log.Printf("Error returned from c.DisconnectVolume: %s", err.Error())
os.Exit(1)
}

// This will disconnect the session as well as clear out the iscsi DB entries associated with it
c.Disconnect()
}
49 changes: 13 additions & 36 deletions iscsi/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
const defaultPort = "3260"

var (
debug *log.Logger
execCommand = exec.Command
execWithTimeout = ExecWithTimeout
debug *log.Logger
execCommand = exec.Command
execCommandContext = exec.CommandContext
execWithTimeout = ExecWithTimeout
osStat = os.Stat
filepathGlob = filepath.Glob
osOpenFile = os.OpenFile
)

type statFunc func(string) (os.FileInfo, error)
type globFunc func(string) ([]string, error)

// iscsiSession contains information avout an iSCSI session
type iscsiSession struct {
Protocol string
Expand Down Expand Up @@ -161,10 +162,6 @@ 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 {
return waitForPathToExistImpl(devicePath, maxRetries, intervalSeconds, deviceTransport, os.Stat, filepath.Glob)
}

func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds uint, deviceTransport string, osStat statFunc, filepathGlob globFunc) error {
if devicePath == nil {
return fmt.Errorf("unable to check unspecified devicePath")
}
Expand All @@ -175,7 +172,7 @@ func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds uint
time.Sleep(time.Second * time.Duration(intervalSeconds))
}

if err := pathExistsImpl(devicePath, deviceTransport, osStat, filepathGlob); err == nil {
if err := pathExists(devicePath, deviceTransport); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
Expand All @@ -187,10 +184,6 @@ func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds uint

// pathExists checks if a file at a path exists on disk
func pathExists(devicePath *string, deviceTransport string) error {
return pathExistsImpl(devicePath, deviceTransport, os.Stat, filepath.Glob)
}

func pathExistsImpl(devicePath *string, deviceTransport string, osStat statFunc, filepathGlob globFunc) error {
if deviceTransport == "tcp" {
_, err := osStat(*devicePath)
if err != nil {
Expand Down Expand Up @@ -423,11 +416,7 @@ func (c *Connector) DisconnectVolume() error {
} else {
devicePath := c.MountTargetDevice.GetPath()
debug.Printf("Removing normal device in path %s.\n", devicePath)
device, err := GetISCSIDevice(devicePath)
if err != nil {
return err
}
if err = RemoveSCSIDevices(*device); err != nil {
if err := RemoveSCSIDevices(*c.MountTargetDevice); err != nil {
return err
}
}
Expand Down Expand Up @@ -460,18 +449,6 @@ func (c *Connector) IsMultipathEnabled() bool {
return len(c.Devices) > 1
}

// GetISCSIDevice get an SCSI device from a device name
func GetISCSIDevice(deviceName string) (*Device, error) {
iscsiDevices, err := GetISCSIDevices([]string{deviceName})
if err != nil {
return nil, err
}
if len(iscsiDevices) == 0 {
return nil, fmt.Errorf("device %q not found", deviceName)
}
return &iscsiDevices[0], nil
}

// GetSCSIDevices get SCSI devices from device paths
// It will returns all SCSI devices if no paths are given
func GetSCSIDevices(devicePaths []string) ([]Device, error) {
Expand Down Expand Up @@ -512,7 +489,7 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {

// lsblk execute the lsblk commands
func lsblk(flags string, devicePaths []string) ([]byte, error) {
out, err := exec.Command("lsblk", append([]string{flags}, devicePaths...)...).CombinedOutput()
out, err := execCommand("lsblk", append([]string{flags}, devicePaths...)...).CombinedOutput()
debug.Printf("lsblk %s %s", flags, strings.Join(devicePaths, " "))
if err != nil {
return nil, fmt.Errorf("lsblk: %s", string(out))
Expand All @@ -526,7 +503,7 @@ func writeInSCSIDeviceFile(hctl string, file string, content string) error {
filename := filepath.Join("/sys/class/scsi_device", hctl, "device", file)
debug.Printf("Write %q in %q.\n", content, filename)

f, err := os.OpenFile(filename, os.O_TRUNC|os.O_WRONLY, 0200)
f, err := osOpenFile(filename, os.O_TRUNC|os.O_WRONLY, 0200)
if err != nil {
debug.Printf("Error while opening file %v: %v\n", filename, err)
return err
Expand All @@ -549,7 +526,7 @@ func RemoveSCSIDevices(devices ...Device) error {
for _, device := range devices {
debug.Printf("Flush SCSI device %v.\n", device.Name)
if err := device.Exists(); err == nil {
out, err := exec.Command("blockdev", "--flushbufs", device.GetPath()).CombinedOutput()
out, err := execCommand("blockdev", "--flushbufs", device.GetPath()).CombinedOutput()
if err != nil {
debug.Printf("Command 'blockdev --flushbufs %s' did not succeed to flush the device: %v\n", device.GetPath(), err)
return errors.New(string(out))
Expand Down Expand Up @@ -617,7 +594,7 @@ func GetConnectorFromFile(filePath string) (*Connector, error) {

// Exists check if the device exists at its path and returns an error otherwise
func (d *Device) Exists() error {
_, err := os.Stat(d.GetPath())
_, err := osStat(d.GetPath())
return err
}

Expand Down
Loading

0 comments on commit df4b0a3

Please sign in to comment.