support local volume with block source reconstruction

This commit is contained in:
Yecheng Fu
2019-10-22 20:22:49 +08:00
parent 9832418870
commit 36a54399a6
5 changed files with 140 additions and 49 deletions

View File

@@ -422,44 +422,97 @@ func testFSGroupMount(plug volume.VolumePlugin, pod *v1.Pod, tmpDir string, fsGr
}
func TestConstructVolumeSpec(t *testing.T) {
tmpDir, plug := getPlugin(t)
defer os.RemoveAll(tmpDir)
volPath := filepath.Join(tmpDir, testMountPath)
spec, err := plug.ConstructVolumeSpec(testPVName, volPath)
if err != nil {
t.Errorf("ConstructVolumeSpec() failed: %v", err)
}
if spec == nil {
t.Fatalf("ConstructVolumeSpec() returned nil")
tests := []struct {
name string
mountPoints []mount.MountPoint
expectedPath string
}{
{
name: "filesystem volume with directory source",
mountPoints: []mount.MountPoint{
{
Device: "/mnt/disk/ssd0",
Path: "pods/poduid/volumes/kubernetes.io~local-volume/pvA",
},
},
expectedPath: "",
},
{
name: "filesystem volume with block source",
mountPoints: []mount.MountPoint{
{
Device: "/dev/loop0",
Path: testMountPath,
},
{
Device: "/dev/loop0",
Path: testBlockFormattingToFSGlobalPath,
},
},
expectedPath: "/dev/loop0",
},
}
volName := spec.Name()
if volName != testPVName {
t.Errorf("Expected volume name %q, got %q", testPVName, volName)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
if err != nil {
t.Fatalf("can't make a temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
plug := &localVolumePlugin{
host: volumetest.NewFakeVolumeHost(tmpDir, nil, nil),
}
mounter := plug.host.GetMounter(plug.GetPluginName())
fakeMountPoints := []mount.MountPoint{}
for _, mp := range tt.mountPoints {
fakeMountPoint := mp
fakeMountPoint.Path = filepath.Join(tmpDir, mp.Path)
fakeMountPoints = append(fakeMountPoints, fakeMountPoint)
}
mounter.(*mount.FakeMounter).MountPoints = fakeMountPoints
volPath := filepath.Join(tmpDir, testMountPath)
spec, err := plug.ConstructVolumeSpec(testPVName, volPath)
if err != nil {
t.Errorf("ConstructVolumeSpec() failed: %v", err)
}
if spec == nil {
t.Fatalf("ConstructVolumeSpec() returned nil")
}
volName := spec.Name()
if volName != testPVName {
t.Errorf("Expected volume name %q, got %q", testPVName, volName)
}
if spec.Volume != nil {
t.Errorf("Volume object returned, expected nil")
}
pv := spec.PersistentVolume
if pv == nil {
t.Fatalf("PersistentVolume object nil")
}
if spec.PersistentVolume.Spec.VolumeMode == nil {
t.Fatalf("Volume mode has not been set.")
}
if *spec.PersistentVolume.Spec.VolumeMode != v1.PersistentVolumeFilesystem {
t.Errorf("Unexpected volume mode %q", *spec.PersistentVolume.Spec.VolumeMode)
}
ls := pv.Spec.PersistentVolumeSource.Local
if ls == nil {
t.Fatalf("LocalVolumeSource object nil")
}
if pv.Spec.PersistentVolumeSource.Local.Path != tt.expectedPath {
t.Fatalf("Unexpected path got %q, expected %q", pv.Spec.PersistentVolumeSource.Local.Path, tt.expectedPath)
}
})
}
if spec.Volume != nil {
t.Errorf("Volume object returned, expected nil")
}
pv := spec.PersistentVolume
if pv == nil {
t.Fatalf("PersistentVolume object nil")
}
if spec.PersistentVolume.Spec.VolumeMode == nil {
t.Fatalf("Volume mode has not been set.")
}
if *spec.PersistentVolume.Spec.VolumeMode != v1.PersistentVolumeFilesystem {
t.Errorf("Unexpected volume mode %q", *spec.PersistentVolume.Spec.VolumeMode)
}
ls := pv.Spec.PersistentVolumeSource.Local
if ls == nil {
t.Fatalf("LocalVolumeSource object nil")
}
}
func TestConstructBlockVolumeSpec(t *testing.T) {