Skip to content

Commit

Permalink
chore: output more info when ExecuteCommandInPod fails
Browse files Browse the repository at this point in the history
This should make investigating things like [this](https://github.com/siderolabs/talos/actions/runs/9411253542/job/25924192027)
easier.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Jun 24, 2024
1 parent 4b5a744 commit c603d2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
43 changes: 31 additions & 12 deletions internal/integration/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,47 @@ func (suite *CommonSuite) TestDNSResolver() {

suite.Require().NoError(err)

defer suite.Clientset.CoreV1().Pods(namespace).Delete(suite.ctx, pod, metav1.DeleteOptions{}) //nolint:errcheck
suite.T().Cleanup(func() {
cleanUpCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute)
defer cleanupCancel()

suite.Require().NoError(
suite.Clientset.CoreV1().Pods(namespace).Delete(cleanUpCtx, pod, metav1.DeleteOptions{}),
)
})

// wait for the pod to be ready
suite.Require().NoError(suite.WaitForPodToBeRunning(suite.ctx, time.Minute, namespace, pod))

stdout, stderr, err := suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "wget https://www.google.com/")
suite.Require().NoError(err)
stdout, stderr, err := suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "wget -S https://www.google.com/")
suite.Assert().NoError(err)
suite.Assert().Equal("", stdout)
suite.Assert().Contains(stderr, "'index.html' saved")

suite.Require().Equal("", stdout)
suite.Require().Contains(stderr, "'index.html' saved")
if suite.T().Failed() {
suite.T().FailNow()
}

_, stderr, err = suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "apk add --update bind-tools")
suite.Require().NoError(err)
suite.Require().Empty(stderr)

suite.Assert().NoError(err)
suite.Assert().Empty(stderr, "stderr: %s", stderr)

if suite.T().Failed() {
suite.T().FailNow()
}

stdout, stderr, err = suite.ExecuteCommandInPod(suite.ctx, namespace, pod, "dig really-long-record.dev.siderolabs.io")
suite.Require().NoError(err)

suite.Require().Contains(stdout, "status: NOERROR")
suite.Require().Contains(stdout, "ANSWER: 34")
suite.Require().NotContains(stdout, "status: NXDOMAIN")
suite.Require().Equal(stderr, "")
suite.Assert().NoError(err)
suite.Assert().Contains(stdout, "status: NOERROR")
suite.Assert().Contains(stdout, "ANSWER: 34")
suite.Assert().NotContains(stdout, "status: NXDOMAIN")
suite.Assert().Equal(stderr, "")

if suite.T().Failed() {
suite.T().FailNow()
}
}

func init() {
Expand Down
16 changes: 13 additions & 3 deletions internal/integration/base/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"io"
"slices"
"strings"
"time"

"github.com/siderolabs/gen/xslices"
Expand Down Expand Up @@ -304,17 +305,26 @@ func (k8sSuite *K8sSuite) ExecuteCommandInPod(ctx context.Context, namespace, po
return "", "", err
}

var stdout, stderr bytes.Buffer
var stdout, stderr strings.Builder

err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: &stdout,
Stderr: &stderr,
})
if err != nil {
return "", "", err
k8sSuite.T().Logf(
"error executing command in pod %s/%s: %v\n\ncommand %q stdout:\n%s\n\ncommand %q stderr:\n%s",
namespace,
podName,
err,
command,
stdout.String(),
command,
stderr.String(),
)
}

return stdout.String(), stderr.String(), nil
return stdout.String(), stderr.String(), err
}

// GetPodsWithLabel returns the pods with the given label in the specified namespace.
Expand Down

0 comments on commit c603d2b

Please sign in to comment.