From 7b275406aa556208ada9517bba171e5df78e4b6f Mon Sep 17 00:00:00 2001 From: timflannagan Date: Wed, 8 Dec 2021 16:03:47 -0500 Subject: [PATCH] test/e2e: Support logging an individual resources' state to stdout Add initial support for debugging individual Kubernetes resources, executing arbitrary shell commands, etc. This is mainly useful in the context of debugging cluster-scoped resources (e.g. the Operator API) that don't necessary fit into the test/e2e/collect-ci-artifacts.sh gather script. Signed-off-by: timflannagan --- test/e2e/ctx/ctx.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/e2e/ctx/ctx.go b/test/e2e/ctx/ctx.go index fcc167f820..4207efd1e6 100644 --- a/test/e2e/ctx/ctx.go +++ b/test/e2e/ctx/ctx.go @@ -1,6 +1,7 @@ package ctx import ( + "bytes" "fmt" "os" "os/exec" @@ -140,6 +141,30 @@ func (ctx TestContext) DumpNamespaceArtifacts(namespace string) error { return nil } +func (ctx TestContext) DescribeResource(command string) error { + ctx.Logf("Running command %s", command) + stdout, stderr, err := ctx.ExecCommand(command) + if err != nil { + return fmt.Errorf("failed to run command: %s", strings.TrimSpace(stderr+err.Error())) + } + ctx.Logf("%s", strings.TrimSpace(stdout)) + return nil +} + +func (ctx TestContext) ExecCommand(command string) (string, string, error) { + var ( + stdoutBuf bytes.Buffer + stderrBuf bytes.Buffer + ) + cmd := exec.Command("bash", "-c", command) + cmd.Stdout = &stdoutBuf + cmd.Stderr = &stderrBuf + cmd.Env = []string{"KUBECONFIG=" + ctx.kubeconfigPath} + + err := cmd.Run() + return stdoutBuf.String(), stderrBuf.String(), err +} + func setDerivedFields(ctx *TestContext) error { if ctx == nil { return fmt.Errorf("nil test context")