Skip to content

Commit 20f842c

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 0fa6f29 commit 20f842c

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"
@@ -140,6 +141,30 @@ func (ctx TestContext) DumpNamespaceArtifacts(namespace string) error {
140141
return nil
141142
}
142143

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

0 commit comments

Comments
 (0)