Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
test/e2e/util: add GetPodLogs function
Browse files Browse the repository at this point in the history
Add GetPodLogs function to test utilities for the developer to be able
to dump logs of a given pod.
  • Loading branch information
sbar95 committed Feb 24, 2020
1 parent 213d80e commit 1b7e27b
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions test/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
goctx "context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -61,6 +61,21 @@ func GetPod(namespace, namePrefix, containsImage string, kubeclient kubernetes.I
return nil, nil
}

// GetPodLogs retrieves logs of a given pod
func GetPodLogs(pod *v1.Pod, kubeclient kubernetes.Interface) (string, error) {
req := kubeclient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{Timestamps: true})
logs, err := req.Stream()
if err != nil {
return "", xerrors.Errorf("could not get logs for pod %q: %w", pod.Name, err)
}
defer logs.Close()
b, err := ioutil.ReadAll(logs)
if err != nil {
return "", xerrors.Errorf("could not get logs for pod %q: %w", pod.Name, err)
}
return string(b), nil
}

// WaitForPod retrieves a specific pod with a known name and namespace and waits for it to be running and available
func WaitForPod(t *testing.T, f *framework.Framework, namespace, name string, retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
Expand Down Expand Up @@ -151,8 +166,7 @@ func DumpJobsLogsOnError(t *testing.T, f *framework.Framework, namespace string)
if !t.Failed() {
return
}
pods := f.KubeClient.CoreV1().Pods(namespace)
podsList, err := pods.List(metav1.ListOptions{})
podsList, err := f.KubeClient.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
t.Logf("failed to list pods in namespace %s: %s", namespace, err)
return
Expand All @@ -169,20 +183,12 @@ func DumpJobsLogsOnError(t *testing.T, f *framework.Framework, namespace string)
continue
}
// Retrieve pod's logs
req := pods.GetLogs(p.Name, &v1.PodLogOptions{Timestamps: true})
logs, err := req.Stream()
if err != nil {
t.Logf("failed to retrieve logs for pod %s: %s", p.Name, err)
continue
}
buf := &bytes.Buffer{}
_, err = io.Copy(buf, logs)
logs.Close()
logs, err := GetPodLogs(&p, f.KubeClient)
if err != nil {
t.Logf("failed to retrieve logs for pod %s: %s", p.Name, err)
continue
}
t.Logf("================ POD LOGS FOR %s ================\n%s\n\n", p.Name, buf.String())
t.Logf("================ POD LOGS FOR %s ================\n%s\n\n", p.Name, logs)
}
}

Expand Down

0 comments on commit 1b7e27b

Please sign in to comment.