gce_pd_block_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 gcepd
  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. const (
  26. testPdName = "pdVol1"
  27. testPVName = "pv1"
  28. testGlobalPath = "plugins/kubernetes.io/gce-pd/volumeDevices/pdVol1"
  29. testPodPath = "pods/poduid/volumeDevices/kubernetes.io~gce-pd"
  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/gce-pd/volumeDevices/pdVol1
  35. tmpVDir, err := utiltesting.MkTmpdir("gceBlockTest")
  36. if err != nil {
  37. t.Fatalf("can't make a temp dir: %v", 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(expectedGlobalPath)
  49. if spec == nil || err != nil {
  50. t.Fatalf("Failed to get spec from GlobalMapPath: %v", err)
  51. }
  52. if spec.PersistentVolume.Spec.GCEPersistentDisk.PDName != testPdName {
  53. t.Errorf("Invalid pdName from GlobalMapPath spec: %s", spec.PersistentVolume.Spec.GCEPersistentDisk.PDName)
  54. }
  55. block := v1.PersistentVolumeBlock
  56. specMode := spec.PersistentVolume.Spec.VolumeMode
  57. if &specMode == nil {
  58. t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", &specMode, block)
  59. }
  60. if *specMode != block {
  61. t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", *specMode, block)
  62. }
  63. }
  64. func getTestVolume(readOnly bool, path string, isBlock bool) *volume.Spec {
  65. pv := &v1.PersistentVolume{
  66. ObjectMeta: metav1.ObjectMeta{
  67. Name: testPVName,
  68. },
  69. Spec: v1.PersistentVolumeSpec{
  70. PersistentVolumeSource: v1.PersistentVolumeSource{
  71. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  72. PDName: testPdName,
  73. },
  74. },
  75. },
  76. }
  77. if isBlock {
  78. blockMode := v1.PersistentVolumeBlock
  79. pv.Spec.VolumeMode = &blockMode
  80. }
  81. return volume.NewSpecFromPersistentVolume(pv, readOnly)
  82. }
  83. func TestGetPodAndPluginMapPaths(t *testing.T) {
  84. tmpVDir, err := utiltesting.MkTmpdir("gceBlockTest")
  85. if err != nil {
  86. t.Fatalf("can't make a temp dir: %v", err)
  87. }
  88. //deferred clean up
  89. defer os.RemoveAll(tmpVDir)
  90. expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
  91. expectedPodPath := filepath.Join(tmpVDir, testPodPath)
  92. spec := getTestVolume(false, tmpVDir, true /*isBlock*/)
  93. plugMgr := volume.VolumePluginMgr{}
  94. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpVDir, nil, nil))
  95. plug, err := plugMgr.FindMapperPluginByName(gcePersistentDiskPluginName)
  96. if err != nil {
  97. os.RemoveAll(tmpVDir)
  98. t.Fatalf("Can't find the plugin by name: %q", gcePersistentDiskPluginName)
  99. }
  100. if plug.GetPluginName() != gcePersistentDiskPluginName {
  101. t.Fatalf("Wrong name: %s", plug.GetPluginName())
  102. }
  103. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  104. mapper, err := plug.NewBlockVolumeMapper(spec, pod, volume.VolumeOptions{})
  105. if err != nil {
  106. t.Fatalf("Failed to make a new Mounter: %v", err)
  107. }
  108. if mapper == nil {
  109. t.Fatalf("Got a nil Mounter")
  110. }
  111. //GetGlobalMapPath
  112. gMapPath, err := mapper.GetGlobalMapPath(spec)
  113. if err != nil || len(gMapPath) == 0 {
  114. t.Fatalf("Invalid GlobalMapPath from spec: %s", spec.PersistentVolume.Spec.GCEPersistentDisk.PDName)
  115. }
  116. if gMapPath != expectedGlobalPath {
  117. t.Errorf("Failed to get GlobalMapPath: %s %s", gMapPath, expectedGlobalPath)
  118. }
  119. //GetPodDeviceMapPath
  120. gDevicePath, gVolName := mapper.GetPodDeviceMapPath()
  121. if gDevicePath != expectedPodPath {
  122. t.Errorf("Got unexpected pod path: %s, expected %s", gDevicePath, expectedPodPath)
  123. }
  124. if gVolName != testPVName {
  125. t.Errorf("Got unexpected volNamne: %s, expected %s", gVolName, testPVName)
  126. }
  127. }