empty_dir.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. Copyright 2016 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 common
  14. import (
  15. "context"
  16. "fmt"
  17. "path"
  18. "github.com/onsi/ginkgo"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/uuid"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  24. imageutils "k8s.io/kubernetes/test/utils/image"
  25. )
  26. const (
  27. volumePath = "/test-volume"
  28. )
  29. var (
  30. testImageRootUid = imageutils.GetE2EImage(imageutils.Mounttest)
  31. testImageNonRootUid = imageutils.GetE2EImage(imageutils.MounttestUser)
  32. )
  33. var _ = ginkgo.Describe("[sig-storage] EmptyDir volumes", func() {
  34. f := framework.NewDefaultFramework("emptydir")
  35. ginkgo.Context("when FSGroup is specified [LinuxOnly] [NodeFeature:FSGroup]", func() {
  36. ginkgo.BeforeEach(func() {
  37. // Windows does not support the FSGroup SecurityContext option.
  38. e2eskipper.SkipIfNodeOSDistroIs("windows")
  39. })
  40. ginkgo.It("new files should be created with FSGroup ownership when container is root", func() {
  41. doTestSetgidFSGroup(f, testImageRootUid, v1.StorageMediumMemory)
  42. })
  43. ginkgo.It("new files should be created with FSGroup ownership when container is non-root", func() {
  44. doTestSetgidFSGroup(f, testImageNonRootUid, v1.StorageMediumMemory)
  45. })
  46. ginkgo.It("nonexistent volume subPath should have the correct mode and owner using FSGroup", func() {
  47. doTestSubPathFSGroup(f, testImageNonRootUid, v1.StorageMediumMemory)
  48. })
  49. ginkgo.It("files with FSGroup ownership should support (root,0644,tmpfs)", func() {
  50. doTest0644FSGroup(f, testImageRootUid, v1.StorageMediumMemory)
  51. })
  52. ginkgo.It("volume on default medium should have the correct mode using FSGroup", func() {
  53. doTestVolumeModeFSGroup(f, testImageRootUid, v1.StorageMediumDefault)
  54. })
  55. ginkgo.It("volume on tmpfs should have the correct mode using FSGroup", func() {
  56. doTestVolumeModeFSGroup(f, testImageRootUid, v1.StorageMediumMemory)
  57. })
  58. })
  59. /*
  60. Release : v1.9
  61. Testname: EmptyDir, medium memory, volume mode default
  62. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume MUST have mode set as -rwxrwxrwx and mount type set to tmpfs.
  63. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or the medium = 'Memory'.
  64. */
  65. framework.ConformanceIt("volume on tmpfs should have the correct mode [LinuxOnly] [NodeConformance]", func() {
  66. doTestVolumeMode(f, testImageRootUid, v1.StorageMediumMemory)
  67. })
  68. /*
  69. Release : v1.9
  70. Testname: EmptyDir, medium memory, volume mode 0644
  71. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume mode set to 0644. The volume MUST have mode -rw-r--r-- and mount type set to tmpfs and the contents MUST be readable.
  72. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID, or the medium = 'Memory'.
  73. */
  74. framework.ConformanceIt("should support (root,0644,tmpfs) [LinuxOnly] [NodeConformance]", func() {
  75. doTest0644(f, testImageRootUid, v1.StorageMediumMemory)
  76. })
  77. /*
  78. Release : v1.9
  79. Testname: EmptyDir, medium memory, volume mode 0666
  80. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume mode set to 0666. The volume MUST have mode -rw-rw-rw- and mount type set to tmpfs and the contents MUST be readable.
  81. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID, or the medium = 'Memory'.
  82. */
  83. framework.ConformanceIt("should support (root,0666,tmpfs) [LinuxOnly] [NodeConformance]", func() {
  84. doTest0666(f, testImageRootUid, v1.StorageMediumMemory)
  85. })
  86. /*
  87. Release : v1.9
  88. Testname: EmptyDir, medium memory, volume mode 0777
  89. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume mode set to 0777. The volume MUST have mode set as -rwxrwxrwx and mount type set to tmpfs and the contents MUST be readable.
  90. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID, or the medium = 'Memory'.
  91. */
  92. framework.ConformanceIt("should support (root,0777,tmpfs) [LinuxOnly] [NodeConformance]", func() {
  93. doTest0777(f, testImageRootUid, v1.StorageMediumMemory)
  94. })
  95. /*
  96. Release : v1.9
  97. Testname: EmptyDir, medium memory, volume mode 0644, non-root user
  98. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume mode set to 0644. Volume is mounted into the container where container is run as a non-root user. The volume MUST have mode -rw-r--r-- and mount type set to tmpfs and the contents MUST be readable.
  99. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID, or the medium = 'Memory'.
  100. */
  101. framework.ConformanceIt("should support (non-root,0644,tmpfs) [LinuxOnly] [NodeConformance]", func() {
  102. doTest0644(f, testImageNonRootUid, v1.StorageMediumMemory)
  103. })
  104. /*
  105. Release : v1.9
  106. Testname: EmptyDir, medium memory, volume mode 0666,, non-root user
  107. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume mode set to 0666. Volume is mounted into the container where container is run as a non-root user. The volume MUST have mode -rw-rw-rw- and mount type set to tmpfs and the contents MUST be readable.
  108. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID, or the medium = 'Memory'.
  109. */
  110. framework.ConformanceIt("should support (non-root,0666,tmpfs) [LinuxOnly] [NodeConformance]", func() {
  111. doTest0666(f, testImageNonRootUid, v1.StorageMediumMemory)
  112. })
  113. /*
  114. Release : v1.9
  115. Testname: EmptyDir, medium memory, volume mode 0777, non-root user
  116. Description: A Pod created with an 'emptyDir' Volume and 'medium' as 'Memory', the volume mode set to 0777. Volume is mounted into the container where container is run as a non-root user. The volume MUST have mode -rwxrwxrwx and mount type set to tmpfs and the contents MUST be readable.
  117. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID, or the medium = 'Memory'.
  118. */
  119. framework.ConformanceIt("should support (non-root,0777,tmpfs) [LinuxOnly] [NodeConformance]", func() {
  120. doTest0777(f, testImageNonRootUid, v1.StorageMediumMemory)
  121. })
  122. /*
  123. Release : v1.9
  124. Testname: EmptyDir, medium default, volume mode default
  125. Description: A Pod created with an 'emptyDir' Volume, the volume MUST have mode set as -rwxrwxrwx and mount type set to tmpfs.
  126. This test is marked LinuxOnly since Windows does not support setting specific file permissions.
  127. */
  128. framework.ConformanceIt("volume on default medium should have the correct mode [LinuxOnly] [NodeConformance]", func() {
  129. doTestVolumeMode(f, testImageRootUid, v1.StorageMediumDefault)
  130. })
  131. /*
  132. Release : v1.9
  133. Testname: EmptyDir, medium default, volume mode 0644
  134. Description: A Pod created with an 'emptyDir' Volume, the volume mode set to 0644. The volume MUST have mode -rw-r--r-- and mount type set to tmpfs and the contents MUST be readable.
  135. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID.
  136. */
  137. framework.ConformanceIt("should support (root,0644,default) [LinuxOnly] [NodeConformance]", func() {
  138. doTest0644(f, testImageRootUid, v1.StorageMediumDefault)
  139. })
  140. /*
  141. Release : v1.9
  142. Testname: EmptyDir, medium default, volume mode 0666
  143. Description: A Pod created with an 'emptyDir' Volume, the volume mode set to 0666. The volume MUST have mode -rw-rw-rw- and mount type set to tmpfs and the contents MUST be readable.
  144. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID.
  145. */
  146. framework.ConformanceIt("should support (root,0666,default) [LinuxOnly] [NodeConformance]", func() {
  147. doTest0666(f, testImageRootUid, v1.StorageMediumDefault)
  148. })
  149. /*
  150. Release : v1.9
  151. Testname: EmptyDir, medium default, volume mode 0777
  152. Description: A Pod created with an 'emptyDir' Volume, the volume mode set to 0777. The volume MUST have mode set as -rwxrwxrwx and mount type set to tmpfs and the contents MUST be readable.
  153. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID.
  154. */
  155. framework.ConformanceIt("should support (root,0777,default) [LinuxOnly] [NodeConformance]", func() {
  156. doTest0777(f, testImageRootUid, v1.StorageMediumDefault)
  157. })
  158. /*
  159. Release : v1.9
  160. Testname: EmptyDir, medium default, volume mode 0644
  161. Description: A Pod created with an 'emptyDir' Volume, the volume mode set to 0644. Volume is mounted into the container where container is run as a non-root user. The volume MUST have mode -rw-r--r-- and mount type set to tmpfs and the contents MUST be readable.
  162. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID.
  163. */
  164. framework.ConformanceIt("should support (non-root,0644,default) [LinuxOnly] [NodeConformance]", func() {
  165. doTest0644(f, testImageNonRootUid, v1.StorageMediumDefault)
  166. })
  167. /*
  168. Release : v1.9
  169. Testname: EmptyDir, medium default, volume mode 0666
  170. Description: A Pod created with an 'emptyDir' Volume, the volume mode set to 0666. Volume is mounted into the container where container is run as a non-root user. The volume MUST have mode -rw-rw-rw- and mount type set to tmpfs and the contents MUST be readable.
  171. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID.
  172. */
  173. framework.ConformanceIt("should support (non-root,0666,default) [LinuxOnly] [NodeConformance]", func() {
  174. doTest0666(f, testImageNonRootUid, v1.StorageMediumDefault)
  175. })
  176. /*
  177. Release : v1.9
  178. Testname: EmptyDir, medium default, volume mode 0777
  179. Description: A Pod created with an 'emptyDir' Volume, the volume mode set to 0777. Volume is mounted into the container where container is run as a non-root user. The volume MUST have mode -rwxrwxrwx and mount type set to tmpfs and the contents MUST be readable.
  180. This test is marked LinuxOnly since Windows does not support setting specific file permissions, or running as UID / GID.
  181. */
  182. framework.ConformanceIt("should support (non-root,0777,default) [LinuxOnly] [NodeConformance]", func() {
  183. doTest0777(f, testImageNonRootUid, v1.StorageMediumDefault)
  184. })
  185. /*
  186. Release : v1.15
  187. Testname: EmptyDir, Shared volumes between containers
  188. Description: A Pod created with an 'emptyDir' Volume, should share volumes between the containeres in the pod. The two busybox image containers shoud share the volumes mounted to the pod.
  189. The main container shoud wait until the sub container drops a file, and main container acess the shared data.
  190. */
  191. framework.ConformanceIt("pod should support shared volumes between containers", func() {
  192. var (
  193. volumeName = "shared-data"
  194. busyBoxMainVolumeMountPath = "/usr/share/volumeshare"
  195. busyBoxSubVolumeMountPath = "/pod-data"
  196. busyBoxMainVolumeFilePath = fmt.Sprintf("%s/shareddata.txt", busyBoxMainVolumeMountPath)
  197. busyBoxSubVolumeFilePath = fmt.Sprintf("%s/shareddata.txt", busyBoxSubVolumeMountPath)
  198. message = "Hello from the busy-box sub-container"
  199. busyBoxMainContainerName = "busybox-main-container"
  200. busyBoxSubContainerName = "busybox-sub-container"
  201. resultString = ""
  202. deletionGracePeriod = int64(0)
  203. )
  204. pod := &v1.Pod{
  205. ObjectMeta: metav1.ObjectMeta{
  206. Name: "pod-sharedvolume-" + string(uuid.NewUUID()),
  207. },
  208. Spec: v1.PodSpec{
  209. Volumes: []v1.Volume{
  210. {
  211. Name: volumeName,
  212. VolumeSource: v1.VolumeSource{
  213. EmptyDir: new(v1.EmptyDirVolumeSource),
  214. },
  215. },
  216. },
  217. Containers: []v1.Container{
  218. {
  219. Name: busyBoxMainContainerName,
  220. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  221. Command: []string{"/bin/sh"},
  222. Args: []string{"-c", "sleep 100000"},
  223. VolumeMounts: []v1.VolumeMount{
  224. {
  225. Name: volumeName,
  226. MountPath: busyBoxMainVolumeMountPath,
  227. },
  228. },
  229. },
  230. {
  231. Name: busyBoxSubContainerName,
  232. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  233. Command: []string{"/bin/sh"},
  234. Args: []string{"-c", fmt.Sprintf("echo %s > %s", message, busyBoxSubVolumeFilePath)},
  235. VolumeMounts: []v1.VolumeMount{
  236. {
  237. Name: volumeName,
  238. MountPath: busyBoxSubVolumeMountPath,
  239. },
  240. },
  241. },
  242. },
  243. TerminationGracePeriodSeconds: &deletionGracePeriod,
  244. RestartPolicy: v1.RestartPolicyNever,
  245. },
  246. }
  247. var err error
  248. ginkgo.By("Creating Pod")
  249. pod = f.PodClient().CreateSync(pod)
  250. ginkgo.By("Waiting for the pod running")
  251. err = f.WaitForPodRunning(pod.Name)
  252. framework.ExpectNoError(err, "failed to deploy pod %s", pod.Name)
  253. ginkgo.By("Geting the pod")
  254. pod, err = f.PodClient().Get(context.TODO(), pod.Name, metav1.GetOptions{})
  255. framework.ExpectNoError(err, "failed to get pod %s", pod.Name)
  256. ginkgo.By("Reading file content from the nginx-container")
  257. result := f.ExecShellInContainer(pod.Name, busyBoxMainContainerName, fmt.Sprintf("cat %s", busyBoxMainVolumeFilePath))
  258. framework.ExpectEqual(result, message, "failed to match expected string %s with %s", message, resultString)
  259. })
  260. })
  261. const (
  262. containerName = "test-container"
  263. volumeName = "test-volume"
  264. )
  265. func doTestSetgidFSGroup(f *framework.Framework, image string, medium v1.StorageMedium) {
  266. var (
  267. filePath = path.Join(volumePath, "test-file")
  268. source = &v1.EmptyDirVolumeSource{Medium: medium}
  269. pod = testPodWithVolume(testImageRootUid, volumePath, source)
  270. )
  271. pod.Spec.Containers[0].Args = []string{
  272. fmt.Sprintf("--fs_type=%v", volumePath),
  273. fmt.Sprintf("--new_file_0660=%v", filePath),
  274. fmt.Sprintf("--file_perm=%v", filePath),
  275. fmt.Sprintf("--file_owner=%v", filePath),
  276. }
  277. fsGroup := int64(123)
  278. pod.Spec.SecurityContext.FSGroup = &fsGroup
  279. msg := fmt.Sprintf("emptydir 0644 on %v", formatMedium(medium))
  280. out := []string{
  281. "perms of file \"/test-volume/test-file\": -rw-rw----",
  282. "content of file \"/test-volume/test-file\": mount-tester new file",
  283. "owner GID of \"/test-volume/test-file\": 123",
  284. }
  285. if medium == v1.StorageMediumMemory {
  286. out = append(out, "mount type of \"/test-volume\": tmpfs")
  287. }
  288. f.TestContainerOutput(msg, pod, 0, out)
  289. }
  290. func doTestSubPathFSGroup(f *framework.Framework, image string, medium v1.StorageMedium) {
  291. var (
  292. subPath = "test-sub"
  293. source = &v1.EmptyDirVolumeSource{Medium: medium}
  294. pod = testPodWithVolume(image, volumePath, source)
  295. )
  296. pod.Spec.Containers[0].Args = []string{
  297. fmt.Sprintf("--fs_type=%v", volumePath),
  298. fmt.Sprintf("--file_perm=%v", volumePath),
  299. fmt.Sprintf("--file_owner=%v", volumePath),
  300. fmt.Sprintf("--file_mode=%v", volumePath),
  301. }
  302. pod.Spec.Containers[0].VolumeMounts[0].SubPath = subPath
  303. fsGroup := int64(123)
  304. pod.Spec.SecurityContext.FSGroup = &fsGroup
  305. msg := fmt.Sprintf("emptydir subpath on %v", formatMedium(medium))
  306. out := []string{
  307. "perms of file \"/test-volume\": -rwxrwxrwx",
  308. "owner UID of \"/test-volume\": 0",
  309. "owner GID of \"/test-volume\": 123",
  310. "mode of file \"/test-volume\": dgtrwxrwxrwx",
  311. }
  312. if medium == v1.StorageMediumMemory {
  313. out = append(out, "mount type of \"/test-volume\": tmpfs")
  314. }
  315. f.TestContainerOutput(msg, pod, 0, out)
  316. }
  317. func doTestVolumeModeFSGroup(f *framework.Framework, image string, medium v1.StorageMedium) {
  318. var (
  319. source = &v1.EmptyDirVolumeSource{Medium: medium}
  320. pod = testPodWithVolume(testImageRootUid, volumePath, source)
  321. )
  322. pod.Spec.Containers[0].Args = []string{
  323. fmt.Sprintf("--fs_type=%v", volumePath),
  324. fmt.Sprintf("--file_perm=%v", volumePath),
  325. }
  326. fsGroup := int64(1001)
  327. pod.Spec.SecurityContext.FSGroup = &fsGroup
  328. msg := fmt.Sprintf("emptydir volume type on %v", formatMedium(medium))
  329. out := []string{
  330. "perms of file \"/test-volume\": -rwxrwxrwx",
  331. }
  332. if medium == v1.StorageMediumMemory {
  333. out = append(out, "mount type of \"/test-volume\": tmpfs")
  334. }
  335. f.TestContainerOutput(msg, pod, 0, out)
  336. }
  337. func doTest0644FSGroup(f *framework.Framework, image string, medium v1.StorageMedium) {
  338. var (
  339. filePath = path.Join(volumePath, "test-file")
  340. source = &v1.EmptyDirVolumeSource{Medium: medium}
  341. pod = testPodWithVolume(image, volumePath, source)
  342. )
  343. pod.Spec.Containers[0].Args = []string{
  344. fmt.Sprintf("--fs_type=%v", volumePath),
  345. fmt.Sprintf("--new_file_0644=%v", filePath),
  346. fmt.Sprintf("--file_perm=%v", filePath),
  347. }
  348. fsGroup := int64(123)
  349. pod.Spec.SecurityContext.FSGroup = &fsGroup
  350. msg := fmt.Sprintf("emptydir 0644 on %v", formatMedium(medium))
  351. out := []string{
  352. "perms of file \"/test-volume/test-file\": -rw-r--r--",
  353. "content of file \"/test-volume/test-file\": mount-tester new file",
  354. }
  355. if medium == v1.StorageMediumMemory {
  356. out = append(out, "mount type of \"/test-volume\": tmpfs")
  357. }
  358. f.TestContainerOutput(msg, pod, 0, out)
  359. }
  360. func doTestVolumeMode(f *framework.Framework, image string, medium v1.StorageMedium) {
  361. var (
  362. source = &v1.EmptyDirVolumeSource{Medium: medium}
  363. pod = testPodWithVolume(testImageRootUid, volumePath, source)
  364. )
  365. pod.Spec.Containers[0].Args = []string{
  366. fmt.Sprintf("--fs_type=%v", volumePath),
  367. fmt.Sprintf("--file_perm=%v", volumePath),
  368. }
  369. msg := fmt.Sprintf("emptydir volume type on %v", formatMedium(medium))
  370. out := []string{
  371. "perms of file \"/test-volume\": -rwxrwxrwx",
  372. }
  373. if medium == v1.StorageMediumMemory {
  374. out = append(out, "mount type of \"/test-volume\": tmpfs")
  375. }
  376. f.TestContainerOutput(msg, pod, 0, out)
  377. }
  378. func doTest0644(f *framework.Framework, image string, medium v1.StorageMedium) {
  379. var (
  380. filePath = path.Join(volumePath, "test-file")
  381. source = &v1.EmptyDirVolumeSource{Medium: medium}
  382. pod = testPodWithVolume(image, volumePath, source)
  383. )
  384. pod.Spec.Containers[0].Args = []string{
  385. fmt.Sprintf("--fs_type=%v", volumePath),
  386. fmt.Sprintf("--new_file_0644=%v", filePath),
  387. fmt.Sprintf("--file_perm=%v", filePath),
  388. }
  389. msg := fmt.Sprintf("emptydir 0644 on %v", formatMedium(medium))
  390. out := []string{
  391. "perms of file \"/test-volume/test-file\": -rw-r--r--",
  392. "content of file \"/test-volume/test-file\": mount-tester new file",
  393. }
  394. if medium == v1.StorageMediumMemory {
  395. out = append(out, "mount type of \"/test-volume\": tmpfs")
  396. }
  397. f.TestContainerOutput(msg, pod, 0, out)
  398. }
  399. func doTest0666(f *framework.Framework, image string, medium v1.StorageMedium) {
  400. var (
  401. filePath = path.Join(volumePath, "test-file")
  402. source = &v1.EmptyDirVolumeSource{Medium: medium}
  403. pod = testPodWithVolume(image, volumePath, source)
  404. )
  405. pod.Spec.Containers[0].Args = []string{
  406. fmt.Sprintf("--fs_type=%v", volumePath),
  407. fmt.Sprintf("--new_file_0666=%v", filePath),
  408. fmt.Sprintf("--file_perm=%v", filePath),
  409. }
  410. msg := fmt.Sprintf("emptydir 0666 on %v", formatMedium(medium))
  411. out := []string{
  412. "perms of file \"/test-volume/test-file\": -rw-rw-rw-",
  413. "content of file \"/test-volume/test-file\": mount-tester new file",
  414. }
  415. if medium == v1.StorageMediumMemory {
  416. out = append(out, "mount type of \"/test-volume\": tmpfs")
  417. }
  418. f.TestContainerOutput(msg, pod, 0, out)
  419. }
  420. func doTest0777(f *framework.Framework, image string, medium v1.StorageMedium) {
  421. var (
  422. filePath = path.Join(volumePath, "test-file")
  423. source = &v1.EmptyDirVolumeSource{Medium: medium}
  424. pod = testPodWithVolume(image, volumePath, source)
  425. )
  426. pod.Spec.Containers[0].Args = []string{
  427. fmt.Sprintf("--fs_type=%v", volumePath),
  428. fmt.Sprintf("--new_file_0777=%v", filePath),
  429. fmt.Sprintf("--file_perm=%v", filePath),
  430. }
  431. msg := fmt.Sprintf("emptydir 0777 on %v", formatMedium(medium))
  432. out := []string{
  433. "perms of file \"/test-volume/test-file\": -rwxrwxrwx",
  434. "content of file \"/test-volume/test-file\": mount-tester new file",
  435. }
  436. if medium == v1.StorageMediumMemory {
  437. out = append(out, "mount type of \"/test-volume\": tmpfs")
  438. }
  439. f.TestContainerOutput(msg, pod, 0, out)
  440. }
  441. func formatMedium(medium v1.StorageMedium) string {
  442. if medium == v1.StorageMediumMemory {
  443. return "tmpfs"
  444. }
  445. return "node default medium"
  446. }
  447. func testPodWithVolume(image, path string, source *v1.EmptyDirVolumeSource) *v1.Pod {
  448. podName := "pod-" + string(uuid.NewUUID())
  449. return &v1.Pod{
  450. TypeMeta: metav1.TypeMeta{
  451. Kind: "Pod",
  452. APIVersion: "v1",
  453. },
  454. ObjectMeta: metav1.ObjectMeta{
  455. Name: podName,
  456. },
  457. Spec: v1.PodSpec{
  458. Containers: []v1.Container{
  459. {
  460. Name: containerName,
  461. Image: image,
  462. VolumeMounts: []v1.VolumeMount{
  463. {
  464. Name: volumeName,
  465. MountPath: path,
  466. },
  467. },
  468. },
  469. },
  470. SecurityContext: &v1.PodSecurityContext{
  471. SELinuxOptions: &v1.SELinuxOptions{
  472. Level: "s0",
  473. },
  474. },
  475. RestartPolicy: v1.RestartPolicyNever,
  476. Volumes: []v1.Volume{
  477. {
  478. Name: volumeName,
  479. VolumeSource: v1.VolumeSource{
  480. EmptyDir: source,
  481. },
  482. },
  483. },
  484. },
  485. }
  486. }