test: add csi raw block mode wiht podinfo ut

This commit is contained in:
phantooom 2021-02-21 23:24:07 +08:00
parent 31aa15284f
commit 0017b602be

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"testing" "testing"
"google.golang.org/grpc/codes" "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) spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
mapper, err := plug.NewBlockVolumeMapper( mapper, err := plug.NewBlockVolumeMapper(
spec, spec,
&api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns}}, &api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns, Name: testPod}},
volume.VolumeOptions{}, volume.VolumeOptions{},
) )
if err != nil { 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) { func TestBlockMapperTearDownDevice(t *testing.T) {
plug, tmpDir := newTestPlugin(t, nil) plug, tmpDir := newTestPlugin(t, nil)
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)