persistent_volumes.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. v1 "k8s.io/api/core/v1"
  16. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  19. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  20. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  21. "k8s.io/kubernetes/test/e2e/upgrades"
  22. "github.com/onsi/ginkgo"
  23. )
  24. // PersistentVolumeUpgradeTest test that a pv is available before and after a cluster upgrade.
  25. type PersistentVolumeUpgradeTest struct {
  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. // Setup creates a pv and then verifies that a pod can consume it. The pod writes data to the volume.
  37. func (t *PersistentVolumeUpgradeTest) Setup(f *framework.Framework) {
  38. var err error
  39. e2eskipper.SkipUnlessProviderIs("gce", "gke", "openstack", "aws", "vsphere", "azure")
  40. ns := f.Namespace.Name
  41. ginkgo.By("Creating a PVC")
  42. pvcConfig := e2epv.PersistentVolumeClaimConfig{
  43. StorageClassName: nil,
  44. }
  45. t.pvc = e2epv.MakePersistentVolumeClaim(pvcConfig, ns)
  46. t.pvc, err = e2epv.CreatePVC(f.ClientSet, ns, t.pvc)
  47. framework.ExpectNoError(err)
  48. ginkgo.By("Consuming the PV before upgrade")
  49. t.testPod(f, pvWriteCmd+";"+pvReadCmd)
  50. }
  51. // Test waits for the upgrade to complete, and then verifies that a pod can still consume the pv
  52. // and that the volume data persists.
  53. func (t *PersistentVolumeUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
  54. <-done
  55. ginkgo.By("Consuming the PV after upgrade")
  56. t.testPod(f, pvReadCmd)
  57. }
  58. // Teardown cleans up any remaining resources.
  59. func (t *PersistentVolumeUpgradeTest) Teardown(f *framework.Framework) {
  60. errs := e2epv.PVPVCCleanup(f.ClientSet, f.Namespace.Name, nil, t.pvc)
  61. if len(errs) > 0 {
  62. framework.Failf("Failed to delete 1 or more PVs/PVCs. Errors: %v", utilerrors.NewAggregate(errs))
  63. }
  64. }
  65. // testPod creates a pod that consumes a pv and prints it out. The output is then verified.
  66. func (t *PersistentVolumeUpgradeTest) testPod(f *framework.Framework, cmd string) {
  67. pod := e2epod.MakePod(f.Namespace.Name, nil, []*v1.PersistentVolumeClaim{t.pvc}, false, cmd)
  68. expectedOutput := []string{pvTestData}
  69. f.TestContainerOutput("pod consumes pv", pod, 0, expectedOutput)
  70. }