2015-04-13 06:36:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-23 11:34:40 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-04-13 17:30:07 +00:00
|
|
|
"net/http"
|
2015-04-13 06:36:04 +00:00
|
|
|
"strings"
|
2015-08-04 11:44:54 +00:00
|
|
|
"sync"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2023-07-27 11:13:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2019-08-05 13:17:42 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-04-13 06:36:04 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestExecResizeAPIHeightWidthNoInt(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 11:13:00 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "top").Stdout()
|
2015-04-13 06:36:04 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
|
2023-07-14 18:02:38 +00:00
|
|
|
res, _, err := request.Post(testutil.GetContext(c), endpoint)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-12-04 22:26:00 +00:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
|
2015-04-13 06:36:04 +00:00
|
|
|
}
|
2015-07-23 11:34:40 +00:00
|
|
|
|
|
|
|
// Part of #14845
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestExecResizeImmediatelyAfterExecStart(c *testing.T) {
|
2015-07-23 11:34:40 +00:00
|
|
|
name := "exec_resize_test"
|
2023-07-27 11:13:00 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "-i", "-t", "--name", name, "--restart", "always", "busybox", "/bin/sh")
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2015-08-04 11:44:54 +00:00
|
|
|
testExecResize := func() error {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"AttachStdin": true,
|
|
|
|
"Cmd": []string{"/bin/sh"},
|
|
|
|
}
|
|
|
|
uri := fmt.Sprintf("/containers/%s/exec", name)
|
2023-07-14 18:02:38 +00:00
|
|
|
res, body, err := request.Post(testutil.GetContext(c), uri, request.JSONBody(data))
|
2015-08-04 11:44:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
if res.StatusCode != http.StatusCreated {
|
2019-08-05 13:17:42 +00:00
|
|
|
return errors.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, res.StatusCode)
|
2015-08-04 11:44:54 +00:00
|
|
|
}
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
buf, err := request.ReadBody(body)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2015-08-04 11:44:54 +00:00
|
|
|
out := map[string]string{}
|
2017-05-24 03:56:26 +00:00
|
|
|
err = json.Unmarshal(buf, &out)
|
2015-08-04 11:44:54 +00:00
|
|
|
if err != nil {
|
2019-08-05 13:17:42 +00:00
|
|
|
return errors.Wrap(err, "ExecCreate returned invalid json")
|
2015-08-04 11:44:54 +00:00
|
|
|
}
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2015-08-04 11:44:54 +00:00
|
|
|
execID := out["Id"]
|
|
|
|
if len(execID) < 1 {
|
2019-08-05 13:17:42 +00:00
|
|
|
return errors.New("ExecCreate got invalid execID")
|
2015-08-04 11:44:54 +00:00
|
|
|
}
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2015-08-04 11:44:54 +00:00
|
|
|
payload := bytes.NewBufferString(`{"Tty":true}`)
|
2019-10-11 09:54:35 +00:00
|
|
|
wc, _, err := requestHijack(http.MethodPost, fmt.Sprintf("/exec/%s/start", execID), payload, "application/json", request.DaemonHost())
|
2015-08-04 11:44:54 +00:00
|
|
|
if err != nil {
|
2019-08-05 13:17:42 +00:00
|
|
|
return errors.Wrap(err, "failed to start the exec")
|
2015-08-04 11:44:54 +00:00
|
|
|
}
|
2019-10-11 09:54:35 +00:00
|
|
|
defer wc.Close()
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
_, rc, err := request.Post(testutil.GetContext(c), fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), request.ContentType("text/plain"))
|
2018-06-08 03:07:48 +00:00
|
|
|
if err != nil {
|
|
|
|
// It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
|
|
|
|
if err == io.ErrUnexpectedEOF {
|
2019-08-05 13:17:42 +00:00
|
|
|
return errors.New("the daemon might have crashed")
|
2018-06-08 03:07:48 +00:00
|
|
|
}
|
|
|
|
// Other error happened, should be reported.
|
2019-08-05 13:17:42 +00:00
|
|
|
return errors.Wrap(err, "failed to exec resize immediately after start")
|
2015-08-04 11:44:54 +00:00
|
|
|
}
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2018-06-08 03:07:48 +00:00
|
|
|
rc.Close()
|
2015-07-23 11:34:40 +00:00
|
|
|
|
2015-08-04 11:44:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The panic happens when daemon.ContainerExecStart is called but the
|
|
|
|
// container.Exec is not called.
|
|
|
|
// Because the panic is not 100% reproducible, we send the requests concurrently
|
|
|
|
// to increase the probability that the problem is triggered.
|
|
|
|
var (
|
|
|
|
n = 10
|
|
|
|
ch = make(chan error, n)
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := testExecResize(); err != nil {
|
|
|
|
ch <- err
|
2015-07-23 11:34:40 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-08-04 11:44:54 +00:00
|
|
|
wg.Wait()
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
c.Fatal(err.Error())
|
|
|
|
default:
|
2015-07-23 11:34:40 +00:00
|
|
|
}
|
|
|
|
}
|