Commit 3b2688f
authored
fix(e2e-framework): return last error from SshRunCommand instead of (… (#54536)
…nil, nil)
## What does this PR do?
Fixes an `err` variable-shadowing bug in `SshRunCommand` (`test/e2e-framework/testing/utils/ssh/ssh.go`) that made the retry helper return `(nil, nil)` — no output **and** no error — whenever a command failed on every retry.
```go
var err error
for range NR_SSH_COMMAND_RETRIES {
sshSession, err := sshClient.NewSession() // := re-declares err in loop scope
...
output, err := sshSession.CombinedOutput(command) // := again, loop-scoped
if err == nil {
return output, nil
}
}
return nil, err // outer err is always nil → returns (nil, nil) on persistent failure
```
Both `:=` statements declare a loop-scoped `err`, so the outer `err` is never assigned. The helper's own doc comment ("Returns the last error if retries exceeded") was effectively a lie — it couldn't.
The fix assigns to the outer `err` (`var` decls + `=`) so the last failure propagates, and closes the `ssh.Session` between retries (it was previously leaked on every attempt).
## Motivation
Regression introduced in #48951 (commit `7b455c21`) when the SSH helper was moved into the shared infra package. It was surfaced by #49708, which added the OpenShift teardown dump.
The only caller is the GCP dump path (`testing/provisioners/gcp/kubernetes/kubernetes_dump.go`):
```go
sshOutput, err := sshutils.SshRunCommand(sshClient, "cat .kube/config", &out)
if err != nil { ... return } // never taken — err is nil
kubeConfig, err := clientcmd.Load(sshOutput) // fed nil/empty bytes instead
```
When `cat .kube/config` over SSH fails, the error guard is skipped and empty bytes flow into `clientcmd.Load`, so OpenShift teardown diagnostics emit misleading garbage (`Forbidden: cannot list ...`) instead of a clean "SSH failed" error — which is exactly the confusing output seen on failing `new-e2e-ssi-openshift` runs (INPLAT-995).
## Describe how you validated your changes
- Added `ssh_test.go` with an in-memory SSH server that completes the handshake but fails every command; asserts `SshRunCommand` returns a non-nil error after exhausting retries.
- Verified the test **fails** (`An error is expected but got nil`) against the original buggy code and **passes** with the fix.
- Confirmed default `go vet` produces **no** shadow warning for the original code — this bug is invisible to standard tooling (follow-up: consider enabling a `shadow` linter).
- `bazel test //test/e2e-framework/testing/utils/ssh:ssh_test` → PASS. `BUILD.bazel` regenerated via gazelle.
## Additional Notes
Test-framework-only change (`test/e2e-framework/`), no user-facing Agent behavior — no release note.
Co-authored-by: frank.spano <frank.spano@datadoghq.com>1 parent c00e3f6 commit 3b2688f
3 files changed
Lines changed: 132 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
84 | 88 | | |
85 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
86 | 92 | | |
87 | 93 | | |
88 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
0 commit comments