cinder_block_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 cinder
  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. const (
  27. testVolName = "vol-1234"
  28. testPVName = "pv1"
  29. testGlobalPath = "plugins/kubernetes.io/cinder/volumeDevices/vol-1234"
  30. testPodPath = "pods/poduid/volumeDevices/kubernetes.io~cinder"
  31. )
  32. func TestGetVolumeSpecFromGlobalMapPath(t *testing.T) {
  33. // make our test path for fake GlobalMapPath
  34. // /tmp symbolized our pluginDir
  35. // /tmp/testGlobalPathXXXXX/plugins/kubernetes.io/cinder/volumeDevices/pdVol1
  36. tmpVDir, err := utiltesting.MkTmpdir("cinderBlockTest")
  37. if err != nil {
  38. t.Fatalf("can't make a temp dir: %v", err)
  39. }
  40. //deferred clean up
  41. defer os.RemoveAll(tmpVDir)
  42. expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
  43. //Bad Path
  44. badspec, err := getVolumeSpecFromGlobalMapPath("", "")
  45. if badspec != nil || err == nil {
  46. t.Errorf("Expected not to get spec from GlobalMapPath but did")
  47. }
  48. // Good Path
  49. spec, err := getVolumeSpecFromGlobalMapPath("myVolume", expectedGlobalPath)
  50. if spec == nil || err != nil {
  51. t.Fatalf("Failed to get spec from GlobalMapPath: %v", err)
  52. }
  53. if spec.PersistentVolume.Name != "myVolume" {
  54. t.Errorf("Invalid PV name from GlobalMapPath spec: %s", spec.PersistentVolume.Name)
  55. }
  56. if spec.PersistentVolume.Spec.Cinder.VolumeID != testVolName {
  57. t.Errorf("Invalid volumeID from GlobalMapPath spec: %s", spec.PersistentVolume.Spec.Cinder.VolumeID)
  58. }
  59. block := v1.PersistentVolumeBlock
  60. specMode := spec.PersistentVolume.Spec.VolumeMode
  61. if &specMode == nil {
  62. t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", &specMode, block)
  63. }
  64. if *specMode != block {
  65. t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", *specMode, block)
  66. }
  67. }
  68. func getTestVolume(readOnly bool, isBlock bool) *volume.Spec {
  69. pv := &v1.PersistentVolume{
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: testPVName,
  72. },
  73. Spec: v1.PersistentVolumeSpec{
  74. PersistentVolumeSource: v1.PersistentVolumeSource{
  75. Cinder: &v1.CinderPersistentVolumeSource{
  76. VolumeID: testVolName,
  77. },
  78. },
  79. },
  80. }
  81. if isBlock {
  82. blockMode := v1.PersistentVolumeBlock
  83. pv.Spec.VolumeMode = &blockMode
  84. }
  85. return volume.NewSpecFromPersistentVolume(pv, readOnly)
  86. }
  87. func TestGetPodAndPluginMapPaths(t *testing.T) {
  88. tmpVDir, err := utiltesting.MkTmpdir("cinderBlockTest")
  89. if err != nil {
  90. t.Fatalf("can't make a temp dir: %v", err)
  91. }
  92. //deferred clean up
  93. defer os.RemoveAll(tmpVDir)
  94. expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
  95. expectedPodPath := filepath.Join(tmpVDir, testPodPath)
  96. spec := getTestVolume(false, true /*isBlock*/)
  97. plugMgr := volume.VolumePluginMgr{}
  98. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpVDir, nil, nil))
  99. plug, err := plugMgr.FindMapperPluginByName(cinderVolumePluginName)
  100. if err != nil {
  101. os.RemoveAll(tmpVDir)
  102. t.Fatalf("Can't find the plugin by name: %q", cinderVolumePluginName)
  103. }
  104. if plug.GetPluginName() != cinderVolumePluginName {
  105. t.Fatalf("Wrong name: %s", plug.GetPluginName())
  106. }
  107. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  108. mapper, err := plug.NewBlockVolumeMapper(spec, pod, volume.VolumeOptions{})
  109. if err != nil {
  110. t.Fatalf("Failed to make a new Mounter: %v", err)
  111. }
  112. if mapper == nil {
  113. t.Fatalf("Got a nil Mounter")
  114. }
  115. //GetGlobalMapPath
  116. gMapPath, err := mapper.GetGlobalMapPath(spec)
  117. if err != nil || len(gMapPath) == 0 {
  118. t.Fatalf("Invalid GlobalMapPath from spec: %s", spec.PersistentVolume.Spec.Cinder.VolumeID)
  119. }
  120. if gMapPath != expectedGlobalPath {
  121. t.Errorf("Failed to get GlobalMapPath: %s %s", gMapPath, expectedGlobalPath)
  122. }
  123. //GetPodDeviceMapPath
  124. gDevicePath, gVolName := mapper.GetPodDeviceMapPath()
  125. if gDevicePath != expectedPodPath {
  126. t.Errorf("Got unexpected pod path: %s, expected %s", gDevicePath, expectedPodPath)
  127. }
  128. if gVolName != testPVName {
  129. t.Errorf("Got unexpected volNamne: %s, expected %s", gVolName, testPVName)
  130. }
  131. }