Files
moby/integration/container/exec_linux_test.go
Laura Brehm c866a7e5f8 daemon/exec: don't overwrite exit code if set
If we fail to start an exec, the deferred error-handling block in [L181-L193](c7e42d855e/daemon/exec.go (L181-L193))
would set the exit code to `126` (`EACCES`). However, if we get far enough along
attempting to start the exec, we set the exit code according to the error returned
from starting the task [L288-L291](c7e42d855e/daemon/exec.go (L288-L291)).

For some situations (such as `docker exec [some-container]
missing-binary`), the 2nd block returns the correct exit code (`127`)
but that then gets overwritten by the 1st block.

This commit changes that logic to only set the default exit code `126`
if the exit code has not been set yet.

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
2024-09-30 15:49:04 +01:00

66 lines
1.6 KiB
Go

package container // import "github.com/docker/docker/integration/container"
import (
"strings"
"testing"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration/internal/container"
"gotest.tools/v3/assert"
"gotest.tools/v3/skip"
)
func TestExecConsoleSize(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.42"), "skip test from new feature")
ctx := setupTest(t)
apiClient := testEnv.APIClient()
cID := container.Run(ctx, t, apiClient, container.WithImage("busybox"))
result, err := container.Exec(ctx, apiClient, cID, []string{"stty", "size"},
func(ec *containertypes.ExecOptions) {
ec.Tty = true
ec.ConsoleSize = &[2]uint{57, 123}
},
)
assert.NilError(t, err)
assert.Equal(t, strings.TrimSpace(result.Stdout()), "57 123")
}
func TestFailedExecExitCode(t *testing.T) {
testCases := []struct {
doc string
command []string
expectedExitCode int
}{
{
doc: "executable not found",
command: []string{"nonexistent"},
expectedExitCode: 127,
},
{
doc: "executable cannot be invoked",
command: []string{"/etc"},
expectedExitCode: 126,
},
}
for _, tc := range testCases {
t.Run(tc.doc, func(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
cID := container.Run(ctx, t, apiClient)
result, err := container.Exec(ctx, apiClient, cID, tc.command)
assert.NilError(t, err)
assert.Equal(t, result.ExitCode, tc.expectedExitCode)
})
}
}