kubelet.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. "bytes"
  16. "context"
  17. "fmt"
  18. "strings"
  19. "time"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/util/uuid"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. "github.com/onsi/ginkgo"
  25. "github.com/onsi/gomega"
  26. )
  27. var _ = framework.KubeDescribe("Kubelet", func() {
  28. f := framework.NewDefaultFramework("kubelet-test")
  29. var podClient *framework.PodClient
  30. ginkgo.BeforeEach(func() {
  31. podClient = f.PodClient()
  32. })
  33. ginkgo.Context("when scheduling a busybox command in a pod", func() {
  34. podName := "busybox-scheduling-" + string(uuid.NewUUID())
  35. /*
  36. Release : v1.13
  37. Testname: Kubelet, log output, default
  38. Description: By default the stdout and stderr from the process being executed in a pod MUST be sent to the pod's logs.
  39. */
  40. framework.ConformanceIt("should print the output to logs [NodeConformance]", func() {
  41. podClient.CreateSync(&v1.Pod{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: podName,
  44. },
  45. Spec: v1.PodSpec{
  46. // Don't restart the Pod since it is expected to exit
  47. RestartPolicy: v1.RestartPolicyNever,
  48. Containers: []v1.Container{
  49. {
  50. Image: framework.BusyBoxImage,
  51. Name: podName,
  52. Command: []string{"sh", "-c", "echo 'Hello World' ; sleep 240"},
  53. },
  54. },
  55. },
  56. })
  57. gomega.Eventually(func() string {
  58. sinceTime := metav1.NewTime(time.Now().Add(time.Duration(-1 * time.Hour)))
  59. rc, err := podClient.GetLogs(podName, &v1.PodLogOptions{SinceTime: &sinceTime}).Stream(context.TODO())
  60. if err != nil {
  61. return ""
  62. }
  63. defer rc.Close()
  64. buf := new(bytes.Buffer)
  65. buf.ReadFrom(rc)
  66. return buf.String()
  67. }, time.Minute, time.Second*4).Should(gomega.Equal("Hello World\n"))
  68. })
  69. })
  70. ginkgo.Context("when scheduling a busybox command that always fails in a pod", func() {
  71. var podName string
  72. ginkgo.BeforeEach(func() {
  73. podName = "bin-false" + string(uuid.NewUUID())
  74. podClient.Create(&v1.Pod{
  75. ObjectMeta: metav1.ObjectMeta{
  76. Name: podName,
  77. },
  78. Spec: v1.PodSpec{
  79. // Don't restart the Pod since it is expected to exit
  80. RestartPolicy: v1.RestartPolicyNever,
  81. Containers: []v1.Container{
  82. {
  83. Image: framework.BusyBoxImage,
  84. Name: podName,
  85. Command: []string{"/bin/false"},
  86. },
  87. },
  88. },
  89. })
  90. })
  91. /*
  92. Release : v1.13
  93. Testname: Kubelet, failed pod, terminated reason
  94. Description: Create a Pod with terminated state. Pod MUST have only one container. Container MUST be in terminated state and MUST have an terminated reason.
  95. */
  96. framework.ConformanceIt("should have an terminated reason [NodeConformance]", func() {
  97. gomega.Eventually(func() error {
  98. podData, err := podClient.Get(context.TODO(), podName, metav1.GetOptions{})
  99. if err != nil {
  100. return err
  101. }
  102. if len(podData.Status.ContainerStatuses) != 1 {
  103. return fmt.Errorf("expected only one container in the pod %q", podName)
  104. }
  105. contTerminatedState := podData.Status.ContainerStatuses[0].State.Terminated
  106. if contTerminatedState == nil {
  107. return fmt.Errorf("expected state to be terminated. Got pod status: %+v", podData.Status)
  108. }
  109. if contTerminatedState.ExitCode == 0 || contTerminatedState.Reason == "" {
  110. return fmt.Errorf("expected non-zero exitCode and non-empty terminated state reason. Got exitCode: %+v and terminated state reason: %+v", contTerminatedState.ExitCode, contTerminatedState.Reason)
  111. }
  112. return nil
  113. }, framework.PodStartTimeout, time.Second*4).Should(gomega.BeNil())
  114. })
  115. /*
  116. Release : v1.13
  117. Testname: Kubelet, failed pod, delete
  118. Description: Create a Pod with terminated state. This terminated pod MUST be able to be deleted.
  119. */
  120. framework.ConformanceIt("should be possible to delete [NodeConformance]", func() {
  121. err := podClient.Delete(context.TODO(), podName, &metav1.DeleteOptions{})
  122. gomega.Expect(err).To(gomega.BeNil(), fmt.Sprintf("Error deleting Pod %v", err))
  123. })
  124. })
  125. ginkgo.Context("when scheduling a busybox Pod with hostAliases", func() {
  126. podName := "busybox-host-aliases" + string(uuid.NewUUID())
  127. /*
  128. Release : v1.13
  129. Testname: Kubelet, hostAliases
  130. Description: Create a Pod with hostAliases and a container with command to output /etc/hosts entries. Pod's logs MUST have matching entries of specified hostAliases to the output of /etc/hosts entries.
  131. Kubernetes mounts the /etc/hosts file into its containers, however, mounting individual files is not supported on Windows Containers. For this reason, this test is marked LinuxOnly.
  132. */
  133. framework.ConformanceIt("should write entries to /etc/hosts [LinuxOnly] [NodeConformance]", func() {
  134. podClient.CreateSync(&v1.Pod{
  135. ObjectMeta: metav1.ObjectMeta{
  136. Name: podName,
  137. },
  138. Spec: v1.PodSpec{
  139. // Don't restart the Pod since it is expected to exit
  140. RestartPolicy: v1.RestartPolicyNever,
  141. Containers: []v1.Container{
  142. {
  143. Image: framework.BusyBoxImage,
  144. Name: podName,
  145. Command: []string{"/bin/sh", "-c", "cat /etc/hosts; sleep 6000"},
  146. },
  147. },
  148. HostAliases: []v1.HostAlias{
  149. {
  150. IP: "123.45.67.89",
  151. Hostnames: []string{"foo", "bar"},
  152. },
  153. },
  154. },
  155. })
  156. gomega.Eventually(func() error {
  157. rc, err := podClient.GetLogs(podName, &v1.PodLogOptions{}).Stream(context.TODO())
  158. if err != nil {
  159. return err
  160. }
  161. defer rc.Close()
  162. buf := new(bytes.Buffer)
  163. buf.ReadFrom(rc)
  164. hostsFileContent := buf.String()
  165. if !strings.Contains(hostsFileContent, "123.45.67.89\tfoo\tbar") {
  166. return fmt.Errorf("expected hosts file to contain entries from HostAliases. Got:\n%+v", hostsFileContent)
  167. }
  168. return nil
  169. }, time.Minute, time.Second*4).Should(gomega.BeNil())
  170. })
  171. })
  172. ginkgo.Context("when scheduling a read only busybox container", func() {
  173. podName := "busybox-readonly-fs" + string(uuid.NewUUID())
  174. /*
  175. Release : v1.13
  176. Testname: Kubelet, pod with read only root file system
  177. Description: Create a Pod with security context set with ReadOnlyRootFileSystem set to true. The Pod then tries to write to the /file on the root, write operation to the root filesystem MUST fail as expected.
  178. This test is marked LinuxOnly since Windows does not support creating containers with read-only access.
  179. */
  180. framework.ConformanceIt("should not write to root filesystem [LinuxOnly] [NodeConformance]", func() {
  181. isReadOnly := true
  182. podClient.CreateSync(&v1.Pod{
  183. ObjectMeta: metav1.ObjectMeta{
  184. Name: podName,
  185. },
  186. Spec: v1.PodSpec{
  187. // Don't restart the Pod since it is expected to exit
  188. RestartPolicy: v1.RestartPolicyNever,
  189. Containers: []v1.Container{
  190. {
  191. Image: framework.BusyBoxImage,
  192. Name: podName,
  193. Command: []string{"/bin/sh", "-c", "echo test > /file; sleep 240"},
  194. SecurityContext: &v1.SecurityContext{
  195. ReadOnlyRootFilesystem: &isReadOnly,
  196. },
  197. },
  198. },
  199. },
  200. })
  201. gomega.Eventually(func() string {
  202. rc, err := podClient.GetLogs(podName, &v1.PodLogOptions{}).Stream(context.TODO())
  203. if err != nil {
  204. return ""
  205. }
  206. defer rc.Close()
  207. buf := new(bytes.Buffer)
  208. buf.ReadFrom(rc)
  209. return buf.String()
  210. }, time.Minute, time.Second*4).Should(gomega.Equal("/bin/sh: can't create /file: Read-only file system\n"))
  211. })
  212. })
  213. })