persistent_volumes-gce.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 storage
  14. import (
  15. "github.com/onsi/ginkgo"
  16. "github.com/onsi/gomega"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apimachinery/pkg/types"
  21. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  25. "k8s.io/kubernetes/test/e2e/framework/providers/gce"
  26. "k8s.io/kubernetes/test/e2e/storage/utils"
  27. )
  28. // verifyGCEDiskAttached performs a sanity check to verify the PD attached to the node
  29. func verifyGCEDiskAttached(diskName string, nodeName types.NodeName) bool {
  30. gceCloud, err := gce.GetGCECloud()
  31. framework.ExpectNoError(err)
  32. isAttached, err := gceCloud.DiskIsAttached(diskName, nodeName)
  33. framework.ExpectNoError(err)
  34. return isAttached
  35. }
  36. // initializeGCETestSpec creates a PV, PVC, and ClientPod that will run until killed by test or clean up.
  37. func initializeGCETestSpec(c clientset.Interface, ns string, pvConfig framework.PersistentVolumeConfig, pvcConfig framework.PersistentVolumeClaimConfig, isPrebound bool) (*v1.Pod, *v1.PersistentVolume, *v1.PersistentVolumeClaim) {
  38. ginkgo.By("Creating the PV and PVC")
  39. pv, pvc, err := framework.CreatePVPVC(c, pvConfig, pvcConfig, ns, isPrebound)
  40. framework.ExpectNoError(err)
  41. framework.ExpectNoError(framework.WaitOnPVandPVC(c, ns, pv, pvc))
  42. ginkgo.By("Creating the Client Pod")
  43. clientPod, err := framework.CreateClientPod(c, ns, pvc)
  44. framework.ExpectNoError(err)
  45. return clientPod, pv, pvc
  46. }
  47. // Testing configurations of single a PV/PVC pair attached to a GCE PD
  48. var _ = utils.SIGDescribe("PersistentVolumes GCEPD", func() {
  49. var (
  50. c clientset.Interface
  51. diskName string
  52. ns string
  53. err error
  54. pv *v1.PersistentVolume
  55. pvc *v1.PersistentVolumeClaim
  56. clientPod *v1.Pod
  57. pvConfig framework.PersistentVolumeConfig
  58. pvcConfig framework.PersistentVolumeClaimConfig
  59. volLabel labels.Set
  60. selector *metav1.LabelSelector
  61. node types.NodeName
  62. )
  63. f := framework.NewDefaultFramework("pv")
  64. ginkgo.BeforeEach(func() {
  65. c = f.ClientSet
  66. ns = f.Namespace.Name
  67. // Enforce binding only within test space via selector labels
  68. volLabel = labels.Set{framework.VolumeSelectorKey: ns}
  69. selector = metav1.SetAsLabelSelector(volLabel)
  70. framework.SkipUnlessProviderIs("gce", "gke")
  71. ginkgo.By("Initializing Test Spec")
  72. diskName, err = framework.CreatePDWithRetry()
  73. framework.ExpectNoError(err)
  74. pvConfig = framework.PersistentVolumeConfig{
  75. NamePrefix: "gce-",
  76. Labels: volLabel,
  77. PVSource: v1.PersistentVolumeSource{
  78. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  79. PDName: diskName,
  80. FSType: "ext3",
  81. ReadOnly: false,
  82. },
  83. },
  84. Prebind: nil,
  85. }
  86. emptyStorageClass := ""
  87. pvcConfig = framework.PersistentVolumeClaimConfig{
  88. Selector: selector,
  89. StorageClassName: &emptyStorageClass,
  90. }
  91. clientPod, pv, pvc = initializeGCETestSpec(c, ns, pvConfig, pvcConfig, false)
  92. node = types.NodeName(clientPod.Spec.NodeName)
  93. })
  94. ginkgo.AfterEach(func() {
  95. e2elog.Logf("AfterEach: Cleaning up test resources")
  96. if c != nil {
  97. framework.ExpectNoError(framework.DeletePodWithWait(f, c, clientPod))
  98. if errs := framework.PVPVCCleanup(c, ns, pv, pvc); len(errs) > 0 {
  99. framework.Failf("AfterEach: Failed to delete PVC and/or PV. Errors: %v", utilerrors.NewAggregate(errs))
  100. }
  101. clientPod, pv, pvc, node = nil, nil, nil, ""
  102. if diskName != "" {
  103. framework.ExpectNoError(framework.DeletePDWithRetry(diskName))
  104. }
  105. }
  106. })
  107. // Attach a persistent disk to a pod using a PVC.
  108. // Delete the PVC and then the pod. Expect the pod to succeed in unmounting and detaching PD on delete.
  109. ginkgo.It("should test that deleting a PVC before the pod does not cause pod deletion to fail on PD detach", func() {
  110. ginkgo.By("Deleting the Claim")
  111. framework.ExpectNoError(framework.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Unable to delete PVC ", pvc.Name)
  112. gomega.Expect(verifyGCEDiskAttached(diskName, node)).To(gomega.BeTrue())
  113. ginkgo.By("Deleting the Pod")
  114. framework.ExpectNoError(framework.DeletePodWithWait(f, c, clientPod), "Failed to delete pod ", clientPod.Name)
  115. ginkgo.By("Verifying Persistent Disk detach")
  116. framework.ExpectNoError(waitForPDDetach(diskName, node), "PD ", diskName, " did not detach")
  117. })
  118. // Attach a persistent disk to a pod using a PVC.
  119. // Delete the PV and then the pod. Expect the pod to succeed in unmounting and detaching PD on delete.
  120. ginkgo.It("should test that deleting the PV before the pod does not cause pod deletion to fail on PD detach", func() {
  121. ginkgo.By("Deleting the Persistent Volume")
  122. framework.ExpectNoError(framework.DeletePersistentVolume(c, pv.Name), "Failed to delete PV ", pv.Name)
  123. gomega.Expect(verifyGCEDiskAttached(diskName, node)).To(gomega.BeTrue())
  124. ginkgo.By("Deleting the client pod")
  125. framework.ExpectNoError(framework.DeletePodWithWait(f, c, clientPod), "Failed to delete pod ", clientPod.Name)
  126. ginkgo.By("Verifying Persistent Disk detaches")
  127. framework.ExpectNoError(waitForPDDetach(diskName, node), "PD ", diskName, " did not detach")
  128. })
  129. // Test that a Pod and PVC attached to a GCEPD successfully unmounts and detaches when the encompassing Namespace is deleted.
  130. ginkgo.It("should test that deleting the Namespace of a PVC and Pod causes the successful detach of Persistent Disk", func() {
  131. ginkgo.By("Deleting the Namespace")
  132. err := c.CoreV1().Namespaces().Delete(ns, nil)
  133. framework.ExpectNoError(err)
  134. err = framework.WaitForNamespacesDeleted(c, []string{ns}, framework.DefaultNamespaceDeletionTimeout)
  135. framework.ExpectNoError(err)
  136. ginkgo.By("Verifying Persistent Disk detaches")
  137. framework.ExpectNoError(waitForPDDetach(diskName, node), "PD ", diskName, " did not detach")
  138. })
  139. })