2015-04-13 02:36:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-23 07:34:40 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-04-13 13:30:07 -04:00
|
|
|
"net/http"
|
2015-04-13 02:36:04 -04:00
|
|
|
"strings"
|
2015-08-04 07:44:54 -04:00
|
|
|
"sync"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2018-05-04 17:15:00 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2019-08-05 09:17:42 -04:00
|
|
|
"github.com/pkg/errors"
|
2019-04-04 09:23:19 -04:00
|
|
|
"gotest.tools/assert"
|
2015-04-13 02:36:04 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestExecResizeAPIHeightWidthNoInt(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-04-13 02:36:04 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
|
2017-05-23 23:56:26 -04:00
|
|
|
res, _, err := request.Post(endpoint)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2018-05-04 17:15:00 -04:00
|
|
|
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusInternalServerError)
|
2018-05-04 17:15:00 -04:00
|
|
|
} else {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
|
2018-05-04 17:15:00 -04:00
|
|
|
}
|
2015-04-13 02:36:04 -04:00
|
|
|
}
|
2015-07-23 07:34:40 -04:00
|
|
|
|
|
|
|
// Part of #14845
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *testing.T) {
|
2015-07-23 07:34:40 -04:00
|
|
|
name := "exec_resize_test"
|
|
|
|
dockerCmd(c, "run", "-d", "-i", "-t", "--name", name, "--restart", "always", "busybox", "/bin/sh")
|
|
|
|
|
2015-08-04 07:44:54 -04:00
|
|
|
testExecResize := func() error {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"AttachStdin": true,
|
|
|
|
"Cmd": []string{"/bin/sh"},
|
|
|
|
}
|
|
|
|
uri := fmt.Sprintf("/containers/%s/exec", name)
|
2017-05-23 23:56:26 -04:00
|
|
|
res, body, err := request.Post(uri, request.JSONBody(data))
|
2015-08-04 07:44:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-23 23:56:26 -04:00
|
|
|
if res.StatusCode != http.StatusCreated {
|
2019-08-05 09:17:42 -04:00
|
|
|
return errors.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, res.StatusCode)
|
2015-08-04 07:44:54 -04:00
|
|
|
}
|
2015-07-23 07:34:40 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
buf, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
|
2015-08-04 07:44:54 -04:00
|
|
|
out := map[string]string{}
|
2017-05-23 23:56:26 -04:00
|
|
|
err = json.Unmarshal(buf, &out)
|
2015-08-04 07:44:54 -04:00
|
|
|
if err != nil {
|
2019-08-05 09:17:42 -04:00
|
|
|
return errors.Wrap(err, "ExecCreate returned invalid json")
|
2015-08-04 07:44:54 -04:00
|
|
|
}
|
2015-07-23 07:34:40 -04:00
|
|
|
|
2015-08-04 07:44:54 -04:00
|
|
|
execID := out["Id"]
|
|
|
|
if len(execID) < 1 {
|
2019-08-05 09:17:42 -04:00
|
|
|
return errors.New("ExecCreate got invalid execID")
|
2015-08-04 07:44:54 -04:00
|
|
|
}
|
2015-07-23 07:34:40 -04:00
|
|
|
|
2015-08-04 07:44:54 -04:00
|
|
|
payload := bytes.NewBufferString(`{"Tty":true}`)
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, _, err := sockRequestHijack(http.MethodPost, fmt.Sprintf("/exec/%s/start", execID), payload, "application/json", request.DaemonHost())
|
2015-08-04 07:44:54 -04:00
|
|
|
if err != nil {
|
2019-08-05 09:17:42 -04:00
|
|
|
return errors.Wrap(err, "failed to start the exec")
|
2015-08-04 07:44:54 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
2015-07-23 07:34:40 -04:00
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
_, rc, err := request.Post(fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), request.ContentType("text/plain"))
|
2018-06-07 23:07:48 -04:00
|
|
|
if err != nil {
|
|
|
|
// It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
|
|
|
|
if err == io.ErrUnexpectedEOF {
|
2019-08-05 09:17:42 -04:00
|
|
|
return errors.New("the daemon might have crashed")
|
2018-06-07 23:07:48 -04:00
|
|
|
}
|
|
|
|
// Other error happened, should be reported.
|
2019-08-05 09:17:42 -04:00
|
|
|
return errors.Wrap(err, "failed to exec resize immediately after start")
|
2015-08-04 07:44:54 -04:00
|
|
|
}
|
2015-07-23 07:34:40 -04:00
|
|
|
|
2018-06-07 23:07:48 -04:00
|
|
|
rc.Close()
|
2015-07-23 07:34:40 -04:00
|
|
|
|
2015-08-04 07:44:54 -04: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 07:34:40 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-08-04 07:44:54 -04:00
|
|
|
wg.Wait()
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
c.Fatal(err.Error())
|
|
|
|
default:
|
2015-07-23 07:34:40 -04:00
|
|
|
}
|
|
|
|
}
|