Skip to content

Commit df4b0a3

Browse files
committed
fix tests and example
1 parent 898cec5 commit df4b0a3

File tree

4 files changed

+119
-146
lines changed

4 files changed

+119
-146
lines changed

example/main.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

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

2222
func main() {
@@ -32,9 +32,9 @@ func main() {
3232
parts := strings.Split(tgtp, ":")
3333
targets = append(targets, iscsi.TargetInfo{
3434
// Specify the target iqn we're dealing with
35-
Iqn: *iqn,
35+
Iqn: *iqn,
3636
Portal: parts[0],
37-
Port: parts[1],
37+
Port: parts[1],
3838
})
3939
}
4040

@@ -62,16 +62,21 @@ func main() {
6262

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

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

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

iscsi/iscsi.go

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import (
2020
const defaultPort = "3260"
2121

2222
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
2630
)
2731

28-
type statFunc func(string) (os.FileInfo, error)
29-
type globFunc func(string) ([]string, error)
30-
3132
// iscsiSession contains information avout an iSCSI session
3233
type iscsiSession struct {
3334
Protocol string
@@ -161,10 +162,6 @@ func getCurrentSessions() ([]iscsiSession, error) {
161162

162163
// waitForPathToExist wait for a file at a path to exists on disk
163164
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 {
168165
if devicePath == nil {
169166
return fmt.Errorf("unable to check unspecified devicePath")
170167
}
@@ -175,7 +172,7 @@ func waitForPathToExistImpl(devicePath *string, maxRetries, intervalSeconds uint
175172
time.Sleep(time.Second * time.Duration(intervalSeconds))
176173
}
177174

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

188185
// pathExists checks if a file at a path exists on disk
189186
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 {
194187
if deviceTransport == "tcp" {
195188
_, err := osStat(*devicePath)
196189
if err != nil {
@@ -423,11 +416,7 @@ func (c *Connector) DisconnectVolume() error {
423416
} else {
424417
devicePath := c.MountTargetDevice.GetPath()
425418
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 {
431420
return err
432421
}
433422
}
@@ -460,18 +449,6 @@ func (c *Connector) IsMultipathEnabled() bool {
460449
return len(c.Devices) > 1
461450
}
462451

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-
475452
// GetSCSIDevices get SCSI devices from device paths
476453
// It will returns all SCSI devices if no paths are given
477454
func GetSCSIDevices(devicePaths []string) ([]Device, error) {
@@ -512,7 +489,7 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
512489

513490
// lsblk execute the lsblk commands
514491
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()
516493
debug.Printf("lsblk %s %s", flags, strings.Join(devicePaths, " "))
517494
if err != nil {
518495
return nil, fmt.Errorf("lsblk: %s", string(out))
@@ -526,7 +503,7 @@ func writeInSCSIDeviceFile(hctl string, file string, content string) error {
526503
filename := filepath.Join("/sys/class/scsi_device", hctl, "device", file)
527504
debug.Printf("Write %q in %q.\n", content, filename)
528505

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)
530507
if err != nil {
531508
debug.Printf("Error while opening file %v: %v\n", filename, err)
532509
return err
@@ -549,7 +526,7 @@ func RemoveSCSIDevices(devices ...Device) error {
549526
for _, device := range devices {
550527
debug.Printf("Flush SCSI device %v.\n", device.Name)
551528
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()
553530
if err != nil {
554531
debug.Printf("Command 'blockdev --flushbufs %s' did not succeed to flush the device: %v\n", device.GetPath(), err)
555532
return errors.New(string(out))
@@ -617,7 +594,7 @@ func GetConnectorFromFile(filePath string) (*Connector, error) {
617594

618595
// Exists check if the device exists at its path and returns an error otherwise
619596
func (d *Device) Exists() error {
620-
_, err := os.Stat(d.GetPath())
597+
_, err := osStat(d.GetPath())
621598
return err
622599
}
623600

0 commit comments

Comments
 (0)