From 0017b602beb8e8e08be540ebcece26778ab0336f Mon Sep 17 00:00:00 2001 From: phantooom Date: Sun, 21 Feb 2021 23:24:07 +0800 Subject: [PATCH] test: add csi raw block mode wiht podinfo ut --- pkg/volume/csi/csi_block_test.go | 49 +++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/pkg/volume/csi/csi_block_test.go b/pkg/volume/csi/csi_block_test.go index 485998e738a..21dd8a599b7 100644 --- a/pkg/volume/csi/csi_block_test.go +++ b/pkg/volume/csi/csi_block_test.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "testing" "google.golang.org/grpc/codes" @@ -40,7 +41,7 @@ func prepareBlockMapperTest(plug *csiPlugin, specVolumeName string, t *testing.T spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly) mapper, err := plug.NewBlockVolumeMapper( spec, - &api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns}}, + &api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns, Name: testPod}}, volume.VolumeOptions{}, ) if err != nil { @@ -367,6 +368,52 @@ func TestBlockMapperMapPodDeviceNotSupportAttach(t *testing.T) { } } +func TestBlockMapperMapPodDeviceWithPodInfo(t *testing.T) { + fakeClient := fakeclient.NewSimpleClientset() + attachRequired := false + podInfo := true + fakeDriver := &storagev1.CSIDriver{ + ObjectMeta: meta.ObjectMeta{ + Name: testDriver, + }, + Spec: storagev1.CSIDriverSpec{ + PodInfoOnMount: &podInfo, + AttachRequired: &attachRequired, + }, + } + _, err := fakeClient.StorageV1().CSIDrivers().Create(context.TODO(), fakeDriver, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Failed to create a fakeDriver: %v", err) + } + + // after the driver is created, create the plugin. newTestPlugin waits for the informer to sync, + // such that csiMapper.SetUpDevice below sees the VolumeAttachment object in the lister. + + plug, tmpDir := newTestPlugin(t, fakeClient) + defer os.RemoveAll(tmpDir) + + csiMapper, _, _, err := prepareBlockMapperTest(plug, "test-pv", t) + if err != nil { + t.Fatalf("Failed to make a new Mapper: %v", err) + } + csiMapper.csiClient = setupClient(t, true) + + // Map device to global and pod device map path + _, err = csiMapper.MapPodDevice() + if err != nil { + t.Fatalf("mapper failed to GetGlobalMapPath: %v", err) + } + pvols := csiMapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes() + pvol, ok := pvols[csiMapper.volumeID] + if !ok { + t.Error("csi server may not have received NodePublishVolume call") + } + + if !reflect.DeepEqual(pvol.VolumeContext, map[string]string{"csi.storage.k8s.io/pod.uid": "test-pod", "csi.storage.k8s.io/serviceAccount.name": "", "csi.storage.k8s.io/pod.name": "test-pod", "csi.storage.k8s.io/pod.namespace": "test-ns", "csi.storage.k8s.io/ephemeral": "false"}) { + t.Error("csi mapper check pod info failed") + } +} + func TestBlockMapperTearDownDevice(t *testing.T) { plug, tmpDir := newTestPlugin(t, nil) defer os.RemoveAll(tmpDir)