volumes.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. "path/filepath"
  20. "github.com/onsi/ginkgo"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/errors"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  26. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  27. "k8s.io/kubernetes/test/e2e/framework/volume"
  28. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  29. imageutils "k8s.io/kubernetes/test/utils/image"
  30. )
  31. type volumesTestSuite struct {
  32. tsInfo TestSuiteInfo
  33. }
  34. var _ TestSuite = &volumesTestSuite{}
  35. // InitVolumesTestSuite returns volumesTestSuite that implements TestSuite interface
  36. func InitVolumesTestSuite() TestSuite {
  37. return &volumesTestSuite{
  38. tsInfo: TestSuiteInfo{
  39. Name: "volumes",
  40. TestPatterns: []testpatterns.TestPattern{
  41. // Default fsType
  42. testpatterns.DefaultFsInlineVolume,
  43. testpatterns.DefaultFsPreprovisionedPV,
  44. testpatterns.DefaultFsDynamicPV,
  45. // ext3
  46. testpatterns.Ext3InlineVolume,
  47. testpatterns.Ext3PreprovisionedPV,
  48. testpatterns.Ext3DynamicPV,
  49. // ext4
  50. testpatterns.Ext4InlineVolume,
  51. testpatterns.Ext4PreprovisionedPV,
  52. testpatterns.Ext4DynamicPV,
  53. // xfs
  54. testpatterns.XfsInlineVolume,
  55. testpatterns.XfsPreprovisionedPV,
  56. testpatterns.XfsDynamicPV,
  57. // ntfs
  58. testpatterns.NtfsInlineVolume,
  59. testpatterns.NtfsPreprovisionedPV,
  60. testpatterns.NtfsDynamicPV,
  61. // block volumes
  62. testpatterns.BlockVolModePreprovisionedPV,
  63. testpatterns.BlockVolModeDynamicPV,
  64. },
  65. SupportedSizeRange: volume.SizeRange{
  66. Min: "1Mi",
  67. },
  68. },
  69. }
  70. }
  71. func (t *volumesTestSuite) GetTestSuiteInfo() TestSuiteInfo {
  72. return t.tsInfo
  73. }
  74. func (t *volumesTestSuite) SkipRedundantSuite(driver TestDriver, pattern testpatterns.TestPattern) {
  75. }
  76. func skipExecTest(driver TestDriver) {
  77. dInfo := driver.GetDriverInfo()
  78. if !dInfo.Capabilities[CapExec] {
  79. e2eskipper.Skipf("Driver %q does not support exec - skipping", dInfo.Name)
  80. }
  81. }
  82. func skipTestIfBlockNotSupported(driver TestDriver) {
  83. dInfo := driver.GetDriverInfo()
  84. if !dInfo.Capabilities[CapBlock] {
  85. e2eskipper.Skipf("Driver %q does not provide raw block - skipping", dInfo.Name)
  86. }
  87. }
  88. func (t *volumesTestSuite) DefineTests(driver TestDriver, pattern testpatterns.TestPattern) {
  89. type local struct {
  90. config *PerTestConfig
  91. driverCleanup func()
  92. resource *VolumeResource
  93. intreeOps opCounts
  94. migratedOps opCounts
  95. }
  96. var dInfo = driver.GetDriverInfo()
  97. var l local
  98. // No preconditions to test. Normally they would be in a BeforeEach here.
  99. // This intentionally comes after checking the preconditions because it
  100. // registers its own BeforeEach which creates the namespace. Beware that it
  101. // also registers an AfterEach which renders f unusable. Any code using
  102. // f must run inside an It or Context callback.
  103. f := framework.NewDefaultFramework("volume")
  104. init := func() {
  105. l = local{}
  106. // Now do the more expensive test initialization.
  107. l.config, l.driverCleanup = driver.PrepareTest(f)
  108. l.intreeOps, l.migratedOps = getMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName)
  109. testVolumeSizeRange := t.GetTestSuiteInfo().SupportedSizeRange
  110. l.resource = CreateVolumeResource(driver, l.config, pattern, testVolumeSizeRange)
  111. if l.resource.VolSource == nil {
  112. e2eskipper.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name)
  113. }
  114. }
  115. cleanup := func() {
  116. var errs []error
  117. if l.resource != nil {
  118. errs = append(errs, l.resource.CleanupResource())
  119. l.resource = nil
  120. }
  121. errs = append(errs, tryFunc(l.driverCleanup))
  122. l.driverCleanup = nil
  123. framework.ExpectNoError(errors.NewAggregate(errs), "while cleaning up resource")
  124. validateMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName, l.intreeOps, l.migratedOps)
  125. }
  126. ginkgo.It("should store data", func() {
  127. if pattern.VolMode == v1.PersistentVolumeBlock {
  128. skipTestIfBlockNotSupported(driver)
  129. }
  130. init()
  131. defer func() {
  132. volume.TestServerCleanup(f, convertTestConfig(l.config))
  133. cleanup()
  134. }()
  135. tests := []volume.Test{
  136. {
  137. Volume: *l.resource.VolSource,
  138. Mode: pattern.VolMode,
  139. File: "index.html",
  140. // Must match content
  141. ExpectedContent: fmt.Sprintf("Hello from %s from namespace %s",
  142. dInfo.Name, f.Namespace.Name),
  143. },
  144. }
  145. config := convertTestConfig(l.config)
  146. var fsGroup *int64
  147. if framework.NodeOSDistroIs("windows") && dInfo.Capabilities[CapFsGroup] {
  148. fsGroupVal := int64(1234)
  149. fsGroup = &fsGroupVal
  150. }
  151. // We set same fsGroup for both pods, because for same volumes (e.g.
  152. // local), plugin skips setting fsGroup if volume is already mounted
  153. // and we don't have reliable way to detect volumes are unmounted or
  154. // not before starting the second pod.
  155. volume.InjectContent(f, config, fsGroup, pattern.FsType, tests)
  156. if driver.GetDriverInfo().Capabilities[CapPersistence] {
  157. volume.TestVolumeClient(f, config, fsGroup, pattern.FsType, tests)
  158. } else {
  159. ginkgo.By("Skipping persistence check for non-persistent volume")
  160. }
  161. })
  162. // Exec works only on filesystem volumes
  163. if pattern.VolMode != v1.PersistentVolumeBlock {
  164. ginkgo.It("should allow exec of files on the volume", func() {
  165. skipExecTest(driver)
  166. init()
  167. defer cleanup()
  168. testScriptInPod(f, string(pattern.VolType), l.resource.VolSource, l.config)
  169. })
  170. }
  171. }
  172. func testScriptInPod(
  173. f *framework.Framework,
  174. volumeType string,
  175. source *v1.VolumeSource,
  176. config *PerTestConfig) {
  177. const (
  178. volPath = "/vol1"
  179. volName = "vol1"
  180. )
  181. suffix := generateSuffixForPodName(volumeType)
  182. fileName := fmt.Sprintf("test-%s", suffix)
  183. var content string
  184. if framework.NodeOSDistroIs("windows") {
  185. content = fmt.Sprintf("ls -n %s", volPath)
  186. } else {
  187. content = fmt.Sprintf("ls %s", volPath)
  188. }
  189. command := generateWriteandExecuteScriptFileCmd(content, fileName, volPath)
  190. pod := &v1.Pod{
  191. ObjectMeta: metav1.ObjectMeta{
  192. Name: fmt.Sprintf("exec-volume-test-%s", suffix),
  193. Namespace: f.Namespace.Name,
  194. },
  195. Spec: v1.PodSpec{
  196. Containers: []v1.Container{
  197. {
  198. Name: fmt.Sprintf("exec-container-%s", suffix),
  199. Image: volume.GetTestImage(imageutils.GetE2EImage(imageutils.Nginx)),
  200. Command: command,
  201. VolumeMounts: []v1.VolumeMount{
  202. {
  203. Name: volName,
  204. MountPath: volPath,
  205. },
  206. },
  207. },
  208. },
  209. Volumes: []v1.Volume{
  210. {
  211. Name: volName,
  212. VolumeSource: *source,
  213. },
  214. },
  215. RestartPolicy: v1.RestartPolicyNever,
  216. },
  217. }
  218. e2epod.SetNodeSelection(pod, config.ClientNodeSelection)
  219. ginkgo.By(fmt.Sprintf("Creating pod %s", pod.Name))
  220. f.TestContainerOutput("exec-volume-test", pod, 0, []string{fileName})
  221. ginkgo.By(fmt.Sprintf("Deleting pod %s", pod.Name))
  222. err := e2epod.DeletePodWithWait(f.ClientSet, pod)
  223. framework.ExpectNoError(err, "while deleting pod")
  224. }
  225. // generateWriteandExecuteScriptFileCmd generates the corresponding command lines to write a file with the given file path
  226. // and also execute this file.
  227. // Depending on the Node OS is Windows or linux, the command will use powershell or /bin/sh
  228. func generateWriteandExecuteScriptFileCmd(content, fileName, filePath string) []string {
  229. // for windows cluster, modify the Pod spec.
  230. if framework.NodeOSDistroIs("windows") {
  231. scriptName := fmt.Sprintf("%s.ps1", fileName)
  232. fullPath := filepath.Join(filePath, scriptName)
  233. cmd := "echo \"" + content + "\" > " + fullPath + "; .\\" + fullPath
  234. framework.Logf("generated pod command %s", cmd)
  235. return []string{"powershell", "/c", cmd}
  236. }
  237. scriptName := fmt.Sprintf("%s.sh", fileName)
  238. fullPath := filepath.Join(filePath, scriptName)
  239. cmd := fmt.Sprintf("echo \"%s\" > %s; chmod u+x %s; %s;", content, fullPath, fullPath, fullPath)
  240. return []string{"/bin/sh", "-ec", cmd}
  241. }