Skip to content
Closed
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
25 changes: 25 additions & 0 deletions test/e2e/ctx/ctx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ctx

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -140,6 +141,30 @@ func (ctx TestContext) DumpNamespaceArtifacts(namespace string) error {
return nil
}

func (ctx TestContext) DescribeResource(command string) error {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be largely be useful to dump out resources (or container logs) when you run into a failed test case:

if CurrentGinkgoTestDescription().Failed {
	ctx.Ctx().DescribeResource(fmt.Sprintf("kubectl get operators %s -o yaml", o.GetName()))
	ctx.Ctx().DescribeResource(fmt.Sprintf("kubectl -n %s logs -l app=olm-operator --tail=50 --prefix --timestamps", *olmNamespace))
}

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")
Expand Down