vsphere_volume_block_test.go 4.7 KB

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