From 9829dc52c19d0ff826282f351ed1f7ec7bddba51 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Wed, 18 Dec 2019 14:11:20 +0530 Subject: [PATCH] Enable logging in E2E if test fails source: https://github.com/kubernetes/kubernetes/blob/master/test/e2e/framework/kubectl/kubectl_utils.go kubectlLogPod function is not exposed in above code so copied it. Signed-off-by: Madhu Rajanna (cherry picked from commit 7d15992769469e25802ab467fa1a0525caa1f972) --- e2e/cephfs.go | 8 ++++++ e2e/log.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ e2e/rbd.go | 8 ++++++ 3 files changed, 91 insertions(+) create mode 100644 e2e/log.go diff --git a/e2e/cephfs.go b/e2e/cephfs.go index 9cf788121..ddb6e37df 100644 --- a/e2e/cephfs.go +++ b/e2e/cephfs.go @@ -57,8 +57,10 @@ func deleteCephfsPlugin() { var _ = Describe("cephfs", func() { f := framework.NewDefaultFramework("cephfs") + var c clientset.Interface // deploy cephfs CSI BeforeEach(func() { + c = f.ClientSet updateCephfsDirPath(f.ClientSet) createFileSystem(f.ClientSet) createConfigMap(cephfsDirPath, f.ClientSet, f) @@ -67,6 +69,12 @@ var _ = Describe("cephfs", func() { }) AfterEach(func() { + if CurrentGinkgoTestDescription().Failed { + // log provisoner + logsCSIPods("app=csi-cephfsplugin-provisioner", c) + // log node plugin + logsCSIPods("app=csi-cephfsplugin", c) + } deleteCephfsPlugin() deleteConfiMap(cephfsDirPath) deleteResource(cephfsExamplePath + "secret.yaml") diff --git a/e2e/log.go b/e2e/log.go new file mode 100644 index 000000000..c16f39f0a --- /dev/null +++ b/e2e/log.go @@ -0,0 +1,75 @@ +/* +Copyright 2019 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package e2e + +import ( + "fmt" + "strconv" + "strings" + + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clientset "k8s.io/client-go/kubernetes" + "k8s.io/kubernetes/test/e2e/framework" + e2elog "k8s.io/kubernetes/test/e2e/framework/log" +) + +func logsCSIPods(label string, c clientset.Interface) { + ns := "default" + opt := metav1.ListOptions{ + LabelSelector: label, + } + podList, err := c.CoreV1().Pods(ns).List(opt) + if err != nil { + e2elog.Logf("failed to list pods with selector %s %v", label, err) + return + } + + for i := range podList.Items { + kubectlLogPod(c, &podList.Items[i]) + } +} + +// source: https://github.com/kubernetes/kubernetes/blob/master/test/e2e/framework/kubectl/kubectl_utils.go +func kubectlLogPod(c clientset.Interface, pod *v1.Pod) { + container := pod.Spec.Containers + for i := range container { + logs, err := framework.GetPodLogs(c, pod.Namespace, pod.Name, container[i].Name) + if err != nil { + logs, err = getPreviousPodLogs(c, pod.Namespace, pod.Name, container[i].Name) + if err != nil { + e2elog.Logf("Failed to get logs of pod %v, container %v, err: %v", pod.Name, container[i].Name, err) + } + } + e2elog.Logf("Logs of %v/%v:%v on node %v\n", pod.Namespace, pod.Name, container[i].Name, pod.Spec.NodeName) + e2elog.Logf("STARTLOG\n\n%s\n\nENDLOG for container %v:%v:%v", logs, pod.Namespace, pod.Name, container[i].Name) + } +} + +func getPreviousPodLogs(c clientset.Interface, namespace, podName, containerName string) (string, error) { + logs, err := c.CoreV1().RESTClient().Get(). + Resource("pods"). + Namespace(namespace). + Name(podName).SubResource("log"). + Param("container", containerName). + Param("previous", strconv.FormatBool(true)). + Do(). + Raw() + if err != nil { + return "", err + } + if strings.Contains(string(logs), "Internal Error") { + return "", fmt.Errorf("fetched log contains \"Internal Error\": %q", string(logs)) + } + return string(logs), err +} diff --git a/e2e/rbd.go b/e2e/rbd.go index e2d99c7c2..dc405aac5 100644 --- a/e2e/rbd.go +++ b/e2e/rbd.go @@ -59,8 +59,10 @@ func deleteRBDPlugin() { var _ = Describe("RBD", func() { f := framework.NewDefaultFramework("rbd") + var c clientset.Interface // deploy RBD CSI BeforeEach(func() { + c = f.ClientSet updaterbdDirPath(f.ClientSet) createRBDPool() createConfigMap(rbdDirPath, f.ClientSet, f) @@ -71,6 +73,12 @@ var _ = Describe("RBD", func() { }) AfterEach(func() { + if CurrentGinkgoTestDescription().Failed { + // log provisoner + logsCSIPods("app=csi-rbdplugin-provisioner", c) + // log node plugin + logsCSIPods("app=csi-rbdplugin", c) + } deleteRBDPlugin() deleteConfiMap(rbdDirPath) deleteRBDPool()