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