Skip to content

Commit 18f1d3e

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 1f2472f commit 18f1d3e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/e2e/ctx/ctx.go

Lines changed: 27 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"
@@ -127,6 +128,32 @@ 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+
151+
if err := cmd.Run(); err != nil {
152+
return stdoutBuf.String(), stderrBuf.String(), err
153+
}
154+
return stdoutBuf.String(), stderrBuf.String(), nil
155+
}
156+
130157
func setDerivedFields(ctx *TestContext) error {
131158
if ctx == nil {
132159
return fmt.Errorf("nil test context")

0 commit comments

Comments
 (0)