aws_ebs_block_test.go 5.1 KB

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