Skip to content

Commit 7f1e276

Browse files
committed
test: create tarball as artifact with pod logs for debugging
1 parent 98f6979 commit 7f1e276

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/e2e/shared/common.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ package shared
2121

2222
import (
2323
"context"
24+
"encoding/base64"
2425
"fmt"
2526
"os"
2627
"path"
2728
"path/filepath"
29+
"strings"
2830
"time"
2931

3032
. "github.com/onsi/ginkgo/v2"
3133
. "github.com/onsi/gomega"
34+
"github.com/pkg/errors"
3235
corev1 "k8s.io/api/core/v1"
3336
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3437
crclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -203,8 +206,18 @@ func DumpMachine(ctx context.Context, e2eCtx *E2EContext, machine infrav1.AWSMac
203206
title: "containerd",
204207
cmd: "journalctl --no-pager -u containerd.service",
205208
},
209+
{
210+
title: "pod-logs",
211+
cmd: "sudo tar -czf - -C /var/log pods | base64 -w0",
212+
},
206213
},
207214
)
215+
216+
// Post-process pod-logs to have a tar.gz file instead of base64 date encoded in the log file.
217+
// Note: It is not possible to directly output the raw tar.gz data because something truncates it.
218+
if err := postProcessBase64LogData(filepath.Dir(f.Name()), "pod-logs.log", "pod-logs.tar.gz"); err != nil {
219+
fmt.Fprintf(GinkgoWriter, "Failed to post-process pod-logs: %v\n", err)
220+
}
208221
}
209222

210223
func DumpSpecResources(ctx context.Context, e2eCtx *E2EContext, namespace *corev1.Namespace) {
@@ -287,3 +300,39 @@ func CreateAWSClusterControllerIdentity(k8sclient crclient.Client) {
287300
func Byf(format string, a ...interface{}) {
288301
By(fmt.Sprintf(format, a...))
289302
}
303+
304+
func postProcessBase64LogData(dir, src, dst string) error {
305+
sourceFile := filepath.Clean(path.Join(dir, src))
306+
destinationFile := filepath.Clean(path.Join(dir, dst))
307+
308+
// Read input data
309+
inputData, err := os.ReadFile(sourceFile)
310+
if err != nil {
311+
return errors.Wrapf(err, "unable to read source file %q", sourceFile)
312+
}
313+
314+
// Extract second line which contains the data (first line contains the command)
315+
inputStringData := strings.Split(string(inputData), "\n")[1]
316+
317+
// Trim spaces and the $ suffix.
318+
inputStringData = strings.TrimSpace(inputStringData)
319+
inputStringData = strings.TrimSuffix(inputStringData, "$")
320+
321+
// Base64 decode the data
322+
outputData, err := base64.StdEncoding.DecodeString(inputStringData)
323+
if err != nil {
324+
return errors.Wrapf(err, "unable to base64 decode input data")
325+
}
326+
327+
// Write the destination file
328+
if err := os.WriteFile(destinationFile, outputData, 0600); err != nil {
329+
return errors.Wrapf(err, "unable to write destination file at %q", destinationFile)
330+
}
331+
332+
// Delete the source file.
333+
if err := os.Remove(sourceFile); err != nil {
334+
return errors.Wrapf(err, "unable to delete source file at %q", sourceFile)
335+
}
336+
337+
return nil
338+
}

0 commit comments

Comments
 (0)