Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e: Fix logging during test failure #454

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
e2e, failed-tests: Fix Logs
Currently during test fail, the kubemacpool manager pod logs are
not printed because the container name is not specified.
- Changing method of extracting the logs using req.Stream.
- Changing to print all container logs.
- Fix minor print formatting.

Signed-off-by: Ram Lavi <ralavi@redhat.com>
  • Loading branch information
RamLavi committed Jan 15, 2025
commit eab736c0130b301003fcc1794faca1a7124990da
52 changes: 41 additions & 11 deletions tests/tests_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tests

import (
"bytes"
"context"
"fmt"
"io"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -35,6 +37,31 @@ var _ = AfterSuite(func() {
removeTestNamespaces()
})

func getPodLogs(podName, containerName string) (string, error) {
req := testClient.VirtClient.CoreV1().Pods(managerNamespace).GetLogs(podName, &corev1.PodLogOptions{
Container: containerName,
})
podLogs, err := req.Stream(context.TODO())
if err != nil {
return "", err
}
defer func(podLogs io.ReadCloser) {
err := podLogs.Close()
if err != nil {
panic(err)
}
}(podLogs)

buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return "", err
}
output := buf.String()

return output, nil
}

func KubemacPoolFailedFunction(message string, callerSkip ...int) {
podList, err := testClient.VirtClient.CoreV1().Pods(managerNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
Expand All @@ -49,16 +76,19 @@ func KubemacPoolFailedFunction(message string, callerSkip ...int) {
Fail(message, callerSkip...)
}

req := testClient.VirtClient.CoreV1().Pods(managerNamespace).GetLogs(pod.Name, &corev1.PodLogOptions{})
output, err := req.DoRaw(context.TODO())
if err != nil {
fmt.Println(err)
Fail(message, callerSkip...)
}
fmt.Printf("\nPod Name: %s \n", pod.Name)
fmt.Printf("Pod Yaml:\n%v \n", *podYaml)

fmt.Printf("Pod Name: %s \n", pod.Name)
fmt.Printf("%v \n", *podYaml)
fmt.Println(string(output))
for i := range pod.Spec.Containers {
containerName := pod.Spec.Containers[i].Name
podLogs, err := getPodLogs(pod.Name, containerName)
if err != nil {
fmt.Println(err)
Fail(message, callerSkip...)
}

fmt.Printf("\nPod container %q Logs:\n%s \n", containerName, podLogs)
}
}

service, err := testClient.VirtClient.CoreV1().Services(managerNamespace).Get(context.TODO(), names.WEBHOOK_SERVICE, metav1.GetOptions{})
Expand All @@ -67,15 +97,15 @@ func KubemacPoolFailedFunction(message string, callerSkip ...int) {
Fail(message, callerSkip...)
}

fmt.Printf("Service: %v", service)
fmt.Printf("Service: %v\n", service)

endpoint, err := testClient.VirtClient.CoreV1().Endpoints(managerNamespace).Get(context.TODO(), names.WEBHOOK_SERVICE, metav1.GetOptions{})
if err != nil {
fmt.Println(err)
Fail(message, callerSkip...)
}

fmt.Printf("Endpoint: %v", endpoint)
fmt.Printf("Endpoint: %v\n", endpoint)

Fail(message, callerSkip...)
}