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 all commits
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
53 changes: 44 additions & 9 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,26 +37,59 @@ 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{})
podList, err := testClient.VirtClient.CoreV1().Pods(managerNamespace).List(context.TODO(),
metav1.ListOptions{LabelSelector: "app=kubemacpool"})
if err != nil {
fmt.Println(err)
Fail(message, callerSkip...)
}

for _, pod := range podList.Items {
podYaml, err := testClient.VirtClient.CoreV1().Pods(managerNamespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})

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("Pod Name: %s \n", pod.Name)
fmt.Printf("%v \n", *podYaml)
fmt.Println(string(output))
fmt.Printf("\nPod Name: %s \n", pod.Name)
fmt.Printf("Pod Yaml:\n%v \n", *podYaml)

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 @@ -63,15 +98,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...)
}
Loading