2017-11-15 22:18:23 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2024-07-24 14:05:41 -04:00
|
|
|
"bytes"
|
2018-04-23 08:13:52 -04:00
|
|
|
"fmt"
|
2024-07-24 14:05:41 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-11-17 12:45:27 -05:00
|
|
|
"strings"
|
2017-11-15 22:18:23 -05:00
|
|
|
"testing"
|
2024-07-24 14:05:41 -04:00
|
|
|
"time"
|
2017-11-15 22:18:23 -05:00
|
|
|
|
2024-07-24 14:05:41 -04:00
|
|
|
"github.com/creack/pty"
|
2018-01-22 18:09:47 -05:00
|
|
|
"github.com/docker/cli/e2e/internal/fixtures"
|
2024-07-24 14:05:41 -04:00
|
|
|
"gotest.tools/v3/assert"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/icmd"
|
2024-07-25 04:27:45 -04:00
|
|
|
"gotest.tools/v3/skip"
|
2017-11-15 22:18:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAttachExitCode(t *testing.T) {
|
2023-11-20 05:10:29 -05:00
|
|
|
const exitCode = 21
|
|
|
|
result := icmd.RunCommand("docker", "run", "-d", "-i", "--rm", fixtures.AlpineImage, "sh", "-c", fmt.Sprintf("read; exit %d", exitCode))
|
|
|
|
result.Assert(t, icmd.Success)
|
2017-11-17 12:45:27 -05:00
|
|
|
|
2023-11-20 05:10:29 -05:00
|
|
|
containerID := strings.TrimSpace(result.Stdout())
|
2017-11-17 12:45:27 -05:00
|
|
|
|
2023-11-20 05:10:29 -05:00
|
|
|
result = icmd.RunCmd(icmd.Command("docker", "attach", containerID), withStdinNewline)
|
|
|
|
result.Assert(t, icmd.Expected{ExitCode: exitCode})
|
2017-11-17 12:45:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func withStdinNewline(cmd *icmd.Cmd) {
|
|
|
|
cmd.Stdin = strings.NewReader("\n")
|
2017-11-15 22:18:23 -05:00
|
|
|
}
|
2024-07-24 14:05:41 -04:00
|
|
|
|
|
|
|
// Regression test for https://github.com/docker/cli/issues/5294
|
|
|
|
func TestAttachInterrupt(t *testing.T) {
|
2024-07-25 04:27:45 -04:00
|
|
|
// this is a new test, it already did not work (inside dind) when over ssh
|
|
|
|
// todo(laurazard): make this test work w/ dind over ssh
|
|
|
|
skip.If(t, strings.Contains(os.Getenv("DOCKER_HOST"), "ssh://"))
|
|
|
|
|
|
|
|
// if
|
|
|
|
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage,
|
|
|
|
"sh", "-c", "trap \"exit 33\" SIGINT; for i in $(seq 100); do sleep 0.1; done; exit 34")
|
2024-07-24 14:05:41 -04:00
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
containerID := strings.TrimSpace(result.Stdout())
|
|
|
|
|
|
|
|
// run it as such so we can signal it later
|
|
|
|
c := exec.Command("docker", "attach", containerID)
|
|
|
|
d := bytes.Buffer{}
|
|
|
|
c.Stdout = &d
|
|
|
|
c.Stderr = &d
|
|
|
|
_, err := pty.Start(c)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// have to wait a bit to give time for the command to execute/print
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
c.Process.Signal(os.Interrupt)
|
|
|
|
|
|
|
|
_ = c.Wait()
|
2024-07-25 04:27:45 -04:00
|
|
|
// the CLI should exit with 33 (the SIGINT was forwarded to the container), and the
|
|
|
|
// CLI process waited for the container exit and properly captured/set the exit code
|
|
|
|
assert.Equal(t, c.ProcessState.ExitCode(), 33)
|
|
|
|
assert.Equal(t, d.String(), "exit status 33\n")
|
2024-07-24 14:05:41 -04:00
|
|
|
}
|