func TestFlockerUtil_CreateVolume(t *testing.T) { assert := assert.New(t) // test CreateVolume happy path pvc := volumetest.CreateTestPVC("3Gi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}) options := volume.VolumeOptions{ PVC: pvc, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete, } fakeFlockerClient := newFakeFlockerClient() provisioner := newTestableProvisioner(assert, options).(*flockerVolumeProvisioner) provisioner.flockerClient = fakeFlockerClient flockerUtil := &FlockerUtil{} datasetID, size, _, err := flockerUtil.CreateVolume(provisioner) assert.NoError(err) assert.Equal(datasetOneID, datasetID) assert.Equal(3, size) // test error during CreateVolume fakeFlockerClient.Error = fmt.Errorf("Do not feel like provisioning") _, _, _, err = flockerUtil.CreateVolume(provisioner) assert.Equal(fakeFlockerClient.Error.Error(), err.Error()) }
func TestProvision(t *testing.T) { assert := assert.New(t) pvc := volumetest.CreateTestPVC("3Gi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}) options := volume.VolumeOptions{ PVC: pvc, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete, } provisioner := newTestableProvisioner(assert, options) persistentSpec, err := provisioner.Provision() assert.NoError(err, "Provision() failed: ", err) cap := persistentSpec.Spec.Capacity[api.ResourceStorage] assert.Equal(int64(3*1024*1024*1024), cap.Value()) assert.Equal( "test-flocker-volume-uuid", persistentSpec.Spec.PersistentVolumeSource.Flocker.DatasetUUID, ) assert.Equal( map[string]string{"fakeflockerutil": "yes"}, persistentSpec.Labels, ) // parameters are not supported options = volume.VolumeOptions{ PVC: pvc, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete, Parameters: map[string]string{ "not-supported-params": "test123", }, } provisioner = newTestableProvisioner(assert, options) persistentSpec, err = provisioner.Provision() assert.Error(err, "Provision() did not fail with Parameters specified") // selectors are not supported pvc.Spec.Selector = &unversioned.LabelSelector{MatchLabels: map[string]string{"key": "value"}} options = volume.VolumeOptions{ PVC: pvc, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete, } provisioner = newTestableProvisioner(assert, options) persistentSpec, err = provisioner.Provision() assert.Error(err, "Provision() did not fail with Selector specified") }
func TestProvisioner(t *testing.T) { tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID()) defer os.RemoveAll(tempPath) err := os.MkdirAll(tempPath, 0750) plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{ProvisioningEnabled: true}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */)) spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: tempPath}}}}} plug, err := plugMgr.FindCreatablePluginBySpec(spec) if err != nil { t.Errorf("Can't find the plugin by name") } options := volume.VolumeOptions{ PVC: volumetest.CreateTestPVC("1Gi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}), PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete, } creater, err := plug.NewProvisioner(options) if err != nil { t.Errorf("Failed to make a new Provisioner: %v", err) } pv, err := creater.Provision() if err != nil { t.Errorf("Unexpected error creating volume: %v", err) } if pv.Spec.HostPath.Path == "" { t.Errorf("Expected pv.Spec.HostPath.Path to not be empty: %#v", pv) } expectedCapacity := resource.NewQuantity(1*1024*1024*1024, resource.BinarySI) actualCapacity := pv.Spec.Capacity[api.ResourceStorage] expectedAmt := expectedCapacity.Value() actualAmt := actualCapacity.Value() if expectedAmt != actualAmt { t.Errorf("Expected capacity %+v but got %+v", expectedAmt, actualAmt) } if pv.Spec.PersistentVolumeReclaimPolicy != api.PersistentVolumeReclaimDelete { t.Errorf("Expected reclaim policy %+v but got %+v", api.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy) } os.RemoveAll(pv.Spec.HostPath.Path) }
func TestPlugin(t *testing.T) { tmpDir, err := utiltesting.MkTmpdir("awsebsTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/aws-ebs") if err != nil { t.Errorf("Can't find the plugin by name") } spec := &v1.Volume{ Name: "vol1", VolumeSource: v1.VolumeSource{ AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{ VolumeID: "pd", FSType: "ext4", }, }, } fakeManager := &fakePDManager{} fakeMounter := &mount.FakeMounter{} mounter, err := plug.(*awsElasticBlockStorePlugin).newMounterInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), fakeManager, fakeMounter) if err != nil { t.Errorf("Failed to make a new Mounter: %v", err) } if mounter == nil { t.Errorf("Got a nil Mounter") } volPath := path.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~aws-ebs/vol1") path := mounter.GetPath() if path != volPath { t.Errorf("Got unexpected path: %s", path) } if err := mounter.SetUp(nil); err != nil { t.Errorf("Expected success, got: %v", err) } if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { t.Errorf("SetUp() failed, volume path not created: %s", path) } else { t.Errorf("SetUp() failed: %v", err) } } if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { t.Errorf("SetUp() failed, volume path not created: %s", path) } else { t.Errorf("SetUp() failed: %v", err) } } fakeManager = &fakePDManager{} unmounter, err := plug.(*awsElasticBlockStorePlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager, fakeMounter) if err != nil { t.Errorf("Failed to make a new Unmounter: %v", err) } if unmounter == nil { t.Errorf("Got a nil Unmounter") } if err := unmounter.TearDown(); err != nil { t.Errorf("Expected success, got: %v", err) } if _, err := os.Stat(path); err == nil { t.Errorf("TearDown() failed, volume path still exists: %s", path) } else if !os.IsNotExist(err) { t.Errorf("SetUp() failed: %v", err) } // Test Provisioner options := volume.VolumeOptions{ PVC: volumetest.CreateTestPVC("100Mi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}), PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete, } provisioner, err := plug.(*awsElasticBlockStorePlugin).newProvisionerInternal(options, &fakePDManager{}) persistentSpec, err := provisioner.Provision() if err != nil { t.Errorf("Provision() failed: %v", err) } if persistentSpec.Spec.PersistentVolumeSource.AWSElasticBlockStore.VolumeID != "test-aws-volume-name" { t.Errorf("Provision() returned unexpected volume ID: %s", persistentSpec.Spec.PersistentVolumeSource.AWSElasticBlockStore.VolumeID) } cap := persistentSpec.Spec.Capacity[v1.ResourceStorage] size := cap.Value() if size != 100*1024*1024*1024 { t.Errorf("Provision() returned unexpected volume size: %v", size) } if persistentSpec.Labels["fakepdmanager"] != "yes" { t.Errorf("Provision() returned unexpected labels: %v", persistentSpec.Labels) } // Test Deleter volSpec := &volume.Spec{ PersistentVolume: persistentSpec, } deleter, err := plug.(*awsElasticBlockStorePlugin).newDeleterInternal(volSpec, &fakePDManager{}) err = deleter.Delete() if err != nil { t.Errorf("Deleter() failed: %v", err) } }
func TestPlugin(t *testing.T) { // Initial setup to test volume plugin tmpDir, err := utiltesting.MkTmpdir("vsphereVolumeTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/vsphere-volume") if err != nil { t.Errorf("Can't find the plugin by name") } spec := &api.Volume{ Name: "vol1", VolumeSource: api.VolumeSource{ VsphereVolume: &api.VsphereVirtualDiskVolumeSource{ VolumePath: "[local] test-volume-name.vmdk", FSType: "ext4", }, }, } // Test Mounter fakeManager := &fakePDManager{} fakeMounter := &mount.FakeMounter{} mounter, err := plug.(*vsphereVolumePlugin).newMounterInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), fakeManager, fakeMounter) if err != nil { t.Errorf("Failed to make a new Mounter: %v", err) } if mounter == nil { t.Errorf("Got a nil Mounter") } mntPath := path.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~vsphere-volume/vol1") path := mounter.GetPath() if path != mntPath { t.Errorf("Got unexpected path: %s", path) } if err := mounter.SetUp(nil); err != nil { t.Errorf("Expected success, got: %v", err) } // Test Unmounter fakeManager = &fakePDManager{} unmounter, err := plug.(*vsphereVolumePlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager, fakeMounter) if err != nil { t.Errorf("Failed to make a new Unmounter: %v", err) } if unmounter == nil { t.Errorf("Got a nil Unmounter") } if err := unmounter.TearDown(); err != nil { t.Errorf("Expected success, got: %v", err) } if _, err := os.Stat(path); err == nil { t.Errorf("TearDown() failed, volume path still exists: %s", path) } else if !os.IsNotExist(err) { t.Errorf("SetUp() failed: %v", err) } // Test Provisioner options := volume.VolumeOptions{ PVC: volumetest.CreateTestPVC("100Mi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}), PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete, } provisioner, err := plug.(*vsphereVolumePlugin).newProvisionerInternal(options, &fakePDManager{}) persistentSpec, err := provisioner.Provision() if err != nil { t.Errorf("Provision() failed: %v", err) } if persistentSpec.Spec.PersistentVolumeSource.VsphereVolume.VolumePath != "[local] test-volume-name.vmdk" { t.Errorf("Provision() returned unexpected path %s", persistentSpec.Spec.PersistentVolumeSource.VsphereVolume.VolumePath) } cap := persistentSpec.Spec.Capacity[api.ResourceStorage] size := cap.Value() if size != 100*1024 { t.Errorf("Provision() returned unexpected volume size: %v", size) } // Test Deleter volSpec := &volume.Spec{ PersistentVolume: persistentSpec, } deleter, err := plug.(*vsphereVolumePlugin).newDeleterInternal(volSpec, &fakePDManager{}) err = deleter.Delete() if err != nil { t.Errorf("Deleter() failed: %v", err) } }