volumes.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. package windows
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/uuid"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  21. imageutils "k8s.io/kubernetes/test/utils/image"
  22. "github.com/onsi/ginkgo"
  23. )
  24. const (
  25. emptyDirVolumePath = "C:\\test-volume"
  26. hostMapPath = "C:\\tmp"
  27. containerName = "test-container"
  28. volumeName = "test-volume"
  29. )
  30. var (
  31. image = imageutils.GetE2EImage(imageutils.Pause)
  32. )
  33. var _ = SIGDescribe("Windows volume mounts ", func() {
  34. f := framework.NewDefaultFramework("windows-volumes")
  35. var (
  36. emptyDirSource = v1.VolumeSource{
  37. EmptyDir: &v1.EmptyDirVolumeSource{
  38. Medium: v1.StorageMediumDefault,
  39. },
  40. }
  41. hostPathDirectoryOrCreate = v1.HostPathDirectoryOrCreate
  42. hostMapSource = v1.VolumeSource{
  43. HostPath: &v1.HostPathVolumeSource{
  44. Path: hostMapPath,
  45. Type: &hostPathDirectoryOrCreate,
  46. },
  47. }
  48. )
  49. ginkgo.BeforeEach(func() {
  50. e2eskipper.SkipUnlessNodeOSDistroIs("windows")
  51. })
  52. ginkgo.Context("check volume mount permissions", func() {
  53. ginkgo.It("container should have readOnly permissions on emptyDir", func() {
  54. ginkgo.By("creating a container with readOnly permissions on emptyDir volume")
  55. doReadOnlyTest(f, emptyDirSource, emptyDirVolumePath)
  56. ginkgo.By("creating two containers, one with readOnly permissions the other with read-write permissions on emptyDir volume")
  57. doReadWriteReadOnlyTest(f, emptyDirSource, emptyDirVolumePath)
  58. })
  59. ginkgo.It("container should have readOnly permissions on hostMapPath", func() {
  60. ginkgo.By("creating a container with readOnly permissions on hostMap volume")
  61. doReadOnlyTest(f, hostMapSource, hostMapPath)
  62. ginkgo.By("creating two containers, one with readOnly permissions the other with read-write permissions on hostMap volume")
  63. doReadWriteReadOnlyTest(f, hostMapSource, hostMapPath)
  64. })
  65. })
  66. })
  67. func doReadOnlyTest(f *framework.Framework, source v1.VolumeSource, volumePath string) {
  68. var (
  69. filePath = volumePath + "\\test-file.txt"
  70. podName = "pod-" + string(uuid.NewUUID())
  71. pod = testPodWithROVolume(podName, source, volumePath)
  72. )
  73. f.PodClient().CreateSync(pod)
  74. cmd := []string{"cmd", "/c", "echo windows-volume-test", ">", filePath}
  75. _, stderr, _ := f.ExecCommandInContainerWithFullOutput(podName, containerName, cmd...)
  76. framework.ExpectEqual(stderr, "Access is denied.")
  77. }
  78. func doReadWriteReadOnlyTest(f *framework.Framework, source v1.VolumeSource, volumePath string) {
  79. var (
  80. filePath = volumePath + "\\test-file"
  81. podName = "pod-" + string(uuid.NewUUID())
  82. pod = testPodWithROVolume(podName, source, volumePath)
  83. rwcontainerName = containerName + "-rw"
  84. )
  85. rwcontainer := v1.Container{
  86. Name: containerName + "-rw",
  87. Image: image,
  88. VolumeMounts: []v1.VolumeMount{
  89. {
  90. Name: volumeName,
  91. MountPath: volumePath,
  92. },
  93. },
  94. }
  95. pod.Spec.Containers = append(pod.Spec.Containers, rwcontainer)
  96. f.PodClient().CreateSync(pod)
  97. cmd := []string{"cmd", "/c", "echo windows-volume-test", ">", filePath}
  98. stdoutRW, stderrRW, errRW := f.ExecCommandInContainerWithFullOutput(podName, rwcontainerName, cmd...)
  99. msg := fmt.Sprintf("cmd: %v, stdout: %q, stderr: %q", cmd, stdoutRW, stderrRW)
  100. framework.ExpectNoError(errRW, msg)
  101. _, stderr, _ := f.ExecCommandInContainerWithFullOutput(podName, containerName, cmd...)
  102. framework.ExpectEqual(stderr, "Access is denied.")
  103. readcmd := []string{"cmd", "/c", "type", filePath}
  104. readout, readerr, err := f.ExecCommandInContainerWithFullOutput(podName, containerName, readcmd...)
  105. readmsg := fmt.Sprintf("cmd: %v, stdout: %q, stderr: %q", readcmd, readout, readerr)
  106. framework.ExpectEqual(readout, "windows-volume-test")
  107. framework.ExpectNoError(err, readmsg)
  108. }
  109. func testPodWithROVolume(podName string, source v1.VolumeSource, path string) *v1.Pod {
  110. return &v1.Pod{
  111. TypeMeta: metav1.TypeMeta{
  112. Kind: "Pod",
  113. APIVersion: "v1",
  114. },
  115. ObjectMeta: metav1.ObjectMeta{
  116. Name: podName,
  117. },
  118. Spec: v1.PodSpec{
  119. Containers: []v1.Container{
  120. {
  121. Name: containerName,
  122. Image: image,
  123. VolumeMounts: []v1.VolumeMount{
  124. {
  125. Name: volumeName,
  126. MountPath: path,
  127. ReadOnly: true,
  128. },
  129. },
  130. },
  131. },
  132. RestartPolicy: v1.RestartPolicyNever,
  133. Volumes: []v1.Volume{
  134. {
  135. Name: volumeName,
  136. VolumeSource: source,
  137. },
  138. },
  139. },
  140. }
  141. }