mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #71892 from fatsheep9146/log_path_test
add test for CRI log path
This commit is contained in:
commit
dced58dca9
@ -17,28 +17,94 @@ limitations under the License.
|
|||||||
package e2e_node
|
package e2e_node
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/uuid"
|
||||||
"k8s.io/kubernetes/pkg/kubelet"
|
"k8s.io/kubernetes/pkg/kubelet"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"k8s.io/kubernetes/test/e2e/framework"
|
"k8s.io/kubernetes/test/e2e/framework"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
logString = "This is the expected log content of this node e2e test"
|
logString = "This is the expected log content of this node e2e test"
|
||||||
|
logContainerName = "logger"
|
||||||
logPodName = "logger-pod"
|
|
||||||
logContName = "logger-container"
|
|
||||||
checkPodName = "checker-pod"
|
|
||||||
checkContName = "checker-container"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = framework.KubeDescribe("ContainerLogPath [NodeConformance]", func() {
|
var _ = framework.KubeDescribe("ContainerLogPath [NodeConformance]", func() {
|
||||||
f := framework.NewDefaultFramework("kubelet-container-log-path")
|
f := framework.NewDefaultFramework("kubelet-container-log-path")
|
||||||
|
var podClient *framework.PodClient
|
||||||
|
|
||||||
Describe("Pod with a container", func() {
|
Describe("Pod with a container", func() {
|
||||||
Context("printed log to stdout", func() {
|
Context("printed log to stdout", func() {
|
||||||
|
makeLogPod := func(podName, log string) *v1.Pod {
|
||||||
|
return &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: podName,
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{
|
||||||
|
// this pod is expected to exit successfully
|
||||||
|
RestartPolicy: v1.RestartPolicyNever,
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{
|
||||||
|
Image: busyboxImage,
|
||||||
|
Name: logContainerName,
|
||||||
|
Command: []string{"sh", "-c", "echo " + log},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
makeLogCheckPod := func(podName, log, expectedLogPath string) *v1.Pod {
|
||||||
|
hostPathType := new(v1.HostPathType)
|
||||||
|
*hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate))
|
||||||
|
|
||||||
|
return &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: podName,
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{
|
||||||
|
// this pod is expected to exit successfully
|
||||||
|
RestartPolicy: v1.RestartPolicyNever,
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{
|
||||||
|
Image: busyboxImage,
|
||||||
|
Name: podName,
|
||||||
|
// If we find expected log file and contains right content, exit 0
|
||||||
|
// else, keep checking until test timeout
|
||||||
|
Command: []string{"sh", "-c", "while true; do if [ -e " + expectedLogPath + " ] && grep -q " + log + " " + expectedLogPath + "; then exit 0; fi; sleep 1; done"},
|
||||||
|
VolumeMounts: []v1.VolumeMount{
|
||||||
|
{
|
||||||
|
Name: "logdir",
|
||||||
|
// mount ContainerLogsDir to the same path in container
|
||||||
|
MountPath: expectedLogPath,
|
||||||
|
ReadOnly: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Volumes: []v1.Volume{
|
||||||
|
{
|
||||||
|
Name: "logdir",
|
||||||
|
VolumeSource: v1.VolumeSource{
|
||||||
|
HostPath: &v1.HostPathVolumeSource{
|
||||||
|
Path: expectedLogPath,
|
||||||
|
Type: hostPathType,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createAndWaitPod := func(pod *v1.Pod) error {
|
||||||
|
podClient.Create(pod)
|
||||||
|
return framework.WaitForPodSuccessInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
var logPodName string
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
if framework.TestContext.ContainerRuntime == "docker" {
|
if framework.TestContext.ContainerRuntime == "docker" {
|
||||||
// Container Log Path support requires JSON logging driver.
|
// Container Log Path support requires JSON logging driver.
|
||||||
@ -62,86 +128,43 @@ var _ = framework.KubeDescribe("ContainerLogPath [NodeConformance]", func() {
|
|||||||
framework.Skipf("Skipping because Docker daemon is running with SELinux support enabled")
|
framework.Skipf("Skipping because Docker daemon is running with SELinux support enabled")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
podClient = f.PodClient()
|
||||||
|
logPodName = "log-pod-" + string(uuid.NewUUID())
|
||||||
|
err := createAndWaitPod(makeLogPod(logPodName, logString))
|
||||||
|
framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logPodName)
|
||||||
})
|
})
|
||||||
It("should print log to correct log path", func() {
|
It("should print log to correct log path", func() {
|
||||||
podClient := f.PodClient()
|
|
||||||
ns := f.Namespace.Name
|
|
||||||
|
|
||||||
logDirVolumeName := "log-dir-vol"
|
|
||||||
logDir := kubelet.ContainerLogsDir
|
logDir := kubelet.ContainerLogsDir
|
||||||
|
|
||||||
logPod := &v1.Pod{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: logPodName,
|
|
||||||
},
|
|
||||||
Spec: v1.PodSpec{
|
|
||||||
// this pod is expected to exit successfully
|
|
||||||
RestartPolicy: v1.RestartPolicyNever,
|
|
||||||
Containers: []v1.Container{
|
|
||||||
{
|
|
||||||
Image: busyboxImage,
|
|
||||||
Name: logContName,
|
|
||||||
Command: []string{"sh", "-c", "echo " + logString},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
podClient.Create(logPod)
|
|
||||||
err := framework.WaitForPodSuccessInNamespace(f.ClientSet, logPodName, ns)
|
|
||||||
framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logPodName)
|
|
||||||
|
|
||||||
// get containerID from created Pod
|
// get containerID from created Pod
|
||||||
createdLogPod, err := podClient.Get(logPodName, metav1.GetOptions{})
|
createdLogPod, err := podClient.Get(logPodName, metav1.GetOptions{})
|
||||||
logConID := kubecontainer.ParseContainerID(createdLogPod.Status.ContainerStatuses[0].ContainerID)
|
logContainerID := kubecontainer.ParseContainerID(createdLogPod.Status.ContainerStatuses[0].ContainerID)
|
||||||
framework.ExpectNoError(err, "Failed to get pod: %s", logPodName)
|
framework.ExpectNoError(err, "Failed to get pod: %s", logPodName)
|
||||||
|
|
||||||
expectedlogFile := logDir + "/" + logPodName + "_" + ns + "_" + logContName + "-" + logConID.ID + ".log"
|
// build log file path
|
||||||
|
expectedlogFile := logDir + "/" + logPodName + "_" + f.Namespace.Name + "_" + logContainerName + "-" + logContainerID.ID + ".log"
|
||||||
|
|
||||||
hostPathType := new(v1.HostPathType)
|
logCheckPodName := "log-check-" + string(uuid.NewUUID())
|
||||||
*hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate))
|
err = createAndWaitPod(makeLogCheckPod(logCheckPodName, logString, expectedlogFile))
|
||||||
|
framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logCheckPodName)
|
||||||
|
})
|
||||||
|
|
||||||
checkPod := &v1.Pod{
|
It("should print log to correct cri log path", func() {
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: checkPodName,
|
|
||||||
},
|
|
||||||
Spec: v1.PodSpec{
|
|
||||||
// this pod is expected to exit successfully
|
|
||||||
RestartPolicy: v1.RestartPolicyNever,
|
|
||||||
Containers: []v1.Container{
|
|
||||||
{
|
|
||||||
Image: busyboxImage,
|
|
||||||
Name: checkContName,
|
|
||||||
// If we find expected log file and contains right content, exit 0
|
|
||||||
// else, keep checking until test timeout
|
|
||||||
Command: []string{"sh", "-c", "while true; do if [ -e " + expectedlogFile + " ] && grep -q " + logString + " " + expectedlogFile + "; then exit 0; fi; sleep 1; done"},
|
|
||||||
VolumeMounts: []v1.VolumeMount{
|
|
||||||
{
|
|
||||||
Name: logDirVolumeName,
|
|
||||||
// mount ContainerLogsDir to the same path in container
|
|
||||||
MountPath: expectedlogFile,
|
|
||||||
ReadOnly: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Volumes: []v1.Volume{
|
|
||||||
{
|
|
||||||
Name: logDirVolumeName,
|
|
||||||
VolumeSource: v1.VolumeSource{
|
|
||||||
HostPath: &v1.HostPathVolumeSource{
|
|
||||||
Path: expectedlogFile,
|
|
||||||
Type: hostPathType,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
podClient.Create(checkPod)
|
logCRIDir := "/var/log/pods"
|
||||||
err = framework.WaitForPodSuccessInNamespace(f.ClientSet, checkPodName, ns)
|
|
||||||
framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", checkPodName)
|
// get podID from created Pod
|
||||||
|
createdLogPod, err := podClient.Get(logPodName, metav1.GetOptions{})
|
||||||
|
podID := string(createdLogPod.UID)
|
||||||
|
|
||||||
|
// build log cri file path
|
||||||
|
expectedCRILogFile := logCRIDir + "/" + podID + "/" + logContainerName + "/0.log"
|
||||||
|
|
||||||
|
logCRICheckPodName := "log-cri-check-" + string(uuid.NewUUID())
|
||||||
|
err = createAndWaitPod(makeLogCheckPod(logCRICheckPodName, logString, expectedCRILogFile))
|
||||||
|
framework.ExpectNoError(err, "Failed waiting for pod: %s to enter success state", logCRICheckPodName)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user