volume_io.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /*
  14. * This test checks that the plugin VolumeSources are working when pseudo-streaming
  15. * various write sizes to mounted files.
  16. */
  17. package testsuites
  18. import (
  19. "fmt"
  20. "math"
  21. "path/filepath"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "github.com/onsi/ginkgo"
  26. v1 "k8s.io/api/core/v1"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. clientset "k8s.io/client-go/kubernetes"
  29. "k8s.io/kubernetes/test/e2e/framework"
  30. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  31. "k8s.io/kubernetes/test/e2e/framework/volume"
  32. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  33. "k8s.io/kubernetes/test/e2e/storage/utils"
  34. )
  35. // MD5 hashes of the test file corresponding to each file size.
  36. // Test files are generated in testVolumeIO()
  37. // If test file generation algorithm changes, these must be recomputed.
  38. var md5hashes = map[int64]string{
  39. testpatterns.FileSizeSmall: "5c34c2813223a7ca05a3c2f38c0d1710",
  40. testpatterns.FileSizeMedium: "f2fa202b1ffeedda5f3a58bd1ae81104",
  41. testpatterns.FileSizeLarge: "8d763edc71bd16217664793b5a15e403",
  42. }
  43. const mountPath = "/opt"
  44. type volumeIOTestSuite struct {
  45. tsInfo TestSuiteInfo
  46. }
  47. var _ TestSuite = &volumeIOTestSuite{}
  48. // InitVolumeIOTestSuite returns volumeIOTestSuite that implements TestSuite interface
  49. func InitVolumeIOTestSuite() TestSuite {
  50. return &volumeIOTestSuite{
  51. tsInfo: TestSuiteInfo{
  52. name: "volumeIO",
  53. testPatterns: []testpatterns.TestPattern{
  54. testpatterns.DefaultFsInlineVolume,
  55. testpatterns.DefaultFsPreprovisionedPV,
  56. testpatterns.DefaultFsDynamicPV,
  57. },
  58. },
  59. }
  60. }
  61. func (t *volumeIOTestSuite) getTestSuiteInfo() TestSuiteInfo {
  62. return t.tsInfo
  63. }
  64. func (t *volumeIOTestSuite) defineTests(driver TestDriver, pattern testpatterns.TestPattern) {
  65. type local struct {
  66. config *PerTestConfig
  67. testCleanup func()
  68. resource *genericVolumeTestResource
  69. intreeOps opCounts
  70. migratedOps opCounts
  71. }
  72. var (
  73. dInfo = driver.GetDriverInfo()
  74. l local
  75. )
  76. // No preconditions to test. Normally they would be in a BeforeEach here.
  77. // This intentionally comes after checking the preconditions because it
  78. // registers its own BeforeEach which creates the namespace. Beware that it
  79. // also registers an AfterEach which renders f unusable. Any code using
  80. // f must run inside an It or Context callback.
  81. f := framework.NewDefaultFramework("volumeio")
  82. init := func() {
  83. l = local{}
  84. // Now do the more expensive test initialization.
  85. l.config, l.testCleanup = driver.PrepareTest(f)
  86. l.intreeOps, l.migratedOps = getMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName)
  87. l.resource = createGenericVolumeTestResource(driver, l.config, pattern)
  88. if l.resource.volSource == nil {
  89. framework.Skipf("Driver %q does not define volumeSource - skipping", dInfo.Name)
  90. }
  91. }
  92. cleanup := func() {
  93. if l.resource != nil {
  94. l.resource.cleanupResource()
  95. l.resource = nil
  96. }
  97. if l.testCleanup != nil {
  98. l.testCleanup()
  99. l.testCleanup = nil
  100. }
  101. validateMigrationVolumeOpCounts(f.ClientSet, dInfo.InTreePluginName, l.intreeOps, l.migratedOps)
  102. }
  103. ginkgo.It("should write files of various sizes, verify size, validate content [Slow]", func() {
  104. init()
  105. defer cleanup()
  106. cs := f.ClientSet
  107. fileSizes := createFileSizes(dInfo.MaxFileSize)
  108. testFile := fmt.Sprintf("%s_io_test_%s", dInfo.Name, f.Namespace.Name)
  109. var fsGroup *int64
  110. if !framework.NodeOSDistroIs("windows") && dInfo.Capabilities[CapFsGroup] {
  111. fsGroupVal := int64(1234)
  112. fsGroup = &fsGroupVal
  113. }
  114. podSec := v1.PodSecurityContext{
  115. FSGroup: fsGroup,
  116. }
  117. err := testVolumeIO(f, cs, convertTestConfig(l.config), *l.resource.volSource, &podSec, testFile, fileSizes)
  118. framework.ExpectNoError(err)
  119. })
  120. }
  121. func createFileSizes(maxFileSize int64) []int64 {
  122. allFileSizes := []int64{
  123. testpatterns.FileSizeSmall,
  124. testpatterns.FileSizeMedium,
  125. testpatterns.FileSizeLarge,
  126. }
  127. fileSizes := []int64{}
  128. for _, size := range allFileSizes {
  129. if size <= maxFileSize {
  130. fileSizes = append(fileSizes, size)
  131. }
  132. }
  133. return fileSizes
  134. }
  135. // Return the plugin's client pod spec. Use an InitContainer to setup the file i/o test env.
  136. func makePodSpec(config volume.TestConfig, initCmd string, volsrc v1.VolumeSource, podSecContext *v1.PodSecurityContext) *v1.Pod {
  137. var gracePeriod int64 = 1
  138. volName := fmt.Sprintf("io-volume-%s", config.Namespace)
  139. return &v1.Pod{
  140. TypeMeta: metav1.TypeMeta{
  141. Kind: "Pod",
  142. APIVersion: "v1",
  143. },
  144. ObjectMeta: metav1.ObjectMeta{
  145. Name: config.Prefix + "-io-client",
  146. Labels: map[string]string{
  147. "role": config.Prefix + "-io-client",
  148. },
  149. },
  150. Spec: v1.PodSpec{
  151. InitContainers: []v1.Container{
  152. {
  153. Name: config.Prefix + "-io-init",
  154. Image: framework.BusyBoxImage,
  155. Command: []string{
  156. "/bin/sh",
  157. "-c",
  158. initCmd,
  159. },
  160. VolumeMounts: []v1.VolumeMount{
  161. {
  162. Name: volName,
  163. MountPath: mountPath,
  164. },
  165. },
  166. },
  167. },
  168. Containers: []v1.Container{
  169. {
  170. Name: config.Prefix + "-io-client",
  171. Image: framework.BusyBoxImage,
  172. Command: []string{
  173. "/bin/sh",
  174. "-c",
  175. "sleep 3600", // keep pod alive until explicitly deleted
  176. },
  177. VolumeMounts: []v1.VolumeMount{
  178. {
  179. Name: volName,
  180. MountPath: mountPath,
  181. },
  182. },
  183. },
  184. },
  185. TerminationGracePeriodSeconds: &gracePeriod,
  186. SecurityContext: podSecContext,
  187. Volumes: []v1.Volume{
  188. {
  189. Name: volName,
  190. VolumeSource: volsrc,
  191. },
  192. },
  193. RestartPolicy: v1.RestartPolicyNever, // want pod to fail if init container fails
  194. NodeName: config.ClientNodeName,
  195. NodeSelector: config.NodeSelector,
  196. },
  197. }
  198. }
  199. // Write `fsize` bytes to `fpath` in the pod, using dd and the `ddInput` file.
  200. func writeToFile(pod *v1.Pod, fpath, ddInput string, fsize int64) error {
  201. ginkgo.By(fmt.Sprintf("writing %d bytes to test file %s", fsize, fpath))
  202. loopCnt := fsize / testpatterns.MinFileSize
  203. writeCmd := fmt.Sprintf("i=0; while [ $i -lt %d ]; do dd if=%s bs=%d >>%s 2>/dev/null; let i+=1; done", loopCnt, ddInput, testpatterns.MinFileSize, fpath)
  204. _, err := utils.PodExec(pod, writeCmd)
  205. return err
  206. }
  207. // Verify that the test file is the expected size and contains the expected content.
  208. func verifyFile(pod *v1.Pod, fpath string, expectSize int64, ddInput string) error {
  209. ginkgo.By("verifying file size")
  210. rtnstr, err := utils.PodExec(pod, fmt.Sprintf("stat -c %%s %s", fpath))
  211. if err != nil || rtnstr == "" {
  212. return fmt.Errorf("unable to get file size via `stat %s`: %v", fpath, err)
  213. }
  214. size, err := strconv.Atoi(strings.TrimSuffix(rtnstr, "\n"))
  215. if err != nil {
  216. return fmt.Errorf("unable to convert string %q to int: %v", rtnstr, err)
  217. }
  218. if int64(size) != expectSize {
  219. return fmt.Errorf("size of file %s is %d, expected %d", fpath, size, expectSize)
  220. }
  221. ginkgo.By("verifying file hash")
  222. rtnstr, err = utils.PodExec(pod, fmt.Sprintf("md5sum %s | cut -d' ' -f1", fpath))
  223. if err != nil {
  224. return fmt.Errorf("unable to test file hash via `md5sum %s`: %v", fpath, err)
  225. }
  226. actualHash := strings.TrimSuffix(rtnstr, "\n")
  227. expectedHash, ok := md5hashes[expectSize]
  228. if !ok {
  229. return fmt.Errorf("File hash is unknown for file size %d. Was a new file size added to the test suite?",
  230. expectSize)
  231. }
  232. if actualHash != expectedHash {
  233. return fmt.Errorf("MD5 hash is incorrect for file %s with size %d. Expected: `%s`; Actual: `%s`",
  234. fpath, expectSize, expectedHash, actualHash)
  235. }
  236. return nil
  237. }
  238. // Delete `fpath` to save some disk space on host. Delete errors are logged but ignored.
  239. func deleteFile(pod *v1.Pod, fpath string) {
  240. ginkgo.By(fmt.Sprintf("deleting test file %s...", fpath))
  241. _, err := utils.PodExec(pod, fmt.Sprintf("rm -f %s", fpath))
  242. if err != nil {
  243. // keep going, the test dir will be deleted when the volume is unmounted
  244. e2elog.Logf("unable to delete test file %s: %v\nerror ignored, continuing test", fpath, err)
  245. }
  246. }
  247. // Create the client pod and create files of the sizes passed in by the `fsizes` parameter. Delete the
  248. // client pod and the new files when done.
  249. // Note: the file name is appended to "/opt/<Prefix>/<namespace>", eg. "/opt/nfs/e2e-.../<file>".
  250. // Note: nil can be passed for the podSecContext parm, in which case it is ignored.
  251. // Note: `fsizes` values are enforced to each be at least `MinFileSize` and a multiple of `MinFileSize`
  252. // bytes.
  253. func testVolumeIO(f *framework.Framework, cs clientset.Interface, config volume.TestConfig, volsrc v1.VolumeSource, podSecContext *v1.PodSecurityContext, file string, fsizes []int64) (err error) {
  254. ddInput := filepath.Join(mountPath, fmt.Sprintf("%s-%s-dd_if", config.Prefix, config.Namespace))
  255. writeBlk := strings.Repeat("abcdefghijklmnopqrstuvwxyz123456", 32) // 1KiB value
  256. loopCnt := testpatterns.MinFileSize / int64(len(writeBlk))
  257. // initContainer cmd to create and fill dd's input file. The initContainer is used to create
  258. // the `dd` input file which is currently 1MiB. Rather than store a 1MiB go value, a loop is
  259. // used to create a 1MiB file in the target directory.
  260. initCmd := fmt.Sprintf("i=0; while [ $i -lt %d ]; do echo -n %s >>%s; let i+=1; done", loopCnt, writeBlk, ddInput)
  261. clientPod := makePodSpec(config, initCmd, volsrc, podSecContext)
  262. ginkgo.By(fmt.Sprintf("starting %s", clientPod.Name))
  263. podsNamespacer := cs.CoreV1().Pods(config.Namespace)
  264. clientPod, err = podsNamespacer.Create(clientPod)
  265. if err != nil {
  266. return fmt.Errorf("failed to create client pod %q: %v", clientPod.Name, err)
  267. }
  268. defer func() {
  269. deleteFile(clientPod, ddInput)
  270. ginkgo.By(fmt.Sprintf("deleting client pod %q...", clientPod.Name))
  271. e := framework.DeletePodWithWait(f, cs, clientPod)
  272. if e != nil {
  273. e2elog.Logf("client pod failed to delete: %v", e)
  274. if err == nil { // delete err is returned if err is not set
  275. err = e
  276. }
  277. } else {
  278. e2elog.Logf("sleeping a bit so kubelet can unmount and detach the volume")
  279. time.Sleep(volume.PodCleanupTimeout)
  280. }
  281. }()
  282. err = framework.WaitForPodRunningInNamespace(cs, clientPod)
  283. if err != nil {
  284. return fmt.Errorf("client pod %q not running: %v", clientPod.Name, err)
  285. }
  286. // create files of the passed-in file sizes and verify test file size and content
  287. for _, fsize := range fsizes {
  288. // file sizes must be a multiple of `MinFileSize`
  289. if math.Mod(float64(fsize), float64(testpatterns.MinFileSize)) != 0 {
  290. fsize = fsize/testpatterns.MinFileSize + testpatterns.MinFileSize
  291. }
  292. fpath := filepath.Join(mountPath, fmt.Sprintf("%s-%d", file, fsize))
  293. defer func() {
  294. deleteFile(clientPod, fpath)
  295. }()
  296. if err = writeToFile(clientPod, fpath, ddInput, fsize); err != nil {
  297. return err
  298. }
  299. if err = verifyFile(clientPod, fpath, fsize, ddInput); err != nil {
  300. return err
  301. }
  302. }
  303. return
  304. }