Commit 5cd5ae8d authored by Jan Safranek's avatar Jan Safranek

Add GCE attacher unit tests.

parent 2b342c1e
...@@ -110,6 +110,9 @@ type Disks interface { ...@@ -110,6 +110,9 @@ type Disks interface {
// is used when instanceID is empty string. // is used when instanceID is empty string.
DetachDisk(devicePath, instanceID string) error DetachDisk(devicePath, instanceID string) error
// DiskIsAttached checks if a disk is attached to the given node.
DiskIsAttached(diskName, instanceID string) (bool, error)
// CreateDisk creates a new PD with given properties. Tags are serialized // CreateDisk creates a new PD with given properties. Tags are serialized
// as JSON into Description field. // as JSON into Description field.
CreateDisk(name string, zone string, sizeGb int64, tags map[string]string) error CreateDisk(name string, zone string, sizeGb int64, tags map[string]string) error
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
"k8s.io/kubernetes/pkg/util/exec" "k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/util/sets" "k8s.io/kubernetes/pkg/util/sets"
...@@ -32,7 +33,8 @@ import ( ...@@ -32,7 +33,8 @@ import (
) )
type gcePersistentDiskAttacher struct { type gcePersistentDiskAttacher struct {
host volume.VolumeHost host volume.VolumeHost
gceDisks gce.Disks
} }
var _ volume.Attacher = &gcePersistentDiskAttacher{} var _ volume.Attacher = &gcePersistentDiskAttacher{}
...@@ -40,7 +42,15 @@ var _ volume.Attacher = &gcePersistentDiskAttacher{} ...@@ -40,7 +42,15 @@ var _ volume.Attacher = &gcePersistentDiskAttacher{}
var _ volume.AttachableVolumePlugin = &gcePersistentDiskPlugin{} var _ volume.AttachableVolumePlugin = &gcePersistentDiskPlugin{}
func (plugin *gcePersistentDiskPlugin) NewAttacher() (volume.Attacher, error) { func (plugin *gcePersistentDiskPlugin) NewAttacher() (volume.Attacher, error) {
return &gcePersistentDiskAttacher{host: plugin.host}, nil gceCloud, err := getCloudProvider(plugin.host.GetCloudProvider())
if err != nil {
return nil, err
}
return &gcePersistentDiskAttacher{
host: plugin.host,
gceDisks: gceCloud,
}, nil
} }
func (plugin *gcePersistentDiskPlugin) GetDeviceName(spec *volume.Spec) (string, error) { func (plugin *gcePersistentDiskPlugin) GetDeviceName(spec *volume.Spec) (string, error) {
...@@ -63,12 +73,7 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st ...@@ -63,12 +73,7 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st
volumeSource, readOnly := getVolumeSource(spec) volumeSource, readOnly := getVolumeSource(spec)
pdName := volumeSource.PDName pdName := volumeSource.PDName
gceCloud, err := getCloudProvider(attacher.host.GetCloudProvider()) attached, err := attacher.gceDisks.DiskIsAttached(pdName, hostName)
if err != nil {
return err
}
attached, err := gceCloud.DiskIsAttached(pdName, hostName)
if err != nil { if err != nil {
// Log error and continue with attach // Log error and continue with attach
glog.Errorf( glog.Errorf(
...@@ -82,7 +87,7 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st ...@@ -82,7 +87,7 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st
return nil return nil
} }
if err = gceCloud.AttachDisk(pdName, hostName, readOnly); err != nil { if err = attacher.gceDisks.AttachDisk(pdName, hostName, readOnly); err != nil {
glog.Errorf("Error attaching PD %q to node %q: %+v", pdName, hostName, err) glog.Errorf("Error attaching PD %q to node %q: %+v", pdName, hostName, err)
return err return err
} }
...@@ -166,13 +171,20 @@ func (attacher *gcePersistentDiskAttacher) MountDevice(spec *volume.Spec, device ...@@ -166,13 +171,20 @@ func (attacher *gcePersistentDiskAttacher) MountDevice(spec *volume.Spec, device
} }
type gcePersistentDiskDetacher struct { type gcePersistentDiskDetacher struct {
host volume.VolumeHost gceDisks gce.Disks
} }
var _ volume.Detacher = &gcePersistentDiskDetacher{} var _ volume.Detacher = &gcePersistentDiskDetacher{}
func (plugin *gcePersistentDiskPlugin) NewDetacher() (volume.Detacher, error) { func (plugin *gcePersistentDiskPlugin) NewDetacher() (volume.Detacher, error) {
return &gcePersistentDiskDetacher{host: plugin.host}, nil gceCloud, err := getCloudProvider(plugin.host.GetCloudProvider())
if err != nil {
return nil, err
}
return &gcePersistentDiskDetacher{
gceDisks: gceCloud,
}, nil
} }
// Detach checks with the GCE cloud provider if the specified volume is already // Detach checks with the GCE cloud provider if the specified volume is already
...@@ -185,12 +197,7 @@ func (plugin *gcePersistentDiskPlugin) NewDetacher() (volume.Detacher, error) { ...@@ -185,12 +197,7 @@ func (plugin *gcePersistentDiskPlugin) NewDetacher() (volume.Detacher, error) {
func (detacher *gcePersistentDiskDetacher) Detach(deviceMountPath string, hostName string) error { func (detacher *gcePersistentDiskDetacher) Detach(deviceMountPath string, hostName string) error {
pdName := path.Base(deviceMountPath) pdName := path.Base(deviceMountPath)
gceCloud, err := getCloudProvider(detacher.host.GetCloudProvider()) attached, err := detacher.gceDisks.DiskIsAttached(pdName, hostName)
if err != nil {
return err
}
attached, err := gceCloud.DiskIsAttached(pdName, hostName)
if err != nil { if err != nil {
// Log error and continue with detach // Log error and continue with detach
glog.Errorf( glog.Errorf(
...@@ -204,7 +211,7 @@ func (detacher *gcePersistentDiskDetacher) Detach(deviceMountPath string, hostNa ...@@ -204,7 +211,7 @@ func (detacher *gcePersistentDiskDetacher) Detach(deviceMountPath string, hostNa
return nil return nil
} }
if err = gceCloud.DetachDisk(pdName, hostName); err != nil { if err = detacher.gceDisks.DetachDisk(pdName, hostName); err != nil {
glog.Errorf("Error detaching PD %q from node %q: %v", pdName, hostName, err) glog.Errorf("Error detaching PD %q from node %q: %v", pdName, hostName, err)
return err return err
} }
......
...@@ -43,7 +43,12 @@ const ( ...@@ -43,7 +43,12 @@ const (
maxChecks = 60 maxChecks = 60
maxRetries = 10 maxRetries = 10
checkSleepDuration = time.Second checkSleepDuration = time.Second
errorSleepDuration = 5 * time.Second )
// These variables are modified only in unit tests and should be constant
// otherwise.
var (
errorSleepDuration time.Duration = 5 * time.Second
) )
type GCEDiskUtil struct{} type GCEDiskUtil struct{}
......
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