Commit 6356d85d authored by Jan Safranek's avatar Jan Safranek

Add Cinder volume plugin attach tests.

parent a8fecd0c
...@@ -30,7 +30,8 @@ import ( ...@@ -30,7 +30,8 @@ import (
) )
type cinderDiskAttacher struct { type cinderDiskAttacher struct {
host volume.VolumeHost host volume.VolumeHost
cinderProvider CinderProvider
} }
var _ volume.Attacher = &cinderDiskAttacher{} var _ volume.Attacher = &cinderDiskAttacher{}
...@@ -42,7 +43,14 @@ const ( ...@@ -42,7 +43,14 @@ const (
) )
func (plugin *cinderPlugin) NewAttacher() (volume.Attacher, error) { func (plugin *cinderPlugin) NewAttacher() (volume.Attacher, error) {
return &cinderDiskAttacher{host: plugin.host}, nil cinder, err := getCloudProvider(plugin.host.GetCloudProvider())
if err != nil {
return nil, err
}
return &cinderDiskAttacher{
host: plugin.host,
cinderProvider: cinder,
}, nil
} }
func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (string, error) { func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (string, error) {
...@@ -53,11 +61,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) ( ...@@ -53,11 +61,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (
volumeID := volumeSource.VolumeID volumeID := volumeSource.VolumeID
cloud, err := getCloudProvider(attacher.host.GetCloudProvider()) instances, res := attacher.cinderProvider.Instances()
if err != nil {
return "", err
}
instances, res := cloud.Instances()
if !res { if !res {
return "", fmt.Errorf("failed to list openstack instances") return "", fmt.Errorf("failed to list openstack instances")
} }
...@@ -68,7 +72,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) ( ...@@ -68,7 +72,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (
if ind := strings.LastIndex(instanceid, "/"); ind >= 0 { if ind := strings.LastIndex(instanceid, "/"); ind >= 0 {
instanceid = instanceid[(ind + 1):] instanceid = instanceid[(ind + 1):]
} }
attached, err := cloud.DiskIsAttached(volumeID, instanceid) attached, err := attacher.cinderProvider.DiskIsAttached(volumeID, instanceid)
if err != nil { if err != nil {
// Log error and continue with attach // Log error and continue with attach
glog.Warningf( glog.Warningf(
...@@ -80,7 +84,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) ( ...@@ -80,7 +84,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (
// Volume is already attached to node. // Volume is already attached to node.
glog.Infof("Attach operation is successful. volume %q is already attached to node %q.", volumeID, instanceid) glog.Infof("Attach operation is successful. volume %q is already attached to node %q.", volumeID, instanceid)
} else { } else {
_, err = cloud.AttachDisk(instanceid, volumeID) _, err = attacher.cinderProvider.AttachDisk(instanceid, volumeID)
if err == nil { if err == nil {
glog.Infof("Attach operation successful: volume %q attached to node %q.", volumeID, instanceid) glog.Infof("Attach operation successful: volume %q attached to node %q.", volumeID, instanceid)
} else { } else {
...@@ -89,7 +93,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) ( ...@@ -89,7 +93,7 @@ func (attacher *cinderDiskAttacher) Attach(spec *volume.Spec, hostName string) (
} }
} }
devicePath, err := cloud.GetAttachmentDiskPath(instanceid, volumeID) devicePath, err := attacher.cinderProvider.GetAttachmentDiskPath(instanceid, volumeID)
if err != nil { if err != nil {
glog.Infof("Attach volume %q to instance %q failed with %v", volumeID, instanceid, err) glog.Infof("Attach volume %q to instance %q failed with %v", volumeID, instanceid, err)
return "", err return "", err
...@@ -181,22 +185,26 @@ func (attacher *cinderDiskAttacher) MountDevice(spec *volume.Spec, devicePath st ...@@ -181,22 +185,26 @@ func (attacher *cinderDiskAttacher) MountDevice(spec *volume.Spec, devicePath st
} }
type cinderDiskDetacher struct { type cinderDiskDetacher struct {
host volume.VolumeHost mounter mount.Interface
cinderProvider CinderProvider
} }
var _ volume.Detacher = &cinderDiskDetacher{} var _ volume.Detacher = &cinderDiskDetacher{}
func (plugin *cinderPlugin) NewDetacher() (volume.Detacher, error) { func (plugin *cinderPlugin) NewDetacher() (volume.Detacher, error) {
return &cinderDiskDetacher{host: plugin.host}, nil cinder, err := getCloudProvider(plugin.host.GetCloudProvider())
if err != nil {
return nil, err
}
return &cinderDiskDetacher{
mounter: plugin.host.GetMounter(),
cinderProvider: cinder,
}, nil
} }
func (detacher *cinderDiskDetacher) Detach(deviceMountPath string, hostName string) error { func (detacher *cinderDiskDetacher) Detach(deviceMountPath string, hostName string) error {
volumeID := path.Base(deviceMountPath) volumeID := path.Base(deviceMountPath)
cloud, err := getCloudProvider(detacher.host.GetCloudProvider()) instances, res := detacher.cinderProvider.Instances()
if err != nil {
return err
}
instances, res := cloud.Instances()
if !res { if !res {
return fmt.Errorf("failed to list openstack instances") return fmt.Errorf("failed to list openstack instances")
} }
...@@ -205,7 +213,7 @@ func (detacher *cinderDiskDetacher) Detach(deviceMountPath string, hostName stri ...@@ -205,7 +213,7 @@ func (detacher *cinderDiskDetacher) Detach(deviceMountPath string, hostName stri
instanceid = instanceid[(ind + 1):] instanceid = instanceid[(ind + 1):]
} }
attached, err := cloud.DiskIsAttached(volumeID, instanceid) attached, err := detacher.cinderProvider.DiskIsAttached(volumeID, instanceid)
if err != nil { if err != nil {
// Log error and continue with detach // Log error and continue with detach
glog.Errorf( glog.Errorf(
...@@ -219,7 +227,7 @@ func (detacher *cinderDiskDetacher) Detach(deviceMountPath string, hostName stri ...@@ -219,7 +227,7 @@ func (detacher *cinderDiskDetacher) Detach(deviceMountPath string, hostName stri
return nil return nil
} }
if err = cloud.DetachDisk(instanceid, volumeID); err != nil { if err = detacher.cinderProvider.DetachDisk(instanceid, volumeID); err != nil {
glog.Errorf("Error detaching volume %q: %v", volumeID, err) glog.Errorf("Error detaching volume %q: %v", volumeID, err)
return err return err
} }
...@@ -249,9 +257,8 @@ func (detacher *cinderDiskDetacher) WaitForDetach(devicePath string, timeout tim ...@@ -249,9 +257,8 @@ func (detacher *cinderDiskDetacher) WaitForDetach(devicePath string, timeout tim
} }
func (detacher *cinderDiskDetacher) UnmountDevice(deviceMountPath string) error { func (detacher *cinderDiskDetacher) UnmountDevice(deviceMountPath string) error {
mounter := detacher.host.GetMounter()
volume := path.Base(deviceMountPath) volume := path.Base(deviceMountPath)
if err := unmountPDAndRemoveGlobalPath(deviceMountPath, mounter); err != nil { if err := unmountPDAndRemoveGlobalPath(deviceMountPath, detacher.mounter); err != nil {
glog.Errorf("Error unmounting %q: %v", volume, err) glog.Errorf("Error unmounting %q: %v", volume, err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment