ephemeral_volume.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 storage
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/rand"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  25. "k8s.io/kubernetes/test/e2e/storage/utils"
  26. imageutils "k8s.io/kubernetes/test/utils/image"
  27. "github.com/onsi/ginkgo"
  28. )
  29. var (
  30. volumePath = "/test-volume"
  31. volumeName = "test-volume"
  32. mountImage = imageutils.GetE2EImage(imageutils.Mounttest)
  33. )
  34. var _ = utils.SIGDescribe("Ephemeralstorage", func() {
  35. var (
  36. c clientset.Interface
  37. )
  38. f := framework.NewDefaultFramework("pv")
  39. ginkgo.BeforeEach(func() {
  40. c = f.ClientSet
  41. })
  42. ginkgo.Describe("When pod refers to non-existent ephemeral storage", func() {
  43. for _, testSource := range invalidEphemeralSource("pod-ephm-test") {
  44. ginkgo.It(fmt.Sprintf("should allow deletion of pod with invalid volume : %s", testSource.volumeType), func() {
  45. pod := testEphemeralVolumePod(f, testSource.volumeType, testSource.source)
  46. pod, err := c.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  47. framework.ExpectNoError(err)
  48. // Allow it to sleep for 30 seconds
  49. time.Sleep(30 * time.Second)
  50. framework.Logf("Deleting pod %q/%q", pod.Namespace, pod.Name)
  51. framework.ExpectNoError(e2epod.DeletePodWithWait(c, pod))
  52. })
  53. }
  54. })
  55. })
  56. type ephemeralTestInfo struct {
  57. volumeType string
  58. source *v1.VolumeSource
  59. }
  60. func testEphemeralVolumePod(f *framework.Framework, volumeType string, source *v1.VolumeSource) *v1.Pod {
  61. var (
  62. suffix = strings.ToLower(fmt.Sprintf("%s-%s", volumeType, rand.String(4)))
  63. )
  64. return &v1.Pod{
  65. ObjectMeta: metav1.ObjectMeta{
  66. Name: fmt.Sprintf("pod-ephm-test-%s", suffix),
  67. Namespace: f.Namespace.Name,
  68. },
  69. Spec: v1.PodSpec{
  70. Containers: []v1.Container{
  71. {
  72. Name: fmt.Sprintf("test-container-subpath-%s", suffix),
  73. Image: mountImage,
  74. VolumeMounts: []v1.VolumeMount{
  75. {
  76. Name: volumeName,
  77. MountPath: volumePath,
  78. },
  79. },
  80. },
  81. },
  82. RestartPolicy: v1.RestartPolicyNever,
  83. Volumes: []v1.Volume{
  84. {
  85. Name: volumeName,
  86. VolumeSource: *source,
  87. },
  88. },
  89. },
  90. }
  91. }
  92. func invalidEphemeralSource(suffix string) []ephemeralTestInfo {
  93. testInfo := []ephemeralTestInfo{
  94. {
  95. volumeType: "secret",
  96. source: &v1.VolumeSource{
  97. Secret: &v1.SecretVolumeSource{
  98. SecretName: fmt.Sprintf("secert-%s", suffix),
  99. },
  100. },
  101. },
  102. {
  103. volumeType: "configmap",
  104. source: &v1.VolumeSource{
  105. ConfigMap: &v1.ConfigMapVolumeSource{
  106. LocalObjectReference: v1.LocalObjectReference{
  107. Name: fmt.Sprintf("configmap-%s", suffix),
  108. },
  109. },
  110. },
  111. },
  112. {
  113. volumeType: "projected",
  114. source: &v1.VolumeSource{
  115. Projected: &v1.ProjectedVolumeSource{
  116. Sources: []v1.VolumeProjection{
  117. {
  118. Secret: &v1.SecretProjection{
  119. LocalObjectReference: v1.LocalObjectReference{
  120. Name: fmt.Sprintf("secret-%s", suffix),
  121. },
  122. },
  123. },
  124. },
  125. },
  126. },
  127. },
  128. }
  129. return testInfo
  130. }