Skip to content

Commit e7f7d81

Browse files
committed
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 <[email protected]>
1 parent d505efc commit e7f7d81

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/e2e/ctx/ctx.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ctx
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"os/exec"
@@ -145,6 +146,30 @@ func (ctx TestContext) DumpNamespaceArtifacts(namespace string) error {
145146
return nil
146147
}
147148

149+
func (ctx TestContext) DescribeResource(command string) error {
150+
ctx.Logf("Running command %s", command)
151+
stdout, stderr, err := ctx.ExecCommand(command)
152+
if err != nil {
153+
return fmt.Errorf("failed to run command: %s", strings.TrimSpace(stderr+err.Error()))
154+
}
155+
ctx.Logf("%s", strings.TrimSpace(stdout))
156+
return nil
157+
}
158+
159+
func (ctx TestContext) ExecCommand(command string) (string, string, error) {
160+
var (
161+
stdoutBuf bytes.Buffer
162+
stderrBuf bytes.Buffer
163+
)
164+
cmd := exec.Command("bash", "-c", command)
165+
cmd.Stdout = &stdoutBuf
166+
cmd.Stderr = &stderrBuf
167+
cmd.Env = []string{"KUBECONFIG=" + ctx.kubeconfigPath}
168+
169+
err := cmd.Run()
170+
return stdoutBuf.String(), stderrBuf.String(), err
171+
}
172+
148173
func setDerivedFields(ctx *TestContext) error {
149174
if ctx == nil {
150175
return fmt.Errorf("nil test context")

0 commit comments

Comments
 (0)