volume_io.go 12 KB

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