volumes.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // This test checks that various VolumeSources are working.
  14. // test/e2e/common/volumes.go duplicates the GlusterFS test from this file. Any changes made to this
  15. // test should be made there as well.
  16. package testsuites
  17. import (
  18. "fmt"
  19. "github.com/onsi/ginkgo"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. "k8s.io/kubernetes/test/e2e/framework/volume"
  24. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  25. imageutils "k8s.io/kubernetes/test/utils/image"
  26. )
  27. type volumesTestSuite struct {
  28. tsInfo TestSuiteInfo
  29. }
  30. var _ TestSuite = &volumesTestSuite{}
  31. // InitVolumesTestSuite returns volumesTestSuite that implements TestSuite interface
  32. func InitVolumesTestSuite() TestSuite {
  33. return &volumesTestSuite{
  34. tsInfo: TestSuiteInfo{
  35. name: "volumes",
  36. testPatterns: []testpatterns.TestPattern{
  37. // Default fsType
  38. testpatterns.DefaultFsInlineVolume,
  39. testpatterns.DefaultFsPreprovisionedPV,
  40. testpatterns.DefaultFsDynamicPV,
  41. // ext3
  42. testpatterns.Ext3InlineVolume,
  43. testpatterns.Ext3PreprovisionedPV,
  44. testpatterns.Ext3DynamicPV,
  45. // ext4
  46. testpatterns.Ext4InlineVolume,
  47. testpatterns.Ext4PreprovisionedPV,
  48. testpatterns.Ext4DynamicPV,
  49. // xfs
  50. testpatterns.XfsInlineVolume,
  51. testpatterns.XfsPreprovisionedPV,
  52. testpatterns.XfsDynamicPV,
  53. // ntfs
  54. testpatterns.NtfsInlineVolume,
  55. testpatterns.NtfsPreprovisionedPV,
  56. testpatterns.NtfsDynamicPV,
  57. },
  58. },
  59. }
  60. }
  61. func (t *volumesTestSuite) getTestSuiteInfo() TestSuiteInfo {
  62. return t.tsInfo
  63. }
  64. func (t *volumesTestSuite) skipUnsupportedTest(pattern testpatterns.TestPattern, driver TestDriver) {
  65. }
  66. func skipPersistenceTest(driver TestDriver) {
  67. dInfo := driver.GetDriverInfo()
  68. if !dInfo.Capabilities[CapPersistence] {
  69. framework.Skipf("Driver %q does not provide persistency - skipping", dInfo.Name)
  70. }
  71. }
  72. func skipExecTest(driver TestDriver) {
  73. dInfo := driver.GetDriverInfo()
  74. if !dInfo.Capabilities[CapExec] {
  75. framework.Skipf("Driver %q does not support exec - skipping", dInfo.Name)
  76. }
  77. }
  78. func (t *volumesTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) {
  79. type local struct {
  80. config *PerTestConfig
  81. testCleanup func()
  82. resource *genericVolumeTestResource
  83. intreeOps opCounts
  84. migratedOps opCounts
  85. }
  86. var dInfo = driver.GetDriverInfo()
  87. var l local
  88. // No preconditions to test. Normally they would be in a BeforeEach here.
  89. // This intentionally comes after checking the preconditions because it
  90. // registers its own BeforeEach which creates the namespace. Beware that it
  91. // also registers an AfterEach which renders f unusable. Any code using
  92. // f must run inside an It or Context callback.
  93. f := framework.NewDefaultFramework("volume")
  94. init := func() {
  95. l = local{}
  96. // Now do the more expensive test initialization.
  97. l.config, l.testCleanup = driver.PrepareTest(f)
  98. l.intreeOps, l.migratedOps = getMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName)
  99. l.resource = createGenericVolumeTestResource(driver, l.config, pattern)
  100. if l.resource.volSource == nil {
  101. framework.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name)
  102. }
  103. }
  104. cleanup := func() {
  105. if l.resource != nil {
  106. l.resource.cleanupResource()
  107. l.resource = nil
  108. }
  109. if l.testCleanup != nil {
  110. l.testCleanup()
  111. l.testCleanup = nil
  112. }
  113. validateMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName, l.intreeOps, l.migratedOps)
  114. }
  115. ginkgo.It("should be mountable", func() {
  116. skipPersistenceTest(driver)
  117. init()
  118. defer func() {
  119. volume.TestCleanup(f, convertTestConfig(l.config))
  120. cleanup()
  121. }()
  122. tests := []volume.Test{
  123. {
  124. Volume: *l.resource.volSource,
  125. File: "index.html",
  126. // Must match content
  127. ExpectedContent: fmt.Sprintf("Hello from %s from namespace %s",
  128. dInfo.Name, f.Namespace.Name),
  129. },
  130. }
  131. config := convertTestConfig(l.config)
  132. var fsGroup *int64
  133. if framework.NodeOSDistroIs("windows") && dInfo.Capabilities[CapFsGroup] {
  134. fsGroupVal := int64(1234)
  135. fsGroup = &fsGroupVal
  136. }
  137. // We set same fsGroup for both pods, because for same volumes (e.g.
  138. // local), plugin skips setting fsGroup if volume is already mounted
  139. // and we don't have reliable way to detect volumes are unmounted or
  140. // not before starting the second pod.
  141. volume.InjectHTML(f.ClientSet, config, fsGroup, tests[0].Volume, tests[0].ExpectedContent)
  142. volume.TestVolumeClient(f.ClientSet, config, fsGroup, pattern.FsType, tests)
  143. })
  144. ginkgo.It("should allow exec of files on the volume", func() {
  145. skipExecTest(driver)
  146. init()
  147. defer cleanup()
  148. testScriptInPod(f, l.resource.volType, l.resource.volSource, l.config)
  149. })
  150. }
  151. func testScriptInPod(
  152. f *framework.Framework,
  153. volumeType string,
  154. source *v1.VolumeSource,
  155. config *PerTestConfig) {
  156. const (
  157. volPath = "/vol1"
  158. volName = "vol1"
  159. )
  160. suffix := generateSuffixForPodName(volumeType)
  161. fileName := fmt.Sprintf("test-%s", suffix)
  162. var content string
  163. if framework.NodeOSDistroIs("windows") {
  164. content = fmt.Sprintf("ls -n %s", volPath)
  165. } else {
  166. content = fmt.Sprintf("ls %s", volPath)
  167. }
  168. command := volume.GenerateWriteandExecuteScriptFileCmd(content, fileName, volPath)
  169. pod := &v1.Pod{
  170. ObjectMeta: metav1.ObjectMeta{
  171. Name: fmt.Sprintf("exec-volume-test-%s", suffix),
  172. Namespace: f.Namespace.Name,
  173. },
  174. Spec: v1.PodSpec{
  175. Containers: []v1.Container{
  176. {
  177. Name: fmt.Sprintf("exec-container-%s", suffix),
  178. Image: volume.GetTestImage(imageutils.GetE2EImage(imageutils.Nginx)),
  179. Command: command,
  180. VolumeMounts: []v1.VolumeMount{
  181. {
  182. Name: volName,
  183. MountPath: volPath,
  184. },
  185. },
  186. },
  187. },
  188. Volumes: []v1.Volume{
  189. {
  190. Name: volName,
  191. VolumeSource: *source,
  192. },
  193. },
  194. RestartPolicy: v1.RestartPolicyNever,
  195. NodeSelector: config.ClientNodeSelector,
  196. NodeName: config.ClientNodeName,
  197. },
  198. }
  199. ginkgo.By(fmt.Sprintf("Creating pod %s", pod.Name))
  200. f.TestContainerOutput("exec-volume-test", pod, 0, []string{fileName})
  201. ginkgo.By(fmt.Sprintf("Deleting pod %s", pod.Name))
  202. err := framework.DeletePodWithWait(f, f.ClientSet, pod)
  203. framework.ExpectNoError(err, "while deleting pod")
  204. }