2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
2018-06-08 03:07:48 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/container"
|
2022-05-10 19:59:00 +00:00
|
|
|
"github.com/docker/docker/libcontainerd/types"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2018-06-08 03:07:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This test simply verify that when a wrong ID used, a specific error should be returned for exec resize.
|
|
|
|
func TestExecResizeNoSuchExec(t *testing.T) {
|
|
|
|
n := "TestExecResize"
|
|
|
|
d := &Daemon{
|
2022-05-10 19:59:00 +00:00
|
|
|
execCommands: container.NewExecStore(),
|
2018-06-08 03:07:48 +00:00
|
|
|
}
|
|
|
|
c := &container.Container{
|
2022-05-10 19:59:00 +00:00
|
|
|
ExecCommands: container.NewExecStore(),
|
2018-06-08 03:07:48 +00:00
|
|
|
}
|
2022-05-10 19:59:00 +00:00
|
|
|
ec := &container.ExecConfig{
|
|
|
|
ID: n,
|
|
|
|
Container: c,
|
2018-06-08 03:07:48 +00:00
|
|
|
}
|
|
|
|
d.registerExecCommand(c, ec)
|
|
|
|
err := d.ContainerExecResize("nil", 24, 8)
|
|
|
|
assert.ErrorContains(t, err, "No such exec instance")
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:59:00 +00:00
|
|
|
type execResizeMockProcess struct {
|
|
|
|
types.Process
|
|
|
|
Width, Height int
|
2018-06-08 03:07:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:59:00 +00:00
|
|
|
func (p *execResizeMockProcess) Resize(ctx context.Context, width, height uint32) error {
|
|
|
|
p.Width = int(width)
|
|
|
|
p.Height = int(height)
|
2018-06-08 03:07:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is to make sure that when exec context is ready, resize should call ResizeTerminal via containerd client.
|
|
|
|
func TestExecResize(t *testing.T) {
|
|
|
|
n := "TestExecResize"
|
|
|
|
width := 24
|
|
|
|
height := 8
|
2022-05-10 19:59:00 +00:00
|
|
|
mp := &execResizeMockProcess{}
|
2018-06-08 03:07:48 +00:00
|
|
|
d := &Daemon{
|
2022-05-10 19:59:00 +00:00
|
|
|
execCommands: container.NewExecStore(),
|
2018-06-08 03:07:48 +00:00
|
|
|
containers: container.NewMemoryStore(),
|
|
|
|
}
|
|
|
|
c := &container.Container{
|
2022-05-10 19:59:00 +00:00
|
|
|
ID: n,
|
|
|
|
ExecCommands: container.NewExecStore(),
|
2018-06-08 03:07:48 +00:00
|
|
|
State: &container.State{Running: true},
|
|
|
|
}
|
2022-05-10 19:59:00 +00:00
|
|
|
ec := &container.ExecConfig{
|
|
|
|
ID: n,
|
|
|
|
Container: c,
|
|
|
|
Process: mp,
|
|
|
|
Started: make(chan struct{}),
|
|
|
|
}
|
|
|
|
close(ec.Started)
|
2018-06-08 03:07:48 +00:00
|
|
|
d.containers.Add(n, c)
|
|
|
|
d.registerExecCommand(c, ec)
|
|
|
|
err := d.ContainerExecResize(n, height, width)
|
|
|
|
assert.NilError(t, err)
|
2022-05-10 19:59:00 +00:00
|
|
|
assert.Equal(t, mp.Width, width)
|
|
|
|
assert.Equal(t, mp.Height, height)
|
2018-06-08 03:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test is to make sure that when exec context is not ready, a timeout error should happen.
|
|
|
|
// TODO: the expect running time for this test is 10s, which would be too long for unit test.
|
|
|
|
func TestExecResizeTimeout(t *testing.T) {
|
|
|
|
n := "TestExecResize"
|
|
|
|
width := 24
|
|
|
|
height := 8
|
2022-05-10 19:59:00 +00:00
|
|
|
mp := &execResizeMockProcess{}
|
2018-06-08 03:07:48 +00:00
|
|
|
d := &Daemon{
|
2022-05-10 19:59:00 +00:00
|
|
|
execCommands: container.NewExecStore(),
|
2018-06-08 03:07:48 +00:00
|
|
|
containers: container.NewMemoryStore(),
|
|
|
|
}
|
|
|
|
c := &container.Container{
|
2022-05-10 19:59:00 +00:00
|
|
|
ID: n,
|
|
|
|
ExecCommands: container.NewExecStore(),
|
2018-06-08 03:07:48 +00:00
|
|
|
State: &container.State{Running: true},
|
|
|
|
}
|
2022-05-10 19:59:00 +00:00
|
|
|
ec := &container.ExecConfig{
|
|
|
|
ID: n,
|
|
|
|
Container: c,
|
|
|
|
Process: mp,
|
|
|
|
Started: make(chan struct{}),
|
|
|
|
}
|
2018-06-08 03:07:48 +00:00
|
|
|
d.containers.Add(n, c)
|
|
|
|
d.registerExecCommand(c, ec)
|
|
|
|
err := d.ContainerExecResize(n, height, width)
|
|
|
|
assert.ErrorContains(t, err, "timeout waiting for exec session ready")
|
|
|
|
}
|