snapshottable.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright 2018 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 testsuites
  14. import (
  15. "fmt"
  16. "time"
  17. "github.com/onsi/ginkgo"
  18. "github.com/onsi/gomega"
  19. v1 "k8s.io/api/core/v1"
  20. apierrs "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. "k8s.io/client-go/dynamic"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  26. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  27. )
  28. // snapshot CRD api group
  29. const snapshotGroup = "snapshot.storage.k8s.io"
  30. // snapshot CRD api version
  31. const snapshotAPIVersion = "snapshot.storage.k8s.io/v1alpha1"
  32. var (
  33. snapshotGVR = schema.GroupVersionResource{Group: snapshotGroup, Version: "v1alpha1", Resource: "volumesnapshots"}
  34. snapshotClassGVR = schema.GroupVersionResource{Group: snapshotGroup, Version: "v1alpha1", Resource: "volumesnapshotclasses"}
  35. snapshotContentGVR = schema.GroupVersionResource{Group: snapshotGroup, Version: "v1alpha1", Resource: "volumesnapshotcontents"}
  36. )
  37. type SnapshotClassTest struct {
  38. Name string
  39. CloudProviders []string
  40. Snapshotter string
  41. Parameters map[string]string
  42. NodeName string
  43. NodeSelector map[string]string // NodeSelector for the pod
  44. }
  45. type snapshottableTestSuite struct {
  46. tsInfo TestSuiteInfo
  47. }
  48. var _ TestSuite = &snapshottableTestSuite{}
  49. // InitSnapshottableTestSuite returns snapshottableTestSuite that implements TestSuite interface
  50. func InitSnapshottableTestSuite() TestSuite {
  51. return &snapshottableTestSuite{
  52. tsInfo: TestSuiteInfo{
  53. name: "snapshottable",
  54. testPatterns: []testpatterns.TestPattern{
  55. testpatterns.DynamicSnapshot,
  56. },
  57. },
  58. }
  59. }
  60. func (s *snapshottableTestSuite) getTestSuiteInfo() TestSuiteInfo {
  61. return s.tsInfo
  62. }
  63. func (s *snapshottableTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) {
  64. var (
  65. sDriver SnapshottableTestDriver
  66. dDriver DynamicPVTestDriver
  67. )
  68. ginkgo.BeforeEach(func() {
  69. // Check preconditions.
  70. gomega.Expect(pattern.SnapshotType).To(gomega.Equal(testpatterns.DynamicCreatedSnapshot))
  71. dInfo := driver.GetDriverInfo()
  72. ok := false
  73. sDriver, ok = driver.(SnapshottableTestDriver)
  74. if !dInfo.Capabilities[CapDataSource] || !ok {
  75. framework.Skipf("Driver %q does not support snapshots - skipping", dInfo.Name)
  76. }
  77. dDriver, ok = driver.(DynamicPVTestDriver)
  78. if !ok {
  79. framework.Skipf("Driver %q does not support dynamic provisioning - skipping", driver.GetDriverInfo().Name)
  80. }
  81. })
  82. // This intentionally comes after checking the preconditions because it
  83. // registers its own BeforeEach which creates the namespace. Beware that it
  84. // also registers an AfterEach which renders f unusable. Any code using
  85. // f must run inside an It or Context callback.
  86. f := framework.NewDefaultFramework("snapshotting")
  87. ginkgo.It("should create snapshot with defaults [Feature:VolumeSnapshotDataSource]", func() {
  88. cs := f.ClientSet
  89. dc := f.DynamicClient
  90. // Now do the more expensive test initialization.
  91. config, testCleanup := driver.PrepareTest(f)
  92. defer testCleanup()
  93. vsc := sDriver.GetSnapshotClass(config)
  94. class := dDriver.GetDynamicProvisionStorageClass(config, "")
  95. if class == nil {
  96. framework.Skipf("Driver %q does not define Dynamic Provision StorageClass - skipping", driver.GetDriverInfo().Name)
  97. }
  98. claimSize := dDriver.GetClaimSize()
  99. pvc := getClaim(claimSize, config.Framework.Namespace.Name)
  100. pvc.Spec.StorageClassName = &class.Name
  101. e2elog.Logf("In creating storage class object and pvc object for driver - sc: %v, pvc: %v", class, pvc)
  102. ginkgo.By("creating a StorageClass " + class.Name)
  103. class, err := cs.StorageV1().StorageClasses().Create(class)
  104. framework.ExpectNoError(err)
  105. defer func() {
  106. e2elog.Logf("deleting storage class %s", class.Name)
  107. framework.ExpectNoError(cs.StorageV1().StorageClasses().Delete(class.Name, nil))
  108. }()
  109. ginkgo.By("creating a claim")
  110. pvc, err = cs.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(pvc)
  111. framework.ExpectNoError(err)
  112. defer func() {
  113. e2elog.Logf("deleting claim %q/%q", pvc.Namespace, pvc.Name)
  114. // typically this claim has already been deleted
  115. err = cs.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(pvc.Name, nil)
  116. if err != nil && !apierrs.IsNotFound(err) {
  117. framework.Failf("Error deleting claim %q. Error: %v", pvc.Name, err)
  118. }
  119. }()
  120. err = framework.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, cs, pvc.Namespace, pvc.Name, framework.Poll, framework.ClaimProvisionTimeout)
  121. framework.ExpectNoError(err)
  122. ginkgo.By("checking the claim")
  123. // Get new copy of the claim
  124. pvc, err = cs.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})
  125. framework.ExpectNoError(err)
  126. // Get the bound PV
  127. pv, err := cs.CoreV1().PersistentVolumes().Get(pvc.Spec.VolumeName, metav1.GetOptions{})
  128. framework.ExpectNoError(err)
  129. ginkgo.By("creating a SnapshotClass")
  130. vsc, err = dc.Resource(snapshotClassGVR).Create(vsc, metav1.CreateOptions{})
  131. framework.ExpectNoError(err)
  132. defer func() {
  133. e2elog.Logf("deleting SnapshotClass %s", vsc.GetName())
  134. framework.ExpectNoError(dc.Resource(snapshotClassGVR).Delete(vsc.GetName(), nil))
  135. }()
  136. ginkgo.By("creating a snapshot")
  137. snapshot := getSnapshot(pvc.Name, pvc.Namespace, vsc.GetName())
  138. snapshot, err = dc.Resource(snapshotGVR).Namespace(snapshot.GetNamespace()).Create(snapshot, metav1.CreateOptions{})
  139. framework.ExpectNoError(err)
  140. defer func() {
  141. e2elog.Logf("deleting snapshot %q/%q", snapshot.GetNamespace(), snapshot.GetName())
  142. // typically this snapshot has already been deleted
  143. err = dc.Resource(snapshotGVR).Namespace(snapshot.GetNamespace()).Delete(snapshot.GetName(), nil)
  144. if err != nil && !apierrs.IsNotFound(err) {
  145. framework.Failf("Error deleting snapshot %q. Error: %v", pvc.Name, err)
  146. }
  147. }()
  148. err = WaitForSnapshotReady(dc, snapshot.GetNamespace(), snapshot.GetName(), framework.Poll, framework.SnapshotCreateTimeout)
  149. framework.ExpectNoError(err)
  150. ginkgo.By("checking the snapshot")
  151. // Get new copy of the snapshot
  152. snapshot, err = dc.Resource(snapshotGVR).Namespace(snapshot.GetNamespace()).Get(snapshot.GetName(), metav1.GetOptions{})
  153. framework.ExpectNoError(err)
  154. // Get the bound snapshotContent
  155. snapshotSpec := snapshot.Object["spec"].(map[string]interface{})
  156. snapshotContentName := snapshotSpec["snapshotContentName"].(string)
  157. snapshotContent, err := dc.Resource(snapshotContentGVR).Get(snapshotContentName, metav1.GetOptions{})
  158. framework.ExpectNoError(err)
  159. snapshotContentSpec := snapshotContent.Object["spec"].(map[string]interface{})
  160. volumeSnapshotRef := snapshotContentSpec["volumeSnapshotRef"].(map[string]interface{})
  161. persistentVolumeRef := snapshotContentSpec["persistentVolumeRef"].(map[string]interface{})
  162. // Check SnapshotContent properties
  163. ginkgo.By("checking the SnapshotContent")
  164. gomega.Expect(snapshotContentSpec["snapshotClassName"]).To(gomega.Equal(vsc.GetName()))
  165. gomega.Expect(volumeSnapshotRef["name"]).To(gomega.Equal(snapshot.GetName()))
  166. gomega.Expect(volumeSnapshotRef["namespace"]).To(gomega.Equal(snapshot.GetNamespace()))
  167. gomega.Expect(persistentVolumeRef["name"]).To(gomega.Equal(pv.Name))
  168. })
  169. }
  170. // WaitForSnapshotReady waits for a VolumeSnapshot to be ready to use or until timeout occurs, whichever comes first.
  171. func WaitForSnapshotReady(c dynamic.Interface, ns string, snapshotName string, Poll, timeout time.Duration) error {
  172. e2elog.Logf("Waiting up to %v for VolumeSnapshot %s to become ready", timeout, snapshotName)
  173. for start := time.Now(); time.Since(start) < timeout; time.Sleep(Poll) {
  174. snapshot, err := c.Resource(snapshotGVR).Namespace(ns).Get(snapshotName, metav1.GetOptions{})
  175. if err != nil {
  176. e2elog.Logf("Failed to get claim %q, retrying in %v. Error: %v", snapshotName, Poll, err)
  177. continue
  178. } else {
  179. status := snapshot.Object["status"]
  180. if status == nil {
  181. e2elog.Logf("VolumeSnapshot %s found but is not ready.", snapshotName)
  182. continue
  183. }
  184. value := status.(map[string]interface{})
  185. if value["readyToUse"] == true {
  186. e2elog.Logf("VolumeSnapshot %s found and is ready", snapshotName, time.Since(start))
  187. return nil
  188. } else if value["ready"] == true {
  189. e2elog.Logf("VolumeSnapshot %s found and is ready", snapshotName, time.Since(start))
  190. return nil
  191. } else {
  192. e2elog.Logf("VolumeSnapshot %s found but is not ready.", snapshotName)
  193. }
  194. }
  195. }
  196. return fmt.Errorf("VolumeSnapshot %s is not ready within %v", snapshotName, timeout)
  197. }