vsphere_volume_diskformat.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright 2017 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
  14. import (
  15. "context"
  16. "path/filepath"
  17. "github.com/onsi/ginkgo"
  18. "github.com/onsi/gomega"
  19. "github.com/vmware/govmomi/object"
  20. "github.com/vmware/govmomi/vim25/types"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/uuid"
  24. clientset "k8s.io/client-go/kubernetes"
  25. "k8s.io/kubernetes/test/e2e/framework"
  26. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  27. "k8s.io/kubernetes/test/e2e/storage/utils"
  28. )
  29. /*
  30. Test to verify diskformat specified in storage-class is being honored while volume creation.
  31. Valid and supported options are eagerzeroedthick, zeroedthick and thin
  32. Steps
  33. 1. Create StorageClass with diskformat set to valid type
  34. 2. Create PVC which uses the StorageClass created in step 1.
  35. 3. Wait for PV to be provisioned.
  36. 4. Wait for PVC's status to become Bound
  37. 5. Create pod using PVC on specific node.
  38. 6. Wait for Disk to be attached to the node.
  39. 7. Get node VM's devices and find PV's Volume Disk.
  40. 8. Get Backing Info of the Volume Disk and obtain EagerlyScrub and ThinProvisioned
  41. 9. Based on the value of EagerlyScrub and ThinProvisioned, verify diskformat is correct.
  42. 10. Delete pod and Wait for Volume Disk to be detached from the Node.
  43. 11. Delete PVC, PV and Storage Class
  44. */
  45. var _ = utils.SIGDescribe("Volume Disk Format [Feature:vsphere]", func() {
  46. f := framework.NewDefaultFramework("volume-disk-format")
  47. const (
  48. NodeLabelKey = "vsphere_e2e_label_volume_diskformat"
  49. )
  50. var (
  51. client clientset.Interface
  52. namespace string
  53. nodeName string
  54. isNodeLabeled bool
  55. nodeKeyValueLabel map[string]string
  56. nodeLabelValue string
  57. )
  58. ginkgo.BeforeEach(func() {
  59. framework.SkipUnlessProviderIs("vsphere")
  60. Bootstrap(f)
  61. client = f.ClientSet
  62. namespace = f.Namespace.Name
  63. if !isNodeLabeled {
  64. nodeName = GetReadySchedulableRandomNodeInfo().Name
  65. nodeLabelValue = "vsphere_e2e_" + string(uuid.NewUUID())
  66. nodeKeyValueLabel = make(map[string]string)
  67. nodeKeyValueLabel[NodeLabelKey] = nodeLabelValue
  68. framework.AddOrUpdateLabelOnNode(client, nodeName, NodeLabelKey, nodeLabelValue)
  69. isNodeLabeled = true
  70. }
  71. })
  72. framework.AddCleanupAction(func() {
  73. // Cleanup actions will be called even when the tests are skipped and leaves namespace unset.
  74. if len(namespace) > 0 && len(nodeLabelValue) > 0 {
  75. framework.RemoveLabelOffNode(client, nodeName, NodeLabelKey)
  76. }
  77. })
  78. ginkgo.It("verify disk format type - eagerzeroedthick is honored for dynamically provisioned pv using storageclass", func() {
  79. ginkgo.By("Invoking Test for diskformat: eagerzeroedthick")
  80. invokeTest(f, client, namespace, nodeName, nodeKeyValueLabel, "eagerzeroedthick")
  81. })
  82. ginkgo.It("verify disk format type - zeroedthick is honored for dynamically provisioned pv using storageclass", func() {
  83. ginkgo.By("Invoking Test for diskformat: zeroedthick")
  84. invokeTest(f, client, namespace, nodeName, nodeKeyValueLabel, "zeroedthick")
  85. })
  86. ginkgo.It("verify disk format type - thin is honored for dynamically provisioned pv using storageclass", func() {
  87. ginkgo.By("Invoking Test for diskformat: thin")
  88. invokeTest(f, client, namespace, nodeName, nodeKeyValueLabel, "thin")
  89. })
  90. })
  91. func invokeTest(f *framework.Framework, client clientset.Interface, namespace string, nodeName string, nodeKeyValueLabel map[string]string, diskFormat string) {
  92. e2elog.Logf("Invoking Test for DiskFomat: %s", diskFormat)
  93. scParameters := make(map[string]string)
  94. scParameters["diskformat"] = diskFormat
  95. ginkgo.By("Creating Storage Class With DiskFormat")
  96. storageClassSpec := getVSphereStorageClassSpec("thinsc", scParameters, nil)
  97. storageclass, err := client.StorageV1().StorageClasses().Create(storageClassSpec)
  98. framework.ExpectNoError(err)
  99. defer client.StorageV1().StorageClasses().Delete(storageclass.Name, nil)
  100. ginkgo.By("Creating PVC using the Storage Class")
  101. pvclaimSpec := getVSphereClaimSpecWithStorageClass(namespace, "2Gi", storageclass)
  102. pvclaim, err := client.CoreV1().PersistentVolumeClaims(namespace).Create(pvclaimSpec)
  103. framework.ExpectNoError(err)
  104. defer func() {
  105. client.CoreV1().PersistentVolumeClaims(namespace).Delete(pvclaimSpec.Name, nil)
  106. }()
  107. ginkgo.By("Waiting for claim to be in bound phase")
  108. err = framework.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, pvclaim.Namespace, pvclaim.Name, framework.Poll, framework.ClaimProvisionTimeout)
  109. framework.ExpectNoError(err)
  110. // Get new copy of the claim
  111. pvclaim, err = client.CoreV1().PersistentVolumeClaims(pvclaim.Namespace).Get(pvclaim.Name, metav1.GetOptions{})
  112. framework.ExpectNoError(err)
  113. // Get the bound PV
  114. pv, err := client.CoreV1().PersistentVolumes().Get(pvclaim.Spec.VolumeName, metav1.GetOptions{})
  115. framework.ExpectNoError(err)
  116. /*
  117. PV is required to be attached to the Node. so that using govmomi API we can grab Disk's Backing Info
  118. to check EagerlyScrub and ThinProvisioned property
  119. */
  120. ginkgo.By("Creating pod to attach PV to the node")
  121. // Create pod to attach Volume to Node
  122. podSpec := getVSpherePodSpecWithClaim(pvclaim.Name, nodeKeyValueLabel, "while true ; do sleep 2 ; done")
  123. pod, err := client.CoreV1().Pods(namespace).Create(podSpec)
  124. framework.ExpectNoError(err)
  125. ginkgo.By("Waiting for pod to be running")
  126. gomega.Expect(framework.WaitForPodNameRunningInNamespace(client, pod.Name, namespace)).To(gomega.Succeed())
  127. isAttached, err := diskIsAttached(pv.Spec.VsphereVolume.VolumePath, nodeName)
  128. gomega.Expect(isAttached).To(gomega.BeTrue())
  129. framework.ExpectNoError(err)
  130. ginkgo.By("Verify Disk Format")
  131. gomega.Expect(verifyDiskFormat(client, nodeName, pv.Spec.VsphereVolume.VolumePath, diskFormat)).To(gomega.BeTrue(), "DiskFormat Verification Failed")
  132. var volumePaths []string
  133. volumePaths = append(volumePaths, pv.Spec.VsphereVolume.VolumePath)
  134. ginkgo.By("Delete pod and wait for volume to be detached from node")
  135. deletePodAndWaitForVolumeToDetach(f, client, pod, nodeName, volumePaths)
  136. }
  137. func verifyDiskFormat(client clientset.Interface, nodeName string, pvVolumePath string, diskFormat string) bool {
  138. ginkgo.By("Verifing disk format")
  139. eagerlyScrub := false
  140. thinProvisioned := false
  141. diskFound := false
  142. pvvmdkfileName := filepath.Base(pvVolumePath) + filepath.Ext(pvVolumePath)
  143. ctx, cancel := context.WithCancel(context.Background())
  144. defer cancel()
  145. nodeInfo := TestContext.NodeMapper.GetNodeInfo(nodeName)
  146. vm := object.NewVirtualMachine(nodeInfo.VSphere.Client.Client, nodeInfo.VirtualMachineRef)
  147. vmDevices, err := vm.Device(ctx)
  148. framework.ExpectNoError(err)
  149. disks := vmDevices.SelectByType((*types.VirtualDisk)(nil))
  150. for _, disk := range disks {
  151. backing := disk.GetVirtualDevice().Backing.(*types.VirtualDiskFlatVer2BackingInfo)
  152. backingFileName := filepath.Base(backing.FileName) + filepath.Ext(backing.FileName)
  153. if backingFileName == pvvmdkfileName {
  154. diskFound = true
  155. if backing.EagerlyScrub != nil {
  156. eagerlyScrub = *backing.EagerlyScrub
  157. }
  158. if backing.ThinProvisioned != nil {
  159. thinProvisioned = *backing.ThinProvisioned
  160. }
  161. break
  162. }
  163. }
  164. gomega.Expect(diskFound).To(gomega.BeTrue(), "Failed to find disk")
  165. isDiskFormatCorrect := false
  166. if diskFormat == "eagerzeroedthick" {
  167. if eagerlyScrub == true && thinProvisioned == false {
  168. isDiskFormatCorrect = true
  169. }
  170. } else if diskFormat == "zeroedthick" {
  171. if eagerlyScrub == false && thinProvisioned == false {
  172. isDiskFormatCorrect = true
  173. }
  174. } else if diskFormat == "thin" {
  175. if eagerlyScrub == false && thinProvisioned == true {
  176. isDiskFormatCorrect = true
  177. }
  178. }
  179. return isDiskFormatCorrect
  180. }