vsphere_volume_block_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package vsphere_volume
  14. import (
  15. "os"
  16. "path/filepath"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. utiltesting "k8s.io/client-go/util/testing"
  22. "k8s.io/kubernetes/pkg/volume"
  23. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  24. )
  25. var (
  26. testVolumePath = "volPath1"
  27. testGlobalPath = "plugins/kubernetes.io/vsphere-volume/volumeDevices/volPath1"
  28. testPodPath = "pods/poduid/volumeDevices/kubernetes.io~vsphere-volume"
  29. )
  30. func TestGetVolumeSpecFromGlobalMapPath(t *testing.T) {
  31. // make our test path for fake GlobalMapPath
  32. // /tmp symbolized our pluginDir
  33. // /tmp/testGlobalPathXXXXX/plugins/kubernetes.io/vsphere-volume/volumeDevices/
  34. tmpVDir, err := utiltesting.MkTmpdir("vsphereBlockVolume")
  35. if err != nil {
  36. t.Fatalf("cant' make a temp dir: %s", err)
  37. }
  38. // deferred clean up
  39. defer os.RemoveAll(tmpVDir)
  40. expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
  41. // Bad Path
  42. badspec, err := getVolumeSpecFromGlobalMapPath("")
  43. if badspec != nil || err == nil {
  44. t.Errorf("Expected not to get spec from GlobalMapPath but did")
  45. }
  46. // Good Path
  47. spec, err := getVolumeSpecFromGlobalMapPath(expectedGlobalPath)
  48. if spec == nil || err != nil {
  49. t.Fatalf("Failed to get spec from GlobalMapPath: %s", err)
  50. }
  51. if spec.PersistentVolume.Spec.VsphereVolume.VolumePath != testVolumePath {
  52. t.Fatalf("Invalid volumePath from GlobalMapPath spec: %s", spec.PersistentVolume.Spec.VsphereVolume.VolumePath)
  53. }
  54. block := v1.PersistentVolumeBlock
  55. specMode := spec.PersistentVolume.Spec.VolumeMode
  56. if &specMode == nil {
  57. t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", &specMode, block)
  58. }
  59. if *specMode != block {
  60. t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", *specMode, block)
  61. }
  62. }
  63. func TestGetPodAndPluginMapPaths(t *testing.T) {
  64. tmpVDir, err := utiltesting.MkTmpdir("vsphereBlockVolume")
  65. if err != nil {
  66. t.Fatalf("cant' make a temp dir: %s", err)
  67. }
  68. // deferred clean up
  69. defer os.RemoveAll(tmpVDir)
  70. expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
  71. expectedPodPath := filepath.Join(tmpVDir, testPodPath)
  72. spec := getTestVolume(true) // block volume
  73. pluginMgr := volume.VolumePluginMgr{}
  74. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil, volumetest.NewFakeVolumeHost(tmpVDir, nil, nil))
  75. plugin, err := pluginMgr.FindMapperPluginByName(vsphereVolumePluginName)
  76. if err != nil {
  77. os.RemoveAll(tmpVDir)
  78. t.Fatalf("Can't find the plugin by name: %q", vsphereVolumePluginName)
  79. }
  80. if plugin.GetPluginName() != vsphereVolumePluginName {
  81. t.Fatalf("Wrong name: %s", plugin.GetPluginName())
  82. }
  83. pod := &v1.Pod{
  84. ObjectMeta: metav1.ObjectMeta{
  85. UID: types.UID("poduid"),
  86. },
  87. }
  88. mapper, err := plugin.NewBlockVolumeMapper(spec, pod, volume.VolumeOptions{})
  89. if err != nil {
  90. t.Fatalf("Failed to make a new Mounter: %v", err)
  91. }
  92. if mapper == nil {
  93. t.Fatalf("Got a nil Mounter")
  94. }
  95. // GetGlobalMapPath
  96. globalMapPath, err := mapper.GetGlobalMapPath(spec)
  97. if err != nil || len(globalMapPath) == 0 {
  98. t.Fatalf("Invalid GlobalMapPath from spec: %s", spec.PersistentVolume.Spec.VsphereVolume.VolumePath)
  99. }
  100. if globalMapPath != expectedGlobalPath {
  101. t.Errorf("Failed to get GlobalMapPath: %s %s", globalMapPath, expectedGlobalPath)
  102. }
  103. // GetPodDeviceMapPath
  104. devicePath, volumeName := mapper.GetPodDeviceMapPath()
  105. if devicePath != expectedPodPath {
  106. t.Errorf("Got unexpected pod path: %s, expected %s", devicePath, expectedPodPath)
  107. }
  108. if volumeName != testVolumePath {
  109. t.Errorf("Got unexpected volNamne: %s, expected %s", volumeName, testVolumePath)
  110. }
  111. }
  112. func getTestVolume(isBlock bool) *volume.Spec {
  113. pv := &v1.PersistentVolume{
  114. ObjectMeta: metav1.ObjectMeta{
  115. Name: testVolumePath,
  116. },
  117. Spec: v1.PersistentVolumeSpec{
  118. PersistentVolumeSource: v1.PersistentVolumeSource{
  119. VsphereVolume: &v1.VsphereVirtualDiskVolumeSource{
  120. VolumePath: testVolumePath,
  121. },
  122. },
  123. },
  124. }
  125. if isBlock {
  126. blockMode := v1.PersistentVolumeBlock
  127. pv.Spec.VolumeMode = &blockMode
  128. }
  129. return volume.NewSpecFromPersistentVolume(pv, true)
  130. }