log_path_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 e2e_node
  14. import (
  15. . "github.com/onsi/ginkgo"
  16. "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/pkg/kubelet"
  20. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. )
  23. const (
  24. logString = "This is the expected log content of this node e2e test"
  25. logContainerName = "logger"
  26. )
  27. var _ = framework.KubeDescribe("ContainerLogPath [NodeConformance]", func() {
  28. f := framework.NewDefaultFramework("kubelet-container-log-path")
  29. var podClient *framework.PodClient
  30. Describe("Pod with a container", func() {
  31. Context("printed log to stdout", func() {
  32. makeLogPod := func(podName, log string) *v1.Pod {
  33. return &v1.Pod{
  34. ObjectMeta: metav1.ObjectMeta{
  35. Name: podName,
  36. },
  37. Spec: v1.PodSpec{
  38. // this pod is expected to exit successfully
  39. RestartPolicy: v1.RestartPolicyNever,
  40. Containers: []v1.Container{
  41. {
  42. Image: busyboxImage,
  43. Name: logContainerName,
  44. Command: []string{"sh", "-c", "echo " + log},
  45. },
  46. },
  47. },
  48. }
  49. }
  50. makeLogCheckPod := func(podName, log, expectedLogPath string) *v1.Pod {
  51. hostPathType := new(v1.HostPathType)
  52. *hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate))
  53. return &v1.Pod{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Name: podName,
  56. },
  57. Spec: v1.PodSpec{
  58. // this pod is expected to exit successfully
  59. RestartPolicy: v1.RestartPolicyNever,
  60. Containers: []v1.Container{
  61. {
  62. Image: busyboxImage,
  63. Name: podName,
  64. // If we find expected log file and contains right content, exit 0
  65. // else, keep checking until test timeout
  66. Command: []string{"sh", "-c", "while true; do if [ -e " + expectedLogPath + " ] && grep -q " + log + " " + expectedLogPath + "; then exit 0; fi; sleep 1; done"},
  67. VolumeMounts: []v1.VolumeMount{
  68. {
  69. Name: "logdir",
  70. // mount ContainerLogsDir to the same path in container
  71. MountPath: expectedLogPath,
  72. ReadOnly: true,
  73. },
  74. },
  75. },
  76. },
  77. Volumes: []v1.Volume{
  78. {
  79. Name: "logdir",
  80. VolumeSource: v1.VolumeSource{
  81. HostPath: &v1.HostPathVolumeSource{
  82. Path: expectedLogPath,
  83. Type: hostPathType,
  84. },
  85. },
  86. },
  87. },
  88. },
  89. }
  90. }
  91. createAndWaitPod := func(pod *v1.Pod) error {
  92. podClient.Create(pod)
  93. return framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
  94. }
  95. var logPodName string
  96. BeforeEach(func() {
  97. if framework.TestContext.ContainerRuntime == "docker" {
  98. // Container Log Path support requires JSON logging driver.
  99. // It does not work when Docker daemon is logging to journald.
  100. d, err := getDockerLoggingDriver()
  101. framework.ExpectNoError(err)
  102. if d != "json-file" {
  103. framework.Skipf("Skipping because Docker daemon is using a logging driver other than \"json-file\": %s", d)
  104. }
  105. // Even if JSON logging is in use, this test fails if SELinux support
  106. // is enabled, since the isolation provided by the SELinux policy
  107. // prevents processes running inside Docker containers (under SELinux
  108. // type svirt_lxc_net_t) from accessing the log files which are owned
  109. // by Docker (and labeled with the container_var_lib_t type.)
  110. //
  111. // Therefore, let's also skip this test when running with SELinux
  112. // support enabled.
  113. e, err := isDockerSELinuxSupportEnabled()
  114. framework.ExpectNoError(err)
  115. if e {
  116. framework.Skipf("Skipping because Docker daemon is running with SELinux support enabled")
  117. }
  118. }
  119. podClient = f.PodClient()
  120. logPodName = "log-pod-" + string(uuid.NewUUID())
  121. err := createAndWaitPod(makeLogPod(logPodName, logString))
  122. framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logPodName)
  123. })
  124. It("should print log to correct log path", func() {
  125. logDir := kubelet.ContainerLogsDir
  126. // get containerID from created Pod
  127. createdLogPod, err := podClient.Get(logPodName, metav1.GetOptions{})
  128. logContainerID := kubecontainer.ParseContainerID(createdLogPod.Status.ContainerStatuses[0].ContainerID)
  129. framework.ExpectNoError(err, "Failed to get pod: %s", logPodName)
  130. // build log file path
  131. expectedlogFile := logDir + "/" + logPodName + "_" + f.Namespace.Name + "_" + logContainerName + "-" + logContainerID.ID + ".log"
  132. logCheckPodName := "log-check-" + string(uuid.NewUUID())
  133. err = createAndWaitPod(makeLogCheckPod(logCheckPodName, logString, expectedlogFile))
  134. framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logCheckPodName)
  135. })
  136. It("should print log to correct cri log path", func() {
  137. logCRIDir := "/var/log/pods"
  138. // get podID from created Pod
  139. createdLogPod, err := podClient.Get(logPodName, metav1.GetOptions{})
  140. podNs := createdLogPod.Namespace
  141. podName := createdLogPod.Name
  142. podID := string(createdLogPod.UID)
  143. // build log cri file path
  144. expectedCRILogFile := logCRIDir + "/" + podNs + "_" + podName + "_" + podID + "/" + logContainerName + "/0.log"
  145. logCRICheckPodName := "log-cri-check-" + string(uuid.NewUUID())
  146. err = createAndWaitPod(makeLogCheckPod(logCRICheckPodName, logString, expectedCRILogFile))
  147. framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logCRICheckPodName)
  148. })
  149. })
  150. })
  151. })