Skip to content

Commit 4ff336e

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 ab923b3 commit 4ff336e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/e2e/ctx/ctx.go

+25
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"
@@ -127,6 +128,30 @@ func (ctx TestContext) DumpNamespaceArtifacts(namespace string) error {
127128
return nil
128129
}
129130

131+
func (ctx TestContext) DescribeResource(command string) error {
132+
ctx.Logf("Running command %s", command)
133+
stdout, stderr, err := ctx.ExecCommand(command)
134+
if err != nil {
135+
return fmt.Errorf("failed to run command: %s", strings.TrimSpace(stderr+err.Error()))
136+
}
137+
ctx.Logf("%s", strings.TrimSpace(stdout))
138+
return nil
139+
}
140+
141+
func (ctx TestContext) ExecCommand(command string) (string, string, error) {
142+
var (
143+
stdoutBuf bytes.Buffer
144+
stderrBuf bytes.Buffer
145+
)
146+
cmd := exec.Command("bash", "-c", command)
147+
cmd.Stdout = &stdoutBuf
148+
cmd.Stderr = &stderrBuf
149+
cmd.Env = []string{"KUBECONFIG=" + ctx.kubeconfigPath}
150+
err := cmd.Run()
151+
152+
return stdoutBuf.String(), stderrBuf.String(), err
153+
}
154+
130155
func setDerivedFields(ctx *TestContext) error {
131156
if ctx == nil {
132157
return fmt.Errorf("nil test context")

0 commit comments

Comments
 (0)