persistent_volumes-vsphere.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "time"
  17. "github.com/onsi/ginkgo"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/labels"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  24. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  25. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  26. "k8s.io/kubernetes/test/e2e/storage/utils"
  27. )
  28. // Testing configurations of single a PV/PVC pair attached to a vSphere Disk
  29. var _ = utils.SIGDescribe("PersistentVolumes:vsphere [Feature:vsphere]", func() {
  30. var (
  31. c clientset.Interface
  32. ns string
  33. volumePath string
  34. pv *v1.PersistentVolume
  35. pvc *v1.PersistentVolumeClaim
  36. clientPod *v1.Pod
  37. pvConfig e2epv.PersistentVolumeConfig
  38. pvcConfig e2epv.PersistentVolumeClaimConfig
  39. err error
  40. node string
  41. volLabel labels.Set
  42. selector *metav1.LabelSelector
  43. nodeInfo *NodeInfo
  44. )
  45. f := framework.NewDefaultFramework("pv")
  46. /*
  47. Test Setup
  48. 1. Create volume (vmdk)
  49. 2. Create PV with volume path for the vmdk.
  50. 3. Create PVC to bind with PV.
  51. 4. Create a POD using the PVC.
  52. 5. Verify Disk and Attached to the node.
  53. */
  54. ginkgo.BeforeEach(func() {
  55. e2eskipper.SkipUnlessProviderIs("vsphere")
  56. Bootstrap(f)
  57. c = f.ClientSet
  58. ns = f.Namespace.Name
  59. clientPod = nil
  60. pvc = nil
  61. pv = nil
  62. nodeInfo = GetReadySchedulableRandomNodeInfo()
  63. volLabel = labels.Set{e2epv.VolumeSelectorKey: ns}
  64. selector = metav1.SetAsLabelSelector(volLabel)
  65. if volumePath == "" {
  66. volumePath, err = nodeInfo.VSphere.CreateVolume(&VolumeOptions{}, nodeInfo.DataCenterRef)
  67. framework.ExpectNoError(err)
  68. pvConfig = e2epv.PersistentVolumeConfig{
  69. NamePrefix: "vspherepv-",
  70. Labels: volLabel,
  71. PVSource: v1.PersistentVolumeSource{
  72. VsphereVolume: &v1.VsphereVirtualDiskVolumeSource{
  73. VolumePath: volumePath,
  74. FSType: "ext4",
  75. },
  76. },
  77. Prebind: nil,
  78. }
  79. emptyStorageClass := ""
  80. pvcConfig = e2epv.PersistentVolumeClaimConfig{
  81. Selector: selector,
  82. StorageClassName: &emptyStorageClass,
  83. }
  84. }
  85. ginkgo.By("Creating the PV and PVC")
  86. pv, pvc, err = e2epv.CreatePVPVC(c, pvConfig, pvcConfig, ns, false)
  87. framework.ExpectNoError(err)
  88. framework.ExpectNoError(e2epv.WaitOnPVandPVC(c, ns, pv, pvc))
  89. ginkgo.By("Creating the Client Pod")
  90. clientPod, err = e2epod.CreateClientPod(c, ns, pvc)
  91. framework.ExpectNoError(err)
  92. node = clientPod.Spec.NodeName
  93. ginkgo.By("Verify disk should be attached to the node")
  94. isAttached, err := diskIsAttached(volumePath, node)
  95. framework.ExpectNoError(err)
  96. framework.ExpectEqual(isAttached, true, "disk is not attached with the node")
  97. })
  98. ginkgo.AfterEach(func() {
  99. framework.Logf("AfterEach: Cleaning up test resources")
  100. if c != nil {
  101. framework.ExpectNoError(e2epod.DeletePodWithWait(c, clientPod), "AfterEach: failed to delete pod ", clientPod.Name)
  102. if pv != nil {
  103. framework.ExpectNoError(e2epv.DeletePersistentVolume(c, pv.Name), "AfterEach: failed to delete PV ", pv.Name)
  104. }
  105. if pvc != nil {
  106. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvc.Name, ns), "AfterEach: failed to delete PVC ", pvc.Name)
  107. }
  108. }
  109. })
  110. /*
  111. Clean up
  112. 1. Wait and verify volume is detached from the node
  113. 2. Delete PV
  114. 3. Delete Volume (vmdk)
  115. */
  116. framework.AddCleanupAction(func() {
  117. // Cleanup actions will be called even when the tests are skipped and leaves namespace unset.
  118. if len(ns) > 0 && len(volumePath) > 0 {
  119. framework.ExpectNoError(waitForVSphereDiskToDetach(volumePath, node))
  120. nodeInfo.VSphere.DeleteVolume(volumePath, nodeInfo.DataCenterRef)
  121. }
  122. })
  123. /*
  124. Delete the PVC and then the pod. Expect the pod to succeed in unmounting and detaching PD on delete.
  125. Test Steps:
  126. 1. Delete PVC.
  127. 2. Delete POD, POD deletion should succeed.
  128. */
  129. ginkgo.It("should test that deleting a PVC before the pod does not cause pod deletion to fail on vsphere volume detach", func() {
  130. ginkgo.By("Deleting the Claim")
  131. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Failed to delete PVC ", pvc.Name)
  132. pvc = nil
  133. ginkgo.By("Deleting the Pod")
  134. framework.ExpectNoError(e2epod.DeletePodWithWait(c, clientPod), "Failed to delete pod ", clientPod.Name)
  135. })
  136. /*
  137. Delete the PV and then the pod. Expect the pod to succeed in unmounting and detaching PD on delete.
  138. Test Steps:
  139. 1. Delete PV.
  140. 2. Delete POD, POD deletion should succeed.
  141. */
  142. ginkgo.It("should test that deleting the PV before the pod does not cause pod deletion to fail on vspehre volume detach", func() {
  143. ginkgo.By("Deleting the Persistent Volume")
  144. framework.ExpectNoError(e2epv.DeletePersistentVolume(c, pv.Name), "Failed to delete PV ", pv.Name)
  145. pv = nil
  146. ginkgo.By("Deleting the pod")
  147. framework.ExpectNoError(e2epod.DeletePodWithWait(c, clientPod), "Failed to delete pod ", clientPod.Name)
  148. })
  149. /*
  150. This test verifies that a volume mounted to a pod remains mounted after a kubelet restarts.
  151. Steps:
  152. 1. Write to the volume
  153. 2. Restart kubelet
  154. 3. Verify that written file is accessible after kubelet restart
  155. */
  156. ginkgo.It("should test that a file written to the vspehre volume mount before kubelet restart can be read after restart [Disruptive]", func() {
  157. e2eskipper.SkipUnlessSSHKeyPresent()
  158. utils.TestKubeletRestartsAndRestoresMount(c, f, clientPod)
  159. })
  160. /*
  161. This test verifies that a volume mounted to a pod that is deleted while the kubelet is down
  162. unmounts volume when the kubelet returns.
  163. Steps:
  164. 1. Verify volume is mounted on the node.
  165. 2. Stop kubelet.
  166. 3. Delete pod.
  167. 4. Start kubelet.
  168. 5. Verify that volume mount not to be found.
  169. */
  170. ginkgo.It("should test that a vspehre volume mounted to a pod that is deleted while the kubelet is down unmounts when the kubelet returns [Disruptive]", func() {
  171. e2eskipper.SkipUnlessSSHKeyPresent()
  172. utils.TestVolumeUnmountsFromDeletedPod(c, f, clientPod)
  173. })
  174. /*
  175. This test verifies that deleting the Namespace of a PVC and Pod causes the successful detach of Persistent Disk
  176. Steps:
  177. 1. Delete Namespace.
  178. 2. Wait for namespace to get deleted. (Namespace deletion should trigger deletion of belonging pods)
  179. 3. Verify volume should be detached from the node.
  180. */
  181. ginkgo.It("should test that deleting the Namespace of a PVC and Pod causes the successful detach of vsphere volume", func() {
  182. ginkgo.By("Deleting the Namespace")
  183. err := c.CoreV1().Namespaces().Delete(context.TODO(), ns, nil)
  184. framework.ExpectNoError(err)
  185. err = framework.WaitForNamespacesDeleted(c, []string{ns}, 3*time.Minute)
  186. framework.ExpectNoError(err)
  187. ginkgo.By("Verifying Persistent Disk detaches")
  188. waitForVSphereDiskToDetach(volumePath, node)
  189. })
  190. })