persistent_volumes.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "k8s.io/api/core/v1"
  16. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. "k8s.io/kubernetes/test/e2e/framework/volume"
  19. "github.com/onsi/ginkgo"
  20. "k8s.io/kubernetes/test/e2e/upgrades"
  21. )
  22. // PersistentVolumeUpgradeTest test that a pv is available before and after a cluster upgrade.
  23. type PersistentVolumeUpgradeTest struct {
  24. pvSource *v1.PersistentVolumeSource
  25. pv *v1.PersistentVolume
  26. pvc *v1.PersistentVolumeClaim
  27. }
  28. // Name returns the tracking name of the test.
  29. func (PersistentVolumeUpgradeTest) Name() string { return "[sig-storage] persistent-volume-upgrade" }
  30. const (
  31. pvTestFile string = "/mnt/volume1/pv_upgrade_test"
  32. pvTestData string = "keep it pv"
  33. pvWriteCmd string = "echo \"" + pvTestData + "\" > " + pvTestFile
  34. pvReadCmd string = "cat " + pvTestFile
  35. )
  36. func (t *PersistentVolumeUpgradeTest) deleteGCEVolume(pvSource *v1.PersistentVolumeSource) error {
  37. return framework.DeletePDWithRetry(pvSource.GCEPersistentDisk.PDName)
  38. }
  39. // Setup creates a pv and then verifies that a pod can consume it. The pod writes data to the volume.
  40. func (t *PersistentVolumeUpgradeTest) Setup(f *framework.Framework) {
  41. var err error
  42. // TODO: generalize this to other providers
  43. framework.SkipUnlessProviderIs("gce", "gke")
  44. ns := f.Namespace.Name
  45. ginkgo.By("Initializing PV source")
  46. t.pvSource, _ = volume.CreateGCEVolume()
  47. pvConfig := framework.PersistentVolumeConfig{
  48. NamePrefix: "pv-upgrade",
  49. PVSource: *t.pvSource,
  50. Prebind: nil,
  51. }
  52. emptyStorageClass := ""
  53. pvcConfig := framework.PersistentVolumeClaimConfig{StorageClassName: &emptyStorageClass}
  54. ginkgo.By("Creating the PV and PVC")
  55. t.pv, t.pvc, err = framework.CreatePVPVC(f.ClientSet, pvConfig, pvcConfig, ns, true)
  56. framework.ExpectNoError(err)
  57. framework.ExpectNoError(framework.WaitOnPVandPVC(f.ClientSet, ns, t.pv, t.pvc))
  58. ginkgo.By("Consuming the PV before upgrade")
  59. t.testPod(f, pvWriteCmd+";"+pvReadCmd)
  60. }
  61. // Test waits for the upgrade to complete, and then verifies that a pod can still consume the pv
  62. // and that the volume data persists.
  63. func (t *PersistentVolumeUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
  64. <-done
  65. ginkgo.By("Consuming the PV after upgrade")
  66. t.testPod(f, pvReadCmd)
  67. }
  68. // Teardown cleans up any remaining resources.
  69. func (t *PersistentVolumeUpgradeTest) Teardown(f *framework.Framework) {
  70. errs := framework.PVPVCCleanup(f.ClientSet, f.Namespace.Name, t.pv, t.pvc)
  71. if err := t.deleteGCEVolume(t.pvSource); err != nil {
  72. errs = append(errs, err)
  73. }
  74. if len(errs) > 0 {
  75. framework.Failf("Failed to delete 1 or more PVs/PVCs and/or the GCE volume. Errors: %v", utilerrors.NewAggregate(errs))
  76. }
  77. }
  78. // testPod creates a pod that consumes a pv and prints it out. The output is then verified.
  79. func (t *PersistentVolumeUpgradeTest) testPod(f *framework.Framework, cmd string) {
  80. pod := framework.MakePod(f.Namespace.Name, nil, []*v1.PersistentVolumeClaim{t.pvc}, false, cmd)
  81. expectedOutput := []string{pvTestData}
  82. f.TestContainerOutput("pod consumes pv", pod, 0, expectedOutput)
  83. }